-
Notifications
You must be signed in to change notification settings - Fork 5
/
log.go
74 lines (65 loc) · 1.69 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
package main
import (
"github.com/fatih/color"
"log"
"os"
"path/filepath"
"runtime"
)
func Log(format string, a ...interface{}) {
_, path, _, _ := runtime.Caller(1)
printf(path, format, a...)
}
func LogFatal(format string, a ...interface{}) {
_, path, _, _ := runtime.Caller(1)
printf(path, format, a...)
os.Exit(1)
}
func LogPanic(format string, a ...interface{}) {
_, path, _, _ := runtime.Caller(1)
printf(path, format, a...)
panic("log panic")
}
func LogRed(format string, a ...interface{}) {
printf("red", format, a...)
}
func printf(path string, format string, a ...interface{}) {
file := filepath.Base(path)
var colored string
if os.Getenv("NCHAINZ_COLORS") == "hi" {
switch file {
case "node.go":
colored = color.HiBlueString(format)
case "blockchains.go", "blockchain.go":
colored = color.HiGreenString(format)
case "consensus_state.go":
colored = color.HiMagentaString(format)
case "miner.go", "pow.go":
colored = color.HiYellowString(format)
case "matcher.go", "orderbook.go":
colored = color.HiRedString(format)
case "red":
colored = color.New(color.FgBlack, color.BgHiWhite).Sprint(format)
default:
colored = format
}
} else {
switch file {
case "node.go":
colored = color.BlueString(format)
case "blockchains.go", "blockchain.go", "block.go":
colored = color.GreenString(format)
case "consensus_state.go":
colored = color.MagentaString(format)
case "miner.go", "pow.go":
colored = color.YellowString(format)
case "matcher.go", "orderbook.go":
colored = color.RedString(format)
case "red":
colored = color.New(color.FgBlack, color.BgYellow).Sprint(format)
default:
colored = format
}
}
log.Printf(colored, a...)
}