Skip to content

Commit 712861f

Browse files
committed
cleanup: Make websockify output qtox-compatible logging.
At the moment this is just because qtox outputs logs in this format and now I'm used to it, so I want to unify our log outputs to this format. We don't really parse this output, but after this change, we could theoretically parse the websockify log output in the qtox/debugtox log viewer.
1 parent 01932ea commit 712861f

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

other/bootstrap_daemon/websocket/websockify/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
22
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
3+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
4+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
35
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
46
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
57
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -17,6 +19,7 @@ golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
1719
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
1820
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
1921
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
22+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
2023
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2124
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2225
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

other/bootstrap_daemon/websocket/websockify/websockify.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// - Proper logging.
99
// - Proper error handling in general.
1010
// - Support both websocket and regular GET requests on /.
11+
// - Write logs in the standard Tox format.
1112
//
1213
// Copyright 2022-2025 The TokTok team.
1314
// Copyright 2021 Michael.liu.
@@ -22,6 +23,9 @@ import (
2223
"log"
2324
"net"
2425
"net/http"
26+
"os"
27+
"regexp"
28+
"strings"
2529

2630
"github.com/gorilla/websocket"
2731
)
@@ -31,18 +35,20 @@ var (
3135
targetAddr = flag.String("t", "127.0.0.1:5900", "tcp service address")
3236
)
3337

38+
// Should be enough to fit any Tox TCP packets.
39+
const bufferSize = 2048
40+
3441
var upgrader = websocket.Upgrader{
35-
// Should be enough to fit any Tox TCP packets.
36-
ReadBufferSize: 2048,
37-
WriteBufferSize: 2048,
42+
ReadBufferSize: bufferSize,
43+
WriteBufferSize: bufferSize,
3844
Subprotocols: []string{"binary"},
3945
CheckOrigin: func(r *http.Request) bool {
4046
return true
4147
},
4248
}
4349

4450
func forwardTCP(wsconn *websocket.Conn, conn net.Conn) {
45-
var tcpbuffer [2048]byte
51+
var tcpbuffer [bufferSize]byte
4652
defer wsconn.Close()
4753
defer conn.Close()
4854
for {
@@ -97,7 +103,52 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
97103

98104
}
99105

106+
type logEntry struct {
107+
time string
108+
file string
109+
line string
110+
message string
111+
}
112+
113+
type logWriter struct{}
114+
115+
// Write implements the io.Writer interface.
116+
//
117+
// This parses the Go log format and outputs it as the standard Tox format.
118+
//
119+
// Go format:
120+
// "15:02:46.433968 websockify.go:106: Starting up websockify endpoint\n"
121+
//
122+
// Standard Tox format:
123+
// "[15:02:46.433 UTC] (websockify) websockify.go:106 : Debug: Starting up websockify endpoint"
124+
func (writer *logWriter) Write(bytes []byte) (int, error) {
125+
// Parse the Go log format (skipping the last 3 digits of the microseconds).
126+
re := regexp.MustCompile(`^(\d{2}:\d{2}:\d{2}\.\d{3})\d{3} ([^:]+):(\d+): (.*)$`)
127+
matches := re.FindStringSubmatch(strings.TrimSuffix(string(bytes), "\n"))
128+
if len(matches) != 5 {
129+
// If the log format doesn't match, just print it as is.
130+
fmt.Fprintf(os.Stderr, "%s (unparsed)", string(bytes))
131+
return len(bytes), nil
132+
}
133+
134+
// Extract the log fields.
135+
entry := logEntry{
136+
time: matches[1],
137+
file: matches[2],
138+
line: matches[3],
139+
message: matches[4],
140+
}
141+
142+
// Print the Go log format in the standard Tox format to stderr.
143+
fmt.Fprintf(os.Stderr, "[%s UTC] (websockify) %s:%s : Debug: %s\n", entry.time, entry.file, entry.line, entry.message)
144+
145+
return len(bytes), nil
146+
}
147+
100148
func main() {
149+
log.SetFlags(log.Ltime | log.Lshortfile | log.LUTC | log.Lmicroseconds)
150+
log.SetOutput(new(logWriter))
151+
101152
flag.Parse()
102153
log.Println("Starting up websockify endpoint")
103154

0 commit comments

Comments
 (0)