]>
git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
b68896cac8ae66f89681d613be50dae6c1944f05
2 * $Id: log.c,v 1.17 2004/12/07 15:39:32 ajs Exp $
5 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
7 * This file is part of GNU Zebra.
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
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.
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
34 struct zlog
*zlog_default
= NULL
;
36 const char *zlog_proto_names
[] =
51 const char *zlog_priority
[] =
66 /* For time string format. */
69 /* Utility routine for current time printing. */
79 tm
= localtime (&clock
);
81 ret
= strftime (buf
, TIME_BUF
, "%Y/%m/%d %H:%M:%S", tm
);
83 zlog_warn ("strftime error");
86 fprintf (fp
, "%s ", buf
);
89 /* va_list version of zlog. */
91 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list args
)
93 /* If zlog is not specified, use default one. */
97 /* When zlog_default is also NULL, use stderr for logging. */
101 fprintf (stderr
, "%s: ", "unknown");
102 vfprintf (stderr
, format
, args
);
103 fprintf (stderr
, "\n");
106 /* In this case we return at here. */
111 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
115 vsyslog (priority
|zlog_default
->facility
, format
, ac
);
120 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
124 if (zl
->record_priority
)
125 fprintf (zl
->fp
, "%s: ", zlog_priority
[priority
]);
126 fprintf (zl
->fp
, "%s: ", zlog_proto_names
[zl
->protocol
]);
128 vfprintf (zl
->fp
, format
, ac
);
130 fprintf (zl
->fp
, "\n");
135 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
139 if (zl
->record_priority
)
140 fprintf (stdout
, "%s: ", zlog_priority
[priority
]);
141 fprintf (stdout
, "%s: ", zlog_proto_names
[zl
->protocol
]);
143 vfprintf (stdout
, format
, ac
);
145 fprintf (stdout
, "\n");
149 /* Terminal monitor. */
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
);
156 str_append(char *dst
, int len
, const char *src
)
158 while ((len
-- > 0) && *src
)
164 num_append(char *s
, int len
, u_long x
)
170 return str_append(s
,len
,"0");
171 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
172 while (x
&& (t
> buf
))
177 return str_append(s
,len
,t
);
181 hex_append(char *s
, int len
, u_long x
)
187 return str_append(s
,len
,"0");
188 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
189 while (x
&& (t
> buf
))
192 *--t
= ((cc
< 10) ? ('0'+cc
) : ('a'+cc
-10));
195 return str_append(s
,len
,t
);
198 static int syslog_fd
= -1;
200 /* Needs to be enhanced to support Solaris. */
209 struct sockaddr_un addr
;
211 if ((fd
= socket(AF_UNIX
,SOCK_DGRAM
,0)) < 0)
213 addr
.sun_family
= AF_UNIX
;
215 #define SYSLOG_SOCKET_PATH _PATH_LOG
217 #define SYSLOG_SOCKET_PATH "/dev/log"
219 s
= str_append(addr
.sun_path
,sizeof(addr
.sun_path
),SYSLOG_SOCKET_PATH
);
220 #undef SYSLOG_SOCKET_PATH
222 if (connect(fd
,(struct sockaddr
*)&addr
,sizeof(addr
)) < 0)
232 syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
234 char buf
[sizeof("<1234567890>ripngd[1234567890]: ")+msglen
+50];
237 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
240 #define LOC s,buf+sizeof(buf)-s
242 s
= str_append(LOC
,"<");
243 s
= num_append(LOC
,priority
);
244 s
= str_append(LOC
,">");
245 /* forget about the timestamp, too difficult in a signal handler */
246 s
= str_append(LOC
,zlog_default
->ident
);
247 if (zlog_default
->syslog_options
& LOG_PID
)
249 s
= str_append(LOC
,"[");
250 s
= num_append(LOC
,getpid());
251 s
= str_append(LOC
,"]");
253 s
= str_append(LOC
,": ");
254 s
= str_append(LOC
,msg
);
255 write(syslog_fd
,buf
,s
-buf
);
259 /* Note: the goal here is to use only async-signal-safe functions. */
261 zlog_signal(int signo
, const char *action
)
264 char buf
[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
266 char *msgstart
= buf
;
267 #define LOC s,buf+sizeof(buf)-s
272 s
= str_append(LOC
,zlog_proto_names
[zlog_default
->protocol
]);
277 s
= str_append(LOC
,"Received signal ");
278 s
= num_append(LOC
,signo
);
279 s
= str_append(LOC
," at ");
280 s
= num_append(LOC
,now
);
281 s
= str_append(LOC
,"; ");
282 s
= str_append(LOC
,action
);
283 if (s
< buf
+sizeof(buf
))
286 /* N.B. implicit priority is most severe */
287 #define PRI LOG_EMERG
289 #define DUMP(FP) write(fileno(FP),buf,s-buf);
294 if ((PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_FILE
]) && zlog_default
->fp
)
295 DUMP(zlog_default
->fp
)
296 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
298 /* Remove trailing '\n' for monitor and syslog */
300 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
301 vty_log_fixed(buf
,s
-buf
);
302 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
303 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
307 zlog_backtrace_sigsafe(PRI
);
312 /* Log a backtrace using only async-signal-safe functions.
313 Needs to be enhanced to support syslog logging. */
315 zlog_backtrace_sigsafe(int priority
)
317 #ifdef HAVE_GLIBC_BACKTRACE
322 #define LOC s,buf+sizeof(buf)-s
324 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
325 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
328 s
= str_append(LOC
,"Backtrace for ");
329 s
= num_append(LOC
,size
);
330 s
= str_append(LOC
," stack frames:\n");
333 write(fileno(FP),buf,s-buf); \
334 backtrace_symbols_fd(array, size, fileno(FP)); \
341 if ((priority
<= zlog_default
->maxlvl
[ZLOG_DEST_FILE
]) &&
343 DUMP(zlog_default
->fp
)
344 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
346 /* Remove trailing '\n' for monitor and syslog */
348 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
349 vty_log_fixed(buf
,s
-buf
);
350 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
351 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
354 /* Just print the function addresses. */
355 for (i
= 0; i
< size
; i
++)
358 s
= str_append(LOC
,"[bt ");
359 s
= num_append(LOC
,i
);
360 s
= str_append(LOC
,"] 0x");
361 s
= hex_append(LOC
,(u_long
)(array
[i
]));
363 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
364 vty_log_fixed(buf
,s
-buf
);
365 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
366 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
372 #endif /* HAVE_GLIBC_BACKTRACE */
376 zlog_backtrace(int priority
)
378 #ifndef HAVE_GLIBC_BACKTRACE
379 zlog(NULL
, priority
, "No backtrace available on this platform.");
385 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
386 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
388 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
389 "(valid range is between 1 and %u)",
390 size
, sizeof(array
)/sizeof(array
[0]));
393 zlog(NULL
, priority
, "Backtrace for %d stack frames:", size
);
394 if (!(strings
= backtrace_symbols(array
, size
)))
396 zlog_err("Cannot get backtrace symbols (out of memory?)");
397 for (i
= 0; i
< size
; i
++)
398 zlog(NULL
, priority
, "[bt %d] %p",i
,array
[i
]);
402 for (i
= 0; i
< size
; i
++)
403 zlog(NULL
, priority
, "[bt %d] %s",i
,strings
[i
]);
406 #endif /* HAVE_GLIBC_BACKTRACE */
410 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
414 va_start(args
, format
);
415 vzlog (zl
, priority
, format
, args
);
419 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
421 FUNCNAME(const char *format, ...) \
424 va_start(args, format); \
425 vzlog (NULL, PRIORITY, format, args); \
429 ZLOG_FUNC(zlog_err
, LOG_ERR
)
431 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
433 ZLOG_FUNC(zlog_info
, LOG_INFO
)
435 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
437 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
441 #define PLOG_FUNC(FUNCNAME,PRIORITY) \
443 FUNCNAME(struct zlog *zl, const char *format, ...) \
446 va_start(args, format); \
447 vzlog (zl, PRIORITY, format, args); \
451 PLOG_FUNC(plog_err
, LOG_ERR
)
453 PLOG_FUNC(plog_warn
, LOG_WARNING
)
455 PLOG_FUNC(plog_info
, LOG_INFO
)
457 PLOG_FUNC(plog_notice
, LOG_NOTICE
)
459 PLOG_FUNC(plog_debug
, LOG_DEBUG
)
464 _zlog_assert_failed (const char *assertion
, const char *file
,
465 unsigned int line
, const char *function
)
467 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
468 assertion
,file
,line
,(function
? function
: "?"));
469 zlog_backtrace(LOG_EMERG
);
474 /* Open log stream */
476 openzlog (const char *progname
, zlog_proto_t protocol
,
477 int syslog_flags
, int syslog_facility
)
482 zl
= XCALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
484 zl
->ident
= progname
;
485 zl
->protocol
= protocol
;
486 zl
->facility
= syslog_facility
;
487 zl
->syslog_options
= syslog_flags
;
489 /* Set default logging levels. */
490 for (i
= 0; i
< sizeof(zl
->maxlvl
)/sizeof(zl
->maxlvl
[0]); i
++)
491 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
492 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
493 zl
->default_lvl
= LOG_DEBUG
;
495 openlog (progname
, syslog_flags
, zl
->facility
);
501 closezlog (struct zlog
*zl
)
506 XFREE (MTYPE_ZLOG
, zl
);
509 /* Called from command.c. */
511 zlog_set_level (struct zlog
*zl
, zlog_dest_t dest
, int log_level
)
516 zl
->maxlvl
[dest
] = log_level
;
520 zlog_set_file (struct zlog
*zl
, const char *filename
, int log_level
)
525 /* There is opend file. */
526 zlog_reset_file (zl
);
528 /* Set default zl. */
533 oldumask
= umask (0777 & ~LOGFILE_MASK
);
534 fp
= fopen (filename
, "a");
540 zl
->filename
= strdup (filename
);
541 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
547 /* Reset opend file. */
549 zlog_reset_file (struct zlog
*zl
)
557 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
566 /* Reopen log file. */
568 zlog_rotate (struct zlog
*zl
)
578 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
579 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
586 oldumask
= umask (0777 & ~LOGFILE_MASK
);
587 zl
->fp
= fopen (zl
->filename
, "a");
592 zlog_err("Log rotate failed: cannot open file %s for append: %s",
593 zl
->filename
, safe_strerror(save_errno
));
596 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
602 /* Message lookup function. */
604 lookup (struct message
*mes
, int key
)
608 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
615 /* Very old hacky version of message lookup function. Still partly
616 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
618 mes_lookup (struct message
*meslist
, int max
, int index
)
620 if (index
< 0 || index
>= max
)
622 zlog_err ("message index out of bound: %d", max
);
625 return meslist
[index
].str
;
628 /* Wrapper around strerror to handle case where it returns NULL. */
630 safe_strerror(int errnum
)
632 const char *s
= strerror(errnum
);
633 return (s
!= NULL
) ? s
: "Unknown error";