# HG changeset patch # User Matti Hamalainen # Date 1352651562 -7200 # Node ID 44f67ec5e945fa5cdda4d98897362f42bdafc498 # Parent 658c188101a6568db5481d5e30bac8e6db0f64b1 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". diff -r 658c188101a6 -r 44f67ec5e945 main.c --- 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; } diff -r 658c188101a6 -r 44f67ec5e945 ui.c --- a/ui.c Sun Nov 11 07:33:31 2012 +0200 +++ b/ui.c Sun Nov 11 18:32:42 2012 +0200 @@ -118,6 +118,7 @@ memset(chatWindows, 0, sizeof(chatWindows)); chatWindows[0] = nn_window_new(NULL); + nn_log_open(chatWindows[0]); currWin = chatWindows[0]; return TRUE; @@ -166,23 +167,26 @@ BOOL nnwin_open(const char *name, BOOL curwin) { + nn_window_t *res; int i; - nn_window_t *res; + if (name == NULL) return FALSE; if ((res = nn_window_new(name)) == NULL) return FALSE; + nn_log_open(res); + for (i = 1; i < SET_MAX_WINDOWS; i++) - if (chatWindows[i] == NULL) - { - res->num = i; - chatWindows[i] = res; - if (curwin) - currWin = res; - return TRUE; - } + if (chatWindows[i] == NULL) + { + res->num = i; + chatWindows[i] = res; + if (curwin) + currWin = res; + return TRUE; + } return FALSE; } @@ -194,13 +198,12 @@ if (win == NULL) return; for (i = 1; i < SET_MAX_WINDOWS; i++) + if (chatWindows[i] == win) { - if (chatWindows[i] == win) - { - chatWindows[i] = NULL; - nn_window_free(win); - return; - } + chatWindows[i] = NULL; + nn_log_close(win); + nn_window_free(win); + return; } } diff -r 658c188101a6 -r 44f67ec5e945 ui.h --- a/ui.h Sun Nov 11 07:33:31 2012 +0200 +++ b/ui.h Sun Nov 11 18:32:42 2012 +0200 @@ -44,6 +44,10 @@ char *id; // Chatter ID, NULL = main window nn_line_t *line; + + // Logging + char *logFilename; + FILE *logFile; } nn_window_t; @@ -83,4 +87,8 @@ BOOL (*callback)(int, nn_editbuf_t *, nn_editstate_t *), void (*update)(nn_editbuf_t *, nn_editstate_t *)); + +BOOL nn_log_open(nn_window_t *win); +void nn_log_close(nn_window_t *win); + #endif diff -r 658c188101a6 -r 44f67ec5e945 util.c --- a/util.c Sun Nov 11 07:33:31 2012 +0200 +++ b/util.c Sun Nov 11 18:32:42 2012 +0200 @@ -715,59 +715,3 @@ th_free(tuple->str); th_free(tuple); } - - -#define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH) -#define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR) - -char *nn_log_parse_filename(const char *fmt, int id) -{ - size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0, pos = 0; - char *bufData = th_malloc(bufSize); - char *copy = th_strdup(fmt); - - while (fmt[pos]) - { - if (fmt[pos] == '%') - { - char tmpBuf[64]; - size_t start = pos++; - - if (fmt[pos] == '-') - pos++; - - while (isdigit(fmt[pos])) pos++; - - switch (fmt[pos]) - { - case 'i': - case 'd': - copy[pos + 1] = 0; - snprintf(tmpBuf, sizeof(tmpBuf), ©[start], id); - VPUTS(tmpBuf); - break; - - case 's': - copy[pos + 1] = 0; -// snprintf(tmpBuf, sizeof(tmpBuf), ©[start], ); - VPUTS(tmpBuf); - break; - - case '%': - VPUTCH('%'); - break; - - default: - goto error; - } - } - else - VPUTCH(fmt[pos]); - pos++; - } - -error: - VPUTCH(0); - th_free(copy); - return bufData; -} diff -r 658c188101a6 -r 44f67ec5e945 util.h --- a/util.h Sun Nov 11 07:33:31 2012 +0200 +++ b/util.h Sun Nov 11 18:32:42 2012 +0200 @@ -86,7 +86,4 @@ char * str_trim_right(char *buf); int str_compare(const void *s1, const void *s2); - -char *nn_log_parse_filename(const char *fmt, int id); - #endif