Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/histolog.c @ 374:bd5638c21834
Improve logging system (traces)
There are now two trace logging levels:
* tracelog_level = 1:
Most messages from the log window are written to disk (LPRINT_LOG)
* tracelog_level =2:
LPRINT_LOG & LPRINT_DEBUG messages are written to disk
The trace file name is set with the "tracelog_file" option.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 25 Jul 2005 21:46:35 +0100 |
parents | c30c0d0eb129 |
children | 2e6c7b1440d1 |
comparison
equal
deleted
inserted
replaced
373:af2f8ddf6a1b | 374:bd5638c21834 |
---|---|
99 */ | 99 */ |
100 | 100 |
101 fp = fopen(filename, "a"); | 101 fp = fopen(filename, "a"); |
102 g_free(filename); | 102 g_free(filename); |
103 if (!fp) { | 103 if (!fp) { |
104 scr_LogPrint("Unable to write history (cannot open logfile)"); | 104 scr_LogPrint(LPRINT_LOGNORM, "Unable to write history " |
105 "(cannot open logfile)"); | |
105 return; | 106 return; |
106 } | 107 } |
107 | 108 |
108 to_iso8601(str_ts, ts); | 109 to_iso8601(str_ts, ts); |
109 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); | 110 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
110 fclose(fp); | 111 fclose(fp); |
111 if (err < 0) { | 112 if (err < 0) { |
112 scr_LogPrint("Error while writing to log file: %s", strerror(errno)); | 113 scr_LogPrint(LPRINT_LOGNORM, "Error while writing to log file: %s", |
114 strerror(errno)); | |
113 } | 115 } |
114 } | 116 } |
115 | 117 |
116 // hlog_read_history() | 118 // hlog_read_history() |
117 // Reads the jid's history logfile | 119 // Reads the jid's history logfile |
130 | 132 |
131 if (!FileLoadLogs) return; | 133 if (!FileLoadLogs) return; |
132 | 134 |
133 data = g_new(char, HBB_BLOCKSIZE+32); | 135 data = g_new(char, HBB_BLOCKSIZE+32); |
134 if (!data) { | 136 if (!data) { |
135 scr_LogPrint("Not enough memory to read history file"); | 137 scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file"); |
136 return; | 138 return; |
137 } | 139 } |
138 | 140 |
139 filename = user_histo_file(jid); | 141 filename = user_histo_file(jid); |
140 | 142 |
144 | 146 |
145 // If file is large (> 512 here), display a message to inform the user | 147 // If file is large (> 512 here), display a message to inform the user |
146 // (it can take a while...) | 148 // (it can take a while...) |
147 if (!fstat(fileno(fp), &bufstat)) { | 149 if (!fstat(fileno(fp), &bufstat)) { |
148 if (bufstat.st_size > 524288) | 150 if (bufstat.st_size > 524288) |
149 scr_LogPrint("Reading <%s> history file...", jid); | 151 scr_LogPrint(LPRINT_NORMAL, "Reading <%s> history file...", jid); |
150 } | 152 } |
151 | 153 |
152 /* See write_histo_line() for line format... */ | 154 /* See write_histo_line() for line format... */ |
153 while (!feof(fp)) { | 155 while (!feof(fp)) { |
154 if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; | 156 if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; |
161 | 163 |
162 if ((type != 'M' && type != 'S') || | 164 if ((type != 'M' && type != 'S') || |
163 ((data[11] != 'T') || (data[20] != 'Z') || | 165 ((data[11] != 'T') || (data[20] != 'Z') || |
164 (data[21] != ' ') || (data[25] != ' '))) { | 166 (data[21] != ' ') || (data[25] != ' '))) { |
165 if (!err) { | 167 if (!err) { |
166 scr_LogPrint("Error in history file format (%s), l.%u", jid, ln); | 168 scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
169 jid, ln); | |
167 err = 1; | 170 err = 1; |
168 } | 171 } |
169 //break; | 172 //break; |
170 continue; | 173 continue; |
171 } | 174 } |
175 | 178 |
176 // Some checks | 179 // Some checks |
177 if (((type == 'M') && (info != 'S' && info != 'R')) || | 180 if (((type == 'M') && (info != 'S' && info != 'R')) || |
178 ((type == 'I') && (!strchr("OAIFDN", info)))) { | 181 ((type == 'I') && (!strchr("OAIFDN", info)))) { |
179 if (!err) { | 182 if (!err) { |
180 scr_LogPrint("Error in history file format (%s), l.%u", jid, ln); | 183 scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
184 jid, ln); | |
181 err = 1; | 185 err = 1; |
182 } | 186 } |
183 //break; | 187 //break; |
184 continue; | 188 continue; |
185 } | 189 } |
194 } | 198 } |
195 // Small check for too long messages | 199 // Small check for too long messages |
196 if (tail >= HBB_BLOCKSIZE+26 + data) { | 200 if (tail >= HBB_BLOCKSIZE+26 + data) { |
197 // Maybe we will have a parse error on next, because this | 201 // Maybe we will have a parse error on next, because this |
198 // message is big (maybe too big). | 202 // message is big (maybe too big). |
199 scr_LogPrint("A message could be too big in history file..."); | 203 scr_LogPrint(LPRINT_LOGNORM, "A message could be too big " |
204 "in history file..."); | |
200 } | 205 } |
201 // Remove last CR (we keep it if the line is empty, too) | 206 // Remove last CR (we keep it if the line is empty, too) |
202 if ((tail > data+26) && (*(tail-1) == '\n')) | 207 if ((tail > data+26) && (*(tail-1) == '\n')) |
203 *(tail-1) = 0; | 208 *(tail-1) = 0; |
204 | 209 |
226 | 231 |
227 if (enable || loadfiles) { | 232 if (enable || loadfiles) { |
228 if (root_dir) { | 233 if (root_dir) { |
229 int l = strlen(root_dir); | 234 int l = strlen(root_dir); |
230 if (l < 1) { | 235 if (l < 1) { |
231 scr_LogPrint("root_dir too short"); | 236 scr_LogPrint(LPRINT_LOGNORM, "Error: logging dir name too short"); |
232 UseFileLogging = FileLoadLogs = FALSE; | 237 UseFileLogging = FileLoadLogs = FALSE; |
233 return; | 238 return; |
234 } | 239 } |
235 // RootDir must be slash-terminated | 240 // RootDir must be slash-terminated |
236 if (root_dir[l-1] == '/') | 241 if (root_dir[l-1] == '/') |
249 } | 254 } |
250 // Check directory permissions (should not be readable by group/others) | 255 // Check directory permissions (should not be readable by group/others) |
251 if (checkset_perm(RootDir, TRUE) == -1) { | 256 if (checkset_perm(RootDir, TRUE) == -1) { |
252 // The directory does not actually exists | 257 // The directory does not actually exists |
253 g_free(RootDir); | 258 g_free(RootDir); |
254 scr_LogPrint("ERROR: Cannot access history log directory, " | 259 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
255 "logging DISABLED"); | 260 "history log directory, logging DISABLED"); |
256 UseFileLogging = FileLoadLogs = FALSE; | 261 UseFileLogging = FileLoadLogs = FALSE; |
257 } | 262 } |
258 } else { // Disable history logging | 263 } else { // Disable history logging |
259 if (RootDir) | 264 if (RootDir) |
260 g_free(RootDir); | 265 g_free(RootDir); |