Only include timestamp and colors in log if stderr is terminal

pull/16/head
brl 9 years ago
parent 04ce8b58e7
commit d8fc095f87

@ -9,14 +9,31 @@ import (
"github.com/op/go-logging" "github.com/op/go-logging"
"github.com/subgraph/fw-daemon/nfqueue" "github.com/subgraph/fw-daemon/nfqueue"
"sync" "sync"
"syscall"
"unsafe"
) )
var log = logging.MustGetLogger("sgfw") var log = logging.MustGetLogger("sgfw")
var format = logging.MustStringFormatter(
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}", "%{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 init() { func init() {
format := logFormat
if(isTerminal(int(os.Stderr.Fd()))) {
format = ttyFormat
}
backend := logging.NewLogBackend(os.Stderr, "", 0) backend := logging.NewLogBackend(os.Stderr, "", 0)
formatter := logging.NewBackendFormatter(backend, format) formatter := logging.NewBackendFormatter(backend, format)
leveler := logging.AddModuleLevel(formatter) leveler := logging.AddModuleLevel(formatter)

Loading…
Cancel
Save