-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
81 lines (65 loc) · 1.61 KB
/
error.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
76
77
78
79
80
81
package log
import (
"errors"
"fmt"
"io"
"os"
"strings"
)
var (
ERR_MUST_POINTER error = errors.New("parameter must be pointer")
ERR_INVALID_TYPE error = errors.New("parameter invalid type")
ERR_NOT_FOUND error = errors.New("data is not found")
ERR_READ_BUFFER error = errors.New("cannot read least data")
ERR_MORE_BUFFER error = errors.New("require more data")
ERR_NO_SUPPORT error = errors.New("this method is not suppored")
ERR_INVALID_INDEX error = errors.New("invalid index number")
ERR_NO_INCLUDE_ROOT error = errors.New("dosent include root buffer")
ERR_INVLIAD_WRITE_SIZE error = errors.New("invalid written size")
)
type LogLevel byte
var CurrentLogLevel LogLevel
var LogW io.Writer = os.Stderr
const (
LOG_ERROR LogLevel = iota
LOG_WARN
LOG_DEBUG
)
type LogArgs struct {
Fmt string
Infs []interface{}
}
type LogFn func() LogArgs //(string, interface{}...)
func SetLogLevel(l LogLevel) {
CurrentLogLevel = l
}
func F(s string, v ...interface{}) LogArgs {
return LogArgs{Fmt: s, Infs: v}
}
// if no output , not eval args
// Log(LOG_DEBUG, func() LogArgs { return F("test %d \n", 1) })
func Log(l LogLevel, fn LogFn) {
if CurrentLogLevel < l {
return
}
var b strings.Builder
switch l {
case LOG_DEBUG:
b.WriteString("D: ")
case LOG_WARN:
b.WriteString("W: ")
case LOG_ERROR:
b.WriteString("E: ")
default:
b.WriteString(" ")
}
args := fn()
fmt.Fprintf(&b, args.Fmt, args.Infs...)
io.WriteString(LogW, b.String())
return
}
func Printf(s string, v ...interface{}) func() LogArgs {
return func() LogArgs {
return F(s, v...)
}
}