📶 [WIP] RouterOS WinBox bruteforce
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mtbf/log.go

99 lines
2.3 KiB

2 years ago
package main
2 years ago
// log.go: logging
2 years ago
import (
"fmt"
"os"
"strings"
"github.com/fatih/color"
2 years ago
)
2 years ago
func init() {
registerParam("log-level", 0, "max log level, useful for debugging. -1 logs everything")
registerParamWithCallback("log-modules", []string{}, "always log output from these modules", updateModuleMap)
registerParamWithCallback("no-log-modules", []string{}, "never log output from these modules", updateModuleMap)
registerParamHidden("log-module-map", map[string]bool{})
registerCommand("no-colors", "disable terminal colors", func() {
color.NoColor = true
})
2 years ago
}
2 years ago
func shouldLog(facility string, level, maxLevel int) bool {
2 years ago
moduleMap := getParam[map[string]bool]("log-module-map")
2 years ago
logModule, ok := moduleMap[strings.ToLower(facility)]
if ok {
return logModule
}
if maxLevel < 0 {
return true // log everything if log-level is -1
}
return maxLevel >= level
}
func log(facility string, level int, s string, params ...interface{}) {
2 years ago
maxLevel := getParamInt("log-level")
2 years ago
if !shouldLog(facility, level, maxLevel) {
return
}
var prefix = ""
if level > 0 {
prefix = color.WhiteString(strings.Repeat("-", level) + "> ")
2 years ago
}
if (level >= 1 || maxLevel < 0) && facility != "" {
prefix = prefix + color.CyanString("["+strings.ToUpper(facility)+"]: ")
2 years ago
}
if len(params) == 0 {
fmt.Printf(prefix + s + "\n")
} else {
fmt.Printf(prefix+s+"\n", params...)
}
}
func fail(s string, params ...any) {
2 years ago
if len(params) == 0 {
fmt.Fprintf(os.Stderr, color.HiRedString("ERROR: ")+s+"\n")
2 years ago
} else {
fmt.Fprintf(os.Stderr, color.HiRedString("ERROR: ")+s+"\n", params...)
2 years ago
}
os.Exit(1)
}
func logIf(condition bool, facility string, level int, s string, params ...interface{}) {
if condition {
log(facility, level, s, params...)
}
}
func failIf(condition bool, s string, params ...interface{}) {
if condition {
fail(s, params...)
}
}
func updateModuleMap() {
2 years ago
logModules := getParamStringSlice("log-modules")
noLogModules := getParamStringSlice("no-log-modules")
2 years ago
newMap := map[string]bool{}
for _, module := range logModules {
module = strings.ToLower(module)
newMap[module] = true
}
for _, module := range noLogModules {
module = strings.ToLower(module)
2 years ago
failIf(newMap[module], "log module \"%v\" is defined both in log-modules and no-log-modules", module)
2 years ago
newMap[module] = false
}
2 years ago
setParam("log-module-map", newMap)
2 years ago
}