Skip to content
Snippets Groups Projects
Commit fcad34fa authored by Tamer Tas's avatar Tamer Tas
Browse files

pkg/util/tlog: simplify log level calculation

parent 7eebc19a
Branches
No related tags found
No related merge requests found
...@@ -29,28 +29,50 @@ const ( ...@@ -29,28 +29,50 @@ const (
QuestionMark = "?" QuestionMark = "?"
) )
var (
logLevel Level
)
// Level is a 16-bit set holding the enabled log levels.
type Level uint16
const ( const (
LevelDebug = 32 LevelDebug = 1 << 5
LevelFatal = 16 LevelFatal = 1 << 4
LevelError = 8 LevelWarn = 1 << 3
LevelWarn = 4 LevelError = 1 << 2
LevelInfo = 2 LevelInfo = 1 << 1
LevelSuccess = 1 LevelSuccess = 1 << 0
) )
var LogLevel uint16 // Set enables the levels upto and including the given log level.
func (lvl *Level) Set(newLvl Level) {
*lvl = (newLvl << 1) - 1
}
// Permits queries whether the given log level is enabled or not.
func (lvl Level) Permits(queryLvl Level) bool {
return lvl&queryLvl > 0
}
// SetLogLevel sets the global logging level.
func SetLogLevel(LogLevelString string) { func SetLogLevel(LogLevelString string) {
switch strings.ToLower(LogLevelString) { levels := map[string]Level{
case "debug": "debug": LevelDebug,
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn | LevelInfo | LevelDebug) "fatal": LevelFatal,
case "info": "warn": LevelWarn,
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn | LevelInfo) "error": LevelError,
case "warn": "info": LevelInfo,
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn) "success": LevelSuccess,
default:
LogLevel |= (LevelSuccess | LevelError | LevelFatal)
} }
newLevel, ok := levels[strings.ToLower(LogLevelString)]
if !ok {
Error(fmt.Sprintf("unknown log level %s", LogLevelString))
return
}
logLevel.Set(newLevel)
} }
// TODO add log levels // TODO add log levels
...@@ -62,7 +84,7 @@ func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attri ...@@ -62,7 +84,7 @@ func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attri
// Debug logs the given message as a debug message. // Debug logs the given message as a debug message.
func Debug(msg string) { func Debug(msg string) {
if 0 == LogLevel & LevelDebug { if !logLevel.Permits(LevelDebug) {
return return
} }
...@@ -71,12 +93,16 @@ func Debug(msg string) { ...@@ -71,12 +93,16 @@ func Debug(msg string) {
// Success logs the given message as a success message. // Success logs the given message as a success message.
func Success(msg string) { func Success(msg string) {
if !logLevel.Permits(LevelSuccess) {
return
}
coloredPrintMsg(CheckMark, msg, color.FgWhite, color.FgGreen) coloredPrintMsg(CheckMark, msg, color.FgWhite, color.FgGreen)
} }
// Info logs the given message as a info message. // Info logs the given message as a info message.
func Info(msg string) { func Info(msg string) {
if 0 == LogLevel & LevelInfo { if !logLevel.Permits(LevelInfo) {
return return
} }
...@@ -85,7 +111,7 @@ func Info(msg string) { ...@@ -85,7 +111,7 @@ func Info(msg string) {
// Warn logs the given message as a warn message. // Warn logs the given message as a warn message.
func Warn(msg string) { func Warn(msg string) {
if 0 == LogLevel & LevelWarn { if !logLevel.Permits(LevelWarn) {
return return
} }
...@@ -94,11 +120,16 @@ func Warn(msg string) { ...@@ -94,11 +120,16 @@ func Warn(msg string) {
// Error logs the given message as a error message. // Error logs the given message as a error message.
func Error(msg string) { func Error(msg string) {
if !logLevel.Permits(LevelError) {
return
}
coloredPrintMsg(ErrorMark, msg, color.FgRed, color.FgRed) coloredPrintMsg(ErrorMark, msg, color.FgRed, color.FgRed)
} }
// Fatal logs the given message as a fatal message. // Fatal logs the given message as a fatal message.
func Fatal(msg string) { func Fatal(msg string) {
// Fatal level is being deprecated
Error(msg) Error(msg)
} }
...@@ -112,3 +143,6 @@ func Prompt(msg string, defval interface{}) { ...@@ -112,3 +143,6 @@ func Prompt(msg string, defval interface{}) {
} }
// TODO use dependency injection wrapper for fmt.Print usage in the code base // TODO use dependency injection wrapper for fmt.Print usage in the code base
func init() {
logLevel.Set(LevelError)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment