Update CCLogger

This commit is contained in:
Thomas Roehl 2022-01-26 17:09:20 +01:00
parent 5600cf1f5f
commit 0a383a3789

View File

@ -2,114 +2,112 @@ package cclogger
import ( import (
"fmt" "fmt"
"runtime"
"os"
"log" "log"
"os"
"runtime"
) )
var ( var (
globalDebug = false globalDebug = false
stdout = os.Stdout stdout = os.Stdout
stderr = os.Stderr stderr = os.Stderr
debugLog *log.Logger = nil debugLog *log.Logger = nil
infoLog *log.Logger = nil infoLog *log.Logger = nil
errorLog *log.Logger = nil errorLog *log.Logger = nil
warnLog *log.Logger = nil warnLog *log.Logger = nil
defaultLog *log.Logger = nil defaultLog *log.Logger = nil
) )
func initLogger() { func initLogger() {
if debugLog == nil { if debugLog == nil {
debugLog = log.New(stderr, "DEBUG ", log.LstdFlags) debugLog = log.New(stderr, "DEBUG ", log.LstdFlags)
} }
if infoLog == nil { if infoLog == nil {
infoLog = log.New(stdout, "INFO ", log.LstdFlags) infoLog = log.New(stdout, "INFO ", log.LstdFlags)
} }
if errorLog == nil { if errorLog == nil {
errorLog = log.New(stderr, "ERROR ", log.LstdFlags) errorLog = log.New(stderr, "ERROR ", log.LstdFlags)
} }
if warnLog == nil { if warnLog == nil {
warnLog = log.New(stderr, "WARN ", log.LstdFlags) warnLog = log.New(stderr, "WARN ", log.LstdFlags)
} }
if defaultLog == nil { if defaultLog == nil {
defaultLog = log.New(stdout, "", log.LstdFlags) defaultLog = log.New(stdout, "", log.LstdFlags)
} }
} }
func Print(e ... interface{}) { func Print(e ...interface{}) {
initLogger() initLogger()
defaultLog.Print(e) defaultLog.Print(e)
} }
func ComponentPrint(component string, e ... interface{}) { func ComponentPrint(component string, e ...interface{}) {
initLogger() initLogger()
defaultLog.Print(fmt.Sprintf("[%s] ", component), e) defaultLog.Print(fmt.Sprintf("[%s] ", component), e)
} }
func Info(e ... interface{}) { func Info(e ...interface{}) {
initLogger() initLogger()
infoLog.Print(e) infoLog.Print(e)
} }
func ComponentInfo(component string, e ... interface{}) { func ComponentInfo(component string, e ...interface{}) {
initLogger() initLogger()
infoLog.Print(fmt.Sprintf("[%s] ", component), e) infoLog.Print(fmt.Sprintf("[%s] ", component), e)
} }
func Debug(e ... interface{}) { func Debug(e ...interface{}) {
initLogger() initLogger()
if globalDebug == true { if globalDebug == true {
debugLog.Print(e) debugLog.Print(e)
} }
} }
func ComponentDebug(component string, e ... interface{}) { func ComponentDebug(component string, e ...interface{}) {
initLogger() initLogger()
if globalDebug == true && debugLog != nil { if globalDebug == true && debugLog != nil {
//CCComponentPrint(debugLog, component, e) //CCComponentPrint(debugLog, component, e)
debugLog.Print(fmt.Sprintf("[%s] ", component), e) debugLog.Print(fmt.Sprintf("[%s] ", component), e)
} }
} }
func Error(e ... interface{}) { func Error(e ...interface{}) {
initLogger() initLogger()
_, fn, line, _ := runtime.Caller(1) _, fn, line, _ := runtime.Caller(1)
errorLog.Print(fmt.Sprintf("[%s:%d] ", fn, line), e) errorLog.Print(fmt.Sprintf("[%s:%d] ", fn, line), e)
} }
func ComponentError(component string, e ... interface{}) { func ComponentError(component string, e ...interface{}) {
initLogger() initLogger()
_, fn, line, _ := runtime.Caller(1) _, fn, line, _ := runtime.Caller(1)
errorLog.Print(fmt.Sprintf("[%s|%s:%d] ", component, fn, line), e) errorLog.Print(fmt.Sprintf("[%s|%s:%d] ", component, fn, line), e)
} }
func SetDebug() { func SetDebug() {
globalDebug = true globalDebug = true
initLogger() initLogger()
} }
func SetOutput(filename string) { func SetOutput(filename string) {
if filename == "stderr" { if filename == "stderr" {
if stderr != os.Stderr && stderr != os.Stdout { if stderr != os.Stderr && stderr != os.Stdout {
stderr.Close() stderr.Close()
} }
stderr = os.Stderr stderr = os.Stderr
} else if filename == "stdout" { } else if filename == "stdout" {
if stderr != os.Stderr && stderr != os.Stdout { if stderr != os.Stderr && stderr != os.Stdout {
stderr.Close() stderr.Close()
} }
stderr = os.Stdout stderr = os.Stdout
} else { } else {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil { if err == nil {
defer file.Close() defer file.Close()
stderr = file stderr = file
} }
} }
debugLog = nil debugLog = nil
errorLog = nil errorLog = nil
warnLog = nil warnLog = nil
initLogger() initLogger()
} }