gllvm/shared/logging.go

71 lines
1.5 KiB
Go
Raw Normal View History

package shared
import (
"fmt"
"os"
"strings"
)
const (
2017-06-29 21:59:23 +02:00
errorV = iota
warningV
infoV
debugV
)
var loggingLevels = map[string]int{
2017-06-29 21:59:23 +02:00
"ERROR": errorV,
"WARNING": warningV,
"INFO": infoV,
"DEBUG": debugV,
}
var level = 0
var filePointer = os.Stderr
func init() {
if envLevelStr := os.Getenv("GLLVM_OUTPUT_LEVEL"); envLevelStr != "" {
if envLevelVal, ok := loggingLevels[envLevelStr]; ok {
level = envLevelVal
}
}
if envFileStr := os.Getenv("GLLVM_OUTPUT_FILE"); envFileStr != "" {
if loggingFP, err := os.OpenFile(envFileStr, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600); err == nil {
filePointer = loggingFP
}
}
}
func makeLogger(lvl int) func(format string, a ...interface{}) {
return func(format string, a ...interface{}) {
if level >= lvl {
msg := fmt.Sprintf(format, a...)
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
filePointer.WriteString(msg)
}
}
}
2017-07-06 02:42:55 +02:00
//LogDebug logs to the configured stream if the logging level is DEBUG.
var LogDebug = makeLogger(debugV)
2017-07-06 02:42:55 +02:00
//LogInfo logs to the configured stream if the logging level is INFO or lower.
var LogInfo = makeLogger(infoV)
2017-07-06 02:42:55 +02:00
//LogWarning logs to the configured stream if the logging level is WARNING or lower.
var LogWarning = makeLogger(warningV)
2017-07-06 02:42:55 +02:00
//LogError logs to the configured stream if the logging level is ERROR or lower.
var LogError = makeLogger(errorV)
2017-07-06 02:42:55 +02:00
//LogFatal logs to the configured stream and then exits.
func LogFatal(format string, a ...interface{}) {
LogError(format, a...)
os.Exit(1)
}
2017-07-10 22:01:21 +02:00
var LogWrite = makeLogger(-1)