forked from moredhel/env_logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
69 lines (59 loc) · 1.43 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package env_logger
import (
"context"
"encoding/json"
"fmt"
"runtime"
"time"
)
// Wrap an error, this is useful in combination with Should and Must
func Wrap(err error, msg string, args ...interface{}) error {
if err != nil {
args = append(args, err)
return fmt.Errorf(msg+": %w", args...)
}
return nil
}
func (e *Entry) Wrap(err error, msg string, args ...interface{}) error {
return Wrap(err, msg, args...)
}
// Wrap an error, this is useful in combination with Should and Must
func WrapFinal(err *error, msg string, args ...interface{}) {
if err != nil && *err != nil {
args = append(args, *err)
*err = fmt.Errorf(msg+": %w", args...) // Change actual value
}
}
func (e *Entry) WrapFinal(err *error, msg string, args ...interface{}) {
WrapFinal(err, msg, args...)
}
// Wrap an error, this is useful in combination with Should and Must
func PanicHandler() {
if r := recover(); r != nil {
Panic(r)
}
}
func (e *Entry) PanicHandler() {
if r := recover(); r != nil {
e.Panic(r)
}
}
// Indent transforms the structure into json by using MarshalIndent
func Indent(arg interface{}) string {
indented, _ := json.MarshalIndent(arg, "", " ")
return string(indented)
}
func (e *Entry) Indent(arg interface{}) string {
return Indent(arg)
}
func logGoRoutines(ctx context.Context) {
t := time.NewTicker(time.Second)
for {
select {
case <-ctx.Done():
return
case <-t.C:
Info("Routines: ", runtime.NumGoroutine())
}
}
}