diff --git a/reallog.go b/reallog.go index fe9062d..9962329 100644 --- a/reallog.go +++ b/reallog.go @@ -8,6 +8,7 @@ import ( "io" reallog "log" "net/http" + "time" ) func DaemonMode(b bool) { @@ -20,8 +21,21 @@ func CaptureMode(w io.Writer) { captureMode = w } +var flusher http.Flusher + func HttpMode(w http.ResponseWriter) { + var ok bool httpMode = w + if w == nil { + flusher = nil + return + } + flusher, ok = w.(http.Flusher) + if !ok { + http.Error(w, "Streaming unsupported!", http.StatusInternalServerError) + flusher = nil + return + } } func DaemonShow() bool { @@ -52,8 +66,13 @@ func realPrintln(a ...any) { } } if httpMode != nil { - s := fmt.Sprint(a...) + now := time.Now() + timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work? + s := timestamp + " " + fmt.Sprint(a...) fmt.Fprintln(httpMode, s) + if flusher != nil { + flusher.Flush() // Flush the data to the client + } } } @@ -74,7 +93,13 @@ func realPrintf(s string, a ...any) { } } if httpMode != nil { - fmt.Fprintln(httpMode, fmt.Sprintf(s, a...)) + 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 + } } }