forked from mbk-lab/rui_orig
2
0
Fork 0
rui/appLog.go

84 lines
1.7 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"log"
"runtime"
)
// ProtocolInDebugLog If it is set to true, then the protocol of the exchange between
// clients and the server is displayed in the debug log
var ProtocolInDebugLog = false
var debugLogFunc func(string) = func(text string) {
log.Println("\033[34m" + text)
}
var errorLogFunc = func(text string) {
log.Println("\033[31m" + text)
//println(text)
}
// SetDebugLog sets a function for outputting debug info.
// The default value is nil (debug info is ignored)
func SetDebugLog(f func(string)) {
debugLogFunc = f
}
// SetErrorLog sets a function for outputting error messages.
// The default value is log.Println(text)
func SetErrorLog(f func(string)) {
errorLogFunc = f
}
// DebugLog print the text to the debug log
func DebugLog(text string) {
if debugLogFunc != nil {
debugLogFunc(text)
}
}
// DebugLogF print the text to the debug log
2022-07-26 18:36:00 +03:00
func DebugLogF(format string, a ...any) {
2021-09-07 17:36:50 +03:00
if debugLogFunc != nil {
debugLogFunc(fmt.Sprintf(format, a...))
}
}
2021-11-04 14:59:25 +03:00
var lastError = ""
2021-09-07 17:36:50 +03:00
// ErrorLog print the text to the error log
func ErrorLog(text string) {
2021-11-04 14:59:25 +03:00
lastError = text
2021-09-07 17:36:50 +03:00
if errorLogFunc != nil {
errorLogFunc(text)
errorStack()
}
}
// ErrorLogF print the text to the error log
2022-07-26 18:36:00 +03:00
func ErrorLogF(format string, a ...any) {
2021-11-04 14:59:25 +03:00
lastError = fmt.Sprintf(format, a...)
2021-09-07 17:36:50 +03:00
if errorLogFunc != nil {
2021-11-04 14:59:25 +03:00
errorLogFunc(lastError)
2021-09-07 17:36:50 +03:00
errorStack()
}
}
2021-11-04 14:59:25 +03:00
// LastError returns the last error text
func LastError() string {
return lastError
}
2021-09-07 17:36:50 +03:00
func errorStack() {
if errorLogFunc != nil {
skip := 2
_, file, line, ok := runtime.Caller(skip)
for ok {
errorLogFunc(fmt.Sprintf("\t%s: line %d", file, line))
skip++
_, file, line, ok = runtime.Caller(skip)
}
}
}