Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/logging.c
Parent Directory
|
Revision Log
Revision 1449 -
(hide annotations)
(download)
(as text)
Tue Jul 2 18:12:58 2013 UTC (11 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 7766 byte(s)
Tue Jul 2 18:12:58 2013 UTC (11 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 7766 byte(s)
Fix bug in time printed by --logtime option.
1 | amb | 520 | /*************************************** |
2 | Functions to handle logging functions. | ||
3 | |||
4 | Part of the Routino routing software. | ||
5 | ******************/ /****************** | ||
6 | amb | 1313 | This file Copyright 2008-2013 Andrew M. Bishop |
7 | amb | 520 | |
8 | This program is free software: you can redistribute it and/or modify | ||
9 | it under the terms of the GNU Affero General Public License as published by | ||
10 | the Free Software Foundation, either version 3 of the License, or | ||
11 | (at your option) any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU Affero General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Affero General Public License | ||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | ***************************************/ | ||
21 | |||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <stdarg.h> | ||
25 | amb | 804 | #include <stdlib.h> |
26 | #include <string.h> | ||
27 | #include <errno.h> | ||
28 | amb | 982 | #include <sys/time.h> |
29 | amb | 520 | |
30 | #include "logging.h" | ||
31 | |||
32 | |||
33 | amb | 680 | /* Global variables */ |
34 | |||
35 | amb | 520 | /*+ The option to print the output in a way that allows logging to a file. +*/ |
36 | int option_loggable=0; | ||
37 | |||
38 | amb | 982 | /*+ The option to print timestamps with the output. +*/ |
39 | amb | 1019 | int option_logtime=0; |
40 | amb | 982 | |
41 | |||
42 | amb | 520 | /* Local functions */ |
43 | |||
44 | static void vfprintf_first(FILE *file,const char *format,va_list ap); | ||
45 | static void vfprintf_middle(FILE *file,const char *format,va_list ap); | ||
46 | static void vfprintf_last(FILE *file,const char *format,va_list ap); | ||
47 | |||
48 | amb | 982 | |
49 | amb | 520 | /* Local variables */ |
50 | |||
51 | amb | 1398 | /*+ The time that program_start() was called. +*/ |
52 | static struct timeval program_start_time; | ||
53 | amb | 982 | |
54 | amb | 1398 | /*+ The time that printf_first() was called. +*/ |
55 | static struct timeval function_start_time; | ||
56 | |||
57 | amb | 648 | /*+ The length of the string printed out last time. +*/ |
58 | amb | 520 | static int printed_length=0; |
59 | |||
60 | |||
61 | /*++++++++++++++++++++++++++++++++++++++ | ||
62 | amb | 1398 | Record the time that the program started. |
63 | ++++++++++++++++++++++++++++++++++++++*/ | ||
64 | |||
65 | void printf_program_start(void) | ||
66 | { | ||
67 | amb | 1400 | gettimeofday(&program_start_time,NULL); |
68 | amb | 1398 | } |
69 | |||
70 | |||
71 | /*++++++++++++++++++++++++++++++++++++++ | ||
72 | Record the time that the program started. | ||
73 | ++++++++++++++++++++++++++++++++++++++*/ | ||
74 | |||
75 | void printf_program_end(void) | ||
76 | { | ||
77 | if(option_logtime) | ||
78 | { | ||
79 | printf("\n"); | ||
80 | fprintf_elapsed_time(stdout,&program_start_time); | ||
81 | printf("Complete\n"); | ||
82 | fflush(stdout); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | |||
87 | /*++++++++++++++++++++++++++++++++++++++ | ||
88 | amb | 680 | Print the first message in an overwriting sequence (to stdout). |
89 | amb | 520 | |
90 | const char *format The format string. | ||
91 | |||
92 | ... The other arguments. | ||
93 | ++++++++++++++++++++++++++++++++++++++*/ | ||
94 | |||
95 | void printf_first(const char *format, ...) | ||
96 | { | ||
97 | va_list ap; | ||
98 | |||
99 | amb | 982 | if(option_logtime) |
100 | amb | 1398 | gettimeofday(&function_start_time,NULL); |
101 | amb | 982 | |
102 | amb | 520 | if(option_loggable) |
103 | return; | ||
104 | |||
105 | va_start(ap,format); | ||
106 | |||
107 | vfprintf_first(stdout,format,ap); | ||
108 | |||
109 | va_end(ap); | ||
110 | } | ||
111 | |||
112 | |||
113 | /*++++++++++++++++++++++++++++++++++++++ | ||
114 | amb | 680 | Print the middle message in an overwriting sequence (to stdout). |
115 | amb | 520 | |
116 | const char *format The format string. | ||
117 | |||
118 | ... The other arguments. | ||
119 | ++++++++++++++++++++++++++++++++++++++*/ | ||
120 | |||
121 | void printf_middle(const char *format, ...) | ||
122 | { | ||
123 | va_list ap; | ||
124 | |||
125 | if(option_loggable) | ||
126 | return; | ||
127 | |||
128 | va_start(ap,format); | ||
129 | |||
130 | vfprintf_middle(stdout,format,ap); | ||
131 | |||
132 | va_end(ap); | ||
133 | } | ||
134 | |||
135 | |||
136 | /*++++++++++++++++++++++++++++++++++++++ | ||
137 | amb | 680 | Print the last message in an overwriting sequence (to stdout). |
138 | amb | 520 | |
139 | const char *format The format string. | ||
140 | |||
141 | ... The other arguments. | ||
142 | ++++++++++++++++++++++++++++++++++++++*/ | ||
143 | |||
144 | void printf_last(const char *format, ...) | ||
145 | { | ||
146 | va_list ap; | ||
147 | |||
148 | va_start(ap,format); | ||
149 | |||
150 | vfprintf_last(stdout,format,ap); | ||
151 | |||
152 | va_end(ap); | ||
153 | } | ||
154 | |||
155 | |||
156 | /*++++++++++++++++++++++++++++++++++++++ | ||
157 | amb | 680 | Print the first message in an overwriting sequence to a specified file. |
158 | amb | 520 | |
159 | FILE *file The file to write to. | ||
160 | |||
161 | const char *format The format string. | ||
162 | |||
163 | ... The other arguments. | ||
164 | ++++++++++++++++++++++++++++++++++++++*/ | ||
165 | |||
166 | void fprintf_first(FILE *file,const char *format, ...) | ||
167 | { | ||
168 | va_list ap; | ||
169 | |||
170 | amb | 982 | if(option_logtime) |
171 | amb | 1398 | gettimeofday(&function_start_time,NULL); |
172 | amb | 982 | |
173 | amb | 520 | if(option_loggable) |
174 | return; | ||
175 | |||
176 | va_start(ap,format); | ||
177 | |||
178 | vfprintf_first(file,format,ap); | ||
179 | |||
180 | va_end(ap); | ||
181 | } | ||
182 | |||
183 | |||
184 | /*++++++++++++++++++++++++++++++++++++++ | ||
185 | amb | 680 | Print the middle message in an overwriting sequence to a specified file. |
186 | amb | 520 | |
187 | FILE *file The file to write to. | ||
188 | |||
189 | const char *format The format string. | ||
190 | |||
191 | ... The other arguments. | ||
192 | ++++++++++++++++++++++++++++++++++++++*/ | ||
193 | |||
194 | void fprintf_middle(FILE *file,const char *format, ...) | ||
195 | { | ||
196 | va_list ap; | ||
197 | |||
198 | if(option_loggable) | ||
199 | return; | ||
200 | |||
201 | va_start(ap,format); | ||
202 | |||
203 | vfprintf_middle(file,format,ap); | ||
204 | |||
205 | va_end(ap); | ||
206 | } | ||
207 | |||
208 | |||
209 | /*++++++++++++++++++++++++++++++++++++++ | ||
210 | amb | 680 | Print the last message in an overwriting sequence to a specified file. |
211 | amb | 520 | |
212 | FILE *file The file to write to. | ||
213 | |||
214 | const char *format The format string. | ||
215 | |||
216 | ... The other arguments. | ||
217 | ++++++++++++++++++++++++++++++++++++++*/ | ||
218 | |||
219 | void fprintf_last(FILE *file,const char *format, ...) | ||
220 | { | ||
221 | va_list ap; | ||
222 | |||
223 | va_start(ap,format); | ||
224 | |||
225 | vfprintf_last(file,format,ap); | ||
226 | |||
227 | va_end(ap); | ||
228 | } | ||
229 | |||
230 | |||
231 | /*++++++++++++++++++++++++++++++++++++++ | ||
232 | amb | 680 | Do the work to print the first message in an overwriting sequence. |
233 | amb | 520 | |
234 | FILE *file The file to write to. | ||
235 | |||
236 | const char *format The format string. | ||
237 | |||
238 | va_list ap The other arguments. | ||
239 | ++++++++++++++++++++++++++++++++++++++*/ | ||
240 | |||
241 | static void vfprintf_first(FILE *file,const char *format,va_list ap) | ||
242 | { | ||
243 | int retval; | ||
244 | |||
245 | amb | 982 | if(option_logtime) |
246 | amb | 1398 | fprintf_elapsed_time(file,&function_start_time); |
247 | amb | 982 | |
248 | amb | 520 | retval=vfprintf(file,format,ap); |
249 | fflush(file); | ||
250 | |||
251 | if(retval>0) | ||
252 | printed_length=retval; | ||
253 | } | ||
254 | |||
255 | |||
256 | /*++++++++++++++++++++++++++++++++++++++ | ||
257 | amb | 680 | Do the work to print the middle message in an overwriting sequence. |
258 | amb | 520 | |
259 | FILE *file The file to write to. | ||
260 | |||
261 | const char *format The format string. | ||
262 | |||
263 | va_list ap The other arguments. | ||
264 | ++++++++++++++++++++++++++++++++++++++*/ | ||
265 | |||
266 | static void vfprintf_middle(FILE *file,const char *format,va_list ap) | ||
267 | { | ||
268 | int retval; | ||
269 | |||
270 | amb | 1193 | fputc('\r',file); |
271 | amb | 982 | |
272 | if(option_logtime) | ||
273 | amb | 1398 | fprintf_elapsed_time(file,&function_start_time); |
274 | amb | 982 | |
275 | amb | 520 | retval=vfprintf(file,format,ap); |
276 | fflush(file); | ||
277 | |||
278 | if(retval>0) | ||
279 | amb | 648 | { |
280 | int new_printed_length=retval; | ||
281 | |||
282 | while(retval++<printed_length) | ||
283 | amb | 1193 | fputc(' ',file); |
284 | amb | 648 | |
285 | printed_length=new_printed_length; | ||
286 | } | ||
287 | amb | 520 | } |
288 | |||
289 | |||
290 | /*++++++++++++++++++++++++++++++++++++++ | ||
291 | amb | 680 | Do the work to print the last message in an overwriting sequence. |
292 | amb | 520 | |
293 | FILE *file The file to write to. | ||
294 | |||
295 | const char *format The format string. | ||
296 | |||
297 | va_list ap The other arguments. | ||
298 | ++++++++++++++++++++++++++++++++++++++*/ | ||
299 | |||
300 | static void vfprintf_last(FILE *file,const char *format,va_list ap) | ||
301 | { | ||
302 | int retval; | ||
303 | |||
304 | if(!option_loggable) | ||
305 | amb | 1193 | fputc('\r',file); |
306 | amb | 982 | |
307 | if(option_logtime) | ||
308 | amb | 1398 | fprintf_elapsed_time(file,&function_start_time); |
309 | amb | 982 | |
310 | amb | 520 | retval=vfprintf(file,format,ap); |
311 | |||
312 | if(retval>0) | ||
313 | while(retval++<printed_length) | ||
314 | amb | 1193 | fputc(' ',file); |
315 | amb | 520 | |
316 | amb | 1193 | fputc('\n',file); |
317 | amb | 520 | fflush(file); |
318 | } | ||
319 | amb | 804 | |
320 | |||
321 | /*++++++++++++++++++++++++++++++++++++++ | ||
322 | amb | 982 | Print the elapsed time without a following newline. |
323 | |||
324 | FILE *file The file to print to. | ||
325 | |||
326 | struct timeval *start The start time from which the elapsed time is to be printed. | ||
327 | ++++++++++++++++++++++++++++++++++++++*/ | ||
328 | |||
329 | void fprintf_elapsed_time(FILE *file,struct timeval *start) | ||
330 | { | ||
331 | struct timeval finish,elapsed; | ||
332 | |||
333 | gettimeofday(&finish,NULL); | ||
334 | |||
335 | elapsed.tv_sec =finish.tv_sec -start->tv_sec; | ||
336 | elapsed.tv_usec=finish.tv_usec-start->tv_usec; | ||
337 | if(elapsed.tv_usec<0) | ||
338 | { | ||
339 | elapsed.tv_sec -=1; | ||
340 | elapsed.tv_usec+=1000000; | ||
341 | } | ||
342 | |||
343 | amb | 1449 | fprintf(file,"[%2ld:%02ld.%03ld] ",elapsed.tv_sec/60,elapsed.tv_sec%60,elapsed.tv_usec/1000); |
344 | amb | 982 | } |
345 | |||
346 | |||
347 | /*++++++++++++++++++++++++++++++++++++++ | ||
348 | amb | 1166 | Log a fatal error and exit |
349 | |||
350 | const char *message The error message. | ||
351 | |||
352 | const char *file The file in which the error occured. | ||
353 | |||
354 | int line The line number in the file at which the error occured. | ||
355 | ++++++++++++++++++++++++++++++++++++++*/ | ||
356 | |||
357 | void _logassert(const char *message,const char *file,int line) | ||
358 | { | ||
359 | fprintf(stderr,"Routino Fatal Error (%s:%d): %s\n",file,line,message); | ||
360 | |||
361 | exit(EXIT_FAILURE); | ||
362 | } |