2024-10-12 16:21:47 -05:00
|
|
|
package log
|
|
|
|
|
|
|
|
// implements 'daemon' mode so switches to fmt
|
|
|
|
// instead of log so that timestamps are not printed twice
|
|
|
|
|
2024-11-07 01:23:43 -06:00
|
|
|
import (
|
2024-10-12 16:21:47 -05:00
|
|
|
"fmt"
|
2024-11-14 04:58:57 -06:00
|
|
|
"io"
|
2024-10-12 16:21:47 -05:00
|
|
|
reallog "log"
|
2024-11-07 01:23:43 -06:00
|
|
|
"net/http"
|
2024-11-28 00:03:36 -06:00
|
|
|
"time"
|
2024-10-12 16:21:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func DaemonMode(b bool) {
|
|
|
|
daemonMode = b
|
|
|
|
}
|
|
|
|
|
2024-11-14 04:58:57 -06:00
|
|
|
var captureMode io.Writer
|
|
|
|
|
|
|
|
func CaptureMode(w io.Writer) {
|
|
|
|
captureMode = w
|
2024-11-13 21:31:55 -06:00
|
|
|
}
|
|
|
|
|
2024-11-28 00:03:36 -06:00
|
|
|
var flusher http.Flusher
|
|
|
|
|
2024-11-07 01:23:43 -06:00
|
|
|
func HttpMode(w http.ResponseWriter) {
|
2024-11-28 00:03:36 -06:00
|
|
|
var ok bool
|
2024-11-07 01:23:43 -06:00
|
|
|
httpMode = w
|
2024-11-28 00:03:36 -06:00
|
|
|
if w == nil {
|
|
|
|
flusher = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
flusher, ok = w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
|
|
flusher = nil
|
|
|
|
return
|
|
|
|
}
|
2024-11-07 01:23:43 -06:00
|
|
|
}
|
|
|
|
|
2024-11-15 20:04:51 -06:00
|
|
|
func DaemonShow() bool {
|
2024-10-12 16:32:08 -05:00
|
|
|
if daemonMode {
|
|
|
|
fmt.Println("daemonMode=true")
|
2024-11-15 20:04:51 -06:00
|
|
|
return true
|
2024-10-12 16:32:08 -05:00
|
|
|
} else {
|
|
|
|
fmt.Println("daemonMode=false")
|
2024-11-15 20:04:51 -06:00
|
|
|
return false
|
2024-10-12 16:32:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 16:21:47 -05:00
|
|
|
func realPrintln(a ...any) {
|
2024-11-07 02:55:46 -06:00
|
|
|
if daemonMode {
|
2024-11-15 20:04:51 -06:00
|
|
|
// in daemon mode, don't put timestamps on each line
|
2024-11-13 21:31:55 -06:00
|
|
|
if captureMode == nil {
|
|
|
|
fmt.Println(a...)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(captureMode, a...)
|
|
|
|
}
|
2024-10-12 16:21:47 -05:00
|
|
|
} else {
|
2024-11-15 20:04:51 -06:00
|
|
|
// put timestamps on each line
|
2024-11-13 21:31:55 -06:00
|
|
|
if captureMode == nil {
|
2024-11-15 20:04:51 -06:00
|
|
|
reallog.Println(a...)
|
2024-11-13 21:31:55 -06:00
|
|
|
} else {
|
|
|
|
// TODO: add datestamp
|
|
|
|
fmt.Fprintln(captureMode, a...)
|
|
|
|
}
|
2024-11-07 02:55:46 -06:00
|
|
|
}
|
|
|
|
if httpMode != nil {
|
2024-11-28 00:03:36 -06:00
|
|
|
now := time.Now()
|
|
|
|
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
|
|
|
|
s := timestamp + " " + fmt.Sprint(a...)
|
2024-11-07 03:11:19 -06:00
|
|
|
fmt.Fprintln(httpMode, s)
|
2024-11-28 00:03:36 -06:00
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush() // Flush the data to the client
|
|
|
|
}
|
2024-10-12 16:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func realPrintf(s string, a ...any) {
|
2024-11-07 02:55:46 -06:00
|
|
|
if daemonMode {
|
2024-11-15 20:04:51 -06:00
|
|
|
// in daemon mode, don't put timestamps on each line
|
2024-11-13 21:31:55 -06:00
|
|
|
if captureMode == nil {
|
|
|
|
fmt.Printf(s, a...)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(captureMode, s, a...)
|
|
|
|
}
|
2024-10-12 16:21:47 -05:00
|
|
|
} else {
|
2024-11-15 20:04:51 -06:00
|
|
|
// put timestamps on each line
|
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 {
|
2024-11-28 00:03:36 -06:00
|
|
|
now := time.Now()
|
|
|
|
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
|
|
|
|
s := timestamp + " " + fmt.Sprintf(s, a...)
|
|
|
|
fmt.Fprintln(httpMode, s)
|
|
|
|
if flusher != nil {
|
|
|
|
flusher.Flush() // Flush the data to the client
|
|
|
|
}
|
2024-10-12 16:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:45:32 -06:00
|
|
|
func realSprint(a ...any) string {
|
|
|
|
return fmt.Sprint(a...)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:11:02 -06:00
|
|
|
func realSprintf(s string, a ...any) string {
|
|
|
|
return fmt.Sprintf(s, a...)
|
|
|
|
}
|
|
|
|
|
2024-11-07 01:23:43 -06:00
|
|
|
func realSprintln(a ...any) string {
|
|
|
|
return fmt.Sprintln(a...)
|
2024-11-04 05:11:02 -06:00
|
|
|
}
|
|
|
|
|
2024-10-12 16:21:47 -05:00
|
|
|
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...)
|
|
|
|
}
|