Mercurial > hg > fxanalyse
comparison logging.c @ 252:d948e3ced2b9
Fix previous commit
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Thu, 28 May 2015 17:20:45 +0200 |
parents | 307fd62f376d |
children | 3622e24a443f |
comparison
equal
deleted
inserted
replaced
251:307fd62f376d | 252:d948e3ced2b9 |
---|---|
21 int yday; | 21 int yday; |
22 void(*onerror)(int, const char *); | 22 void(*onerror)(int, const char *); |
23 }; | 23 }; |
24 | 24 |
25 | 25 |
26 static const char *levels[] = { | |
27 [ERROR] = "ERROR", | |
28 [WARNING] = "WARNING", | |
29 [INFO] = "INFO", | |
30 [DEBUG] = "DEBUG", | |
31 }; | |
32 | |
33 | |
26 static inline int strtolevel(const char *str) | 34 static inline int strtolevel(const char *str) |
27 { | 35 { |
28 for (int i = 0; i < 4; i++) { | 36 for (int i = 0; i < 4; i++) { |
29 if (streq(levels[i], str)) | 37 if (streq(levels[i], str)) |
30 return i; | 38 return i; |
33 } | 41 } |
34 | 42 |
35 | 43 |
36 static int rotate(struct logger *l, struct tm *t) | 44 static int rotate(struct logger *l, struct tm *t) |
37 { | 45 { |
38 int rv; | 46 int len, rv; |
39 char id[7]; | 47 char id[7]; |
40 | 48 |
41 if (! l->dirpath) | 49 if (! l->dirpath) |
42 return; | 50 return 0; |
43 | 51 |
44 if (l->yday == t.rm_yday) | 52 if (l->yday == t->tm_yday) |
45 return 0; | 53 return 0; |
46 | 54 |
47 /* close old file descriptor */ | 55 /* close old file descriptor */ |
48 close(l->fd); | 56 close(l->fd); |
49 l->fd = -1; | 57 l->fd = -1; |
50 | 58 |
51 /* construct new file name */ | 59 /* construct new file name */ |
52 strftime(id, sizeof(id), "%y%m%d", t); | 60 strftime(id, sizeof(id), "%y%m%d", t); |
53 rv = asprintf(&l->filepath, "%s\\%s-Log.txt", l->dirpath, id); | 61 len = strlen(l->dirpath) + 1 + sizeof(id) + strlen("-Log.txt") + 1; |
54 if (rv < 0) | 62 l->filepath = malloc(len); |
55 return rv; | 63 snprintf(l->filepath, len, "%s\\%s-Log.txt", l->dirpath, id); |
56 | 64 |
57 /* open new log file */ | 65 /* open new log file */ |
58 rv = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744); | 66 rv = open(l->filepath, O_CREAT|O_WRONLY|O_APPEND, 00744); |
59 if (rv < 0) | 67 if (rv < 0) |
60 return rv; | 68 return rv; |
61 | 69 |
62 /* update status */ | 70 /* update status */ |
63 l->fd = rv; | 71 l->fd = rv; |
64 l->yday = t.rm_yday; | 72 l->yday = t->tm_yday; |
73 __logmessage(l, DEBUG, "logging to '%s'", l->filepath); | |
65 | 74 |
66 return 1; | 75 return 1; |
67 } | 76 } |
68 | 77 |
69 | 78 |
70 int __logger_init(struct logger *l, void(*onerror)(int, const char *)) | 79 struct logger * __logger_init(void(*onerror)(int, const char *)) |
71 { | 80 { |
72 int rv, panel; | 81 int rv, panel; |
73 char path[MAX_PATHNAME_LEN], *filename; | 82 char path[MAX_PATHNAME_LEN], *str; |
74 | 83 |
75 l = malloc(sizeof(struct logger)); | 84 struct logger *l = malloc(sizeof(struct logger)); |
76 memset(l, 0, sizeof(*l)); | 85 memset(l, 0, sizeof(*l)); |
77 | 86 |
78 l->onerror = onerror; | 87 l->onerror = onerror; |
79 l->fd = -1; | 88 l->fd = -1; |
80 l->level = DEBUG; | 89 l->level = DEBUG; |
81 | 90 |
82 panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); | 91 panel = LoadPanel(0, "FXAnalyse.uir", LOGGING); |
83 if (panel < 0) | 92 if (panel < 0) |
84 return -1; | 93 return NULL; |
85 l->panel = panel; | 94 l->panel = panel; |
86 | 95 |
87 /* configuration file path */ | 96 /* configuration file path */ |
88 GetIniFilePath(path); | 97 GetIniFilePath(path); |
89 | 98 |
90 /* load configuration file */ | 99 /* load configuration file */ |
91 IniText configuration = Ini_New(TRUE); | 100 IniText configuration = Ini_New(TRUE); |
92 Ini_ReadFromFile(configuration, path); | 101 Ini_ReadFromFile(configuration, path); |
93 | 102 |
94 /* logging folder path */ | 103 /* logging folder path */ |
95 rv = Ini_GetStringCopy(configuration, "logging", "folder", &l->dirname); | 104 rv = Ini_GetStringCopy(configuration, "logging", "folder", &l->dirpath); |
96 | 105 |
97 /* logging level */ | 106 /* logging level */ |
98 rv = Ini_GetStringCopy(configuration, "logging", "level", &str); | 107 rv = Ini_GetStringCopy(configuration, "logging", "level", &str); |
99 if (rv > 0) { | 108 if (rv > 0) { |
100 l->level = strtolevel(str); | 109 l->level = strtolevel(str); |
101 free(str); | 110 free(str); |
102 } | 111 } |
103 | 112 |
104 Ini_Dispose(configuration); | 113 Ini_Dispose(configuration); |
105 | 114 |
106 return 0; | 115 return l; |
107 } | 116 } |
108 | 117 |
109 | 118 |
110 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) | 119 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...) |
111 { | 120 { |
112 static const char *levels[] = { | |
113 [ERROR] = "ERROR", | |
114 [DEBUG] = "DEBUG", | |
115 [INFO] = "INFO", | |
116 [WARNING] = "WARNING", | |
117 }; | |
118 | |
119 char msg[1024]; | 121 char msg[1024]; |
120 int len = 0; | 122 int len = 0; |
121 | 123 |
122 /* timestamp */ | 124 /* timestamp */ |
123 len += sprintf(msg, "%014.3f ", utc); | 125 len += sprintf(msg, "%014.3f ", utc); |
141 msg[len++] = '\r'; | 143 msg[len++] = '\r'; |
142 msg[len++] = '\n'; | 144 msg[len++] = '\n'; |
143 | 145 |
144 /* string terminator */ | 146 /* string terminator */ |
145 msg[len] = '\0'; | 147 msg[len] = '\0'; |
148 | |
149 /* rotate log file based on current date */ | |
150 rotate(l, t); | |
151 | |
152 /* write to log file */ | |
153 if ((level <= l->level) && (l->fd >= 0)) | |
154 write(l->fd, msg, len); | |
146 | 155 |
147 /* display message */ | 156 /* display message */ |
148 SetCtrlVal(l->panel, LOGGING_LOGGING, msg); | 157 SetCtrlVal(l->panel, LOGGING_LOGGING, msg); |
149 | 158 |
150 /* report error */ | 159 /* report error */ |
151 if (level == ERROR) | 160 if (level == ERROR) |
152 l->onerror(level, msg); | 161 l->onerror(level, msg); |
153 | |
154 /* rotate log file based on current date */ | |
155 rotate(l, t); | |
156 | |
157 /* write to log file */ | |
158 if ((level <= l->level) && (l->fd >= 0)) | |
159 write(l->fd, msg, len); | |
160 } | 162 } |
161 | 163 |
162 | 164 |
163 void __logger_panel_visible(struct logger *l, visible) | 165 void __logger_panel_visible(struct logger *l, int visible) |
164 { | 166 { |
165 SetPanelAttribute(l->panel, ATTR_VISIBLE, visible); | 167 SetPanelAttribute(l->panel, ATTR_VISIBLE, visible); |
166 } | 168 } |