-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (93 loc) · 2.23 KB
/
main.go
File metadata and controls
117 lines (93 loc) · 2.23 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package logger
import (
"io"
"log"
"os"
"flag"
"runtime"
"fmt"
"path/filepath"
)
var logger *log.Logger
var logLevel int
var root string
func init() {
logger = log.New(io.Writer(os.Stderr), "", log.Ldate|log.Lmicroseconds)
flag.IntVar(&logLevel, "log", 2, "larger value for detail log")
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
logger.Println("not read root dir")
return
}
root = dir
if filepath.Base(dir) == "bin" {
root += "/.."
}
logger.Println("root patch", root)
}
func _pares_source(log_level string) string {
_, filename, line, _ := runtime.Caller(3)
filename, _ = filepath.Rel(root, filename)
return fmt.Sprintf("[%s @./%s:%d]", log_level, filename, line)
}
func _printf(log_level string, format string, a ...interface{}) {
logger.Printf("%s %s\n",_pares_source(log_level), fmt.Sprintf(format, a...))
}
func _println(log string, a ...interface{}) {
tmp := []interface{}{_pares_source(log)}
for _, val := range a {
tmp = append(tmp, val)
}
logger.Println(tmp...)
}
func Debugf(format string, a ...interface{}) {
if logLevel > 2 {
_printf("DBG", format, a...)
}
}
func Debug(a ...interface{}){
if logLevel > 2 {
_println("DBG", a...)
}
}
func Infof(format string, a ...interface{}) {
if logLevel > 1 {
_printf("INF", format, a...)
}
}
func Info (a ...interface{}) {
if logLevel > 1 {
_println("INF", a...)
}
}
func Errorf (format string, a ...interface{}) {
if logLevel > 0 {
_printf("ERR",format, a...)
}
}
func Error (a ...interface{}) {
if logLevel > 0 {
_println("ERR",a...)
}
}
func Panic(format string, a ...interface{}) {
_printf("DBG",format, a...)
panic("!!")
}
func Logf(format string, a ...interface{}) {
_printf("INF",format, a...)
}
func Log(a ...interface{}) {
_println("INF",a...)
}
func Stack(format string, a ...interface{}){
_printf("DBG",format, a...)
buf := make([]byte, 8192)
runtime.Stack(buf, false)
_printf("DBG","!!!!!stack!!!!!: %s", buf)
}
func Recover() {
if err := recover(); err != nil {
Stack("goroutine failed:%v", err)
}
}