mirror of https://github.com/subgraph/fw-daemon
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
8 years ago
|
package sgfw
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"syscall"
|
||
|
"unsafe"
|
||
|
|
||
|
"github.com/op/go-logging"
|
||
|
)
|
||
|
|
||
|
var LevelToId = map[int32]string{
|
||
|
int32(logging.ERROR): "error",
|
||
|
int32(logging.WARNING): "warning",
|
||
|
int32(logging.NOTICE): "notice",
|
||
|
int32(logging.INFO): "info",
|
||
|
int32(logging.DEBUG): "debug",
|
||
|
}
|
||
|
|
||
|
var IdToLevel = func() map[string]int32 {
|
||
|
m := make(map[string]int32)
|
||
|
for k, v := range LevelToId {
|
||
|
m[v] = k
|
||
|
}
|
||
|
return m
|
||
|
}()
|
||
|
|
||
|
var log = logging.MustGetLogger("sgfw")
|
||
|
|
||
|
var logFormat = logging.MustStringFormatter(
|
||
|
"%{level:.4s} %{id:03x} %{message}",
|
||
|
)
|
||
|
var ttyFormat = logging.MustStringFormatter(
|
||
|
"%{color}%{time:15:04:05} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
|
||
|
)
|
||
|
|
||
|
const ioctlReadTermios = 0x5401
|
||
|
|
||
|
func isTerminal(fd int) bool {
|
||
|
var termios syscall.Termios
|
||
|
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||
|
return err == 0
|
||
|
}
|
||
|
|
||
|
func setupLoggerBackend(lvl logging.Level) logging.LeveledBackend {
|
||
|
format := logFormat
|
||
|
if isTerminal(int(os.Stderr.Fd())) {
|
||
|
format = ttyFormat
|
||
|
}
|
||
|
backend := logging.NewLogBackend(os.Stderr, "", 0)
|
||
|
formatter := logging.NewBackendFormatter(backend, format)
|
||
|
leveler := logging.AddModuleLevel(formatter)
|
||
|
leveler.SetLevel(lvl, "sgfw")
|
||
|
return leveler
|
||
|
}
|