]>
git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
2 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
28 struct zlog
*zlog_default
= NULL
;
30 const char *zlog_proto_names
[] =
44 const char *zlog_priority
[] =
59 /* For time string format. */
62 /* Utility routine for current time printing. */
72 tm
= localtime (&clock
);
74 ret
= strftime (buf
, TIME_BUF
, "%Y/%m/%d %H:%M:%S", tm
);
76 zlog_warn ("strftime error");
79 fprintf (fp
, "%s ", buf
);
82 /* va_list version of zlog. */
84 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list *args
)
86 /* If zlog is not specified, use default one. */
90 /* When zlog_default is also NULL, use stderr for logging. */
94 fprintf (stderr
, "%s: ", "unknown");
95 vfprintf (stderr
, format
, args
[ZLOG_NOLOG_INDEX
]);
96 fprintf (stderr
, "\n");
99 /* In this case we return at here. */
103 /* only log this information if it has not been masked out */
104 if ( priority
> zl
->maskpri
)
108 if (zl
->flags
& ZLOG_SYSLOG
)
109 vsyslog (priority
, format
, args
[ZLOG_SYSLOG_INDEX
]);
112 if (zl
->flags
& ZLOG_FILE
)
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");
123 if (zl
->flags
& ZLOG_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");
134 if (zl
->flags
& ZLOG_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");
144 /* Terminal monitor. */
145 vty_log (zlog_proto_names
[zl
->protocol
], format
, args
[ZLOG_NOLOG_INDEX
]);
149 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
151 va_list args
[ZLOG_MAX_INDEX
];
154 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
155 va_start(args
[index
], format
);
157 vzlog (zl
, priority
, format
, args
);
159 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
160 va_end (args
[index
]);
164 zlog_err (const char *format
, ...)
166 va_list args
[ZLOG_MAX_INDEX
];
169 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
170 va_start(args
[index
], format
);
172 vzlog (NULL
, LOG_ERR
, format
, args
);
174 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
175 va_end (args
[index
]);
179 zlog_warn (const char *format
, ...)
181 va_list args
[ZLOG_MAX_INDEX
];
184 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
185 va_start(args
[index
], format
);
187 vzlog (NULL
, LOG_WARNING
, format
, args
);
189 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
190 va_end (args
[index
]);
194 zlog_info (const char *format
, ...)
196 va_list args
[ZLOG_MAX_INDEX
];
199 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
200 va_start(args
[index
], format
);
202 vzlog (NULL
, LOG_INFO
, format
, args
);
204 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
205 va_end (args
[index
]);
209 zlog_notice (const char *format
, ...)
211 va_list args
[ZLOG_MAX_INDEX
];
214 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
215 va_start(args
[index
], format
);
217 vzlog (NULL
, LOG_NOTICE
, format
, args
);
219 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
220 va_end (args
[index
]);
224 zlog_debug (const char *format
, ...)
226 va_list args
[ZLOG_MAX_INDEX
];
229 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
230 va_start(args
[index
], format
);
232 vzlog (NULL
, LOG_DEBUG
, format
, args
);
234 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
235 va_end (args
[index
]);
239 plog_err (struct zlog
*zl
, const char *format
, ...)
241 va_list args
[ZLOG_MAX_INDEX
];
244 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
245 va_start(args
[index
], format
);
247 vzlog (zl
, LOG_ERR
, format
, args
);
249 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
250 va_end (args
[index
]);
254 plog_warn (struct zlog
*zl
, const char *format
, ...)
256 va_list args
[ZLOG_MAX_INDEX
];
259 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
260 va_start(args
[index
], format
);
262 vzlog (zl
, LOG_WARNING
, format
, args
);
264 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
265 va_end (args
[index
]);
269 plog_info (struct zlog
*zl
, const char *format
, ...)
271 va_list args
[ZLOG_MAX_INDEX
];
274 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
275 va_start(args
[index
], format
);
277 vzlog (zl
, LOG_INFO
, format
, args
);
279 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
280 va_end (args
[index
]);
284 plog_notice (struct zlog
*zl
, const char *format
, ...)
286 va_list args
[ZLOG_MAX_INDEX
];
289 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
290 va_start(args
[index
], format
);
292 vzlog (zl
, LOG_NOTICE
, format
, args
);
294 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
295 va_end (args
[index
]);
299 plog_debug (struct zlog
*zl
, const char *format
, ...)
301 va_list args
[ZLOG_MAX_INDEX
];
304 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
305 va_start(args
[index
], format
);
307 vzlog (zl
, LOG_DEBUG
, format
, args
);
309 for (index
= 0; index
< ZLOG_MAX_INDEX
; index
++)
310 va_end (args
[index
]);
314 /* Open log stream */
316 openzlog (const char *progname
, int flags
, zlog_proto_t protocol
,
317 int syslog_flags
, int syslog_facility
)
321 zl
= XMALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
322 memset (zl
, 0, sizeof (struct zlog
));
324 zl
->ident
= progname
;
326 zl
->protocol
= protocol
;
327 zl
->facility
= syslog_facility
;
328 zl
->maskpri
= LOG_DEBUG
;
329 zl
->record_priority
= 0;
331 openlog (progname
, syslog_flags
, zl
->facility
);
337 closezlog (struct zlog
*zl
)
342 XFREE (MTYPE_ZLOG
, zl
);
345 /* Called from command.c. */
347 zlog_set_flag (struct zlog
*zl
, int flags
)
356 zlog_reset_flag (struct zlog
*zl
, int flags
)
365 zlog_set_file (struct zlog
*zl
, int flags
, char *filename
)
369 /* There is opend file. */
370 zlog_reset_file (zl
);
372 /* Set default zl. */
377 fp
= fopen (filename
, "a");
382 zl
->filename
= strdup (filename
);
383 zl
->flags
|= ZLOG_FILE
;
389 /* Reset opend file. */
391 zlog_reset_file (struct zlog
*zl
)
396 zl
->flags
&= ~ZLOG_FILE
;
409 /* Reopen log file. */
411 zlog_rotate (struct zlog
*zl
)
424 fp
= fopen (zl
->filename
, "a");
433 static char *zlog_cwd
= NULL
;
440 cwd
= getcwd (NULL
, MAXPATHLEN
);
442 zlog_cwd
= XMALLOC (MTYPE_TMP
, strlen (cwd
) + 1);
443 strcpy (zlog_cwd
, cwd
);
456 XFREE (MTYPE_TMP
, zlog_cwd
);
459 /* Message lookup function. */
461 lookup (struct message
*mes
, int key
)
465 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
472 /* Very old hacky version of message lookup function. Still partly
473 used in bgpd and ospfd. */
475 mes_lookup (struct message
*meslist
, int max
, int index
)
477 if (index
< 0 || index
>= max
)
479 zlog_err ("message index out of bound: %d", max
);
482 return meslist
[index
].str
;