diff --git a/log.go b/log/log.go similarity index 66% rename from log.go rename to log/log.go index 41642b7..8ffde89 100644 --- a/log.go +++ b/log/log.go @@ -1,4 +1,5 @@ -package main +// Package log instantiates global loggers for stable and error information. +package log import ( "fmt" @@ -12,8 +13,8 @@ const ( ) var ( - // logInfo is used to log stable runtime information. - logInfo = log.New(os.Stdout, fmt.Sprintf("%s%s", "info", sep), 0) - // logErr is used to log errors. - logErr = log.New(os.Stdout, fmt.Sprintf("%s%s", "error", sep), 0) + // Info is used to log stable runtime information. + Info = log.New(os.Stdout, fmt.Sprintf("%s%s", "info", sep), 0) + // Err is used to log errors. + Err = log.New(os.Stdout, fmt.Sprintf("%s%s", "error", sep), 0) ) diff --git a/main.go b/main.go index dfcddfb..306dfb6 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/http" + "wssrv/log" "wssrv/ws" "wssrv/wshttp" @@ -51,14 +52,14 @@ func acceptWsCon(tCon net.Conn) *websocket.Conn { for con == nil { if req, err = http.ReadRequest(r); err != nil { eMsg = fmt.Sprintf("unable to read request: %v", err) - logErr.Print(eMsg) + log.Err.Print(eMsg) http.Error(rw, fmt.Sprintf("error: %s", eMsg), http.StatusBadRequest) continue } if con, err = websocket.Upgrade(rw, req, nil, defBufSiz, defBufSiz); err != nil { eMsg = fmt.Sprintf("unable to create websocket connection: %v", err) - logErr.Print(eMsg) + log.Err.Print(eMsg) http.Error(rw, fmt.Sprintf("error: %s", eMsg), http.StatusBadRequest) } } @@ -75,11 +76,12 @@ func main() { listenAddr = fmt.Sprintf("%s:%d", defHost, port) ) - logInfo.Printf("listening on '%s'", listenAddr) + log.Info.Printf("listening on '%s'", listenAddr) if tCon, err = acceptTcpCon(listenAddr); err != nil { panic(err) } con = acceptWsCon(tCon) + log.Info.Printf("connected to '%s'", con.RemoteAddr()) defer con.Close() fmt.Println(ws.SendStdinLines(con))