master > master: code go - vereinfachte logging
This commit is contained in:
@@ -5,160 +5,37 @@ package logging
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"ads/internal/core/utils"
|
||||
"ads/pkg/re"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES
|
||||
* MAIN METHODS logging
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
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 Plain(line interface{}, args ...interface{}) {
|
||||
logGeneric(os.Stdout, "", line, args...)
|
||||
}
|
||||
|
||||
func SetQuietMode(mode bool) {
|
||||
quietmode = mode
|
||||
func Info(line interface{}, args ...interface{}) {
|
||||
logGeneric(os.Stdout, "[\033[94;1mINFO\033[0m]", line, args...)
|
||||
}
|
||||
|
||||
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{}) {
|
||||
func Debug(line interface{}, args ...interface{}) {
|
||||
if !debugmode {
|
||||
return
|
||||
}
|
||||
SetTagAll(true)
|
||||
logGeneric(os.Stdout, "[\033[96;1mDEBUG\033[0m]", lines...)
|
||||
logGeneric(os.Stdout, "[\033[96;1mDEBUG\033[0m]", line, args...)
|
||||
}
|
||||
|
||||
func LogWarn(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric(os.Stdout, "[\033[93;1mWARNING\033[0m]", lines...)
|
||||
func Warn(line interface{}, args ...interface{}) {
|
||||
logGeneric(os.Stdout, "[\033[93;1mWARNING\033[0m]", line, args...)
|
||||
}
|
||||
|
||||
func LogError(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric(os.Stderr, "[\033[91;1mERROR\033[0m]", lines...)
|
||||
func Error(line interface{}, args ...interface{}) {
|
||||
logGeneric(os.Stderr, "[\033[91;1mERROR\033[0m]", line, args...)
|
||||
}
|
||||
|
||||
func LogFatal(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric(os.Stderr, "[\033[91;1mFATAL\033[0m]", lines...)
|
||||
func Fatal(line interface{}, args ...interface{}) {
|
||||
logGeneric(os.Stderr, "[\033[91;1mFATAL\033[0m]", line, args...)
|
||||
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")
|
||||
}
|
||||
|
||||
45
code/golang/internal/core/logging/logging_auxiliary.go
Normal file
45
code/golang/internal/core/logging/logging_auxiliary.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package logging
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"ads/internal/core/utils"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* AUXILIARY METHODS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func logGeneric(pipe *os.File, tag string, line interface{}, args ...interface{}) {
|
||||
if !force && quietmode {
|
||||
return
|
||||
}
|
||||
if !(tag == "") {
|
||||
tag = tag + " "
|
||||
}
|
||||
fmt.Fprintln(pipe, decorateLine(loggingPrefix, tag, expandLine(line, args)))
|
||||
}
|
||||
|
||||
func expandLine(line interface{}, args []interface{}) string {
|
||||
return fmt.Sprintf(fmt.Sprintf("%s", line), args...)
|
||||
}
|
||||
|
||||
func decorateLine(loggingPrefix string, tag string, line string) string {
|
||||
return stripAnsiIfNecessary(fmt.Sprintf("%[1]s%[2]s%[3]v", loggingPrefix, tag, line))
|
||||
}
|
||||
|
||||
func stripAnsiIfNecessary(line string) string {
|
||||
if !ansimode {
|
||||
line = utils.StripAnsi(line)
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
func ClearScreen() {
|
||||
fmt.Print("\033[2J")
|
||||
}
|
||||
46
code/golang/internal/core/logging/logging_prompt.go
Normal file
46
code/golang/internal/core/logging/logging_prompt.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package logging
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"ads/pkg/re"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD prompts
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
// 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 {
|
||||
tag := ""
|
||||
for _, line := range lines {
|
||||
logGeneric(pipe, tag, line)
|
||||
}
|
||||
logGeneric(pipe, tag, "")
|
||||
}
|
||||
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
|
||||
}
|
||||
49
code/golang/internal/core/logging/logging_settings.go
Normal file
49
code/golang/internal/core/logging/logging_settings.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package logging
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* 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
|
||||
}
|
||||
Reference in New Issue
Block a user