log/reallog.go

96 lines
1.5 KiB
Go
Raw Normal View History

package log
// implements 'daemon' mode so switches to fmt
// instead of log so that timestamps are not printed twice
import (
"fmt"
reallog "log"
"net/http"
2024-11-13 21:31:55 -06:00
"os"
)
func DaemonMode(b bool) {
daemonMode = b
}
2024-11-13 21:31:55 -06:00
func CaptureMode(f *os.File) {
captureMode = f
}
func HttpMode(w http.ResponseWriter) {
httpMode = w
}
func DaemonShow() {
if daemonMode {
fmt.Println("daemonMode=true")
} else {
fmt.Println("daemonMode=false")
}
}
func realPrintln(a ...any) {
2024-11-07 02:55:46 -06:00
if daemonMode {
2024-11-13 21:31:55 -06:00
if captureMode == nil {
fmt.Println(a...)
} else {
fmt.Fprintln(captureMode, a...)
}
} else {
2024-11-13 21:31:55 -06:00
if captureMode == nil {
fmt.Println(a...)
} else {
// TODO: add datestamp
fmt.Fprintln(captureMode, a...)
}
2024-11-07 02:55:46 -06:00
}
if httpMode != nil {
2024-11-07 05:08:42 -06:00
s := fmt.Sprint(a...)
2024-11-07 03:11:19 -06:00
fmt.Fprintln(httpMode, s)
}
}
func realPrintf(s string, a ...any) {
2024-11-07 02:55:46 -06:00
if daemonMode {
2024-11-13 21:31:55 -06:00
if captureMode == nil {
fmt.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
} else {
2024-11-13 21:31:55 -06:00
if captureMode == nil {
reallog.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
2024-11-07 02:55:46 -06:00
}
if httpMode != nil {
fmt.Fprintln(httpMode, fmt.Sprintf(s, a...))
}
}
func realSprint(a ...any) string {
return fmt.Sprint(a...)
}
func realSprintf(s string, a ...any) string {
return fmt.Sprintf(s, a...)
}
func realSprintln(a ...any) string {
return fmt.Sprintln(a...)
}
func realFatalln(a ...any) {
reallog.Fatalln(a...)
}
func realFatalf(s string, a ...any) {
reallog.Fatalf(s, a...)
}
func realFatal(s string, a ...any) {
reallog.Fatalf(s, a...)
}