ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
gdt | ad57239 | 2004-12-09 14:53:32 +0000 | [diff] [blame] | 2 | * $Id: log.c,v 1.18 2004/12/09 14:53:32 gdt 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 | |
gdt | ad57239 | 2004-12-09 14:53:32 +0000 | [diff] [blame] | 180 | /* |
| 181 | * XXX warning: `hex_append' defined but not used |
| 182 | * Apparently this is used only if HAVE_GLIBC_BACKTRACE is defined. |
| 183 | */ |
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 | |
| 202 | static int syslog_fd = -1; |
| 203 | |
| 204 | /* Needs to be enhanced to support Solaris. */ |
| 205 | static int |
| 206 | syslog_connect(void) |
| 207 | { |
| 208 | #ifdef SUNOS_5 |
| 209 | return -1; |
| 210 | #else |
| 211 | int fd; |
| 212 | char *s; |
| 213 | struct sockaddr_un addr; |
| 214 | |
| 215 | if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0) |
| 216 | return -1; |
| 217 | addr.sun_family = AF_UNIX; |
| 218 | #ifdef _PATH_LOG |
| 219 | #define SYSLOG_SOCKET_PATH _PATH_LOG |
| 220 | #else |
| 221 | #define SYSLOG_SOCKET_PATH "/dev/log" |
| 222 | #endif |
| 223 | s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH); |
| 224 | #undef SYSLOG_SOCKET_PATH |
| 225 | *s = '\0'; |
| 226 | if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) |
| 227 | { |
| 228 | close(fd); |
| 229 | return -1; |
| 230 | } |
| 231 | return fd; |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | static void |
| 236 | syslog_sigsafe(int priority, const char *msg, size_t msglen) |
| 237 | { |
| 238 | char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50]; |
| 239 | char *s; |
| 240 | |
| 241 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) |
| 242 | return; |
| 243 | |
| 244 | #define LOC s,buf+sizeof(buf)-s |
| 245 | s = buf; |
| 246 | s = str_append(LOC,"<"); |
| 247 | s = num_append(LOC,priority); |
| 248 | s = str_append(LOC,">"); |
| 249 | /* forget about the timestamp, too difficult in a signal handler */ |
| 250 | s = str_append(LOC,zlog_default->ident); |
| 251 | if (zlog_default->syslog_options & LOG_PID) |
| 252 | { |
| 253 | s = str_append(LOC,"["); |
| 254 | s = num_append(LOC,getpid()); |
| 255 | s = str_append(LOC,"]"); |
| 256 | } |
| 257 | s = str_append(LOC,": "); |
| 258 | s = str_append(LOC,msg); |
| 259 | write(syslog_fd,buf,s-buf); |
| 260 | #undef LOC |
| 261 | } |
| 262 | |
| 263 | /* Note: the goal here is to use only async-signal-safe functions. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 264 | void |
| 265 | zlog_signal(int signo, const char *action) |
| 266 | { |
| 267 | time_t now; |
| 268 | char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60]; |
| 269 | char *s = buf; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 270 | char *msgstart = buf; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 271 | #define LOC s,buf+sizeof(buf)-s |
| 272 | |
| 273 | time(&now); |
| 274 | if (zlog_default) |
| 275 | { |
| 276 | s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); |
| 277 | *s++ = ':'; |
| 278 | *s++ = ' '; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 279 | msgstart = s; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 280 | } |
| 281 | s = str_append(LOC,"Received signal "); |
| 282 | s = num_append(LOC,signo); |
| 283 | s = str_append(LOC," at "); |
| 284 | s = num_append(LOC,now); |
| 285 | s = str_append(LOC,"; "); |
| 286 | s = str_append(LOC,action); |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 287 | if (s < buf+sizeof(buf)) |
| 288 | *s++ = '\n'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 289 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 290 | /* N.B. implicit priority is most severe */ |
| 291 | #define PRI LOG_EMERG |
| 292 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 293 | #define DUMP(FP) write(fileno(FP),buf,s-buf); |
| 294 | if (!zlog_default) |
| 295 | DUMP(stderr) |
| 296 | else |
| 297 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 298 | if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 299 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 300 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 301 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 302 | /* Remove trailing '\n' for monitor and syslog */ |
| 303 | *--s = '\0'; |
| 304 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 305 | vty_log_fixed(buf,s-buf); |
| 306 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 307 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 308 | } |
| 309 | #undef DUMP |
| 310 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 311 | zlog_backtrace_sigsafe(PRI); |
| 312 | #undef PRI |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 313 | #undef LOC |
| 314 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 315 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 316 | /* Log a backtrace using only async-signal-safe functions. |
| 317 | Needs to be enhanced to support syslog logging. */ |
| 318 | void |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 319 | zlog_backtrace_sigsafe(int priority) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 320 | { |
| 321 | #ifdef HAVE_GLIBC_BACKTRACE |
| 322 | void *array[20]; |
| 323 | int size; |
| 324 | char buf[100]; |
| 325 | char *s; |
| 326 | #define LOC s,buf+sizeof(buf)-s |
| 327 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 328 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 329 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 330 | return; |
| 331 | s = buf; |
| 332 | s = str_append(LOC,"Backtrace for "); |
| 333 | s = num_append(LOC,size); |
| 334 | s = str_append(LOC," stack frames:\n"); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 335 | |
| 336 | #define DUMP(FP) { \ |
| 337 | write(fileno(FP),buf,s-buf); \ |
| 338 | backtrace_symbols_fd(array, size, fileno(FP)); \ |
| 339 | } |
| 340 | |
| 341 | if (!zlog_default) |
| 342 | DUMP(stderr) |
| 343 | else |
| 344 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 345 | if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && |
| 346 | zlog_default->fp) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 347 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 348 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 349 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 350 | /* Remove trailing '\n' for monitor and syslog */ |
| 351 | *--s = '\0'; |
| 352 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 353 | vty_log_fixed(buf,s-buf); |
| 354 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 355 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 356 | { |
| 357 | int i; |
| 358 | /* Just print the function addresses. */ |
| 359 | for (i = 0; i < size; i++) |
| 360 | { |
| 361 | s = buf; |
| 362 | s = str_append(LOC,"[bt "); |
| 363 | s = num_append(LOC,i); |
| 364 | s = str_append(LOC,"] 0x"); |
| 365 | s = hex_append(LOC,(u_long)(array[i])); |
| 366 | *s = '\0'; |
| 367 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 368 | vty_log_fixed(buf,s-buf); |
| 369 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 370 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 373 | } |
| 374 | #undef DUMP |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 375 | #undef LOC |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 376 | #endif /* HAVE_GLIBC_BACKTRACE */ |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | zlog_backtrace(int priority) |
| 381 | { |
| 382 | #ifndef HAVE_GLIBC_BACKTRACE |
| 383 | zlog(NULL, priority, "No backtrace available on this platform."); |
| 384 | #else |
| 385 | void *array[20]; |
| 386 | int size, i; |
| 387 | char **strings; |
| 388 | |
| 389 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 390 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 391 | { |
| 392 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " |
| 393 | "(valid range is between 1 and %u)", |
| 394 | size, sizeof(array)/sizeof(array[0])); |
| 395 | return; |
| 396 | } |
| 397 | zlog(NULL, priority, "Backtrace for %d stack frames:", size); |
| 398 | if (!(strings = backtrace_symbols(array, size))) |
| 399 | { |
| 400 | zlog_err("Cannot get backtrace symbols (out of memory?)"); |
| 401 | for (i = 0; i < size; i++) |
| 402 | zlog(NULL, priority, "[bt %d] %p",i,array[i]); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | for (i = 0; i < size; i++) |
| 407 | zlog(NULL, priority, "[bt %d] %s",i,strings[i]); |
| 408 | free(strings); |
| 409 | } |
| 410 | #endif /* HAVE_GLIBC_BACKTRACE */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 411 | } |
| 412 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 413 | void |
| 414 | zlog (struct zlog *zl, int priority, const char *format, ...) |
| 415 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 416 | va_list args; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 418 | va_start(args, format); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | vzlog (zl, priority, format, args); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 420 | va_end (args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 421 | } |
| 422 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 423 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 424 | void \ |
| 425 | FUNCNAME(const char *format, ...) \ |
| 426 | { \ |
| 427 | va_list args; \ |
| 428 | va_start(args, format); \ |
| 429 | vzlog (NULL, PRIORITY, format, args); \ |
| 430 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 431 | } |
| 432 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 433 | ZLOG_FUNC(zlog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 434 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 435 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
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_info, LOG_INFO) |
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_notice, LOG_NOTICE) |
| 440 | |
| 441 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
| 442 | |
| 443 | #undef ZLOG_FUNC |
| 444 | |
| 445 | #define PLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 446 | void \ |
| 447 | FUNCNAME(struct zlog *zl, const char *format, ...) \ |
| 448 | { \ |
| 449 | va_list args; \ |
| 450 | va_start(args, format); \ |
| 451 | vzlog (zl, PRIORITY, format, args); \ |
| 452 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 453 | } |
| 454 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 455 | PLOG_FUNC(plog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 456 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 457 | PLOG_FUNC(plog_warn, LOG_WARNING) |
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_info, LOG_INFO) |
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_notice, LOG_NOTICE) |
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_debug, LOG_DEBUG) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 465 | #undef PLOG_FUNC |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 467 | void |
| 468 | _zlog_assert_failed (const char *assertion, const char *file, |
| 469 | unsigned int line, const char *function) |
| 470 | { |
| 471 | zlog_err("Assertion `%s' failed in file %s, line %u, function %s", |
| 472 | assertion,file,line,(function ? function : "?")); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 473 | zlog_backtrace(LOG_EMERG); |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 474 | abort(); |
| 475 | } |
| 476 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 477 | |
| 478 | /* Open log stream */ |
| 479 | struct zlog * |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 480 | openzlog (const char *progname, zlog_proto_t protocol, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | int syslog_flags, int syslog_facility) |
| 482 | { |
| 483 | struct zlog *zl; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 484 | u_int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 486 | zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | |
| 488 | zl->ident = progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | zl->protocol = protocol; |
| 490 | zl->facility = syslog_facility; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 491 | zl->syslog_options = syslog_flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 493 | /* Set default logging levels. */ |
| 494 | for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++) |
| 495 | zl->maxlvl[i] = ZLOG_DISABLED; |
| 496 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; |
| 497 | zl->default_lvl = LOG_DEBUG; |
| 498 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 499 | openlog (progname, syslog_flags, zl->facility); |
| 500 | |
| 501 | return zl; |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | closezlog (struct zlog *zl) |
| 506 | { |
| 507 | closelog(); |
| 508 | fclose (zl->fp); |
| 509 | |
| 510 | XFREE (MTYPE_ZLOG, zl); |
| 511 | } |
| 512 | |
| 513 | /* Called from command.c. */ |
| 514 | void |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 515 | zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 516 | { |
| 517 | if (zl == NULL) |
| 518 | zl = zlog_default; |
| 519 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 520 | zl->maxlvl[dest] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | int |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 524 | zlog_set_file (struct zlog *zl, const char *filename, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | { |
| 526 | FILE *fp; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 527 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | |
| 529 | /* There is opend file. */ |
| 530 | zlog_reset_file (zl); |
| 531 | |
| 532 | /* Set default zl. */ |
| 533 | if (zl == NULL) |
| 534 | zl = zlog_default; |
| 535 | |
| 536 | /* Open file. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 537 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | fp = fopen (filename, "a"); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 539 | umask(oldumask); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 540 | if (fp == NULL) |
| 541 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | |
| 543 | /* Set flags. */ |
| 544 | zl->filename = strdup (filename); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 545 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | zl->fp = fp; |
| 547 | |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | /* Reset opend file. */ |
| 552 | int |
| 553 | zlog_reset_file (struct zlog *zl) |
| 554 | { |
| 555 | if (zl == NULL) |
| 556 | zl = zlog_default; |
| 557 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 558 | if (zl->fp) |
| 559 | fclose (zl->fp); |
| 560 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 561 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | |
| 563 | if (zl->filename) |
| 564 | free (zl->filename); |
| 565 | zl->filename = NULL; |
| 566 | |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | /* Reopen log file. */ |
| 571 | int |
| 572 | zlog_rotate (struct zlog *zl) |
| 573 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 574 | int level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | |
| 576 | if (zl == NULL) |
| 577 | zl = zlog_default; |
| 578 | |
| 579 | if (zl->fp) |
| 580 | fclose (zl->fp); |
| 581 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 582 | level = zl->maxlvl[ZLOG_DEST_FILE]; |
| 583 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | |
| 585 | if (zl->filename) |
| 586 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 587 | mode_t oldumask; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 588 | int save_errno; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 589 | |
| 590 | oldumask = umask (0777 & ~LOGFILE_MASK); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 591 | zl->fp = fopen (zl->filename, "a"); |
| 592 | save_errno = errno; |
| 593 | umask(oldumask); |
| 594 | if (zl->fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 595 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 596 | zlog_err("Log rotate failed: cannot open file %s for append: %s", |
| 597 | zl->filename, safe_strerror(save_errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 598 | return -1; |
| 599 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 600 | zl->maxlvl[ZLOG_DEST_FILE] = level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | return 1; |
| 604 | } |
| 605 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 606 | /* Message lookup function. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 607 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 608 | lookup (struct message *mes, int key) |
| 609 | { |
| 610 | struct message *pnt; |
| 611 | |
| 612 | for (pnt = mes; pnt->key != 0; pnt++) |
| 613 | if (pnt->key == key) |
| 614 | return pnt->str; |
| 615 | |
| 616 | return ""; |
| 617 | } |
| 618 | |
| 619 | /* Very old hacky version of message lookup function. Still partly |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 620 | 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] | 621 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | mes_lookup (struct message *meslist, int max, int index) |
| 623 | { |
| 624 | if (index < 0 || index >= max) |
| 625 | { |
| 626 | zlog_err ("message index out of bound: %d", max); |
| 627 | return NULL; |
| 628 | } |
| 629 | return meslist[index].str; |
| 630 | } |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 631 | |
| 632 | /* Wrapper around strerror to handle case where it returns NULL. */ |
| 633 | const char * |
| 634 | safe_strerror(int errnum) |
| 635 | { |
| 636 | const char *s = strerror(errnum); |
| 637 | return (s != NULL) ? s : "Unknown error"; |
| 638 | } |