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