changeset 252:d948e3ced2b9

Fix previous commit
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 28 May 2015 17:20:45 +0200
parents 307fd62f376d
children 10bf2bcdbbd4
files logging.c logging.h
diffstat 2 files changed, 36 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/logging.c	Thu May 28 16:12:18 2015 +0200
+++ b/logging.c	Thu May 28 17:20:45 2015 +0200
@@ -23,6 +23,14 @@
 };
 
 
+static const char *levels[] = {
+	[ERROR] = "ERROR",
+	[WARNING] = "WARNING",
+	[INFO] = "INFO",
+	[DEBUG] = "DEBUG",
+};
+
+
 static inline int strtolevel(const char *str)
 {
 	for (int i = 0; i < 4; i++) {
@@ -35,13 +43,13 @@
 
 static int rotate(struct logger *l, struct tm *t)
 {
-	int rv;
+	int len, rv;
 	char id[7];
 	
 	if (! l->dirpath)
-		return;
+		return 0;
 
-	if (l->yday == t.rm_yday)
+	if (l->yday == t->tm_yday)
 		return 0;
 
 	/* close old file descriptor */
@@ -50,29 +58,30 @@
 	
 	/* construct new file name */
 	strftime(id, sizeof(id), "%y%m%d", t);
-	rv = asprintf(&l->filepath, "%s\\%s-Log.txt", l->dirpath, id);
-	if (rv < 0)
-		return rv;
+	len = strlen(l->dirpath) + 1 + sizeof(id) + strlen("-Log.txt") + 1;
+	l->filepath = malloc(len);
+	snprintf(l->filepath, len, "%s\\%s-Log.txt", l->dirpath, id);
 
 	/* open new log file */
-	rv = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744);
+	rv = open(l->filepath, O_CREAT|O_WRONLY|O_APPEND, 00744);
 	if (rv < 0)
 		return rv;
 
 	/* update status */
 	l->fd = rv;
-	l->yday = t.rm_yday;
+	l->yday = t->tm_yday;
+	__logmessage(l, DEBUG, "logging to '%s'", l->filepath);
 	
 	return 1;
 }
 
 
-int __logger_init(struct logger *l, void(*onerror)(int, const char *))
+struct logger * __logger_init(void(*onerror)(int, const char *))
 {
 	int rv, panel;
-	char path[MAX_PATHNAME_LEN], *filename;
+	char path[MAX_PATHNAME_LEN], *str;
 
-	l = malloc(sizeof(struct logger));
+	struct logger *l = malloc(sizeof(struct logger));
 	memset(l, 0, sizeof(*l));
 	
 	l->onerror = onerror;
@@ -81,7 +90,7 @@
 	
 	panel = LoadPanel(0, "FXAnalyse.uir", LOGGING);
 	if (panel < 0)
-		return -1;
+		return NULL;
 	l->panel = panel;
 	
 	/* configuration file path */
@@ -92,7 +101,7 @@
 	Ini_ReadFromFile(configuration, path);
 
 	/* logging folder path */
-	rv = Ini_GetStringCopy(configuration, "logging", "folder", &l->dirname);
+	rv = Ini_GetStringCopy(configuration, "logging", "folder", &l->dirpath);
 	
 	/* logging level */
 	rv = Ini_GetStringCopy(configuration, "logging", "level", &str);
@@ -103,19 +112,12 @@
 	
 	Ini_Dispose(configuration);
 	
-	return 0;
+	return l;
 }
 
 
 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...)
 {
-	static const char *levels[] = {
-		[ERROR] = "ERROR",
-		[DEBUG] = "DEBUG",
-		[INFO] = "INFO",
-		[WARNING] = "WARNING",
-	};
-
 	char msg[1024];
 	int len = 0;
 	
@@ -143,6 +145,13 @@
 	
 	/* string terminator */
 	msg[len] = '\0';
+
+	/* rotate log file based on current date */
+	rotate(l, t);
+	
+	/* write to log file */
+	if ((level <= l->level) && (l->fd >= 0))
+		write(l->fd, msg, len);
 	
 	/* display message */
 	SetCtrlVal(l->panel, LOGGING_LOGGING, msg);
@@ -150,17 +159,10 @@
 	/* report error */
 	if (level == ERROR)
 		l->onerror(level, msg);
-
-	/* rotate log file based on current date */
-	rotate(l, t);
-	
-	/* write to log file */
-	if ((level <= l->level) && (l->fd >= 0))
-		write(l->fd, msg, len);
 }
 
 
-void __logger_panel_visible(struct logger *l, visible)
+void __logger_panel_visible(struct logger *l, int visible)
 {
 	SetPanelAttribute(l->panel, ATTR_VISIBLE, visible);
 }
--- a/logging.h	Thu May 28 16:12:18 2015 +0200
+++ b/logging.h	Thu May 28 17:20:45 2015 +0200
@@ -14,16 +14,16 @@
 
 enum loglevel {
 	ERROR = 0,
-	DEBUG,
+	WARNING,
 	INFO,
-	WARNING,
+	DEBUG,
 };
 
-int __logger_init(struct logger *l, void(*onerror)(int, const char *));
+struct logger * __logger_init(void(*onerror)(int, const char *));
 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...);
-int __logger_panel_visible(struct logger *l, int visible);
+void __logger_panel_visible(struct logger *l, int visible);
 
-#define logger_init(onerror) __logger_init(__logger, onerror)
+#define logger_init(onerror) do { __logger = __logger_init(onerror); } while (0)
 #define logmessage(level, msg, ...) __logmessage(__logger, (level), (msg), ##__VA_ARGS__)
 #define logmsg(msg, ...) __logmessage(__logger, INFO, msg, ##__VA_ARGS__)
 #define logger_panel_visible(visible)  __logger_panel_visible(__logger, visible)