From 3b7e9385f3004fa70ce4ee5b3e8e6abdb7bd2f10 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 12 Dec 2024 08:05:46 +0800 Subject: Use meselog instead of slog --- Makefile | 6 ++---- clients.go | 9 +++++---- cmd_nick.go | 4 ++-- main.go | 25 +++++++++++-------------- meselog/meselog.go | 26 ++++++++++++++++++++++++++ servers.go | 5 +++-- 6 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 meselog/meselog.go diff --git a/Makefile b/Makefile index 83d9213..0281628 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,2 @@ -# Only GNU Make is supported - -meseircd: *.go - go build -o $@ +meseircd: *.go */*.go + go build -o meseircd diff --git a/clients.go b/clients.go index eb97ebe..0744bcf 100644 --- a/clients.go +++ b/clients.go @@ -1,9 +1,10 @@ package main import ( - "log/slog" "net" "sync" + + "git.sr.ht/~runxiyu/meseircd/meselog" ) type Client struct { @@ -27,7 +28,7 @@ func (client *Client) SendRaw(s string) error { if client.conn == nil { panic("not implemented") } - slog.Debug("send", "line", s, "client", client.CID) + meselog.Debug("send", "line", s, "client", client.CID) _, err := (*client.conn).Write([]byte(s)) if err != nil { // TODO: Should shut down the netFd instead but the stdlib @@ -52,11 +53,11 @@ func (client *Client) Teardown() { (*client.conn).Close() } if !cidToClient.CompareAndDelete(client.CID, client) { - slog.Error("cid inconsistent", "cid", client.CID, "client", client) + meselog.Error("cid inconsistent", "cid", client.CID, "client", client) } if client.State >= ClientStateRegistered || client.Nick != "*" { if !nickToClient.CompareAndDelete(client.Nick, client) { - slog.Error("nick inconsistent", "nick", client.Nick, "client", client) + meselog.Error("nick inconsistent", "nick", client.Nick, "client", client) } } } diff --git a/cmd_nick.go b/cmd_nick.go index 4c84a37..0ca7173 100644 --- a/cmd_nick.go +++ b/cmd_nick.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "log/slog" + "git.sr.ht/~runxiyu/meseircd/meselog" ) func init() { @@ -23,7 +23,7 @@ func handleClientNick(msg RMsg, client *Client) error { } } else { if (client.State >= ClientStateRegistered || client.Nick != "*") && !nickToClient.CompareAndDelete(client.Nick, client) { - slog.Error("nick inconsistent", "nick", client.Nick, "client", client) + meselog.Error("nick inconsistent", "nick", client.Nick, "client", client) return fmt.Errorf("%w: %v", ErrInconsistentClient, client) } if client.State == ClientStateRegistered { diff --git a/main.go b/main.go index ecfb4c5..e38974e 100644 --- a/main.go +++ b/main.go @@ -3,17 +3,14 @@ package main import ( "bufio" "log" - "log/slog" "net" - "os" + + "git.sr.ht/~runxiyu/meseircd/meselog" ) const VERSION = "MeseIRCd-0.0.0" func main() { - logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) - slog.SetDefault(logger) - setupCapls() self = &Server{ @@ -38,13 +35,13 @@ func main() { defer func() { raised := recover() if raised != nil { - slog.Error("connection routine panicked", "raised", raised) + meselog.Error("connection routine panicked", "raised", raised) } }() defer conn.Close() client, err := NewLocalClient(&conn) if err != nil { - slog.Error("cannot make new local client", "error", err) + meselog.Error("cannot make new local client", "error", err) } defer client.Teardown() client.handleConnection() @@ -58,11 +55,11 @@ messageLoop: for { line, err := reader.ReadString('\n') if err != nil { - slog.Error("error while reading from connection", "error", err) + meselog.Error("error while reading from connection", "error", err) (*client.conn).Close() return } - slog.Debug("recv", "line", line, "client", client.CID) + meselog.Debug("recv", "line", line, "client", client.CID) msg, err := parseIRCMsg(line) if err != nil { switch err { @@ -71,7 +68,7 @@ messageLoop: case ErrIllegalByte: err := client.Send(MakeMsg(self, "ERROR", err.Error())) if err != nil { - slog.Error("error while reporting illegal byte", "error", err, "client", client) + meselog.Error("error while reporting illegal byte", "error", err, "client", client) return } return @@ -80,14 +77,14 @@ messageLoop: case ErrBodyTooLong: err := client.Send(MakeMsg(self, ERR_INPUTTOOLONG, err.Error())) if err != nil { - slog.Error("error while reporting body too long", "error", err, "client", client) + meselog.Error("error while reporting body too long", "error", err, "client", client) return } continue messageLoop default: err := client.Send(MakeMsg(self, "ERROR", err.Error())) if err != nil { - slog.Error("error while reporting parser error", "error", err, "client", client) + meselog.Error("error while reporting parser error", "error", err, "client", client) } return } @@ -97,7 +94,7 @@ messageLoop: if !ok { err := client.Send(MakeMsg(self, ERR_UNKNOWNCOMMAND, msg.Command, "Unknown command")) if err != nil { - slog.Error("error while reporting unknown command", "error", err, "client", client) + meselog.Error("error while reporting unknown command", "error", err, "client", client) return } continue @@ -105,7 +102,7 @@ messageLoop: err = handler(msg, client) if err != nil { - slog.Error("handler error", "error", err, "client", client) + meselog.Error("handler error", "error", err, "client", client) return } } diff --git a/meselog/meselog.go b/meselog/meselog.go new file mode 100644 index 0000000..d9c84f5 --- /dev/null +++ b/meselog/meselog.go @@ -0,0 +1,26 @@ +package meselog + +import ( + "fmt" +) + +func log(str string, keyvals []any) { + fmt.Print(str+" ") + for i, j := range keyvals { + if i & 1 == 0 { + fmt.Printf("%v=", j) + } else if i == len(keyvals) - 1 { + fmt.Printf("%#v", j) + } else { + fmt.Printf("%#v ", j) + } + } + fmt.Print("\n") +} + +func Error(str string, keyvals ...any) { + log("ERROR "+str, keyvals) +} +func Debug(str string, keyvals ...any) { + log("DEBUG "+str, keyvals) +} diff --git a/servers.go b/servers.go index 59ed979..af31093 100644 --- a/servers.go +++ b/servers.go @@ -1,8 +1,9 @@ package main import ( - "log/slog" "net" + + "git.sr.ht/~runxiyu/meseircd/meselog" ) type Server struct { @@ -23,7 +24,7 @@ func (server *Server) SendRaw(s string) error { // TODO: Propagate across mesh return ErrNotConnectedServer } - slog.Debug("send", "line", s, "conn", server.conn) + meselog.Debug("send", "line", s, "conn", server.conn) _, err := (*server.conn).Write([]byte(s)) if err != nil { // TODO: Should shut down the netFd instead but the stdlib -- cgit v1.2.3