# HG changeset patch # User Matti Hamalainen # Date 1352740290 -7200 # Node ID 13901e9be15b865aab103604472713cc4b03c99b # Parent b464409fa643e8db7acc23d771c4a96f8f55fd30 Implement daily logs for room logs. diff -r b464409fa643 -r 13901e9be15b main.c --- 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); diff -r b464409fa643 -r 13901e9be15b ui.h --- 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 #endif +#include #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;