log/reallog.go

104 lines
1.7 KiB
Go

package log
// implements 'daemon' mode so switches to fmt
// instead of log so that timestamps are not printed twice
import (
"fmt"
"io"
reallog "log"
"net/http"
)
func DaemonMode(b bool) {
daemonMode = b
}
var captureMode io.Writer
func CaptureMode(w io.Writer) {
captureMode = w
}
func HttpMode(w http.ResponseWriter) {
httpMode = w
}
func DaemonShow() bool {
if daemonMode {
fmt.Println("daemonMode=true")
return true
} else {
fmt.Println("daemonMode=false")
return false
}
}
func realPrintln(a ...any) {
if daemonMode {
// in daemon mode, don't put timestamps on each line
if captureMode == nil {
fmt.Println(a...)
} else {
fmt.Fprintln(captureMode, a...)
}
} else {
// put timestamps on each line
if captureMode == nil {
reallog.Println(a...)
} else {
// TODO: add datestamp
fmt.Fprintln(captureMode, a...)
}
}
if httpMode != nil {
s := fmt.Sprint(a...)
fmt.Fprintln(httpMode, s)
}
}
func realPrintf(s string, a ...any) {
if daemonMode {
// in daemon mode, don't put timestamps on each line
if captureMode == nil {
fmt.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
} else {
// put timestamps on each line
if captureMode == nil {
reallog.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
}
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...)
}