4 * Copyright (c) Tuomo Valkonen 1999-2004.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
20 static ErrorLog *current_log=NULL;
22 static void add_to_log(ErrorLog *el, const char *message, int l)
27 /* Also write to stderr */
28 fwrite(message, sizeof(char), l, stderr);
35 fwrite(message, sizeof(char), l, el->file);
40 el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE);
42 fprintf(stderr, "%s: %s\n", libtu_progname(), strerror(errno));
51 if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){
53 if(l<ERRORLOG_MAX_SIZE-1){
54 n=ERRORLOG_MAX_SIZE-1-l;
55 memmove(el->msgs, el->msgs+el->msgs_len-n, n);
57 memcpy(el->msgs+n, message+l-(ERRORLOG_MAX_SIZE-1-n),
58 ERRORLOG_MAX_SIZE-1-n);
59 el->msgs[ERRORLOG_MAX_SIZE]='\0';
60 el->msgs_len=ERRORLOG_MAX_SIZE-1;
62 memcpy(el->msgs+el->msgs_len, message, l);
63 el->msgs[el->msgs_len+l]='\0';
69 static void log_warn_handler(const char *message)
71 const char *p=strchr(message, '\n');
75 add_to_log(current_log, lineno==0 ? ">> " : " ", 3);
78 add_to_log(current_log, message, p-message+1);
80 log_warn_handler(p+1);
85 add_to_log(current_log, message, strlen(message));
86 add_to_log(current_log, "\n", 1);
90 void errorlog_begin_file(ErrorLog *el, FILE *file)
97 el->old_handler=set_warn_handler(log_warn_handler);
102 void errorlog_begin(ErrorLog *el)
104 errorlog_begin_file(el, NULL);
108 bool errorlog_end(ErrorLog *el)
110 current_log=el->prev;
111 set_warn_handler(el->old_handler);
113 el->old_handler=NULL;
118 void errorlog_deinit(ErrorLog *el)