0
|
1 /*
|
|
2 failures.c : Failure manager
|
|
3
|
|
4 Copyright (c) 2001-2002 Timo Sirainen
|
|
5
|
|
6 Permission is hereby granted, free of charge, to any person obtaining
|
|
7 a copy of this software and associated documentation files (the
|
|
8 "Software"), to deal in the Software without restriction, including
|
|
9 without limitation the rights to use, copy, modify, merge, publish,
|
|
10 distribute, sublicense, and/or sell copies of the Software, and to
|
|
11 permit persons to whom the Software is furnished to do so, subject to
|
|
12 the following conditions:
|
|
13
|
|
14 The above copyright notice and this permission notice shall be
|
|
15 included in all copies or substantial portions of the Software.
|
|
16
|
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24 */
|
|
25
|
|
26
|
|
27 #include "lib.h"
|
|
28 #include "ioloop.h"
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <syslog.h>
|
|
32 #include <time.h>
|
|
33
|
|
34 static FailureFunc panic_handler __attr_noreturn__, fatal_handler __attr_noreturn__;
|
|
35 static FailureFunc error_handler, warning_handler;
|
|
36
|
|
37 static FILE *log_fd;
|
|
38 static char *log_prefix, *log_stamp_format;
|
|
39
|
|
40 static void default_panic_handler(const char *format, va_list args)
|
|
41 __attr_noreturn__;
|
|
42 static void default_fatal_handler(const char *format, va_list args)
|
|
43 __attr_noreturn__;
|
|
44
|
|
45 static void write_prefix(void)
|
|
46 {
|
|
47 struct tm *tm;
|
|
48 char str[256];
|
|
49
|
|
50 if (log_prefix != NULL)
|
|
51 fputs(log_prefix, log_fd);
|
|
52
|
|
53 if (log_stamp_format != NULL) {
|
|
54 tm = localtime(&ioloop_time);
|
|
55
|
|
56 if (strftime(str, sizeof(str), log_stamp_format, tm) > 0)
|
|
57 fputs(str, log_fd);
|
|
58 }
|
|
59 }
|
|
60
|
|
61 static void default_panic_handler(const char *format, va_list args)
|
|
62 {
|
|
63 write_prefix();
|
|
64
|
|
65 fputs("Panic: ", log_fd);
|
|
66 vfprintf(log_fd, format, args);
|
|
67 fputc('\n', log_fd);
|
|
68
|
|
69 abort();
|
|
70 }
|
|
71
|
|
72 static void default_fatal_handler(const char *format, va_list args)
|
|
73 {
|
|
74 write_prefix();
|
|
75
|
|
76 fputs("Fatal: ", log_fd);
|
|
77 vfprintf(log_fd, format, args);
|
|
78 fputc('\n', log_fd);
|
|
79
|
|
80 exit(98);
|
|
81 }
|
|
82
|
|
83 static void default_error_handler(const char *format, va_list args)
|
|
84 {
|
|
85 write_prefix();
|
|
86
|
|
87 fputs("Error: ", log_fd);
|
|
88 vfprintf(log_fd, format, args);
|
|
89 fputc('\n', log_fd);
|
|
90
|
|
91 fflush(log_fd);
|
|
92 }
|
|
93
|
|
94 static void default_warning_handler(const char *format, va_list args)
|
|
95 {
|
|
96 write_prefix();
|
|
97
|
|
98 fputs("Warning: ", log_fd);
|
|
99 vfprintf(log_fd, format, args);
|
|
100 fputc('\n', log_fd);
|
|
101
|
|
102 fflush(log_fd);
|
|
103 }
|
|
104
|
|
105 void i_panic(const char *format, ...)
|
|
106 {
|
|
107 va_list args;
|
|
108
|
|
109 va_start(args, format);
|
|
110 panic_handler(format, args);
|
|
111 va_end(args);
|
|
112 }
|
|
113
|
|
114 void i_fatal(const char *format, ...)
|
|
115 {
|
|
116 va_list args;
|
|
117
|
|
118 va_start(args, format);
|
|
119 fatal_handler(format, args);
|
|
120 va_end(args);
|
|
121 }
|
|
122
|
|
123 void i_error(const char *format, ...)
|
|
124 {
|
|
125 va_list args;
|
|
126
|
|
127 va_start(args, format);
|
|
128 error_handler(format, args);
|
|
129 va_end(args);
|
|
130 }
|
|
131
|
|
132 void i_warning(const char *format, ...)
|
|
133 {
|
|
134 va_list args;
|
|
135
|
|
136 va_start(args, format);
|
|
137 warning_handler(format, args);
|
|
138 va_end(args);
|
|
139 }
|
|
140
|
|
141 void i_set_panic_handler(FailureFunc func __attr_noreturn__)
|
|
142 {
|
|
143 if (func == NULL)
|
|
144 func = default_panic_handler;
|
|
145 panic_handler = func;
|
|
146 }
|
|
147
|
|
148 void i_set_fatal_handler(FailureFunc func __attr_noreturn__)
|
|
149 {
|
|
150 if (func == NULL)
|
|
151 func = default_fatal_handler;
|
|
152 fatal_handler = func;
|
|
153 }
|
|
154
|
|
155 void i_set_error_handler(FailureFunc func)
|
|
156 {
|
|
157 if (func == NULL)
|
|
158 func = default_error_handler;
|
|
159 error_handler = func;
|
|
160 }
|
|
161
|
|
162 void i_set_warning_handler(FailureFunc func)
|
|
163 {
|
|
164 if (func == NULL)
|
|
165 func = default_warning_handler;
|
|
166 warning_handler = func;
|
|
167 }
|
|
168
|
|
169 void i_syslog_panic_handler(const char *fmt, va_list args)
|
|
170 {
|
|
171 vsyslog(LOG_CRIT, fmt, args);
|
|
172 abort();
|
|
173 }
|
|
174
|
|
175 void i_syslog_fatal_handler(const char *fmt, va_list args)
|
|
176 {
|
|
177 vsyslog(LOG_CRIT, fmt, args);
|
|
178 exit(98);
|
|
179 }
|
|
180
|
|
181 void i_syslog_error_handler(const char *fmt, va_list args)
|
|
182 {
|
|
183 vsyslog(LOG_ERR, fmt, args);
|
|
184 }
|
|
185
|
|
186 void i_syslog_warning_handler(const char *fmt, va_list args)
|
|
187 {
|
|
188 vsyslog(LOG_WARNING, fmt, args);
|
|
189 }
|
|
190
|
|
191 void i_set_failure_file(const char *path, const char *prefix)
|
|
192 {
|
|
193 if (log_fd != stderr)
|
|
194 (void)fclose(log_fd);
|
|
195
|
|
196 log_fd = fopen(path, "a");
|
|
197 if (log_fd == NULL)
|
|
198 i_fatal("Can't open log file %s: %m", path);
|
|
199
|
|
200 i_free(log_prefix);
|
|
201 log_prefix = i_strconcat(prefix, ": ", NULL);
|
|
202 }
|
|
203
|
|
204 void i_set_failure_timestamp_format(const char *fmt)
|
|
205 {
|
|
206 i_free(log_stamp_format);
|
|
207 log_stamp_format = i_strdup(fmt);
|
|
208 }
|
|
209
|
|
210 void failures_init(void)
|
|
211 {
|
|
212 log_fd = stderr;
|
|
213 log_prefix = NULL;
|
|
214 log_stamp_format = NULL;
|
|
215
|
|
216 i_set_panic_handler(NULL);
|
|
217 i_set_fatal_handler(NULL);
|
|
218 i_set_error_handler(NULL);
|
|
219 i_set_warning_handler(NULL);
|
|
220 }
|
|
221
|
|
222 void failures_deinit(void)
|
|
223 {
|
|
224 if (log_fd != stderr) {
|
|
225 (void)fclose(log_fd);
|
|
226 log_fd = stderr;
|
|
227 }
|
|
228 }
|