-
Notifications
You must be signed in to change notification settings - Fork 27
/
log.go
89 lines (75 loc) · 1.81 KB
/
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
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
package main
import (
"fmt"
"io"
"os"
"strings"
)
var todayStr = ""
var lastLogPath = ""
var logFile *os.File
var logwriter = LogWriter{}
type LogWriter struct {
w io.Writer
}
func (w LogWriter) Write(p []byte) (n int, err error) {
Log("LogWriter", string(p), true)
return len(p), nil
}
func Log(module string, str string, logToFile bool, args ...interface{}) {
if !strings.HasPrefix(module, "Debug") {
if module == "LogWriter" {
str = StrTrim(str)
if strings.HasPrefix(str, "[proxy.Provider") {
return
}
}
} else if config.Debug {
if config.LogDebug {
logToFile = true
}
} else {
return
}
logStr := fmt.Sprintf("["+GetDateTime(true)+"]["+module+"] "+str+".\n", args...)
if config.LogToFile && logToFile && logFile != nil {
if _, err := logFile.Write([]byte(logStr)); err != nil {
Log("Log", GetLangText("Error-Log_Write"), false, err.Error())
}
}
fmt.Print(logStr)
}
func LoadLog() bool {
if !config.LogToFile || config.LogPath == "" {
return false
}
if err := os.Mkdir(config.LogPath, os.ModePerm); err != nil && !os.IsExist(err) {
Log("LoadLog", GetLangText("Error-LoadLog_Mkdir"), false, err.Error())
return false
}
tmpTodayStr := GetDateTime(false)
newDay := (todayStr != tmpTodayStr)
newLogPath := (lastLogPath != config.LogPath)
if !newDay && !newLogPath {
return true
}
if newDay {
todayStr = tmpTodayStr
}
if newLogPath {
if lastLogPath != "" {
Log("LoadLog", GetLangText("LoadLog_HotReload"), false, config.LogPath)
}
lastLogPath = config.LogPath
}
tLogFile, err := os.OpenFile(config.LogPath+"/"+todayStr+".txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
tLogFile.Close()
tLogFile = nil
Log("LoadLog", GetLangText("Error-LoadLog_Close"), false, err.Error())
return false
}
logFile.Close()
logFile = tLogFile
return true
}