ads1_2021/code/golang/internal/core/logging/logging.go

165 lines
3.5 KiB
Go

package logging
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"bufio"
"fmt"
"io"
"os"
"ads/internal/core/utils"
"ads/pkg/re"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES
* ---------------------------------------------------------------- */
var quietmode bool = false
var debugmode bool = false
var ansimode bool = false
var loggingPrefix string = ""
var force bool = false
var tagAll bool = false
var promptSymb string = ">"
func GetQuietMode() bool {
return quietmode
}
func SetQuietMode(mode bool) {
quietmode = mode
}
func GetDebugMode() bool {
return debugmode
}
func SetDebugMode(mode bool) {
debugmode = mode
}
func GetAnsiMode() bool {
return ansimode
}
func SetAnsiMode(mode bool) {
ansimode = mode
}
func GetForce() bool {
return force
}
func SetForce(mode bool) {
force = mode
}
func SetTagAll(mode bool) {
tagAll = mode
}
/* ---------------------------------------------------------------- *
* METHODS logging
* ---------------------------------------------------------------- */
func logGeneric(pipe *os.File, tag string, lines ...interface{}) {
if !force && quietmode {
return
}
if !(tag == "") {
tag = tag + " "
}
for _, line := range lines {
fmt.Fprintln(pipe, StripAnsiIfNecessary(fmt.Sprintf("%[1]s%[2]s%[3]v", loggingPrefix, tag, line)))
if !tagAll {
tag = ""
}
}
}
func LogPlain(lines ...interface{}) {
SetTagAll(false)
logGeneric(os.Stdout, "", lines...)
}
func LogInfo(lines ...interface{}) {
SetTagAll(true)
logGeneric(os.Stdout, "[\033[94;1mINFO\033[0m]", lines...)
}
func LogDebug(lines ...interface{}) {
if !debugmode {
return
}
SetTagAll(true)
logGeneric(os.Stdout, "[\033[96;1mDEBUG\033[0m]", lines...)
}
func LogWarn(lines ...interface{}) {
SetTagAll(false)
logGeneric(os.Stdout, "[\033[93;1mWARNING\033[0m]", lines...)
}
func LogError(lines ...interface{}) {
SetTagAll(false)
logGeneric(os.Stderr, "[\033[91;1mERROR\033[0m]", lines...)
}
func LogFatal(lines ...interface{}) {
SetTagAll(false)
logGeneric(os.Stderr, "[\033[91;1mFATAL\033[0m]", lines...)
os.Exit(1)
}
/* ---------------------------------------------------------------- *
* AUXILIARY METHODS
* ---------------------------------------------------------------- */
func StripAnsiIfNecessary(line string) string {
if !ansimode {
line = utils.StripAnsi(line)
}
return line
}
/* ---------------------------------------------------------------- *
* METHOD prompt
* ---------------------------------------------------------------- */
// Zeigt Prompt an und liest Usereingabe aus, erkennt auch ob Meta+D geklickt wurde
func Prompt(lines ...interface{}) (string, bool, error) {
pipe := os.Stdout
if len(lines) > 0 {
logGeneric(pipe, "", lines...)
logGeneric(pipe, "", "")
}
fmt.Fprintf(pipe, "%s ", promptSymb)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
line = re.Sub(`\s+$`, "", line)
if err != nil && err == io.EOF {
fmt.Fprintln(pipe, "")
return line, true, err
}
return line, false, err
}
func PromptAnyKeyToContinue() bool {
pipe := os.Stdout
fmt.Fprint(pipe, StripAnsiIfNecessary("\033[2;3mEingabetaste (Enter) zum Fortsetzen drücken:\033[0m "))
_, exit, _ := Prompt()
return exit
}
/* ---------------------------------------------------------------- *
* METHODS terminal
* ---------------------------------------------------------------- */
func ClearScreen() {
fmt.Print("\033[2J")
}