rui_orig/appLog.go

75 lines
1.5 KiB
Go

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
func DebugLogF(format string, a ...interface{}) {
if debugLogFunc != nil {
debugLogFunc(fmt.Sprintf(format, a...))
}
}
// ErrorLog print the text to the error log
func ErrorLog(text string) {
if errorLogFunc != nil {
errorLogFunc(text)
errorStack()
}
}
// ErrorLogF print the text to the error log
func ErrorLogF(format string, a ...interface{}) {
if errorLogFunc != nil {
errorLogFunc(fmt.Sprintf(format, a...))
errorStack()
}
}
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)
}
}
}