Released version 3-2015061300
[notion.git] / libtu / errorlog.c
blob2bb223550a696dd18ed4922148ede22921f61781
1 /*
2 * libtu/errorlog.c
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.
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <stdio.h>
14 #include "util.h"
15 #include "types.h"
16 #include "output.h"
17 #include "misc.h"
18 #include "errorlog.h"
20 static ErrorLog *current_log=NULL;
22 static void add_to_log(ErrorLog *el, const char *message, int l)
24 if(message==NULL)
25 return;
27 /* Also write to stderr */
28 fwrite(message, sizeof(char), l, stderr);
30 if(el==NULL)
31 return;
33 if(el->file!=NULL){
34 el->errors=TRUE;
35 fwrite(message, sizeof(char), l, el->file);
36 return;
39 if(el->msgs==NULL){
40 el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE);
41 if(el->msgs==NULL){
42 fprintf(stderr, "%s: %s\n", libtu_progname(), strerror(errno));
43 return;
45 el->msgs[0]=0;
46 el->msgs_len=0;
49 el->errors=TRUE;
51 if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){
52 int n=0;
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;
61 }else{
62 memcpy(el->msgs+el->msgs_len, message, l);
63 el->msgs[el->msgs_len+l]='\0';
64 el->msgs_len+=l;
69 static void log_warn_handler(const char *message)
71 const char *p=strchr(message, '\n');
72 static int lineno=0;
74 add_to_log(current_log, lineno==0 ? ">> " : " ", 3);
76 if(p!=NULL){
77 add_to_log(current_log, message, p-message+1);
78 lineno++;
79 log_warn_handler(p+1);
80 lineno--;
81 return;
84 add_to_log(current_log, message, strlen(message));
85 add_to_log(current_log, "\n", 1);
89 void errorlog_begin_file(ErrorLog *el, FILE *file)
91 el->msgs=NULL;
92 el->msgs_len=0;
93 el->file=file;
94 el->prev=current_log;
95 el->errors=FALSE;
96 el->old_handler=set_warn_handler(log_warn_handler);
97 current_log=el;
101 void errorlog_begin(ErrorLog *el)
103 errorlog_begin_file(el, NULL);
107 bool errorlog_end(ErrorLog *el)
109 current_log=el->prev;
110 set_warn_handler(el->old_handler);
111 el->prev=NULL;
112 el->old_handler=NULL;
113 return el->errors;
117 void errorlog_deinit(ErrorLog *el)
119 if(el->msgs!=NULL)
120 free(el->msgs);
121 el->msgs=NULL;
122 el->msgs_len=0;
123 el->file=NULL;
124 el->errors=FALSE;