| 1 |
package log |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"regexp" |
| 7 |
"strings" |
| 8 |
"sync" |
| 9 |
|
| 10 |
"github.com/evilsocket/islazy/tui" |
| 11 |
) |
| 12 |
|
| 13 |
type MessageCallback = func(verbosity Verbosity, message string) |
| 14 |
|
| 15 |
var ( |
| 16 |
// Level represents the current verbosity level of the logging system. |
| 17 |
Level = INFO |
| 18 |
// Output represents the log output file path if filled or, if empty, stdout. |
| 19 |
Output = "" |
| 20 |
// NoEffects disables all effects and colors if set to true. |
| 21 |
NoEffects = false |
| 22 |
// OnFatal represents the callback/action to execute on Fatal messages. |
| 23 |
OnFatal = ExitOnFatal |
| 24 |
// A custom callback to execute for every log message. |
| 25 |
Callback = dummyCallback |
| 26 |
|
| 27 |
lock = &sync.Mutex{} |
| 28 |
currMessage = "" |
| 29 |
currLevel = INFO |
| 30 |
writer = os.Stdout |
| 31 |
|
| 32 |
reEffects = []*regexp.Regexp{ |
| 33 |
regexp.MustCompile("\x033\\[\\d+m"), |
| 34 |
regexp.MustCompile("\\\\e\\[\\d+m"), |
| 35 |
regexp.MustCompile("\x1b\\[\\d+m"), |
| 36 |
} |
| 37 |
) |
| 38 |
|
| 39 |
// Open initializes the logging system. |
| 40 |
func Open() (err error) { |
| 41 |
if Output != "" { |
| 42 |
writer, err = os.OpenFile(Output, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644) |
| 43 |
} |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
// Close finalizes the logging system. |
| 48 |
func Close() { |
| 49 |
if writer != os.Stdout { |
| 50 |
writer.Close() |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func dummyCallback(verbosity Verbosity, message string) { |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
func removeEffects(s string) string { |
| 59 |
for _, re := range reEffects { |
| 60 |
s = re.ReplaceAllString(s, "") |
| 61 |
} |
| 62 |
return s |
| 63 |
} |
| 64 |
|
| 65 |
func emit(v Verbosity, s string) { |
| 66 |
// remove all effects if found |
| 67 |
plain := removeEffects(s) |
| 68 |
|
| 69 |
Callback(v, plain) |
| 70 |
|
| 71 |
if NoEffects { |
| 72 |
s = plain |
| 73 |
} |
| 74 |
fmt.Fprintf(writer, "%s\n", s) |
| 75 |
} |
| 76 |
|
| 77 |
func do(v Verbosity, format string, args ...interface{}) { |
| 78 |
lock.Lock() |
| 79 |
defer lock.Unlock() |
| 80 |
|
| 81 |
if Level > v { |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
logLine := Format |
| 86 |
currLevel = v |
| 87 |
currMessage = format |
| 88 |
if args != nil { |
| 89 |
currMessage = fmt.Sprintf(format, args...) |
| 90 |
} |
| 91 |
// process token -> callback |
| 92 |
for token, cb := range Tokens { |
| 93 |
logLine = strings.Replace(logLine, token, cb(), -1) |
| 94 |
} |
| 95 |
// process token -> effect |
| 96 |
for token, effect := range Effects { |
| 97 |
logLine = strings.Replace(logLine, token, effect, -1) |
| 98 |
} |
| 99 |
// make sure an user error does not screw the log |
| 100 |
if tui.HasEffect(logLine) && !strings.HasSuffix(logLine, tui.RESET) { |
| 101 |
logLine += tui.RESET |
| 102 |
} |
| 103 |
|
| 104 |
emit(v, logLine) |
| 105 |
} |
| 106 |
|
| 107 |
// Raw emits a message without format to the logs. |
| 108 |
func Raw(format string, args ...interface{}) { |
| 109 |
lock.Lock() |
| 110 |
defer lock.Unlock() |
| 111 |
|
| 112 |
currMessage = fmt.Sprintf(format, args...) |
| 113 |
emit(INFO, currMessage) |
| 114 |
} |
| 115 |
|
| 116 |
// Debug emits a debug message. |
| 117 |
func Debug(format string, args ...interface{}) { |
| 118 |
do(DEBUG, format, args...) |
| 119 |
} |
| 120 |
|
| 121 |
// Info emits an informative message. |
| 122 |
func Info(format string, args ...interface{}) { |
| 123 |
do(INFO, format, args...) |
| 124 |
} |
| 125 |
|
| 126 |
// Important emits an important informative message. |
| 127 |
func Important(format string, args ...interface{}) { |
| 128 |
do(IMPORTANT, format, args...) |
| 129 |
} |
| 130 |
|
| 131 |
// Warning emits a warning message. |
| 132 |
func Warning(format string, args ...interface{}) { |
| 133 |
do(WARNING, format, args...) |
| 134 |
} |
| 135 |
|
| 136 |
// Error emits an error message. |
| 137 |
func Error(format string, args ...interface{}) { |
| 138 |
do(ERROR, format, args...) |
| 139 |
} |
| 140 |
|
| 141 |
// Fatal emits a fatal error message and calls the log.OnFatal callback. |
| 142 |
func Fatal(format string, args ...interface{}) { |
| 143 |
do(FATAL, format, args...) |
| 144 |
OnFatal() |
| 145 |
} |