This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl.go
111 lines (96 loc) · 2.08 KB
/
impl.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
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
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package logger
import (
"context"
"fmt"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
)
// TLogLevel s.e.
type TLogLevel int32
// Log Levels
const (
LogLevelNone = TLogLevel(iota)
LogLevelError
LogLevelWarning
LogLevelInfo
LogLevelDebug
)
// LogLevel s.e.
var globalLogLevel = LogLevelInfo
// SetLogLevel s.e.
func SetLogLevel(logLevel TLogLevel) {
atomic.StoreInt32((*int32)(&globalLogLevel), int32(logLevel))
}
// IsEnabled s.e.
func IsEnabled(logLevel TLogLevel) bool {
curLogLevel := TLogLevel(atomic.LoadInt32((*int32)(&globalLogLevel)))
return curLogLevel >= logLevel
}
// IsDebug s.e.
func IsDebug(ctx context.Context) bool {
return IsEnabled(LogLevelDebug)
}
var m sync.Mutex
func print(msgType string, args ...interface{}) {
m.Lock()
defer m.Unlock()
var funcName string
pc, _, line, ok := runtime.Caller(2)
details := runtime.FuncForPC(pc)
if ok && details != nil {
elems := strings.Split(details.Name(), "/")
if len(elems) > 1 {
funcName = elems[len(elems)-1]
} else {
funcName = details.Name()
}
} else {
funcName = ""
}
t := time.Now()
out := fmt.Sprint(t.Format("01/02 15:04:05.000"))
out += fmt.Sprint(": " + msgType)
out += fmt.Sprintf(": [%v:%v]:", funcName, line)
if len(args) > 0 {
var s string
for _, arg := range args {
s = s + fmt.Sprint(" ", arg)
}
for i := len(s); i < 60; i++ {
s = s + " "
}
out += fmt.Sprint(s)
}
fmt.Println(out)
}
// Error s.e.
func Error(ctx context.Context, args ...interface{}) {
print("**** ERR", args...)
}
// Warning s.e.
func Warning(ctx context.Context, args ...interface{}) {
if IsEnabled(LogLevelWarning) {
print("!!! WARN", args...)
}
}
// Info s.e.
func Info(ctx context.Context, args ...interface{}) {
if IsEnabled(LogLevelInfo) {
print("=== INFO", args...)
}
}
// Debug s.e.
func Debug(ctx context.Context, args ...interface{}) {
if IsDebug(ctx) {
print("--- DEBU", args...)
}
}