errorlog.c

Thu, 06 Feb 2014 23:36:29 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 06 Feb 2014 23:36:29 +0000
changeset 118
dbf3a6323fda
parent 109
96a557abc364
permissions
-rw-r--r--

oops

61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
1 /*
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
2 * libtu/errorlog.c
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
3 *
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2004.
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
5 *
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
8 */
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
9
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
10 #include <string.h>
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
11 #include <errno.h>
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
12 #include <stdio.h>
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
13
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
14 #include "util.h"
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
15 #include "types.h"
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
16 #include "output.h"
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
17 #include "misc.h"
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
18 #include "errorlog.h"
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
19
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
20 static ErrorLog *current_log=NULL;
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
21
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
22 static void add_to_log(ErrorLog *el, const char *message, int l)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
23 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
24 if(message==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
25 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
26
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
27 /* Also write to stderr */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
28 fwrite(message, sizeof(char), l, stderr);
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
29
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
30 if(el==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
31 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
32
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
33 if(el->file!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
34 el->errors=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
35 fwrite(message, sizeof(char), l, el->file);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
36 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
37 }
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
38
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
39 if(el->msgs==NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
40 el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
41 if(el->msgs==NULL){
109
96a557abc364 Renamed `prog_execname` `libtu_progname` and added `libtu_progbasename`.
Tuomo Valkonen <tuomov@iki.fi>
parents: 62
diff changeset
42 fprintf(stderr, "%s: %s\n", libtu_progname(), strerror(errno));
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
43 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
44 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
45 el->msgs[0]=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
46 el->msgs_len=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
47 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
48
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
49 el->errors=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
50
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
51 if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
52 int n=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
53 if(l<ERRORLOG_MAX_SIZE-1){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
54 n=ERRORLOG_MAX_SIZE-1-l;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
55 memmove(el->msgs, el->msgs+el->msgs_len-n, n);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
56 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
57 memcpy(el->msgs+n, message+l-(ERRORLOG_MAX_SIZE-1-n),
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
58 ERRORLOG_MAX_SIZE-1-n);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
59 el->msgs[ERRORLOG_MAX_SIZE]='\0';
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
60 el->msgs_len=ERRORLOG_MAX_SIZE-1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
61 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
62 memcpy(el->msgs+el->msgs_len, message, l);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
63 el->msgs[el->msgs_len+l]='\0';
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
64 el->msgs_len+=l;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
65 }
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
66 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
67
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
68
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
69 static void log_warn_handler(const char *message)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
70 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
71 const char *p=strchr(message, '\n');
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
72 static int lineno=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
73 int alternat=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
74
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
75 add_to_log(current_log, lineno==0 ? ">> " : " ", 3);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
76
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
77 if(p!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
78 add_to_log(current_log, message, p-message+1);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
79 lineno++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
80 log_warn_handler(p+1);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
81 lineno--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
82 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
83 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
84
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
85 add_to_log(current_log, message, strlen(message));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
86 add_to_log(current_log, "\n", 1);
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
87 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
88
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
89
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
90 void errorlog_begin_file(ErrorLog *el, FILE *file)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
91 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
92 el->msgs=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
93 el->msgs_len=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
94 el->file=file;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
95 el->prev=current_log;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
96 el->errors=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
97 el->old_handler=set_warn_handler(log_warn_handler);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
98 current_log=el;
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
99 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
100
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
101
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
102 void errorlog_begin(ErrorLog *el)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
103 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
104 errorlog_begin_file(el, NULL);
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
105 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
106
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
107
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
108 bool errorlog_end(ErrorLog *el)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
109 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
110 current_log=el->prev;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
111 set_warn_handler(el->old_handler);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
112 el->prev=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
113 el->old_handler=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
114 return el->errors;
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
115 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
116
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
117
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
118 void errorlog_deinit(ErrorLog *el)
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
119 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
120 if(el->msgs!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
121 free(el->msgs);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
122 el->msgs=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
123 el->msgs_len=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
124 el->file=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 61
diff changeset
125 el->errors=FALSE;
61
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
126 }
fc585645ad05 trunk: changeset 1316
tuomov
parents:
diff changeset
127

mercurial