-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
54 lines (46 loc) · 1.22 KB
/
global.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
package log
import (
"unsafe"
"io"
"syscall"
"os"
)
var _GLOBAL_loggingContext LoggingContext
var _GLOBAL_loggingContextLock chan bool = make(chan bool, 1)
func init() {
GetGlobalLoggingContext()
}
func IsTerminal(term io.Writer) bool {
return hasTerminal(term)
}
func GetGlobalLoggingContext() LoggingContext {
_GLOBAL_loggingContextLock <- true
if _GLOBAL_loggingContext == nil {
_GLOBAL_loggingContext = CreateLoggingContext()
// Set up a default output stream listener.
formatter := NewLogEntryFormatter()
if hasTerminal(os.Stdout) {
formatter.SetFlags(PrintColor)
}
stdoutLogger := NewWriterLogger("default-stdout", os.Stdout, formatter)
_GLOBAL_loggingContext.AddGlobalLogListener(stdoutLogger, Trace)
//fmt.Println("INIT")
}
<-_GLOBAL_loggingContextLock
return _GLOBAL_loggingContext
}
func Logger(name string) Log {
stream, _ := GetGlobalLoggingContext().Stream(name)
return stream
}
func hasTerminal(writer io.Writer) bool {
var termios syscall.Termios
switch v := writer.(type) {
case *os.File: {
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(v.Fd()),
syscall.TCGETS, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
return err == 0
}
}
return false
}