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