-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
56 lines (47 loc) · 906 Bytes
/
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
package golog
import (
"fmt"
"log"
"runtime"
"sync"
)
var mutex = &sync.Mutex{}
var wg sync.WaitGroup
// D : Default Logger
func D(message string) {
generateMessage(message)
}
// E : Error Logger
func E(message string) {
generateMessage("[ERROR] " + message)
}
func generateMessage(message string) {
var resultMessage string
if IsCallingFunctionEnabled == true {
resultMessage += "[" + getCallingFunctionName() + "]"
}
resultMessage += " " + message
wg.Add(1)
go func() {
defer wg.Done()
printMessage(resultMessage)
} ()
wg.Wait()
}
func printMessage(resultMessage string) {
mutex.Lock()
defer mutex.Unlock()
if IsTimeEnabled {
log.Println(resultMessage)
return
}
fmt.Println(resultMessage)
}
func getCallingFunctionName() string {
pc, _, _, ok := runtime.Caller(3)
details := runtime.FuncForPC(pc)
if ok && details != nil {
return details.Name()
}
return ""
}