104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package common
|
|
|
|
/*
|
|
* This file is part of Autogits.
|
|
*
|
|
* Copyright © 2024 SUSE LLC
|
|
*
|
|
* Autogits is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
)
|
|
|
|
var id uint
|
|
|
|
func CreateStdoutLogger(stdout, stderr io.Writer) (*log.Logger, *log.Logger) {
|
|
id++
|
|
idStr := fmt.Sprintf("[%d] ", id)
|
|
stdLogger := log.New(stdout, idStr, log.Lmsgprefix)
|
|
errLogger := log.New(stderr, idStr, log.Lmsgprefix)
|
|
return stdLogger, errLogger
|
|
}
|
|
|
|
func PanicOnError(err error) {
|
|
if err != nil {
|
|
LogError(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func PanicOnErrorWithMsg(err error, msg ...any) {
|
|
if err != nil {
|
|
LogError(msg...)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type LogLevel int
|
|
|
|
const (
|
|
LogLevelNone = 0
|
|
LogLevelError = 2
|
|
LogLevelInfo = 5
|
|
LogLevelDebug = 10
|
|
)
|
|
|
|
var logLevel LogLevel = LogLevelInfo
|
|
|
|
func SetLoggingLevel(ll LogLevel) {
|
|
logLevel = ll
|
|
}
|
|
|
|
func GetLoggingLevel() LogLevel {
|
|
return logLevel
|
|
}
|
|
|
|
func SetLoggingLevelFromString(ll string) error {
|
|
switch ll {
|
|
case "info":
|
|
SetLoggingLevel(LogLevelInfo)
|
|
case "debug":
|
|
SetLoggingLevel(LogLevelDebug)
|
|
case "error":
|
|
SetLoggingLevel(LogLevelError)
|
|
case "none":
|
|
SetLoggingLevel(LogLevelNone)
|
|
default:
|
|
return fmt.Errorf("Unknown logging level: %s", ll)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func LogError(params ...any) {
|
|
if logLevel >= LogLevelError {
|
|
log.Println(append([]any{"[E]"}, params...)...)
|
|
}
|
|
}
|
|
|
|
func LogDebug(params ...any) {
|
|
if logLevel >= LogLevelDebug {
|
|
log.Println(append([]any{"[D]"}, params...)...)
|
|
}
|
|
}
|
|
|
|
func LogInfo(params ...any) {
|
|
if logLevel >= LogLevelInfo {
|
|
log.Println(append([]any{"[I]"}, params...)...)
|
|
}
|
|
}
|