-
Notifications
You must be signed in to change notification settings - Fork 28
/
logger.go
97 lines (83 loc) · 2.1 KB
/
logger.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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
)
const (
LOG_EXIT = 0
LOG_VERBOSE = 1
LOG_INFO = 2
LOG_ERROR = 3
LOG_ALERT = 4
)
var loggingVerbosity int = 3
var loggingPath string = ""
var loggingFile *os.File
var unitTesting bool
func LogTesting(testing bool) {
unitTesting = testing
if !testing {
log.SetOutput(os.Stderr)
}
}
// LogMessage output message to the specific standard / error output
func LogMessage(logType int, logMessage ...interface{}) {
aString := make([]string, len(logMessage))
for i, v := range logMessage {
aString[i] = fmt.Sprintf("%v", v)
}
message := strings.Join(aString, " ")
if UIactive && AppStarted && !unitTesting {
currentTime := time.Now()
message = "[" + currentTime.Format("2006-01-02 15:04:05") + "] " + message
if logType == LOG_INFO || logType == LOG_VERBOSE || logType == LOG_EXIT {
txtStdout.ScrollToEnd()
fmt.Fprintf(txtStdout, "%s\n", message)
} else if logType == LOG_ALERT {
txtMatchs.ScrollToEnd()
fmt.Fprintf(txtMatchs, "%s\n", message)
} else {
txtStderr.ScrollToEnd()
fmt.Fprintf(txtStderr, "%s\n", message)
}
} else {
if !unitTesting {
if logType == LOG_ERROR {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(os.Stdout)
}
}
log.Println(message)
}
if len(loggingPath) > 0 {
LogToFile(logType, message)
}
}
// LogFatal use LogMessage and exit program
func LogFatal(message string) {
LogMessage(LOG_ERROR, message)
ExitProgram(1, !UIactive)
}
// LogToFile copy output log flow to the specified file according to the desired loglevel
func LogToFile(logType int, message string) {
var err error
if loggingFile == nil {
loggingFile, err = os.OpenFile(loggingPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
loggingPath = ""
LogMessage(LOG_ERROR, "(ERROR)", "Unable to write log file")
ExitProgram(1, !UIactive)
}
}
if logType == LOG_EXIT || logType >= loggingVerbosity {
if _, err := loggingFile.WriteString(message + "\n"); err != nil {
loggingPath = ""
LogMessage(LOG_ERROR, "(ERROR)", "Unable to write log file")
ExitProgram(1, !UIactive)
}
}
}