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 */
35 static const bird_clock_t rate_limit_time
= 5;
36 static const int rate_limit_count
= 5;
42 static pthread_mutex_t log_mutex
;
43 static inline void log_lock(void) { pthread_mutex_lock(&log_mutex
); }
44 static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex
); }
48 static inline void log_lock(void) { }
49 static inline void log_unlock(void) { }
55 #include <sys/syslog.h>
57 static int syslog_priorities
[] = {
71 static char *class_names
[] = {
86 * log_commit - commit a log message
87 * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
89 * This function writes a message prepared in the log buffer to the
90 * log file (as specified in the configuration). The log buffer is
91 * reset after that. The log message is a full line, log_commit()
94 * The message class is an integer, not a first char of a string like
95 * in log(), so it should be written like *L_INFO.
98 log_commit(int class, buffer
*buf
)
100 struct log_config
*l
;
102 if (buf
->pos
== buf
->end
)
103 strcpy(buf
->end
- 100, " ... <too long>");
106 WALK_LIST(l
, *current_log_list
)
108 if (!(l
->mask
& (1 << class)))
112 if (l
->terminal_flag
)
113 fputs("bird: ", l
->fh
);
116 byte tbuf
[TM_DATETIME_BUFFER_SIZE
];
117 tm_format_datetime(tbuf
, &config
->tf_log
, now
);
118 fprintf(l
->fh
, "%s <%s> ", tbuf
, class_names
[class]);
120 fputs(buf
->start
, l
->fh
);
126 syslog(syslog_priorities
[class], "%s", buf
->start
);
131 /* FIXME: cli_echo is not thread-safe */
132 cli_echo(class, buf
->start
);
135 int buffer_vprint(buffer
*buf
, const char *fmt
, va_list args
);
138 vlog(int class, const char *msg
, va_list args
)
141 LOG_BUFFER_INIT(buf
);
142 buffer_vprint(&buf
, msg
, args
);
143 log_commit(class, &buf
);
149 * log - log a message
150 * @msg: printf-like formatting string with message class information
151 * prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
153 * This function formats a message according to the format string @msg
154 * and writes it to the corresponding log file (as specified in the
155 * configuration). Please note that the message is automatically
156 * formatted as a full line, no need to include |\n| inside.
157 * It is essentially a sequence of log_reset(), logn() and log_commit().
160 log_msg(char *msg
, ...)
166 if (*msg
>= 1 && *msg
<= 8)
168 vlog(class, msg
, args
);
173 log_rl(struct rate_limit
*rl
, char *msg
, ...)
178 bird_clock_t delta
= now
- rl
->timestamp
;
179 if ((0 <= delta
) && (delta
< rate_limit_time
))
189 if (rl
->count
> rate_limit_count
)
193 if (*msg
>= 1 && *msg
<= 8)
195 vlog(class, msg
, args
);
196 if (rl
->count
== rate_limit_count
)
197 vlog(class, "...", args
);
202 * bug - report an internal error
203 * @msg: a printf-like error message
205 * This function logs an internal error and aborts execution
214 vlog(L_BUG
[0], msg
, args
);
219 * bug - report a fatal error
220 * @msg: a printf-like error message
222 * This function logs a fatal error and aborts execution
231 vlog(L_FATAL
[0], msg
, args
);
236 * debug - write to debug output
237 * @msg: a printf-like message
239 * This function formats the message @msg and prints it out
240 * to the debugging output. No newline character is appended.
243 debug(char *msg
, ...)
251 if (bvsnprintf(buf
, sizeof(buf
), msg
, args
) < 0)
252 bsprintf(buf
+ sizeof(buf
) - 100, " ... <too long>\n");
259 default_log_list(int debug
, int init
, char **syslog_name
)
261 static list init_log_list
;
262 init_list(&init_log_list
);
268 static struct log_config lc_syslog
= { mask
: ~0 };
269 add_tail(&init_log_list
, &lc_syslog
.n
);
270 *syslog_name
= bird_name
;
272 return &init_log_list
;
276 static struct log_config lc_stderr
= { mask
: ~0, terminal_flag
: 1 };
277 lc_stderr
.fh
= stderr
;
278 add_tail(&init_log_list
, &lc_stderr
.n
);
279 return &init_log_list
;
283 log_switch(int debug
, list
*l
, char *new_syslog_name
)
285 if (!l
|| EMPTY_LIST(*l
))
286 l
= default_log_list(debug
, !l
, &new_syslog_name
);
288 current_log_list
= l
;
291 if (current_syslog_name
&& new_syslog_name
&&
292 !strcmp(current_syslog_name
, new_syslog_name
))
295 if (current_syslog_name
)
299 openlog(new_syslog_name
, LOG_CONS
| LOG_NDELAY
, LOG_DAEMON
);
301 current_syslog_name
= new_syslog_name
;
308 log_init_debug(char *f
)
310 if (dbgf
&& dbgf
!= stderr
)
316 else if (!(dbgf
= fopen(f
, "a")))
317 log(L_ERR
"Error opening debug file `%s': %m", f
);
319 setvbuf(dbgf
, NULL
, _IONBF
, 0);
323 mrt_dump_message(struct proto
*p
, u16 type
, u16 subtype
, byte
*buf
, u32 len
)
326 put_u32(buf
+0, now_real
);
327 put_u16(buf
+4, type
);
328 put_u16(buf
+6, subtype
);
329 put_u32(buf
+8, len
- MRTDUMP_HDR_LENGTH
);
331 if (p
->cf
->global
->mrtdump_file
!= -1)
332 write(p
->cf
->global
->mrtdump_file
, buf
, len
);