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