attempt 'capture' mode

This commit is contained in:
Jeff Carr 2024-11-13 21:31:55 -06:00
parent 1bcdcfa2f4
commit 72d75ee478
2 changed files with 28 additions and 4 deletions

View File

@ -27,6 +27,7 @@ package log
import ( import (
"net/http" "net/http"
"os"
"sync" "sync"
) )
@ -56,6 +57,7 @@ type LogFlag struct {
var flags []*LogFlag var flags []*LogFlag
var daemonMode bool var daemonMode bool
var captureMode *os.File
var httpMode http.ResponseWriter var httpMode http.ResponseWriter
func init() { func init() {

View File

@ -7,12 +7,17 @@ import (
"fmt" "fmt"
reallog "log" reallog "log"
"net/http" "net/http"
"os"
) )
func DaemonMode(b bool) { func DaemonMode(b bool) {
daemonMode = b daemonMode = b
} }
func CaptureMode(f *os.File) {
captureMode = f
}
func HttpMode(w http.ResponseWriter) { func HttpMode(w http.ResponseWriter) {
httpMode = w httpMode = w
} }
@ -27,9 +32,18 @@ func DaemonShow() {
func realPrintln(a ...any) { func realPrintln(a ...any) {
if daemonMode { if daemonMode {
fmt.Println(a...) if captureMode == nil {
fmt.Println(a...)
} else {
fmt.Fprintln(captureMode, a...)
}
} else { } else {
reallog.Println(a...) if captureMode == nil {
fmt.Println(a...)
} else {
// TODO: add datestamp
fmt.Fprintln(captureMode, a...)
}
} }
if httpMode != nil { if httpMode != nil {
s := fmt.Sprint(a...) s := fmt.Sprint(a...)
@ -39,9 +53,17 @@ func realPrintln(a ...any) {
func realPrintf(s string, a ...any) { func realPrintf(s string, a ...any) {
if daemonMode { if daemonMode {
fmt.Printf(s, a...) if captureMode == nil {
fmt.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
} else { } else {
reallog.Printf(s, a...) if captureMode == nil {
reallog.Printf(s, a...)
} else {
fmt.Fprintf(captureMode, s, a...)
}
} }
if httpMode != nil { if httpMode != nil {
fmt.Fprintln(httpMode, fmt.Sprintf(s, a...)) fmt.Fprintln(httpMode, fmt.Sprintf(s, a...))