-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
58 lines (51 loc) · 1016 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
57
58
package engine
import (
"fmt"
"log"
"os"
)
var (
hist string // console history for GUI
logfile *os.File // saves history of input commands + output
)
// returns log file of input commands, opening it first if needed
func openlog() *os.File {
if logfile == nil {
var err error
logfile, err = os.OpenFile(OD+"/input.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Println(err)
}
}
return logfile
}
func logFile(msg ...interface{}) {
out := openlog()
if out != nil {
fmt.Fprintln(out, msg...)
}
}
func LogInput(msg ...interface{}) {
logFile(msg...)
logGUI(msg...)
}
func LogOutput(msg ...interface{}) {
logGUI(msg...)
msg2 := "/*" + fmt.Sprintln(msg...) + "*/"
logFile(msg2)
}
func logGUI(msg ...interface{}) {
m := fmt.Sprintln(msg...)
m = m[:len(m)-1] // strip newline
if len(m) > 1000 {
m = m[:1000-3] + "..."
}
if hist != "" { // prepend newline
hist += "\n"
}
hist += m
fmt.Println(m)
if GUI.Page != nil {
GUI.Set("console", hist)
}
}