jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 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 | "ISIS", |
| 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 | |
| 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 | |
| 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) |
| 110 | vsyslog (priority, format, args[ZLOG_SYSLOG_INDEX]); |
| 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 | |
| 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; |
| 369 | |
| 370 | /* There is opend file. */ |
| 371 | zlog_reset_file (zl); |
| 372 | |
| 373 | /* Set default zl. */ |
| 374 | if (zl == NULL) |
| 375 | zl = zlog_default; |
| 376 | |
| 377 | /* Open file. */ |
| 378 | fp = fopen (filename, "a"); |
| 379 | if (fp == NULL) |
| 380 | return 0; |
| 381 | |
| 382 | /* Set flags. */ |
| 383 | zl->filename = strdup (filename); |
| 384 | zl->flags |= ZLOG_FILE; |
| 385 | zl->fp = fp; |
| 386 | |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | /* Reset opend file. */ |
| 391 | int |
| 392 | zlog_reset_file (struct zlog *zl) |
| 393 | { |
| 394 | if (zl == NULL) |
| 395 | zl = zlog_default; |
| 396 | |
| 397 | zl->flags &= ~ZLOG_FILE; |
| 398 | |
| 399 | if (zl->fp) |
| 400 | fclose (zl->fp); |
| 401 | zl->fp = NULL; |
| 402 | |
| 403 | if (zl->filename) |
| 404 | free (zl->filename); |
| 405 | zl->filename = NULL; |
| 406 | |
| 407 | return 1; |
| 408 | } |
| 409 | |
| 410 | /* Reopen log file. */ |
| 411 | int |
| 412 | zlog_rotate (struct zlog *zl) |
| 413 | { |
| 414 | FILE *fp; |
| 415 | |
| 416 | if (zl == NULL) |
| 417 | zl = zlog_default; |
| 418 | |
| 419 | if (zl->fp) |
| 420 | fclose (zl->fp); |
| 421 | zl->fp = NULL; |
| 422 | |
| 423 | if (zl->filename) |
| 424 | { |
| 425 | fp = fopen (zl->filename, "a"); |
| 426 | if (fp == NULL) |
| 427 | return -1; |
| 428 | zl->fp = fp; |
| 429 | } |
| 430 | |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | static char *zlog_cwd = NULL; |
| 435 | |
| 436 | void |
| 437 | zlog_save_cwd () |
| 438 | { |
| 439 | char *cwd; |
| 440 | |
| 441 | cwd = getcwd (NULL, MAXPATHLEN); |
| 442 | |
| 443 | zlog_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1); |
| 444 | strcpy (zlog_cwd, cwd); |
| 445 | } |
| 446 | |
| 447 | char * |
| 448 | zlog_get_cwd () |
| 449 | { |
| 450 | return zlog_cwd; |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | zlog_free_cwd () |
| 455 | { |
| 456 | if (zlog_cwd) |
| 457 | XFREE (MTYPE_TMP, zlog_cwd); |
| 458 | } |
| 459 | |
| 460 | /* Message lookup function. */ |
| 461 | char * |
| 462 | lookup (struct message *mes, int key) |
| 463 | { |
| 464 | struct message *pnt; |
| 465 | |
| 466 | for (pnt = mes; pnt->key != 0; pnt++) |
| 467 | if (pnt->key == key) |
| 468 | return pnt->str; |
| 469 | |
| 470 | return ""; |
| 471 | } |
| 472 | |
| 473 | /* Very old hacky version of message lookup function. Still partly |
| 474 | used in bgpd and ospfd. */ |
| 475 | char * |
| 476 | mes_lookup (struct message *meslist, int max, int index) |
| 477 | { |
| 478 | if (index < 0 || index >= max) |
| 479 | { |
| 480 | zlog_err ("message index out of bound: %d", max); |
| 481 | return NULL; |
| 482 | } |
| 483 | return meslist[index].str; |
| 484 | } |