rui_orig/appLog.go

77 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
"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
2022-11-09 13:40:08 +03:00
var debugLogFunc func(string) = debugLog
var errorLogFunc func(string) = errorLog
2021-09-07 17:36:50 +03:00
// 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)
}
}
}