Mercurial > hg > nnchat
comparison main.c @ 563:c3e4e8f3c658
Refactor message/error/etc printing functions to be a bit more sane.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 15 Nov 2012 20:26:10 +0200 |
parents | 1ff9e85a1fcc |
children | 6e5789cbb4d4 |
comparison
equal
deleted
inserted
replaced
562:dbeca46a7c58 | 563:c3e4e8f3c658 |
---|---|
251 | 251 |
252 void printMsgF(nn_window_t *win, int flags, const char *fmt, ...); | 252 void printMsgF(nn_window_t *win, int flags, const char *fmt, ...); |
253 BOOL nn_log_reopen(nn_window_t *win); | 253 BOOL nn_log_reopen(nn_window_t *win); |
254 | 254 |
255 | 255 |
256 void printMsgV(nn_window_t *win, int flags, const char *fmt, va_list ap) | 256 void printMsgConst(nn_window_t *win, int flags, const char *msg) |
257 { | 257 { |
258 char tmpStr[128], *buf; | 258 char tmpStr[128]; |
259 nn_window_t *tmpwin = (win != NULL) ? win : nnwin_main_window(); | 259 nn_window_t *tmpwin = (win != NULL) ? win : nnwin_main_window(); |
260 | 260 |
261 // Only the main window | 261 // Only the main window |
262 if (win == NULL && (flags & LOG_RECURSIVE) == 0) | 262 if (win == NULL && (flags & LOG_RECURSIVE) == 0) |
263 { | 263 { |
280 if (flags & LOG_STAMP) | 280 if (flags & LOG_STAMP) |
281 { | 281 { |
282 str_get_timestamp(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ "); | 282 str_get_timestamp(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ "); |
283 } | 283 } |
284 | 284 |
285 buf = th_strdup_vprintf(fmt, ap); | |
286 | |
287 if (flags & LOG_FILE) | 285 if (flags & LOG_FILE) |
288 { | 286 { |
289 printMsgFile(win != NULL ? win : nnwin_main_window(), flags, tmpStr, buf); | 287 printMsgFile(win != NULL ? win : nnwin_main_window(), flags, tmpStr, msg); |
290 } | 288 } |
291 | 289 |
292 if (flags & LOG_FILE2) | 290 if (flags & LOG_FILE2) |
293 { | 291 { |
294 printMsgFile(nnwin_main_window(), flags, tmpStr, buf); | 292 printMsgFile(nnwin_main_window(), flags, tmpStr, msg); |
295 } | 293 } |
296 | 294 |
297 if (!optDaemon && (flags & LOG_WINDOW)) | 295 if (!optDaemon && (flags & LOG_WINDOW)) |
298 { | 296 { |
299 if (flags & LOG_STAMP) nnwin_print(tmpwin, tmpStr); | 297 if (flags & LOG_STAMP) nnwin_print(tmpwin, tmpStr); |
300 nnwin_print(tmpwin, buf); | 298 nnwin_print(tmpwin, msg); |
301 } | 299 } |
302 | 300 } |
301 | |
302 void printMsgV(nn_window_t *win, int flags, const char *fmt, va_list ap) | |
303 { | |
304 char *buf = th_strdup_vprintf(fmt, ap); | |
305 printMsgConst(win, flags, buf); | |
303 th_free(buf); | 306 th_free(buf); |
304 } | 307 } |
305 | 308 |
306 void printMsg(nn_window_t *win, const char *fmt, ...) | 309 void printMsg(nn_window_t *win, const char *fmt, ...) |
307 { | 310 { |
331 } | 334 } |
332 | 335 |
333 | 336 |
334 char *errorMessages = NULL; | 337 char *errorMessages = NULL; |
335 | 338 |
339 void errorMsgConst(const char *msg) | |
340 { | |
341 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); | |
342 | |
343 if (errorMessages != NULL) | |
344 { | |
345 // XXX Yes, this is lazy. | |
346 char *tmp = th_strdup_printf("%s%s", errorMessages, msg); | |
347 th_free(errorMessages); | |
348 errorMessages = tmp; | |
349 } | |
350 else | |
351 errorMessages = th_strdup(msg); | |
352 } | |
353 | |
354 | |
336 void errorMsgV(const char *fmt, va_list ap) | 355 void errorMsgV(const char *fmt, va_list ap) |
337 { | 356 { |
338 char *tmp = th_strdup_vprintf(fmt, ap); | 357 char *msg = th_strdup_vprintf(fmt, ap); |
339 | 358 errorMsgConst(msg); |
340 printMsg(NULL, "%s", tmp); | 359 th_free(msg); |
341 | |
342 if (errorMessages != NULL) | |
343 { | |
344 char *tmp2 = th_strdup_printf("%s%s", errorMessages, tmp); | |
345 th_free(errorMessages); | |
346 th_free(tmp); | |
347 errorMessages = tmp2; | |
348 } | |
349 else | |
350 errorMessages = tmp; | |
351 } | 360 } |
352 | 361 |
353 | 362 |
354 void errorMsg(const char *fmt, ...) | 363 void errorMsg(const char *fmt, ...) |
355 { | 364 { |