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. Message classes
14 * used by this module are described in |birdlib.h| and also in the
24 #include "nest/bird.h"
26 #include "nest/mrtdump.h"
27 #include "lib/string.h"
28 #include "lib/lists.h"
32 static list
*current_log_list
;
33 static char *current_syslog_name
; /* NULL -> syslog closed */
40 static pthread_mutex_t log_mutex
;
41 static inline void log_lock(void) { pthread_mutex_lock(&log_mutex
); }
42 static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex
); }
44 static pthread_t main_thread
;
45 void main_thread_init(void) { main_thread
= pthread_self(); }
46 static int main_thread_self(void) { return pthread_equal(pthread_self(), main_thread
); }
50 static inline void log_lock(void) { }
51 static inline void log_unlock(void) { }
52 void main_thread_init(void) { }
53 static int main_thread_self(void) { return 1; }
59 #include <sys/syslog.h>
61 static int syslog_priorities
[] = {
75 static char *class_names
[] = {
90 * log_commit - commit a log message
91 * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
93 * This function writes a message prepared in the log buffer to the
94 * log file (as specified in the configuration). The log buffer is
95 * reset after that. The log message is a full line, log_commit()
98 * The message class is an integer, not a first char of a string like
99 * in log(), so it should be written like *L_INFO.
102 log_commit(int class, buffer
*buf
)
104 struct log_config
*l
;
106 if (buf
->pos
== buf
->end
)
107 strcpy(buf
->end
- 100, " ... <too long>");
110 WALK_LIST(l
, *current_log_list
)
112 if (!(l
->mask
& (1 << class)))
116 if (l
->terminal_flag
)
117 fputs("bird: ", l
->fh
);
120 byte tbuf
[TM_DATETIME_BUFFER_SIZE
];
121 tm_format_datetime(tbuf
, &config
->tf_log
, now
);
122 fprintf(l
->fh
, "%s <%s> ", tbuf
, class_names
[class]);
124 fputs(buf
->start
, l
->fh
);
130 syslog(syslog_priorities
[class], "%s", buf
->start
);
135 /* cli_echo is not thread-safe, so call it just from the main thread */
136 if (main_thread_self())
137 cli_echo(class, buf
->start
);
139 buf
->pos
= buf
->start
;
142 int buffer_vprint(buffer
*buf
, const char *fmt
, va_list args
);
145 vlog(int class, const char *msg
, va_list args
)
148 LOG_BUFFER_INIT(buf
);
149 buffer_vprint(&buf
, msg
, args
);
150 log_commit(class, &buf
);
155 * log - log a message
156 * @msg: printf-like formatting string with message class information
157 * prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
159 * This function formats a message according to the format string @msg
160 * and writes it to the corresponding log file (as specified in the
161 * configuration). Please note that the message is automatically
162 * formatted as a full line, no need to include |\n| inside.
163 * It is essentially a sequence of log_reset(), logn() and log_commit().
166 log_msg(const char *msg
, ...)
172 if (*msg
>= 1 && *msg
<= 8)
174 vlog(class, msg
, args
);
179 log_rl(struct tbf
*f
, const char *msg
, ...)
181 int last_hit
= f
->mark
;
185 /* Rate limiting is a bit tricky here as it also logs '...' during the first hit */
186 if (tbf_limit(f
) && last_hit
)
189 if (*msg
>= 1 && *msg
<= 8)
193 vlog(class, (f
->mark
? "..." : msg
), args
);
198 * bug - report an internal error
199 * @msg: a printf-like error message
201 * This function logs an internal error and aborts execution
205 bug(const char *msg
, ...)
210 vlog(L_BUG
[0], msg
, args
);
215 * bug - report a fatal error
216 * @msg: a printf-like error message
218 * This function logs a fatal error and aborts execution
222 die(const char *msg
, ...)
227 vlog(L_FATAL
[0], msg
, args
);
232 * debug - write to debug output
233 * @msg: a printf-like message
235 * This function formats the message @msg and prints it out
236 * to the debugging output. No newline character is appended.
239 debug(const char *msg
, ...)
247 if (bvsnprintf(buf
, sizeof(buf
), msg
, args
) < 0)
248 bsprintf(buf
+ sizeof(buf
) - 100, " ... <too long>\n");
255 default_log_list(int debug
, int init
, char **syslog_name
)
257 static list init_log_list
;
258 init_list(&init_log_list
);
264 static struct log_config lc_syslog
= { .mask
= ~0 };
265 add_tail(&init_log_list
, &lc_syslog
.n
);
266 *syslog_name
= bird_name
;
268 return &init_log_list
;
272 static struct log_config lc_stderr
= { .mask
= ~0, .terminal_flag
= 1 };
273 lc_stderr
.fh
= stderr
;
274 add_tail(&init_log_list
, &lc_stderr
.n
);
275 return &init_log_list
;
279 log_switch(int debug
, list
*l
, char *new_syslog_name
)
281 if (!l
|| EMPTY_LIST(*l
))
282 l
= default_log_list(debug
, !l
, &new_syslog_name
);
284 current_log_list
= l
;
287 if (current_syslog_name
&& new_syslog_name
&&
288 !strcmp(current_syslog_name
, new_syslog_name
))
291 if (current_syslog_name
)
295 openlog(new_syslog_name
, LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
297 current_syslog_name
= new_syslog_name
;
304 log_init_debug(char *f
)
306 if (dbgf
&& dbgf
!= stderr
)
312 else if (!(dbgf
= fopen(f
, "a")))
313 log(L_ERR
"Error opening debug file `%s': %m", f
);
315 setvbuf(dbgf
, NULL
, _IONBF
, 0);
319 mrt_dump_message(struct proto
*p
, u16 type
, u16 subtype
, byte
*buf
, u32 len
)
322 put_u32(buf
+0, now_real
);
323 put_u16(buf
+4, type
);
324 put_u16(buf
+6, subtype
);
325 put_u32(buf
+8, len
- MRTDUMP_HDR_LENGTH
);
327 if (p
->cf
->global
->mrtdump_file
!= -1)
328 write(p
->cf
->global
->mrtdump_file
, buf
, len
);