# HG changeset patch # User Matti Hamalainen # Date 1303314522 -10800 # Node ID cb86d7543be2a7a2f86a2fc9947187203760e5f2 # Parent a1ee6c76ca1c515d12c845e7ed2114eb3c62d47c Add functionality for generating logfile names based on port etc. and configuration option for logfile name pattern and enabling logging. diff -r a1ee6c76ca1c -r cb86d7543be2 nnchat.c --- a/nnchat.c Wed Apr 20 18:47:22 2011 +0300 +++ b/nnchat.c Wed Apr 20 18:48:42 2011 +0300 @@ -66,7 +66,7 @@ BOOL setPrvMode = FALSE; BOOL setIgnoreMode = FALSE; BOOL optDebug = FALSE; - +BOOL optLogEnable = FALSE; qlist_t *nnIgnoreList = NULL; nn_userhash_t *nnUsers = NULL; @@ -130,6 +130,7 @@ case 5: optLogFilename = optArg; + optLogEnable = TRUE; break; case 7: @@ -941,42 +942,42 @@ return FALSE; } +#define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH) +#define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR) + char *logParseFilename(const char *fmt, int id) { - size_t tmpLen = strlen(fmt) + 32; - char *res = NULL, tmpBuf[32]; + size_t bufSize = strlen(fmt) + 32, bufLen = 0; + char *bufData = NULL, tmpBuf[32]; const char *s = fmt; - if ((res = th_malloc(len)) == NULL) - return NULL; - while (*s) { if (*s == '%') { s++; switch (*s) { case 'i': - - { snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id); - len -= strlen(tmpBuf) + 1; - if (len <= 0) { - res = th_realloc(res, - } VPUTS(tmpBuf); - } break; case '%': - VPUTCH(res, '%'); + VPUTCH('%'); break; default: - VPUTCH(res, '%'); - VPUTCH(res, *s); + VPUTCH('%'); + VPUTCH(*s); break; } + s++; + } else { + VPUTCH(*s); + s++; } } + + VPUTCH(0); + return bufData; } @@ -984,21 +985,28 @@ { char *filename; - if (optLogFilename == NULL) + if (optLogFilename == NULL || !optLogEnable) return FALSE; - + filename = logParseFilename(optLogFilename, optPort); if ((optLogFile = fopen(filename, "a")) == NULL) { - errMsg("Could not open logfile '%s' for appending!\n", filename); + errorMsg("Could not open logfile '%s' for appending!\n", filename); th_free(filename); return FALSE; } + + th_free(filename); + return TRUE; } -BOOL logFileClose(void) +void logFileClose(void) { + if (optLogFile) { + fclose(optLogFile); + optLogFile = NULL; + } } char *promptRequester(const char *info, BOOL allowEmpty) @@ -1073,10 +1081,9 @@ tmpcfg = NULL; th_cfg_add_comment(&tmpcfg, "Enable logging"); - th_cfg_add_string(&tmpcfg, "host", &optServer, optServer); - th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)"); - th_cfg_add_int(&tmpcfg, "port", &optPort, optPort); - th_cfg_add_section(&cfg, "server", tmpcfg); + th_cfg_add_bool(&tmpcfg, "log", &optLogEnable, optLogEnable); + th_cfg_add_comment(&tmpcfg, "Log filename format"); + th_cfg_add_string(&tmpcfg, "logformat", &optLogFilename, optLogFilename); #ifdef __WIN32 { @@ -1134,14 +1141,8 @@ } /* Open logfile */ - if (optLogFilename) { - THMSG(1, "Opening logfile '%s'\n", optLogFilename); + logFileOpen(); - if ((optLogFile = fopen(optLogFilename, "a")) == NULL) { - THERR("Could not open logfile for appending!\n"); - return -9; - } - } if (!nn_network_init()) { THERR("Could not initialize network subsystem.\n"); @@ -1577,10 +1578,7 @@ THMSG(1, "Connection terminated.\n"); - if (optLogFile) { - THMSG(1, "Closing logfile.\n"); - fclose(optLogFile); - } + logFileClose(); return 0; }