From 64b537d7991036d9585b89028bbf5d386a0e8396 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 7 Apr 2020 21:50:12 +0200 Subject: [PATCH] Extract logic to build log filepath into a function This will get re-used when parsing logs. --- logger.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/logger.go b/logger.go index f3c107e..6cc5302 100644 --- a/logger.go +++ b/logger.go @@ -14,8 +14,8 @@ type messageLogger struct { conn *upstreamConn entity string - filename string - file *os.File + path string + file *os.File } func newMessageLogger(uc *upstreamConn, entity string) *messageLogger { @@ -25,6 +25,16 @@ func newMessageLogger(uc *upstreamConn, entity string) *messageLogger { } } +func logPath(network *network, entity string, t time.Time) string { + user := network.user + srv := user.srv + + // TODO: handle/forbid network/entity names with illegal path characters + year, month, day := t.Date() + filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day) + return filepath.Join(srv.LogPath, user.Username, network.GetName(), entity, filename) +} + func (ml *messageLogger) Append(msg *irc.Message) error { s := formatMessage(msg) if s == "" { @@ -36,32 +46,29 @@ func (ml *messageLogger) Append(msg *irc.Message) error { // TODO: enforce maximum open file handles (LRU cache of file handles) // TODO: handle non-monotonic clock behaviour now := time.Now() - year, month, day := now.Date() - filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day) - if ml.filename != filename { + path := logPath(ml.conn.network, ml.entity, now) + if ml.path != path { if ml.file != nil { ml.file.Close() } - // TODO: handle/forbid network/entity names with illegal path characters - dir := filepath.Join(ml.conn.srv.LogPath, ml.conn.user.Username, ml.conn.network.GetName(), ml.entity) + dir := filepath.Dir(path) if err := os.MkdirAll(dir, 0700); err != nil { return fmt.Errorf("failed to create logs directory %q: %v", dir, err) } - path := filepath.Join(dir, filename) f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { return fmt.Errorf("failed to open log file %q: %v", path, err) } - ml.filename = filename + ml.path = path ml.file = f } _, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s) if err != nil { - return fmt.Errorf("failed to log message to %q: %v", ml.filename, err) + return fmt.Errorf("failed to log message to %q: %v", ml.path, err) } return nil }