logger
Some checks failed
builder / build (push) Failing after 24s

This commit is contained in:
abs3nt 2024-10-26 19:27:45 -07:00
parent 41b6637ede
commit 88af3135fc
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
2 changed files with 88 additions and 2 deletions

View File

@ -22,7 +22,8 @@ func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
Commander: c, Commander: c,
} }
rpc.Register(&CommandHandler) server := rpc.NewServer()
server.Register(&CommandHandler)
listener, err := net.Listen("unix", socketPath) listener, err := net.Listen("unix", socketPath)
if err != nil { if err != nil {
@ -39,6 +40,7 @@ func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
log.Println("Accept error:", err) log.Println("Accept error:", err)
continue continue
} }
go rpc.ServeConn(conn) codec := NewLoggingServerCodec(conn)
go server.ServeCodec(codec)
} }
} }

View File

@ -0,0 +1,84 @@
package daemon
import (
"encoding/gob"
"io"
"log/slog"
"net/rpc"
"os"
"reflect"
)
var logger *slog.Logger
func init() {
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
logger = slog.New(handler)
}
type LoggingServerCodec struct {
dec *gob.Decoder
enc *gob.Encoder
c io.Closer
}
func NewLoggingServerCodec(conn io.ReadWriteCloser) *LoggingServerCodec {
return &LoggingServerCodec{
dec: gob.NewDecoder(conn),
enc: gob.NewEncoder(conn),
c: conn,
}
}
func formatBody(body interface{}) slog.Value {
if body == nil {
return slog.StringValue("null")
}
v := reflect.ValueOf(body)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
body = v.Interface()
}
return slog.AnyValue(body)
}
func (c *LoggingServerCodec) ReadRequestHeader(r *rpc.Request) error {
err := c.dec.Decode(r)
if err == nil {
logger.Info("Received Request",
"ServiceMethod", r.ServiceMethod,
)
}
return err
}
func (c *LoggingServerCodec) ReadRequestBody(body interface{}) error {
err := c.dec.Decode(body)
if err == nil {
logger.Info("Request Body",
"Body", formatBody(body),
)
}
return err
}
func (c *LoggingServerCodec) WriteResponse(r *rpc.Response, body interface{}) error {
if err := c.enc.Encode(r); err != nil {
return err
}
if err := c.enc.Encode(body); err != nil {
return err
}
logger.Info("Sent Response",
"ServiceMethod", r.ServiceMethod,
"Error", r.Error,
"Body", formatBody(body),
)
return nil
}
func (c *LoggingServerCodec) Close() error {
return c.c.Close()
}