diff main.c @ 541:44f67ec5e945

Improve logging facilities. Private chats in query windows are now logged separately. A log file directory can be set in configuration. Room log files are always of format room_%d.ext. Log file extension can be set, default is ".log".
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 11 Nov 2012 18:32:42 +0200
parents ffacb78d9b9f
children d8184a3c241f
line wrap: on
line diff
--- a/main.c	Sun Nov 11 07:33:31 2012 +0200
+++ b/main.c	Sun Nov 11 18:32:42 2012 +0200
@@ -21,10 +21,10 @@
 
 #ifdef __WIN32
 #define SET_CONFIG_FILE    "nnchat.txt"
-#define SET_DIR_SEPARATOR  "\\"
+#define SET_DIR_SEPARATOR  '\\'
 #else
 #define SET_CONFIG_FILE    ".nnchat"
-#define SET_DIR_SEPARATOR  "/"
+#define SET_DIR_SEPARATOR  '/'
 #endif
 
 #define SET_PROFILE_PREFIX "http://www.newbienudes.com/profile/%s/"
@@ -47,12 +47,12 @@
         *optUserNameEnc = NULL,
         *optPassword = NULL,
         *optPasswordCmd = NULL,
-        *optLogFilename = NULL,
+        *optLogPath = NULL,
+        *optLogExtension = ".log",
         *optSite = "NN",
         *optNickSepStr = NULL;
 char    optNickSep;
 BOOL    optDaemon = FALSE;
-FILE    *optLogFile = NULL;
 BOOL    setIgnoreMode = FALSE,
         optDebug = FALSE,
         optLogEnable = FALSE,
@@ -89,7 +89,6 @@
     { 2, 'p', "port",       "Connect to port", OPT_ARGREQ },
     { 3, 's', "server",     "Server to connect to", OPT_ARGREQ },
     { 4, 'C', "color",      "Initial color in RGB hex 000000", OPT_ARGREQ },
-    { 5, 'l', "logfile",    "Log filename", OPT_ARGREQ },
     { 6, 'D', "daemon",     "A pseudo-daemon mode for logging", OPT_NONE },
     { 7, 'f', "force-site", "Force site (default: NN)", OPT_ARGREQ },
     { 8, 'd', "debug",      "Enable various debug features", OPT_NONE },
@@ -142,11 +141,6 @@
         THMSG(1, "Using color #%06x\n", optUserColor);
         break;
 
-    case 5:
-        optLogFilename = optArg;
-        optLogEnable = TRUE;
-        break;
-
     case 7:
         optSite = optArg;
         break;
@@ -247,11 +241,11 @@
 
     buf = th_strdup_vprintf(fmt, ap);
 
-    if (optLogFile && (flags & LOG_FILE))
+    if (win != NULL && win->logFile && (flags & LOG_FILE))
     {
-        if (flags & LOG_STAMP) printFile(optLogFile, tmpStr);
-        printFile(optLogFile, buf);
-        fflush(optLogFile);
+        if (flags & LOG_STAMP) printFile(win->logFile, tmpStr);
+        printFile(win->logFile, buf);
+        fflush(win->logFile);
     }
 
     if (!optDaemon && (flags & LOG_WINDOW))
@@ -1332,38 +1326,6 @@
 }
 
 
-BOOL nn_log_file_open(void)
-{
-    char *filename;
-
-    if (optLogFilename == NULL || !optLogEnable)
-        return FALSE;
-
-    filename = nn_log_parse_filename(optLogFilename, optPort);
-
-    if ((optLogFile = fopen(filename, "a")) == NULL)
-    {
-        errorMsg("Could not open logfile '%s' for appending!\n", filename);
-        th_free(filename);
-        return FALSE;
-    }
-
-    th_free(filename);
-
-    return TRUE;
-}
-
-
-void nn_log_file_close(void)
-{
-    if (optLogFile)
-    {
-        fclose(optLogFile);
-        optLogFile = NULL;
-    }
-}
-
-
 BOOL processUserInput(int c, nn_editbuf_t *editBuf, nn_editstate_t *editState)
 {
     // Chat window switching via Meta/Esc-[1..9]
@@ -1522,6 +1484,82 @@
     st->debugMsg = debugMsg;
 }
 
