# HG changeset patch # User Daniele Nicolodi # Date 1396278211 -7200 # Node ID 0fed60877099c2329fe6812a1fdfb0935d327aac # Parent c03bf6fe82970fe9d1f29ce649452bc1a8c743a2 Allow to write log to file specified in configuration file diff -r c03bf6fe8297 -r 0fed60877099 logging.c --- a/logging.c Mon Mar 31 17:03:31 2014 +0200 +++ b/logging.c Mon Mar 31 17:03:31 2014 +0200 @@ -1,5 +1,8 @@ #include +#include +#include +#include "config.h" #include "logging.h" /* this refers to the event time defined as a global variable in the main program */ @@ -8,17 +11,38 @@ int __logger_init(struct logger *l) { - int panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); + int rv, panel; + char path[MAX_PATHNAME_LEN], *filename; + + l->fd = -1; + + panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); if (panel < 0) return -1; l->panel = panel; + + /* configuration file path */ + GetIniFilePath(path); + + /* load configuration file */ + IniText configuration = Ini_New(TRUE); + Ini_ReadFromFile(configuration, path); + + /* logging file name */ + rv = Ini_GetStringCopy(configuration, "logging", "filename", &filename); + if (rv > 0) + l->fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744); + + free(filename); + Ini_Dispose(configuration); + return 0; } void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) { - static const char levels[][16] = { + static const char *levels[] = { "DEBUG", "INFO", "WARNING", @@ -28,26 +52,33 @@ char msg[1024]; int len = 0; - // timestamp + /* timestamp */ len += sprintf(msg, "%014.3f ", utc); time_t now = time(NULL); struct tm *t = localtime(&now); len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t); - // level + /* level */ len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]); - // message + /* message */ va_list args; va_start(args, frmt); len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args); va_end(args); - // add newline - len = MIN(len, sizeof(msg) - 2); - msg[len] = '\n'; - msg[len + 1] = '\0'; + /* newline */ + len = MIN(len, sizeof(msg) - 3); + msg[len++] = '\r'; + msg[len++] = '\n'; + + /* string terminator */ + msg[len] = '\0'; - // display message + /* display message */ SetCtrlVal(l->panel, LOGGING_LOGGING, msg); + + /* write to log file */ + if (l->fd >= 0) + write(l->fd, msg, len); }