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

160 lines
3.3 KiB
Go

package logging
/* ---------------------------------------------------------------- *
* IMPORTS
* ---------------------------------------------------------------- */
import (
"bufio"
"fmt"
"io"
"os"
"github.com/buger/goterm"
"ads/internal/core/utils"
"ads/pkg/re"
)
/* ---------------------------------------------------------------- *
* GLOBAL VARIABLES
* ---------------------------------------------------------------- */
var quietmode bool = false
var debugmode bool = false
var ansimode bool = true
var loggingPrefix string = ""
var force bool = false
var tagAll bool = false
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 {
_line := fmt.Sprintf("%[1]s%[2]s%[3]v", loggingPrefix, tag, line)
if !ansimode {
_line = utils.StripAnsi(_line)
}
fmt.Fprintln(pipe, _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)
}
/* ---------------------------------------------------------------- *
* 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.Fprint(pipe, "$ ")
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, "\033[2;3mEingabetaste (Enter) zum Fortsetzen drücken:\033[0m ")
_, exit, _ := Prompt()
return exit
}
/* ---------------------------------------------------------------- *
* METHODS terminal
* ---------------------------------------------------------------- */
func ClearScreen() {
goterm.Clear()
goterm.Flush()
}