changeset 191:0fed60877099

Allow to write log to file specified in configuration file
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:31 +0200
parents c03bf6fe8297
children f105ac22da05
files logging.c
diffstat 1 files changed, 41 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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 <ansi_c.h>
+#include <inifile.h>
+#include <lowlvlio.h>
 
+#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);
 }