/
logger.go
118 lines (104 loc) · 3.53 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright © 2018 The Havener
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"github.com/gonvenience/wait"
"github.com/gonvenience/bunt"
"github.com/homeport/havener/pkg/havener"
"github.com/spf13/viper"
)
var progressIndicator *wait.ProgressIndicator
// LogTask processes all log messages coming
// from havener package.
func LogTask() {
channel := havener.GetLogChannel()
for {
message := <-channel
targetLevel := translateLogLevel()
log(message, targetLevel)
}
}
// setCurrentProgressIndicator updates the gobal variable
// which is used for text updates of the current indicator.
// For resetting use nil value.
func setCurrentProgressIndicator(pi *wait.ProgressIndicator) {
progressIndicator = pi
}
// translateLogLevel transates the flag boolean to
// the associated log level.
// Levels: Fatal < Error < Warn < Verbose < Debug < Trace
func translateLogLevel() havener.LogLevel {
logLevel := havener.Off
fatalLevel := viper.GetBool("fatal")
errorLevel := viper.GetBool("error")
warnLevel := viper.GetBool("warn")
verboseLevel := viper.GetBool("verbose")
debugLevel := viper.GetBool("debug")
traceLevel := viper.GetBool("trace")
if fatalLevel && havener.Fatal > logLevel {
logLevel = havener.Fatal
}
if errorLevel && havener.Error > logLevel {
logLevel = havener.Error
}
if warnLevel && havener.Warn > logLevel {
logLevel = havener.Warn
}
if verboseLevel && havener.Verbose > logLevel {
logLevel = havener.Verbose
}
if debugLevel && havener.Debug > logLevel {
logLevel = havener.Debug
}
if traceLevel && havener.Trace > logLevel {
logLevel = havener.Trace
}
return logLevel
}
// log processes all log messages and logs them differently
// according to their level
func log(message havener.LogMessage, targetLevel havener.LogLevel) {
if targetLevel >= message.Level {
switch message.Level {
case havener.Fatal:
printLogf("*[FATAL]* %s\n", message.Message)
case havener.Error:
printLogf("*[ERROR]* %s\n", message.Message)
case havener.Warn:
printLogf("*[WARN]* %s\n", message.Message)
case havener.Verbose:
printLogf("*[INFO]* %s\n", message.Message)
case havener.Debug:
printLogf("*[DEBUG]* %s\n", message.Message)
case havener.Trace:
printLogf("*[TRACE]* %s\n", message.Message)
default:
printLogf("*[INFO]* %s\n", message.Message)
}
}
}
// printLogf formats and prints a log message
func printLogf(format string, args ...interface{}) {
if progressIndicator != nil {
progressIndicator.SetText(format, args...)
} else {
bunt.Printf(format, args...)
}
}