2017-07-05 17:14:11 +02:00
|
|
|
package shared
|
2017-06-28 03:55:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-29 21:43:12 +02:00
|
|
|
"os"
|
2017-06-28 03:55:27 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-29 21:59:23 +02:00
|
|
|
errorV = iota
|
|
|
|
warningV
|
|
|
|
infoV
|
|
|
|
debugV
|
2017-06-28 03:55:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var loggingLevels = map[string]int{
|
2017-06-29 21:59:23 +02:00
|
|
|
"ERROR": errorV,
|
|
|
|
"WARNING": warningV,
|
|
|
|
"INFO": infoV,
|
|
|
|
"DEBUG": debugV,
|
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-30 19:52:53 +02:00
|
|
|
if envLevelStr := os.Getenv("GLLVM_OUTPUT_LEVEL"); envLevelStr != "" {
|
2017-06-28 03:55:27 +02:00
|
|
|
if envLevelVal, ok := loggingLevels[envLevelStr]; ok {
|
|
|
|
level = envLevelVal
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 19:52:53 +02:00
|
|
|
if envFileStr := os.Getenv("GLLVM_OUTPUT_FILE"); envFileStr != "" {
|
2017-06-28 03:55:27 +02:00
|
|
|
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-07-06 02:42:55 +02:00
|
|
|
//LogDebug logs to the configured stream if the logging level is DEBUG.
|
2017-07-05 17:14:11 +02:00
|
|
|
var LogDebug = makeLogger(debugV)
|
2017-07-06 02:48:40 +02:00
|
|
|
|
2017-07-06 02:42:55 +02:00
|
|
|
//LogInfo logs to the configured stream if the logging level is INFO or lower.
|
2017-07-05 17:14:11 +02:00
|
|
|
var LogInfo = makeLogger(infoV)
|
2017-07-06 02:48:40 +02:00
|
|
|
|
2017-07-06 02:42:55 +02:00
|
|
|
//LogWarning logs to the configured stream if the logging level is WARNING or lower.
|
2017-07-05 17:14:11 +02:00
|
|
|
var LogWarning = makeLogger(warningV)
|
2017-07-06 02:48:40 +02:00
|
|
|
|
2017-07-06 02:42:55 +02:00
|
|
|
//LogError logs to the configured stream if the logging level is ERROR or lower.
|
2017-07-05 17:14:11 +02:00
|
|
|
var LogError = makeLogger(errorV)
|
2017-06-28 23:03:36 +02:00
|
|
|
|
2017-07-06 02:42:55 +02:00
|
|
|
//LogFatal logs to the configured stream and then exits.
|
2017-07-05 17:14:11 +02:00
|
|
|
func LogFatal(format string, a ...interface{}) {
|
|
|
|
LogError(format, a...)
|
2017-06-28 23:03:36 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-07-10 22:01:21 +02:00
|
|
|
|
|
|
|
var LogWrite = makeLogger(-1)
|