]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Logging of zebra |
2 | * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "log.h" | |
25 | #include "memory.h" | |
26 | #include "command.h" | |
27 | ||
28 | struct zlog *zlog_default = NULL; | |
29 | ||
30 | const char *zlog_proto_names[] = | |
31 | { | |
32 | "NONE", | |
33 | "DEFAULT", | |
34 | "ZEBRA", | |
35 | "RIP", | |
36 | "BGP", | |
37 | "OSPF", | |
38 | "RIPNG", | |
39 | "OSPF6", | |
9e867fe6 | 40 | "ISIS", |
718e3744 | 41 | "MASC", |
42 | NULL, | |
43 | }; | |
44 | ||
45 | const char *zlog_priority[] = | |
46 | { | |
47 | "emergencies", | |
48 | "alerts", | |
49 | "critical", | |
50 | "errors", | |
51 | "warnings", | |
52 | "notifications", | |
53 | "informational", | |
54 | "debugging", | |
55 | NULL, | |
56 | }; | |
57 | ||
58 | ||
59 | \f | |
60 | /* For time string format. */ | |
61 | #define TIME_BUF 27 | |
62 | ||
63 | /* Utility routine for current time printing. */ | |
64 | static void | |
65 | time_print (FILE *fp) | |
66 | { | |
67 | int ret; | |
68 | char buf [TIME_BUF]; | |
69 | time_t clock; | |
70 | struct tm *tm; | |
71 | ||
72 | time (&clock); | |
73 | tm = localtime (&clock); | |
74 | ||
75 | ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm); | |
76 | if (ret == 0) { | |
77 | zlog_warn ("strftime error"); | |
78 | } | |
79 | ||
80 | fprintf (fp, "%s ", buf); | |
81 | } | |
82 | \f | |
83 | /* va_list version of zlog. */ | |
84 | void | |
85 | vzlog (struct zlog *zl, int priority, const char *format, va_list *args) | |
86 | { | |
87 | /* If zlog is not specified, use default one. */ | |
88 | if (zl == NULL) | |
89 | zl = zlog_default; | |
90 | ||
91 | /* When zlog_default is also NULL, use stderr for logging. */ | |
92 | if (zl == NULL) | |
93 | { | |
94 | time_print (stderr); | |
95 | fprintf (stderr, "%s: ", "unknown"); | |
96 | vfprintf (stderr, format, args[ZLOG_NOLOG_INDEX]); | |
97 | fprintf (stderr, "\n"); | |
98 | fflush (stderr); | |
99 | ||
100 | /* In this case we return at here. */ | |
101 | return; | |
102 | } | |
103 | ||
104 | /* only log this information if it has not been masked out */ | |
105 | if ( priority > zl->maskpri ) | |
106 | return ; | |
107 | ||
108 | /* Syslog output */ | |
109 | if (zl->flags & ZLOG_SYSLOG) | |
828eb7fb | 110 | vsyslog (priority|zlog_default->facility, format, args[ZLOG_SYSLOG_INDEX]); |
718e3744 | 111 | |
112 | /* File output. */ | |
113 | if (zl->flags & ZLOG_FILE) | |
114 | { | |
115 | time_print (zl->fp); | |
116 | if (zl->record_priority) fprintf (zl->fp, "%s: ", zlog_priority[priority]); | |
117 | fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]); | |
118 | vfprintf (zl->fp, format, args[ZLOG_FILE_INDEX]); | |
119 | fprintf (zl->fp, "\n"); | |
120 | fflush (zl->fp); | |
121 | } | |
122 | ||
123 | /* stdout output. */ | |
124 | if (zl->flags & ZLOG_STDOUT) | |
125 | { | |
126 | time_print (stdout); | |
127 | if (zl->record_priority) fprintf (stdout, "%s: ", zlog_priority[priority]); | |
128 | fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]); | |
129 | vfprintf (stdout, format, args[ZLOG_STDOUT_INDEX]); | |
130 | fprintf (stdout, "\n"); | |
131 | fflush (stdout); | |
132 | } | |
133 | ||
134 | /* stderr output. */ | |
135 | if (zl->flags & ZLOG_STDERR) | |
136 | { | |
137 | time_print (stderr); | |
138 | if (zl->record_priority) fprintf (stderr, "%s: ", zlog_priority[priority]); | |
139 | fprintf (stderr, "%s: ", zlog_proto_names[zl->protocol]); | |
140 | vfprintf (stderr, format, args[ZLOG_STDERR_INDEX]); | |
141 | fprintf (stderr, "\n"); | |
142 | fflush (stderr); | |
143 | } | |
144 | ||
145 | /* Terminal monitor. */ | |
146 | vty_log (zlog_proto_names[zl->protocol], format, args[ZLOG_NOLOG_INDEX]); | |
147 | } | |
148 | ||
149 | void | |
150 | zlog (struct zlog *zl, int priority, const char *format, ...) | |
151 | { | |
152 | va_list args[ZLOG_MAX_INDEX]; | |
153 | int index; | |
154 | ||
155 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
156 | va_start(args[index], format); | |
157 | ||
158 | vzlog (zl, priority, format, args); | |
159 | ||
160 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
161 | va_end (args[index]); | |
162 | } | |
163 | ||
164 | void | |
165 | zlog_err (const char *format, ...) | |
166 | { | |
167 | va_list args[ZLOG_MAX_INDEX]; | |
168 | int index; | |
169 | ||
170 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
171 | va_start(args[index], format); | |
172 | ||
173 | vzlog (NULL, LOG_ERR, format, args); | |
174 | ||
175 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
176 | va_end (args[index]); | |
177 | } | |
178 | ||
179 | void | |
180 | zlog_warn (const char *format, ...) | |
181 | { | |
182 | va_list args[ZLOG_MAX_INDEX]; | |
183 | int index; | |
184 | ||
185 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
186 | va_start(args[index], format); | |
187 | ||
188 | vzlog (NULL, LOG_WARNING, format, args); | |
189 | ||
190 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
191 | va_end (args[index]); | |
192 | } | |
193 | ||
194 | void | |
195 | zlog_info (const char *format, ...) | |
196 | { | |
197 | va_list args[ZLOG_MAX_INDEX]; | |
198 | int index; | |
199 | ||
200 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
201 | va_start(args[index], format); | |
202 | ||
203 | vzlog (NULL, LOG_INFO, format, args); | |
204 | ||
205 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
206 | va_end (args[index]); | |
207 | } | |
208 | ||
209 | void | |
210 | zlog_notice (const char *format, ...) | |
211 | { | |
212 | va_list args[ZLOG_MAX_INDEX]; | |
213 | int index; | |
214 | ||
215 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
216 | va_start(args[index], format); | |
217 | ||
218 | vzlog (NULL, LOG_NOTICE, format, args); | |
219 | ||
220 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
221 | va_end (args[index]); | |
222 | } | |
223 | ||
224 | void | |
225 | zlog_debug (const char *format, ...) | |
226 | { | |
227 | va_list args[ZLOG_MAX_INDEX]; | |
228 | int index; | |
229 | ||
230 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
231 | va_start(args[index], format); | |
232 | ||
233 | vzlog (NULL, LOG_DEBUG, format, args); | |
234 | ||
235 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
236 | va_end (args[index]); | |
237 | } | |
238 | ||
239 | void | |
240 | plog_err (struct zlog *zl, const char *format, ...) | |
241 | { | |
242 | va_list args[ZLOG_MAX_INDEX]; | |
243 | int index; | |
244 | ||
245 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
246 | va_start(args[index], format); | |
247 | ||
248 | vzlog (zl, LOG_ERR, format, args); | |
249 | ||
250 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
251 | va_end (args[index]); | |
252 | } | |
253 | ||
254 | void | |
255 | plog_warn (struct zlog *zl, const char *format, ...) | |
256 | { | |
257 | va_list args[ZLOG_MAX_INDEX]; | |
258 | int index; | |
259 | ||
260 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
261 | va_start(args[index], format); | |
262 | ||
263 | vzlog (zl, LOG_WARNING, format, args); | |
264 | ||
265 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
266 | va_end (args[index]); | |
267 | } | |
268 | ||
269 | void | |
270 | plog_info (struct zlog *zl, const char *format, ...) | |
271 | { | |
272 | va_list args[ZLOG_MAX_INDEX]; | |
273 | int index; | |
274 | ||
275 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
276 | va_start(args[index], format); | |
277 | ||
278 | vzlog (zl, LOG_INFO, format, args); | |
279 | ||
280 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
281 | va_end (args[index]); | |
282 | } | |
283 | ||
284 | void | |
285 | plog_notice (struct zlog *zl, const char *format, ...) | |
286 | { | |
287 | va_list args[ZLOG_MAX_INDEX]; | |
288 | int index; | |
289 | ||
290 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
291 | va_start(args[index], format); | |
292 | ||
293 | vzlog (zl, LOG_NOTICE, format, args); | |
294 | ||
295 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
296 | va_end (args[index]); | |
297 | } | |
298 | ||
299 | void | |
300 | plog_debug (struct zlog *zl, const char *format, ...) | |
301 | { | |
302 | va_list args[ZLOG_MAX_INDEX]; | |
303 | int index; | |
304 | ||
305 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
306 | va_start(args[index], format); | |
307 | ||
308 | vzlog (zl, LOG_DEBUG, format, args); | |
309 | ||
310 | for (index = 0; index < ZLOG_MAX_INDEX; index++) | |
311 | va_end (args[index]); | |
312 | } | |
313 | ||
314 | \f | |
315 | /* Open log stream */ | |
316 | struct zlog * | |
317 | openzlog (const char *progname, int flags, zlog_proto_t protocol, | |
318 | int syslog_flags, int syslog_facility) | |
319 | { | |
320 | struct zlog *zl; | |
321 | ||
322 | zl = XMALLOC(MTYPE_ZLOG, sizeof (struct zlog)); | |
323 | memset (zl, 0, sizeof (struct zlog)); | |
324 | ||
325 | zl->ident = progname; | |
326 | zl->flags = flags; | |
327 | zl->protocol = protocol; | |
328 | zl->facility = syslog_facility; | |
329 | zl->maskpri = LOG_DEBUG; | |
330 | zl->record_priority = 0; | |
331 | ||
332 | openlog (progname, syslog_flags, zl->facility); | |
333 | ||
334 | return zl; | |
335 | } | |
336 | ||
337 | void | |
338 | closezlog (struct zlog *zl) | |
339 | { | |
340 | closelog(); | |
341 | fclose (zl->fp); | |
342 | ||
343 | XFREE (MTYPE_ZLOG, zl); | |
344 | } | |
345 | ||
346 | /* Called from command.c. */ | |
347 | void | |
348 | zlog_set_flag (struct zlog *zl, int flags) | |
349 | { | |
350 | if (zl == NULL) | |
351 | zl = zlog_default; | |
352 | ||
353 | zl->flags |= flags; | |
354 | } | |
355 | ||
356 | void | |
357 | zlog_reset_flag (struct zlog *zl, int flags) | |
358 | { | |
359 | if (zl == NULL) | |
360 | zl = zlog_default; | |
361 | ||
362 | zl->flags &= ~flags; | |
363 | } | |
364 | ||
365 | int | |
366 | zlog_set_file (struct zlog *zl, int flags, char *filename) | |
367 | { | |
368 | FILE *fp; | |
aa593d5e | 369 | mode_t oldumask; |
718e3744 | 370 | |
371 | /* There is opend file. */ | |
372 | zlog_reset_file (zl); | |
373 | ||
374 | /* Set default zl. */ | |
375 | if (zl == NULL) | |
376 | zl = zlog_default; | |
377 | ||
378 | /* Open file. */ | |
aa593d5e | 379 | oldumask = umask (0777 & ~LOGFILE_MASK); |
718e3744 | 380 | fp = fopen (filename, "a"); |
381 | if (fp == NULL) | |
aa593d5e | 382 | { |
383 | umask(oldumask); | |
384 | return 0; | |
385 | } | |
386 | umask(oldumask); | |
718e3744 | 387 | |
388 | /* Set flags. */ | |
389 | zl->filename = strdup (filename); | |
390 | zl->flags |= ZLOG_FILE; | |
391 | zl->fp = fp; | |
392 | ||
393 | return 1; | |
394 | } | |
395 | ||
396 | /* Reset opend file. */ | |
397 | int | |
398 | zlog_reset_file (struct zlog *zl) | |
399 | { | |
400 | if (zl == NULL) | |
401 | zl = zlog_default; | |
402 | ||
403 | zl->flags &= ~ZLOG_FILE; | |
404 | ||
405 | if (zl->fp) | |
406 | fclose (zl->fp); | |
407 | zl->fp = NULL; | |
408 | ||
409 | if (zl->filename) | |
410 | free (zl->filename); | |
411 | zl->filename = NULL; | |
412 | ||
413 | return 1; | |
414 | } | |
415 | ||
416 | /* Reopen log file. */ | |
417 | int | |
418 | zlog_rotate (struct zlog *zl) | |
419 | { | |
420 | FILE *fp; | |
421 | ||
422 | if (zl == NULL) | |
423 | zl = zlog_default; | |
424 | ||
425 | if (zl->fp) | |
426 | fclose (zl->fp); | |
427 | zl->fp = NULL; | |
428 | ||
429 | if (zl->filename) | |
430 | { | |
aa593d5e | 431 | mode_t oldumask; |
432 | ||
433 | oldumask = umask (0777 & ~LOGFILE_MASK); | |
718e3744 | 434 | fp = fopen (zl->filename, "a"); |
435 | if (fp == NULL) | |
aa593d5e | 436 | { |
437 | umask(oldumask); | |
438 | return -1; | |
439 | } | |
440 | umask(oldumask); | |
718e3744 | 441 | zl->fp = fp; |
442 | } | |
443 | ||
444 | return 1; | |
445 | } | |
446 | \f | |
447 | static char *zlog_cwd = NULL; | |
448 | ||
449 | void | |
450 | zlog_save_cwd () | |
451 | { | |
452 | char *cwd; | |
453 | ||
454 | cwd = getcwd (NULL, MAXPATHLEN); | |
455 | ||
456 | zlog_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1); | |
457 | strcpy (zlog_cwd, cwd); | |
458 | } | |
459 | ||
460 | char * | |
461 | zlog_get_cwd () | |
462 | { | |
463 | return zlog_cwd; | |
464 | } | |
465 | ||
466 | void | |
467 | zlog_free_cwd () | |
468 | { | |
469 | if (zlog_cwd) | |
470 | XFREE (MTYPE_TMP, zlog_cwd); | |
471 | } | |
472 | \f | |
473 | /* Message lookup function. */ | |
474 | char * | |
475 | lookup (struct message *mes, int key) | |
476 | { | |
477 | struct message *pnt; | |
478 | ||
479 | for (pnt = mes; pnt->key != 0; pnt++) | |
480 | if (pnt->key == key) | |
481 | return pnt->str; | |
482 | ||
483 | return ""; | |
484 | } | |
485 | ||
486 | /* Very old hacky version of message lookup function. Still partly | |
487 | used in bgpd and ospfd. */ | |
488 | char * | |
489 | mes_lookup (struct message *meslist, int max, int index) | |
490 | { | |
491 | if (index < 0 || index >= max) | |
492 | { | |
493 | zlog_err ("message index out of bound: %d", max); | |
494 | return NULL; | |
495 | } | |
496 | return meslist[index].str; | |
497 | } |