-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
75 lines (66 loc) · 1.33 KB
/
log.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
70
71
72
73
74
75
package main
import (
"fmt"
"io"
"log"
"os"
"path"
"runtime/debug"
"strings"
)
const (
ansiReset = "\u001B[0m"
ansiRedBG = "\u001B[41m"
BUG = ansiRedBG + "Ж" + ansiReset
)
var (
letf = log.New(os.Stdout, BUG, log.Ltime|log.Lshortfile)
ltf = log.New(os.Stdout, " ", log.Ltime|log.Lshortfile)
let = log.New(os.Stdout, BUG, log.Ltime)
lt = log.New(os.Stdout, " ", log.Ltime)
li = log.New(os.Stdout, "\t", 0)
)
// Get source of code
func src(deep int) (s string) {
s = string(debug.Stack())
str := strings.Split(s, "\n")
if l := len(str); l <= deep {
deep = l - 1
for k, v := range str {
fmt.Println(k, v)
}
}
s = str[deep]
s = strings.Split(s, " +0x")[0]
_, s = path.Split(s)
s += ":"
return
}
// Wrap source of code and message to error
func Errorf(format string, args ...any) error {
return fmt.Errorf(src(8)+" %w", fmt.Errorf(format, args...))
}
// Wrap source of code and error to error
func srcError(err error) error {
if err == nil {
return nil
}
return fmt.Errorf(src(8)+" %w", err)
}
func PrintOk(s string, err error) {
if err != nil {
let.Println(src(8), s, err)
} else {
lt.Println(src(8), s, "ok")
}
}
func logOff() {
for _, l := range []*log.Logger{letf, ltf, let, lt, li} {
l.SetOutput(io.Discard)
}
}
func pressEnter() {
logOff()
fmt.Print("Press Enter>")
fmt.Scanln()
}