paul | 718e374 | 2002-12-13 20:15:29 +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 | "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 | |
| 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 | |
| 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) |
paul | 828eb7f | 2003-07-26 06:05:18 +0000 | [diff] [blame] | 109 | vsyslog (priority|zlog_default->facility, format, args[ZLOG_SYSLOG_INDEX]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
| 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; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 368 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 378 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 379 | fp = fopen (filename, "a"); |
| 380 | if (fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 381 | { |
| 382 | umask(oldumask); |
| 383 | return 0; |
| 384 | } |
| 385 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | |
| 387 | /* Set flags. */ |
| 388 | zl->filename = strdup (filename); |
| 389 | zl->flags |= ZLOG_FILE; |
| 390 | zl->fp = fp; |
| 391 | |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | /* Reset opend file. */ |
| 396 | int |
| 397 | zlog_reset_file (struct zlog *zl) |
| 398 | { |
| 399 | if (zl == NULL) |
| 400 | zl = zlog_default; |
| 401 | |
| 402 | zl->flags &= ~ZLOG_FILE; |
| 403 | |
| 404 | if (zl->fp) |
| 405 | fclose (zl->fp); |
| 406 | zl->fp = NULL; |
| 407 | |
| 408 | if (zl->filename) |
| 409 | free (zl->filename); |
| 410 | zl->filename = NULL; |
| 411 | |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* Reopen log file. */ |
| 416 | int |
| 417 | zlog_rotate (struct zlog *zl) |
| 418 | { |
| 419 | FILE *fp; |
| 420 | |
| 421 | if (zl == NULL) |
| 422 | zl = zlog_default; |
| 423 | |
| 424 | if (zl->fp) |
| 425 | fclose (zl->fp); |
| 426 | zl->fp = NULL; |
| 427 | |
| 428 | if (zl->filename) |
| 429 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 430 | mode_t oldumask; |
| 431 | |
| 432 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 433 | fp = fopen (zl->filename, "a"); |
| 434 | if (fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 435 | { |
| 436 | umask(oldumask); |
| 437 | return -1; |
| 438 | } |
| 439 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | zl->fp = fp; |
| 441 | } |
| 442 | |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | static char *zlog_cwd = NULL; |
| 447 | |
| 448 | void |
| 449 | zlog_save_cwd () |
| 450 | { |
| 451 | char *cwd; |
| 452 | |
| 453 | cwd = getcwd (NULL, MAXPATHLEN); |
| 454 | |
| 455 | zlog_cwd = XMALLOC (MTYPE_TMP, strlen (cwd) + 1); |
| 456 | strcpy (zlog_cwd, cwd); |
| 457 | } |
| 458 | |
| 459 | char * |
| 460 | zlog_get_cwd () |
| 461 | { |
| 462 | return zlog_cwd; |
| 463 | } |
| 464 | |
| 465 | void |
| 466 | zlog_free_cwd () |
| 467 | { |
| 468 | if (zlog_cwd) |
| 469 | XFREE (MTYPE_TMP, zlog_cwd); |
| 470 | } |
| 471 | |
| 472 | /* Message lookup function. */ |
| 473 | char * |
| 474 | lookup (struct message *mes, int key) |
| 475 | { |
| 476 | struct message *pnt; |
| 477 | |
| 478 | for (pnt = mes; pnt->key != 0; pnt++) |
| 479 | if (pnt->key == key) |
| 480 | return pnt->str; |
| 481 | |
| 482 | return ""; |
| 483 | } |
| 484 | |
| 485 | /* Very old hacky version of message lookup function. Still partly |
| 486 | used in bgpd and ospfd. */ |
| 487 | char * |
| 488 | mes_lookup (struct message *meslist, int max, int index) |
| 489 | { |
| 490 | if (index < 0 || index >= max) |
| 491 | { |
| 492 | zlog_err ("message index out of bound: %d", max); |
| 493 | return NULL; |
| 494 | } |
| 495 | return meslist[index].str; |
| 496 | } |