From 84ff772ee1642c4f139605fc1a2716ce49f4b015 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 27 Jul 2022 20:14:48 +0200 Subject: add simple leveled log wrapper --- llog/llog.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 llog/llog.go diff --git a/llog/llog.go b/llog/llog.go new file mode 100644 index 0000000..e7fb924 --- /dev/null +++ b/llog/llog.go @@ -0,0 +1,71 @@ +package llog + +import ( + "fmt" + "log" +) + +type LogLevel int + +const ( + l_Fatal LogLevel = iota + 1 + l_Error + l_Warning + l_Info + l_Debug + l_DDebug +) + +var Level LogLevel = l_Warning + +func getLevelNames() []string { + return []string{"Fatal", "Error", "Warning", "Info", "Debug", "DDebug"} +} + +func (l LogLevel) IsValid() bool { + return l >= l_Fatal && l <= l_DDebug +} + +func (l LogLevel) String() string { + if !l.IsValid() { + return fmt.Sprintf("LogLevel(%d)", int(l)) + } + + return getLevelNames()[l-1] +} + +func Log(l LogLevel, msg string) { + msg = fmt.Sprintf("[%s] %s", l.String(), msg) + + if l == l_Fatal { + log.Fatalln(msg) + } + + if l <= Level { + log.Println(msg) + } +} + +func Fatal(msg string) { + Log(l_Fatal, msg) +} + +func Error(msg string) { + Log(l_Error, msg) +} + +func Warning(msg string) { + Log(l_Warning, msg) +} + +func Info(msg string) { + Log(l_Info, msg) +} + +func Debug(msg string) { + Log(l_Debug, msg) +} + +func DDebug(msg string) { + Log(l_DDebug, msg) +} -- cgit v1.2.3