changeset 554:13901e9be15b

Implement daily logs for room logs.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 12 Nov 2012 19:11:30 +0200
parents b464409fa643
children 66d18a82d6b9
files main.c ui.h
diffstat 2 files changed, 56 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Mon Nov 12 19:10:58 2012 +0200
+++ b/main.c	Mon Nov 12 19:11:30 2012 +0200
@@ -61,6 +61,7 @@
         setIgnoreMode = FALSE,
         optDebug = FALSE,
         optLogEnable = FALSE,
+        optLogDaily = FALSE,
         optOnlyFriendPrv = FALSE;
 
 qlist_t *setIgnoreList = NULL,
@@ -83,6 +84,7 @@
     LOG_WINDOW     = 0x0002,
     LOG_STAMP      = 0x0004,
     LOG_FILE2      = 0x0008,
+    LOG_RECURSIVE  = 0x0010,
 };
 
 
@@ -246,9 +248,34 @@
     }
 }
 
+
+void printMsgF(nn_window_t *win, int flags, const char *fmt, ...);
+BOOL nn_log_reopen(nn_window_t *win);
+
+
 void printMsgV(nn_window_t *win, int flags, const char *fmt, va_list ap)
 {
     char tmpStr[128], *buf;
+    nn_window_t *tmpwin = (win != NULL) ? win : nnwin_main_window();
+
+    // Only the main window 
+    if (win == NULL && (flags & LOG_RECURSIVE) == 0)
+    {
+        time_t currTime = time(NULL);
+        struct tm *currTm, *prevTm;
+
+        if ((currTm = localtime(&currTime)) != NULL &&
+            currTm->tm_hour == 0 &&
+            (prevTm = localtime(&tmpwin->logPrevMsgTime)) != NULL &&
+            prevTm->tm_hour == 23)
+        {
+            str_get_timestamp(tmpStr, sizeof(tmpStr), "%d %b %Y");
+            printMsgF(win, LOG_RECURSIVE, "Day changed to %s.\n", tmpStr);
+            nn_log_reopen(tmpwin);
+        }
+
+        tmpwin->logPrevMsgTime = currTime;
+    }
 
     if (flags & LOG_STAMP)
     {
@@ -269,9 +296,8 @@
 
     if (!optDaemon && (flags & LOG_WINDOW))
     {
-        nn_window_t *tmp = (win != NULL) ? win : nnwin_main_window();
-        if (flags & LOG_STAMP) nnwin_print(tmp, tmpStr);
-        nnwin_print(tmp, buf);
+        if (flags & LOG_STAMP) nnwin_print(tmpwin, tmpStr);
+        nnwin_print(tmpwin, buf);
     }
 
     th_free(buf);
@@ -1522,8 +1548,20 @@
 
     if (win->id == NULL)
     {
-        win->logFilename = th_strdup_printf("%sroom_%d%s",
-            path != NULL ? path : "", optPort, optLogExtension);
+        // Main window log (aka room log)
+        if (optLogDaily)
+        {
+            char stamp[64];
+            str_get_timestamp(stamp, sizeof(stamp), "%Y-%m-%d");
+            win->logFilename = th_strdup_printf("%sroom_%d-%s%s",
+                path != NULL ? path : "", optPort,
+                stamp, optLogExtension);
+        }
+        else
+        {
+            win->logFilename = th_strdup_printf("%sroom_%d%s",
+                path != NULL ? path : "", optPort, optLogExtension);
+        }
     }
     else
     {
@@ -1580,6 +1618,13 @@
 }
 
 
+BOOL nn_log_reopen(nn_window_t *win)
+{
+    nn_log_close(win);
+    return nn_log_open(win);
+}
+
+
 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable)
 {
 #ifdef __WIN32
@@ -1719,6 +1764,10 @@
     tmpcfg = NULL;
     th_cfg_add_comment(&tmpcfg, "Enable logging");
     th_cfg_add_bool(&tmpcfg, "enable", &optLogEnable, optLogEnable);
+
+    th_cfg_add_comment(&tmpcfg, "Use daily logfiles for room logs");
+    th_cfg_add_bool(&tmpcfg, "daily", &optLogDaily, optLogDaily);
+
     th_cfg_add_comment(&tmpcfg, "Log files path");
     th_cfg_add_string(&tmpcfg, "path", &optLogPath, optLogPath);
 
--- a/ui.h	Mon Nov 12 19:10:58 2012 +0200
+++ b/ui.h	Mon Nov 12 19:11:30 2012 +0200
@@ -19,6 +19,7 @@
 #else
 #include <curses.h>
 #endif
+#include <time.h>
 #include "th_types.h"
 #include "th_string.h"
 #include "network.h"
@@ -48,6 +49,7 @@
     // Logging
     char *logFilename;
     FILE *logFile;
+    time_t logPrevMsgTime;
 } nn_window_t;