]>
git.ipfire.org Git - thirdparty/bird.git/blob - sysdep/unix/log.c
6bbb64452d58d14bebf1915bd295e696a7279752
2 * BIRD Library -- Logging Functions
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 * The Logging module offers a simple set of functions for writing
13 * messages to system logs and to the debug output.
21 #include "nest/bird.h"
23 #include "lib/string.h"
24 #include "lib/lists.h"
27 static FILE *dbgf
= NULL
;
28 static list
*current_log_list
;
29 static list init_log_list
;
32 #include <sys/syslog.h>
34 static int syslog_priorities
[] = {
48 static char *class_names
[] = {
62 vlog(int class, char *msg
, va_list args
)
67 if (bvsnprintf(buf
, sizeof(buf
)-1, msg
, args
) < 0)
68 bsprintf(buf
+ sizeof(buf
) - 100, " ... <too long>");
70 WALK_LIST(l
, *current_log_list
)
72 if (!(l
->mask
& (1 << class)))
76 time_t now
= time(NULL
);
77 struct tm
*tm
= localtime(&now
);
80 fputs("bird: ", l
->fh
);
83 fprintf(l
->fh
, "%02d-%02d-%04d %02d:%02d:%02d <%s> ",
98 syslog(syslog_priorities
[class], "%s", buf
);
101 cli_echo(class, buf
);
105 * log - log a message
106 * @msg: printf-like formatting string with message class information
107 * prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
109 * This function formats a message according to the format string @msg
110 * and writes it to the corresponding log file (as specified in the
111 * configuration). Please note that the message is automatically
112 * formatted as a full line, no need to include |\n| inside.
121 if (*msg
>= 1 && *msg
<= 8)
123 vlog(class, msg
, args
);
128 * bug - report an internal error
129 * @msg: a printf-like error message
131 * This function logs an internal error and aborts execution
140 vlog(L_BUG
[0], msg
, args
);
145 * bug - report a fatal error
146 * @msg: a printf-like error message
148 * This function logs a fatal error and aborts execution
157 vlog(L_FATAL
[0], msg
, args
);
162 * debug - write to debug output
163 * @msg: a printf-like message
165 * This function formats the message @msg and prints it out
166 * to the debugging output. No newline character is appended.
169 debug(char *msg
, ...)
177 if (bvsnprintf(buf
, sizeof(buf
), msg
, args
) < 0)
178 bsprintf(buf
+ sizeof(buf
) - 100, " ... <too long>\n");
187 static struct log_config lc_stderr
= { mask
: ~0, terminal_flag
: 1 };
189 init_list(&init_log_list
);
190 current_log_list
= &init_log_list
;
195 static struct log_config lc_syslog
= { mask
: ~0 };
196 openlog("bird", LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
197 add_tail(current_log_list
, &lc_syslog
.n
);
201 lc_stderr
.fh
= stderr
;
202 add_tail(current_log_list
, &lc_stderr
.n
);
209 current_log_list
= &init_log_list
;
211 current_log_list
= l
;
215 log_init_debug(char *f
)
217 if (dbgf
&& dbgf
!= stderr
)
223 else if (!(dbgf
= fopen(f
, "a")))
224 log(L_ERR
"Error opening debug file `%s': %m", f
);
226 setvbuf(dbgf
, NULL
, _IONBF
, 0);