Skip to content

Commit f939e32

Browse files
committedApr 1, 2025·
refactor: enhance logging mechanism by implementing logWriter and updating logAction method
1 parent 36af1ed commit f939e32

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
 

‎hub.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"html"
22+
"io"
2223
"os"
2324
"runtime"
2425
"runtime/debug"
@@ -256,7 +257,7 @@ func (h *hub) checkCmd(m []byte) {
256257
}
257258
}()
258259
} else if strings.HasPrefix(sl, "log") {
259-
go logAction(sl)
260+
go h.logAction(sl)
260261
} else if strings.HasPrefix(sl, "restart") {
261262
log.Println("Received restart from the daemon. Why? Boh")
262263
// TODO enable them
@@ -276,12 +277,26 @@ func (h *hub) checkCmd(m []byte) {
276277
}
277278
}
278279

279-
func logAction(sl string) {
280+
type logWriter struct {
281+
onWrite func([]byte)
282+
}
283+
284+
func (u *logWriter) Write(p []byte) (n int, err error) {
285+
u.onWrite(p)
286+
return len(p), nil
287+
}
288+
289+
func (h *hub) logAction(sl string) {
280290
if strings.HasPrefix(sl, "log on") {
281291
*logDump = "on"
282-
// FIXME: pass the loggerSw in the constructor and enable again the log e
283-
// multiWriter := io.MultiWriter(&loggerWs, os.Stderr)
284-
// log.SetOutput(multiWriter)
292+
293+
logWriter := logWriter{}
294+
logWriter.onWrite = func(p []byte) {
295+
h.broadcastSys <- p
296+
}
297+
298+
multiWriter := io.MultiWriter(&logWriter, os.Stderr)
299+
log.SetOutput(multiWriter)
285300
} else if strings.HasPrefix(sl, "log off") {
286301
*logDump = "off"
287302
log.SetOutput(os.Stderr)

0 commit comments

Comments
 (0)
Please sign in to comment.