+
+BOOL nn_log_open(nn_window_t *win)
+{
+    char *path = NULL;
+
+    if (!optLogEnable)
+        return FALSE;
+
+    th_free(win->logFilename);
+    win->logFilename = NULL;
+
+    if (optLogPath != NULL)
+    {
+        char *lt = strrchr(optLogPath, SET_DIR_SEPARATOR);
+        if (lt == NULL || lt[1] != 0)
+            path = th_strdup_printf("%s%c", optLogPath, SET_DIR_SEPARATOR);
+        else
+            path = th_strdup(optLogPath);
+    }
+
+    if (win->id == NULL)
+    {
+        win->logFilename = th_strdup_printf("%sroom_%d%s",
+            path, optPort, optLogExtension);
+    }
+    else
+    {
+        size_t pos;
+        char *cleaned;
+        
+        if ((cleaned = th_strdup(win->id)) == NULL)
+            return FALSE;
+
+        for (pos = 0; cleaned[pos] != 0; pos++)
+        {
+            if (!isalnum(cleaned[pos]))
+                cleaned[pos] = '_';
+        }
+
+        win->logFilename = th_strdup_printf("%s%s%s", path, cleaned, optLogExtension);
+        th_free(cleaned);
+    }
+    
+    if (win->logFilename == NULL)
+        goto error;
+
+    if ((win->logFile = fopen(win->logFilename, "a")) == NULL)
+    {
+        errorMsg("Could not open logfile '%s' for appending!\n", win->logFilename);
+        goto error;
+    }
+
+    printMsg(win, "Logging to '%s'.\n", win->logFilename);
+
+    th_free(path);
+    return TRUE;
+
+error:
+    th_free(path);
+    th_free(win->logFilename);
+    win->logFilename = NULL;
+    return FALSE;
+}
+
+
+void nn_log_close(nn_window_t *win)
+{
+    if (win->logFile != NULL)
+        fclose(win->logFile);
+    win->logFile = NULL;
+
+    th_free(win->logFilename);
+    win->logFilename = NULL;
+}
+
+
 int main(int argc, char *argv[])
 {
     char *tmpStr;
@@ -1586,11 +1624,16 @@
     th_cfg_add_int(&tmpcfg, "port", &optProxyPort, optProxyPort);
     th_cfg_add_section(&cfg, "proxy", tmpcfg);
 
+
     tmpcfg = NULL;
     th_cfg_add_comment(&tmpcfg, "Enable logging");
     th_cfg_add_bool(&tmpcfg, "enable", &optLogEnable, optLogEnable);
-    th_cfg_add_comment(&tmpcfg, "Log filename format");
-    th_cfg_add_string(&tmpcfg, "filename", &optLogFilename, optLogFilename);
+    th_cfg_add_comment(&tmpcfg, "Log files path");
+    th_cfg_add_string(&tmpcfg, "path", &optLogPath, optLogPath);
+
+    th_cfg_add_comment(&tmpcfg, "Log filename extension");
+    th_cfg_add_string(&tmpcfg, "extension", &optLogExtension, optLogExtension);
+
     th_cfg_add_section(&cfg, "logging", tmpcfg);
 
     // Get home directory path
@@ -1620,7 +1663,7 @@
     if (setHomeDir != NULL)
     {
         FILE *cfgfile;
-        setConfigFile = th_strdup_printf("%s" SET_DIR_SEPARATOR "%s", setHomeDir, SET_CONFIG_FILE);
+        setConfigFile = th_strdup_printf("%s%c%s", setHomeDir, SET_DIR_SEPARATOR, SET_CONFIG_FILE);
 
         THMSG(0, "Reading configuration from '%s'.\n", setConfigFile);
 
@@ -1631,11 +1674,7 @@
         }
     }
 
-    if (optNickSepStr)
-        optNickSep = optNickSepStr[0];
-    else
-        optNickSep = SET_NICK_SEPARATOR;
-
+    optNickSep =  optNickSepStr ? optNickSepStr[0] : SET_NICK_SEPARATOR;
 
     setBrowser = getenv("BROWSER");
     if (setBrowser == NULL)
@@ -1668,9 +1707,6 @@
         th_llist_append(&setIdleMessages, th_strdup("."));
     }
 
-    // Open logfile
-    nn_log_file_open();
-
     // Initialize network
     if (!nn_network_init())
     {
@@ -1898,7 +1934,5 @@
 
     THMSG(1, "Connection terminated.\n");
 
-    nn_log_file_close();
-
     return 0;
 }