home » torr/wssrv.git
Author C. Torres <torr.c@mailgw.com> 2024-12-13 23:45:36
Committer C. Torres <torr.c@mailgw.com> 2024-12-13 23:45:36
Commit 7770d0d (patch)
Tree 888064e
Parent(s)

Log when accepting a connection Add a log message with the remote address once a new websocket connection is established. Signed-off-by: C. Torres <torr.c@mailgw.com>


commits diff: f373204..7770d0d
2 files changed, 11 insertions, 8 deletionsdownload


Diffstat
-rw-r--r-- log/log.go 11
-rw-r--r-- main.go 8

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+6/-5 R   log.go -> log/log.go
index 41642b7..8ffde89
old size: 356B - new size: 420B
@@ -1,4 +1,5 @@
1 - package main
1 + // Package log instantiates global loggers for stable and error information.
2 + package log
2 3
3 4 import (
4 5 "fmt"
@@ -12,8 +13,8 @@ const (
12 13 )
13 14
14 15 var (
15 - // logInfo is used to log stable runtime information.
16 - logInfo = log.New(os.Stdout, fmt.Sprintf("%s%s", "info", sep), 0)
17 - // logErr is used to log errors.
18 - logErr = log.New(os.Stdout, fmt.Sprintf("%s%s", "error", sep), 0)
16 + // Info is used to log stable runtime information.
17 + Info = log.New(os.Stdout, fmt.Sprintf("%s%s", "info", sep), 0)
18 + // Err is used to log errors.
19 + Err = log.New(os.Stdout, fmt.Sprintf("%s%s", "error", sep), 0)
19 20 )

+5/-3 M   main.go
index dfcddfb..306dfb6
old size: 1K - new size: 2K
@@ -5,6 +5,7 @@ import (
5 5 "fmt"
6 6 "net"
7 7 "net/http"
8 + "wssrv/log"
8 9 "wssrv/ws"
9 10 "wssrv/wshttp"
10 11
@@ -51,14 +52,14 @@ func acceptWsCon(tCon net.Conn) *websocket.Conn {
51 52 for con == nil {
52 53 if req, err = http.ReadRequest(r); err != nil {
53 54 eMsg = fmt.Sprintf("unable to read request: %v", err)
54 - logErr.Print(eMsg)
55 + log.Err.Print(eMsg)
55 56 http.Error(rw, fmt.Sprintf("error: %s", eMsg), http.StatusBadRequest)
56 57 continue
57 58 }
58 59
59 60 if con, err = websocket.Upgrade(rw, req, nil, defBufSiz, defBufSiz); err != nil {
60 61 eMsg = fmt.Sprintf("unable to create websocket connection: %v", err)
61 - logErr.Print(eMsg)
62 + log.Err.Print(eMsg)
62 63 http.Error(rw, fmt.Sprintf("error: %s", eMsg), http.StatusBadRequest)
63 64 }
64 65 }
@@ -75,11 +76,12 @@ func main() {
75 76 listenAddr = fmt.Sprintf("%s:%d", defHost, port)
76 77 )
77 78
78 - logInfo.Printf("listening on '%s'", listenAddr)
79 + log.Info.Printf("listening on '%s'", listenAddr)
79 80 if tCon, err = acceptTcpCon(listenAddr); err != nil {
80 81 panic(err)
81 82 }
82 83 con = acceptWsCon(tCon)
84 + log.Info.Printf("connected to '%s'", con.RemoteAddr())
83 85 defer con.Close()
84 86
85 87 fmt.Println(ws.SendStdinLines(con))