2017-06-28 03:55:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-29 21:43:12 +02:00
|
|
|
"os"
|
2017-06-28 03:55:27 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
error_v = iota
|
|
|
|
warning_v
|
|
|
|
info_v
|
|
|
|
debug_v
|
|
|
|
)
|
|
|
|
|
2017-06-28 15:33:20 +02:00
|
|
|
const (
|
|
|
|
loggingLevelEnvVar = "GLLVM_OUTPUT_LEVEL"
|
2017-06-29 21:43:12 +02:00
|
|
|
loggingFileEnvVar = "GLLVM_OUTPUT_FILE"
|
2017-06-28 15:33:20 +02:00
|
|
|
)
|
2017-06-28 03:55:27 +02:00
|
|
|
|
|
|
|
var loggingLevels = map[string]int{
|
2017-06-29 21:43:12 +02:00
|
|
|
"ERROR": error_v,
|
|
|
|
"WARNING": warning_v,
|
|
|
|
"INFO": info_v,
|
|
|
|
"DEBUG": debug_v,
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-06-28 03:55:27 +02:00
|
|
|
|
|
|
|
var level = 0
|
|
|
|
|
|
|
|
var filePointer = os.Stderr
|
|
|
|
|
2017-06-29 21:43:12 +02:00
|
|
|
func init() {
|
2017-06-28 03:55:27 +02:00
|
|
|
if envLevelStr := os.Getenv(loggingLevelEnvVar); envLevelStr != "" {
|
|
|
|
if envLevelVal, ok := loggingLevels[envLevelStr]; ok {
|
|
|
|
level = envLevelVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if envFileStr := os.Getenv(loggingFileEnvVar); 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{}) {
|
2017-06-29 21:43:12 +02:00
|
|
|
return func(format string, a ...interface{}) {
|
2017-06-28 03:55:27 +02:00
|
|
|
if level >= lvl {
|
|
|
|
msg := fmt.Sprintf(format, a...)
|
|
|
|
if !strings.HasSuffix(msg, "\n") {
|
|
|
|
msg += "\n"
|
|
|
|
}
|
|
|
|
filePointer.WriteString(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:43:12 +02:00
|
|
|
var logDebug = makeLogger(debug_v)
|
|
|
|
var logInfo = makeLogger(info_v)
|
2017-06-28 03:55:27 +02:00
|
|
|
var logWarning = makeLogger(warning_v)
|
2017-06-29 21:43:12 +02:00
|
|
|
var logError = makeLogger(error_v)
|
2017-06-28 23:03:36 +02:00
|
|
|
|
2017-06-29 21:43:12 +02:00
|
|
|
func logFatal(format string, a ...interface{}) {
|
2017-06-28 23:03:36 +02:00
|
|
|
logError(format, a...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|