-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwlog.go
134 lines (111 loc) · 2.48 KB
/
cwlog.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"time"
)
var (
logDesc *os.File
logName string
logReady bool
logBuf []string
logBufLines int
logBufLock sync.Mutex
gameLock sync.Mutex
)
/*
* Log this, can use printf arguments
* Write to buffer, async write
*/
func doLog(withTrace bool, format string, args ...interface{}) {
if WASMMode {
return
}
var buf string
if withTrace {
/* Get current time */
ctime := time.Now()
/* Get calling function and line */
_, filename, line, _ := runtime.Caller(1)
/* printf conversion */
text := fmt.Sprintf(format, args...)
/* Add current date */
date := fmt.Sprintf("%2v:%2v.%2v", ctime.Hour(), ctime.Minute(), ctime.Second())
/* Date, go file, go file line, text */
buf = fmt.Sprintf("%v: %15v:%5v: %v\n", date, filepath.Base(filename), line, text)
} else {
/* Get current time */
ctime := time.Now()
/* printf conversion */
text := fmt.Sprintf(format, args...)
/* Add current date */
date := fmt.Sprintf("%2v:%2v.%2v", ctime.Hour(), ctime.Minute(), ctime.Second())
/* Date, go file, go file line, text */
buf = fmt.Sprintf("%v: %v\n", date, text)
}
if !logReady || logDesc == nil {
fmt.Print(buf)
return
}
/* Add to buffer */
logBufLock.Lock()
logBuf = append(logBuf, buf)
logBufLines++
logBufLock.Unlock()
}
func LogDaemon() {
if WASMMode {
return
}
go func() {
for {
logBufLock.Lock()
/* Are there lines to write? */
if logBufLines == 0 {
logBufLock.Unlock()
time.Sleep(time.Millisecond * 100)
continue
}
/* Write line */
_, err := logDesc.WriteString(logBuf[0])
if err != nil {
fmt.Println("DoLog: WriteString failure")
logDesc.Close()
logDesc = nil
}
fmt.Print(logBuf[0])
/* Remove line from buffer */
logBuf = logBuf[1:]
logBufLines--
logBufLock.Unlock()
}
}()
}
/* Prep logger */
func StartLog() {
if WASMMode {
return
}
t := time.Now()
/* Create our log file names */
logName = fmt.Sprintf("log/auth-%v-%v-%v.log", t.Day(), t.Month(), t.Year())
/* Make log directory */
errr := os.MkdirAll("log", os.ModePerm)
if errr != nil {
fmt.Print(errr.Error())
return
}
/* Open log files */
bdesc, errb := os.OpenFile(logName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
/* Handle file errors */
if errb != nil {
fmt.Printf("An error occurred when attempting to create the log. Details: %s", errb)
return
}
/* Save descriptors, open/closed elsewhere */
logDesc = bdesc
logReady = true
}