/* * BIRD Library -- Logging Functions * * (c) 1998--2000 Martin Mares * * Can be freely distributed and used under the terms of the GNU GPL. */ /** * DOC: Logging * * The Logging module offers a simple set of functions for writing * messages to system logs and to the debug output. Message classes * used by this module are described in |birdlib.h| and also in the * user's manual. */ #include #include #include #include #include #include #include #include #include #include "nest/bird.h" #include "nest/cli.h" #include "conf/conf.h" #include "lib/string.h" #include "lib/lists.h" #include "sysdep/unix/unix.h" static struct rfile *dbg_rf; static list *current_log_list; static char *current_syslog_name; /* NULL -> syslog closed */ _Atomic uint max_thread_id = ATOMIC_VAR_INIT(1); _Thread_local uint this_thread_id; #include static pthread_mutex_t log_mutex; static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); } static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); } /* Logging flags to validly prepare logging messages */ #define LOGGING_TO_TERMINAL 0x1 #define LOGGING_TO_FILE 0x2 static _Atomic uint logging_flags; #ifdef HAVE_SYSLOG_H #include static int syslog_priorities[] = { LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_ERR, LOG_WARNING, LOG_ERR, LOG_ERR, LOG_CRIT, LOG_CRIT }; #endif static char *class_names[] = { "???", "DBG", "TRACE", "INFO", "RMT", "WARN", "ERR", "AUTH", "FATAL", "BUG" }; static inline off_t log_size(struct log_config *l) { struct stat st; return (!fstat(rf_fileno(l->rf), &st) && S_ISREG(st.st_mode)) ? st.st_size : 0; } static void log_close(struct log_config *l) { if (l->rf != &rf_stderr) rfree(l->rf); l->rf = NULL; } static int log_open(struct log_config *l) { l->rf = rf_open(config->pool, l->filename, RF_APPEND); if (!l->rf) { /* Well, we cannot do much in case of error as log is closed */ l->mask = 0; return -1; } l->pos = log_size(l); return 0; } static int log_rotate(struct log_config *l) { log_close(l); /* If we cannot rename the logfile, we at least try to delete it in order to continue logging and not exceeding logfile size */ if ((rename(l->filename, l->backup) < 0) && (unlink(l->filename) < 0)) { l->mask = 0; return -1; } return log_open(l); } #define LOG_MSG_OFFSET (TM_DATETIME_BUFFER_SIZE + 64) /** * log_commit - commit a log message * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|) * @buf: message to write * * This function writes a message prepared in the log buffer to the * log file (as specified in the configuration). The log buffer is * reset after that. The log message is a full line, log_commit() * terminates it. * * The message class is an integer, not a first char of a string like * in log(), so it should be written like *L_INFO. */ void log_commit(log_buffer *buf) { struct log_config *l; if (buf->buf.pos == buf->buf.end) #define TOO_LONG " ... " memcpy(buf->buf.end - sizeof TOO_LONG, TOO_LONG, sizeof TOO_LONG); #undef TOO_LONG log_lock(); WALK_LIST(l, *current_log_list) { if (!(l->mask & (1 << buf->class))) continue; if (l->rf && buf->tm_pos) { *buf->buf.pos = '\n'; byte *begin = l->terminal_flag ? buf->buf.start : buf->tm_pos; off_t msg_len = buf->buf.pos - begin + 1; if (l->limit && (l->pos + msg_len > l->limit) && (log_rotate(l) < 0)) continue; l->pos += msg_len; while ((write(rf_fileno(l->rf), buf->tm_pos, msg_len) < 0) && (errno == EINTR)) ; } #ifdef HAVE_SYSLOG_H else { *buf->buf.pos = '\0'; syslog(syslog_priorities[buf->class], "%s", buf->msg_pos); } #endif } log_unlock(); buf->msg_pos = buf->tm_pos = NULL; } int buffer_vprint(buffer *buf, const char *fmt, va_list args); void log_prepare(log_buffer *buf, int class) { buf->buf.start = buf->buf.pos = buf->block; buf->buf.end = buf->block + sizeof buf->block; int lf = atomic_load_explicit(&logging_flags, memory_order_acquire); if (lf & LOGGING_TO_TERMINAL) buffer_puts(&buf->buf, "bird: "); if (lf & LOGGING_TO_FILE) { const char *fmt = config ? config->tf_log.fmt1 : "%F %T.%3f"; buf->tm_pos = buf->buf.pos; int t = tm_format_real_time(buf->buf.pos, buf->buf.end - buf->buf.pos, fmt, current_real_time()); if (t) buf->buf.pos += t; else buffer_puts(&buf->buf, "