Mercurial > hg > fxanalyse
comparison logging.c @ 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 | 97112b45b838 |
children | 111610d871af |
comparison
equal
deleted
inserted
replaced
190:c03bf6fe8297 | 191:0fed60877099 |
---|---|
1 #include <ansi_c.h> | 1 #include <ansi_c.h> |
2 #include <inifile.h> | |
3 #include <lowlvlio.h> | |
2 | 4 |
5 #include "config.h" | |
3 #include "logging.h" | 6 #include "logging.h" |
4 | 7 |
5 /* this refers to the event time defined as a global variable in the main program */ | 8 /* this refers to the event time defined as a global variable in the main program */ |
6 extern double utc; | 9 extern double utc; |
7 | 10 |
8 | 11 |
9 int __logger_init(struct logger *l) | 12 int __logger_init(struct logger *l) |
10 { | 13 { |
11 int panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); | 14 int rv, panel; |
15 char path[MAX_PATHNAME_LEN], *filename; | |
16 | |
17 l->fd = -1; | |
18 | |
19 panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); | |
12 if (panel < 0) | 20 if (panel < 0) |
13 return -1; | 21 return -1; |
14 l->panel = panel; | 22 l->panel = panel; |
23 | |
24 /* configuration file path */ | |
25 GetIniFilePath(path); | |
26 | |
27 /* load configuration file */ | |
28 IniText configuration = Ini_New(TRUE); | |
29 Ini_ReadFromFile(configuration, path); | |
30 | |
31 /* logging file name */ | |
32 rv = Ini_GetStringCopy(configuration, "logging", "filename", &filename); | |
33 if (rv > 0) | |
34 l->fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744); | |
35 | |
36 free(filename); | |
37 Ini_Dispose(configuration); | |
38 | |
15 return 0; | 39 return 0; |
16 } | 40 } |
17 | 41 |
18 | 42 |
19 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) | 43 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) |
20 { | 44 { |
21 static const char levels[][16] = { | 45 static const char *levels[] = { |
22 "DEBUG", | 46 "DEBUG", |
23 "INFO", | 47 "INFO", |
24 "WARNING", | 48 "WARNING", |
25 "ERROR", | 49 "ERROR", |
26 }; | 50 }; |
27 | 51 |
28 char msg[1024]; | 52 char msg[1024]; |
29 int len = 0; | 53 int len = 0; |
30 | 54 |
31 // timestamp | 55 /* timestamp */ |
32 len += sprintf(msg, "%014.3f ", utc); | 56 len += sprintf(msg, "%014.3f ", utc); |
33 time_t now = time(NULL); | 57 time_t now = time(NULL); |
34 struct tm *t = localtime(&now); | 58 struct tm *t = localtime(&now); |
35 len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t); | 59 len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t); |
36 | 60 |
37 // level | 61 /* level */ |
38 len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]); | 62 len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]); |
39 | 63 |
40 // message | 64 /* message */ |
41 va_list args; | 65 va_list args; |
42 va_start(args, frmt); | 66 va_start(args, frmt); |
43 len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args); | 67 len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args); |
44 va_end(args); | 68 va_end(args); |
45 | 69 |
46 // add newline | 70 /* newline */ |
47 len = MIN(len, sizeof(msg) - 2); | 71 len = MIN(len, sizeof(msg) - 3); |
48 msg[len] = '\n'; | 72 msg[len++] = '\r'; |
49 msg[len + 1] = '\0'; | 73 msg[len++] = '\n'; |
50 | 74 |
51 // display message | 75 /* string terminator */ |
76 msg[len] = '\0'; | |
77 | |
78 /* display message */ | |
52 SetCtrlVal(l->panel, LOGGING_LOGGING, msg); | 79 SetCtrlVal(l->panel, LOGGING_LOGGING, msg); |
80 | |
81 /* write to log file */ | |
82 if (l->fd >= 0) | |
83 write(l->fd, msg, len); | |
53 } | 84 } |