summaryrefslogtreecommitdiff
path: root/reallog.go
blob: 37a290a0b33a9b48fc9aff0abe3840c62dd31e66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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"
	"time"
)

func DaemonMode(b bool) {
	daemonMode = b
}

func Timestamps(b bool) {
	timestamps = b
}

var captureMode io.Writer

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 {
	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 {
			if timestamps {
				reallog.Println(a...)
			} else {
				fmt.Println(a...)
			}
		} else {
			// TODO: add datestamp
			fmt.Fprintln(captureMode, a...)
		}
	}
	if httpMode != nil {
		var timestamp string
		if timestamps {
			now := time.Now()
			timestamp = now.Format("2006/01/02 15:04:05") // todo: fix GO so Nov 5 1955 works here
		}
		s := timestamp + " " + fmt.Sprint(a...)
		fmt.Fprintln(httpMode, s)
		if flusher != nil {
			flusher.Flush() // Flush the data to the client
		}
	}
}

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 {
			if timestamps {
				reallog.Printf(s, a...)
			} else {
				fmt.Printf(s, a...)
			}
		} else {
			fmt.Fprintf(captureMode, s, a...)
		}
	}
	if httpMode != nil {
		var timestamp string
		if timestamps {
			now := time.Now()
			timestamp = now.Format("2006/01/02 15:04:05")
		}
		s := timestamp + " " + fmt.Sprintf(s, a...)
		fmt.Fprintln(httpMode, s)
		if flusher != nil {
			flusher.Flush() // Flush the data to the client
		}
	}
}

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...)
}