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" |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 27 | #ifndef SUNOS_5 |
| 28 | #include <sys/un.h> |
| 29 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | |
| 31 | struct zlog *zlog_default = NULL; |
| 32 | |
| 33 | const char *zlog_proto_names[] = |
| 34 | { |
| 35 | "NONE", |
| 36 | "DEFAULT", |
| 37 | "ZEBRA", |
| 38 | "RIP", |
| 39 | "BGP", |
| 40 | "OSPF", |
| 41 | "RIPNG", |
| 42 | "OSPF6", |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 43 | "ISIS", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | "MASC", |
| 45 | NULL, |
| 46 | }; |
| 47 | |
| 48 | const char *zlog_priority[] = |
| 49 | { |
| 50 | "emergencies", |
| 51 | "alerts", |
| 52 | "critical", |
| 53 | "errors", |
| 54 | "warnings", |
| 55 | "notifications", |
| 56 | "informational", |
| 57 | "debugging", |
| 58 | NULL, |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | |
| 63 | /* For time string format. */ |
| 64 | #define TIME_BUF 27 |
| 65 | |
| 66 | /* Utility routine for current time printing. */ |
| 67 | static void |
| 68 | time_print (FILE *fp) |
| 69 | { |
| 70 | int ret; |
| 71 | char buf [TIME_BUF]; |
| 72 | time_t clock; |
| 73 | struct tm *tm; |
| 74 | |
| 75 | time (&clock); |
| 76 | tm = localtime (&clock); |
| 77 | |
| 78 | ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm); |
| 79 | if (ret == 0) { |
| 80 | zlog_warn ("strftime error"); |
| 81 | } |
| 82 | |
| 83 | fprintf (fp, "%s ", buf); |
| 84 | } |
| 85 | |
| 86 | /* va_list version of zlog. */ |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 87 | static void |
| 88 | vzlog (struct zlog *zl, int priority, const char *format, va_list args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | { |
| 90 | /* If zlog is not specified, use default one. */ |
| 91 | if (zl == NULL) |
| 92 | zl = zlog_default; |
| 93 | |
| 94 | /* When zlog_default is also NULL, use stderr for logging. */ |
| 95 | if (zl == NULL) |
| 96 | { |
| 97 | time_print (stderr); |
| 98 | fprintf (stderr, "%s: ", "unknown"); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 99 | vfprintf (stderr, format, args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | fprintf (stderr, "\n"); |
| 101 | fflush (stderr); |
| 102 | |
| 103 | /* In this case we return at here. */ |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /* only log this information if it has not been masked out */ |
| 108 | if ( priority > zl->maskpri ) |
| 109 | return ; |
| 110 | |
| 111 | /* Syslog output */ |
| 112 | if (zl->flags & ZLOG_SYSLOG) |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 113 | { |
| 114 | va_list ac; |
| 115 | va_copy(ac, args); |
| 116 | vsyslog (priority|zlog_default->facility, format, ac); |
| 117 | va_end(ac); |
| 118 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | |
| 120 | /* File output. */ |
| 121 | if (zl->flags & ZLOG_FILE) |
| 122 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 123 | va_list ac; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | time_print (zl->fp); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 125 | if (zl->record_priority) |
| 126 | fprintf (zl->fp, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 128 | va_copy(ac, args); |
| 129 | vfprintf (zl->fp, format, ac); |
| 130 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 131 | fprintf (zl->fp, "\n"); |
| 132 | fflush (zl->fp); |
| 133 | } |
| 134 | |
| 135 | /* stdout output. */ |
| 136 | if (zl->flags & ZLOG_STDOUT) |
| 137 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 138 | va_list ac; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 139 | time_print (stdout); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 140 | if (zl->record_priority) |
| 141 | fprintf (stdout, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 142 | fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 143 | va_copy(ac, args); |
| 144 | vfprintf (stdout, format, ac); |
| 145 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | fprintf (stdout, "\n"); |
| 147 | fflush (stdout); |
| 148 | } |
| 149 | |
| 150 | /* stderr output. */ |
| 151 | if (zl->flags & ZLOG_STDERR) |
| 152 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 153 | va_list ac; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 154 | time_print (stderr); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 155 | if (zl->record_priority) |
| 156 | fprintf (stderr, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | fprintf (stderr, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 158 | va_copy(ac, args); |
| 159 | vfprintf (stderr, format, ac); |
| 160 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | fprintf (stderr, "\n"); |
| 162 | fflush (stderr); |
| 163 | } |
| 164 | |
| 165 | /* Terminal monitor. */ |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 166 | vty_log (zlog_proto_names[zl->protocol], format, args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 169 | static char * |
| 170 | str_append(char *dst, int len, const char *src) |
| 171 | { |
| 172 | while ((len-- > 0) && *src) |
| 173 | *dst++ = *src++; |
| 174 | return dst; |
| 175 | } |
| 176 | |
| 177 | static char * |
| 178 | num_append(char *s, int len, u_long x) |
| 179 | { |
| 180 | char buf[30]; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 181 | char *t; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 182 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 183 | if (!x) |
| 184 | return str_append(s,len,"0"); |
| 185 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 186 | while (x && (t > buf)) |
| 187 | { |
| 188 | *--t = '0'+(x % 10); |
| 189 | x /= 10; |
| 190 | } |
| 191 | return str_append(s,len,t); |
| 192 | } |
| 193 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 194 | static char * |
| 195 | hex_append(char *s, int len, u_long x) |
| 196 | { |
| 197 | char buf[30]; |
| 198 | char *t; |
| 199 | |
| 200 | if (!x) |
| 201 | return str_append(s,len,"0"); |
| 202 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
| 203 | while (x && (t > buf)) |
| 204 | { |
| 205 | u_int cc = (x % 16); |
| 206 | *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10)); |
| 207 | x /= 16; |
| 208 | } |
| 209 | return str_append(s,len,t); |
| 210 | } |
| 211 | |
| 212 | static int syslog_fd = -1; |
| 213 | |
| 214 | /* Needs to be enhanced to support Solaris. */ |
| 215 | static int |
| 216 | syslog_connect(void) |
| 217 | { |
| 218 | #ifdef SUNOS_5 |
| 219 | return -1; |
| 220 | #else |
| 221 | int fd; |
| 222 | char *s; |
| 223 | struct sockaddr_un addr; |
| 224 | |
| 225 | if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0) |
| 226 | return -1; |
| 227 | addr.sun_family = AF_UNIX; |
| 228 | #ifdef _PATH_LOG |
| 229 | #define SYSLOG_SOCKET_PATH _PATH_LOG |
| 230 | #else |
| 231 | #define SYSLOG_SOCKET_PATH "/dev/log" |
| 232 | #endif |
| 233 | s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH); |
| 234 | #undef SYSLOG_SOCKET_PATH |
| 235 | *s = '\0'; |
| 236 | if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) |
| 237 | { |
| 238 | close(fd); |
| 239 | return -1; |
| 240 | } |
| 241 | return fd; |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | syslog_sigsafe(int priority, const char *msg, size_t msglen) |
| 247 | { |
| 248 | char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50]; |
| 249 | char *s; |
| 250 | |
| 251 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) |
| 252 | return; |
| 253 | |
| 254 | #define LOC s,buf+sizeof(buf)-s |
| 255 | s = buf; |
| 256 | s = str_append(LOC,"<"); |
| 257 | s = num_append(LOC,priority); |
| 258 | s = str_append(LOC,">"); |
| 259 | /* forget about the timestamp, too difficult in a signal handler */ |
| 260 | s = str_append(LOC,zlog_default->ident); |
| 261 | if (zlog_default->syslog_options & LOG_PID) |
| 262 | { |
| 263 | s = str_append(LOC,"["); |
| 264 | s = num_append(LOC,getpid()); |
| 265 | s = str_append(LOC,"]"); |
| 266 | } |
| 267 | s = str_append(LOC,": "); |
| 268 | s = str_append(LOC,msg); |
| 269 | write(syslog_fd,buf,s-buf); |
| 270 | #undef LOC |
| 271 | } |
| 272 | |
| 273 | /* Note: the goal here is to use only async-signal-safe functions. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 274 | void |
| 275 | zlog_signal(int signo, const char *action) |
| 276 | { |
| 277 | time_t now; |
| 278 | char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60]; |
| 279 | char *s = buf; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 280 | char *msgstart = buf; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 281 | #define LOC s,buf+sizeof(buf)-s |
| 282 | |
| 283 | time(&now); |
| 284 | if (zlog_default) |
| 285 | { |
| 286 | s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); |
| 287 | *s++ = ':'; |
| 288 | *s++ = ' '; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 289 | msgstart = s; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 290 | } |
| 291 | s = str_append(LOC,"Received signal "); |
| 292 | s = num_append(LOC,signo); |
| 293 | s = str_append(LOC," at "); |
| 294 | s = num_append(LOC,now); |
| 295 | s = str_append(LOC,"; "); |
| 296 | s = str_append(LOC,action); |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 297 | if (s < buf+sizeof(buf)) |
| 298 | *s++ = '\n'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 299 | |
| 300 | #define DUMP(FP) write(fileno(FP),buf,s-buf); |
| 301 | if (!zlog_default) |
| 302 | DUMP(stderr) |
| 303 | else |
| 304 | { |
| 305 | if ((zlog_default->flags & ZLOG_FILE) && zlog_default->fp) |
| 306 | DUMP(zlog_default->fp) |
| 307 | if (zlog_default->flags & ZLOG_STDOUT) |
| 308 | DUMP(stdout) |
| 309 | if (zlog_default->flags & ZLOG_STDERR) |
| 310 | DUMP(stderr) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 311 | if (zlog_default->flags & ZLOG_SYSLOG) |
| 312 | { |
| 313 | *--s = '\0'; |
| 314 | syslog_sigsafe(LOG_ERR|zlog_default->facility,msgstart,s-msgstart); |
| 315 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 316 | } |
| 317 | #undef DUMP |
| 318 | |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 319 | zlog_backtrace_sigsafe(LOG_ERR); |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 320 | #undef LOC |
| 321 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 322 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 323 | /* Log a backtrace using only async-signal-safe functions. |
| 324 | Needs to be enhanced to support syslog logging. */ |
| 325 | void |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 326 | zlog_backtrace_sigsafe(int priority) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 327 | { |
| 328 | #ifdef HAVE_GLIBC_BACKTRACE |
| 329 | void *array[20]; |
| 330 | int size; |
| 331 | char buf[100]; |
| 332 | char *s; |
| 333 | #define LOC s,buf+sizeof(buf)-s |
| 334 | |
| 335 | /* only log this information if it has not been masked out */ |
| 336 | if (zlog_default && (priority > zlog_default->maskpri)) |
| 337 | return; |
| 338 | |
| 339 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 340 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 341 | return; |
| 342 | s = buf; |
| 343 | s = str_append(LOC,"Backtrace for "); |
| 344 | s = num_append(LOC,size); |
| 345 | s = str_append(LOC," stack frames:\n"); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 346 | |
| 347 | #define DUMP(FP) { \ |
| 348 | write(fileno(FP),buf,s-buf); \ |
| 349 | backtrace_symbols_fd(array, size, fileno(FP)); \ |
| 350 | } |
| 351 | |
| 352 | if (!zlog_default) |
| 353 | DUMP(stderr) |
| 354 | else |
| 355 | { |
| 356 | if ((zlog_default->flags & ZLOG_FILE) && zlog_default->fp) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 357 | DUMP(zlog_default->fp) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 358 | if (zlog_default->flags & ZLOG_STDOUT) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 359 | DUMP(stdout) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 360 | if (zlog_default->flags & ZLOG_STDERR) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 361 | DUMP(stderr) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 362 | if (zlog_default->flags & ZLOG_SYSLOG) |
| 363 | { |
| 364 | int i; |
| 365 | *--s = '\0'; |
| 366 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 367 | /* Just print the function addresses. */ |
| 368 | for (i = 0; i < size; i++) |
| 369 | { |
| 370 | s = buf; |
| 371 | s = str_append(LOC,"[bt "); |
| 372 | s = num_append(LOC,i); |
| 373 | s = str_append(LOC,"] 0x"); |
| 374 | s = hex_append(LOC,(u_long)(array[i])); |
| 375 | *s = '\0'; |
| 376 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 377 | } |
| 378 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 379 | } |
| 380 | #undef DUMP |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 381 | #undef LOC |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 382 | #endif /* HAVE_GLIBC_BACKTRACE */ |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | zlog_backtrace(int priority) |
| 387 | { |
| 388 | #ifndef HAVE_GLIBC_BACKTRACE |
| 389 | zlog(NULL, priority, "No backtrace available on this platform."); |
| 390 | #else |
| 391 | void *array[20]; |
| 392 | int size, i; |
| 393 | char **strings; |
| 394 | |
| 395 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 396 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 397 | { |
| 398 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " |
| 399 | "(valid range is between 1 and %u)", |
| 400 | size, sizeof(array)/sizeof(array[0])); |
| 401 | return; |
| 402 | } |
| 403 | zlog(NULL, priority, "Backtrace for %d stack frames:", size); |
| 404 | if (!(strings = backtrace_symbols(array, size))) |
| 405 | { |
| 406 | zlog_err("Cannot get backtrace symbols (out of memory?)"); |
| 407 | for (i = 0; i < size; i++) |
| 408 | zlog(NULL, priority, "[bt %d] %p",i,array[i]); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | for (i = 0; i < size; i++) |
| 413 | zlog(NULL, priority, "[bt %d] %s",i,strings[i]); |
| 414 | free(strings); |
| 415 | } |
| 416 | #endif /* HAVE_GLIBC_BACKTRACE */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 417 | } |
| 418 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | void |
| 420 | zlog (struct zlog *zl, int priority, const char *format, ...) |
| 421 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 422 | va_list args; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 424 | va_start(args, format); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 425 | vzlog (zl, priority, format, args); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 426 | va_end (args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | } |
| 428 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 429 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 430 | void \ |
| 431 | FUNCNAME(const char *format, ...) \ |
| 432 | { \ |
| 433 | va_list args; \ |
| 434 | va_start(args, format); \ |
| 435 | vzlog (NULL, PRIORITY, format, args); \ |
| 436 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | } |
| 438 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 439 | ZLOG_FUNC(zlog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 441 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 443 | ZLOG_FUNC(zlog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 445 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
| 446 | |
| 447 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
| 448 | |
| 449 | #undef ZLOG_FUNC |
| 450 | |
| 451 | #define PLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 452 | void \ |
| 453 | FUNCNAME(struct zlog *zl, const char *format, ...) \ |
| 454 | { \ |
| 455 | va_list args; \ |
| 456 | va_start(args, format); \ |
| 457 | vzlog (zl, PRIORITY, format, args); \ |
| 458 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | } |
| 460 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 461 | PLOG_FUNC(plog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 463 | PLOG_FUNC(plog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 465 | PLOG_FUNC(plog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 467 | PLOG_FUNC(plog_notice, LOG_NOTICE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 469 | PLOG_FUNC(plog_debug, LOG_DEBUG) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 470 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 471 | #undef PLOG_FUNC |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 472 | |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 473 | void |
| 474 | _zlog_assert_failed (const char *assertion, const char *file, |
| 475 | unsigned int line, const char *function) |
| 476 | { |
| 477 | zlog_err("Assertion `%s' failed in file %s, line %u, function %s", |
| 478 | assertion,file,line,(function ? function : "?")); |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 479 | zlog_backtrace(LOG_ERR); |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 480 | abort(); |
| 481 | } |
| 482 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | |
| 484 | /* Open log stream */ |
| 485 | struct zlog * |
| 486 | openzlog (const char *progname, int flags, zlog_proto_t protocol, |
| 487 | int syslog_flags, int syslog_facility) |
| 488 | { |
| 489 | struct zlog *zl; |
| 490 | |
| 491 | zl = XMALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
| 492 | memset (zl, 0, sizeof (struct zlog)); |
| 493 | |
| 494 | zl->ident = progname; |
| 495 | zl->flags = flags; |
| 496 | zl->protocol = protocol; |
| 497 | zl->facility = syslog_facility; |
| 498 | zl->maskpri = LOG_DEBUG; |
| 499 | zl->record_priority = 0; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 500 | zl->syslog_options = syslog_flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | |
| 502 | openlog (progname, syslog_flags, zl->facility); |
| 503 | |
| 504 | return zl; |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | closezlog (struct zlog *zl) |
| 509 | { |
| 510 | closelog(); |
| 511 | fclose (zl->fp); |
| 512 | |
| 513 | XFREE (MTYPE_ZLOG, zl); |
| 514 | } |
| 515 | |
| 516 | /* Called from command.c. */ |
| 517 | void |
| 518 | zlog_set_flag (struct zlog *zl, int flags) |
| 519 | { |
| 520 | if (zl == NULL) |
| 521 | zl = zlog_default; |
| 522 | |
| 523 | zl->flags |= flags; |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | zlog_reset_flag (struct zlog *zl, int flags) |
| 528 | { |
| 529 | if (zl == NULL) |
| 530 | zl = zlog_default; |
| 531 | |
| 532 | zl->flags &= ~flags; |
| 533 | } |
| 534 | |
| 535 | int |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 536 | zlog_set_file (struct zlog *zl, const char *filename) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | { |
| 538 | FILE *fp; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 539 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 540 | |
| 541 | /* There is opend file. */ |
| 542 | zlog_reset_file (zl); |
| 543 | |
| 544 | /* Set default zl. */ |
| 545 | if (zl == NULL) |
| 546 | zl = zlog_default; |
| 547 | |
| 548 | /* Open file. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 549 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 550 | fp = fopen (filename, "a"); |
| 551 | if (fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 552 | { |
| 553 | umask(oldumask); |
| 554 | return 0; |
| 555 | } |
| 556 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 557 | |
| 558 | /* Set flags. */ |
| 559 | zl->filename = strdup (filename); |
| 560 | zl->flags |= ZLOG_FILE; |
| 561 | zl->fp = fp; |
| 562 | |
| 563 | return 1; |
| 564 | } |
| 565 | |
| 566 | /* Reset opend file. */ |
| 567 | int |
| 568 | zlog_reset_file (struct zlog *zl) |
| 569 | { |
| 570 | if (zl == NULL) |
| 571 | zl = zlog_default; |
| 572 | |
| 573 | zl->flags &= ~ZLOG_FILE; |
| 574 | |
| 575 | if (zl->fp) |
| 576 | fclose (zl->fp); |
| 577 | zl->fp = NULL; |
| 578 | |
| 579 | if (zl->filename) |
| 580 | free (zl->filename); |
| 581 | zl->filename = NULL; |
| 582 | |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | /* Reopen log file. */ |
| 587 | int |
| 588 | zlog_rotate (struct zlog *zl) |
| 589 | { |
| 590 | FILE *fp; |
| 591 | |
| 592 | if (zl == NULL) |
| 593 | zl = zlog_default; |
| 594 | |
| 595 | if (zl->fp) |
| 596 | fclose (zl->fp); |
| 597 | zl->fp = NULL; |
| 598 | |
| 599 | if (zl->filename) |
| 600 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 601 | mode_t oldumask; |
| 602 | |
| 603 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | fp = fopen (zl->filename, "a"); |
| 605 | if (fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 606 | { |
| 607 | umask(oldumask); |
| 608 | return -1; |
| 609 | } |
| 610 | umask(oldumask); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | zl->fp = fp; |
| 612 | } |
| 613 | |
| 614 | return 1; |
| 615 | } |
| 616 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | /* Message lookup function. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 618 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | lookup (struct message *mes, int key) |
| 620 | { |
| 621 | struct message *pnt; |
| 622 | |
| 623 | for (pnt = mes; pnt->key != 0; pnt++) |
| 624 | if (pnt->key == key) |
| 625 | return pnt->str; |
| 626 | |
| 627 | return ""; |
| 628 | } |
| 629 | |
| 630 | /* Very old hacky version of message lookup function. Still partly |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 631 | used in bgpd and ospfd. FIXME Seems that it's not used any more. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 632 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 633 | mes_lookup (struct message *meslist, int max, int index) |
| 634 | { |
| 635 | if (index < 0 || index >= max) |
| 636 | { |
| 637 | zlog_err ("message index out of bound: %d", max); |
| 638 | return NULL; |
| 639 | } |
| 640 | return meslist[index].str; |
| 641 | } |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 642 | |
| 643 | /* Wrapper around strerror to handle case where it returns NULL. */ |
| 644 | const char * |
| 645 | safe_strerror(int errnum) |
| 646 | { |
| 647 | const char *s = strerror(errnum); |
| 648 | return (s != NULL) ? s : "Unknown error"; |
| 649 | } |