/* * 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 "nest/bird.h" #include "nest/cli.h" #include "conf/conf.h" #include "lib/string.h" #include "lib/lists.h" #include "sysdep/unix/unix.h" #include "sysdep/unix/io-loop.h" static pool *log_pool; static struct rfile *dbg_rf; static char *current_syslog_name = NULL; /* NULL -> syslog closed */ _Atomic uint max_thread_id = ATOMIC_VAR_INIT(1); _Thread_local uint this_thread_id; #include DEFINE_DOMAIN(logging); static DOMAIN(logging) log_domain; #define log_lock() LOCK_DOMAIN(logging, log_domain); #define log_unlock() UNLOCK_DOMAIN(logging, log_domain); static struct log_channel * _Atomic global_logs; /* Logging flags to validly prepare logging messages */ #define LOGGING_TO_TERMINAL 0x1 #define LOGGING_TO_FILE 0x2 static _Atomic uint logging_flags; static _Atomic uint logging_mask; #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" }; struct log_channel { struct log_channel * _Atomic next; const char *filename; /* Log filename */ const char *backup; /* Secondary filename (for log rotation) */ struct rfile * _Atomic rf; /* File handle */ off_t limit; /* Log size limit */ _Atomic uint mask; /* Classes to log */ uint new_mask; /* Pending new mask */ uint terminal:1; /* Is terminal */ }; struct log_thread_syncer { struct bird_thread_syncer sync; struct log_channel *lc_close; struct rfile *rf_close; const char *name; event lts_event; }; static void lts_done(struct bird_thread_syncer *sync) { struct log_thread_syncer *lts = SKIP_BACK(struct log_thread_syncer, sync, sync); log_lock(); if (lts->lc_close) { lts->rf_close = atomic_load_explicit(<s->lc_close->rf, memory_order_relaxed); mb_free(lts->lc_close); } if (lts->rf_close && lts->rf_close != &rf_stderr) rfree(lts->rf_close); mb_free(lts); log_unlock(); } static void lts_event(void *_lts) { struct log_thread_syncer *lts = _lts; bird_thread_sync_all(<s->sync, NULL, lts_done, lts->name); } static void lts_request(struct log_channel *lc_close, struct rfile *rf_close, const char *name) { struct log_thread_syncer *lts = mb_allocz(log_pool, sizeof *lts); lts->lc_close = lc_close; lts->rf_close = rf_close; lts->name = name; lts->lts_event = (event) { .hook = lts_event, .data = lts, }; ev_send_loop(&main_birdloop, <s->lts_event); } static void log_rotate(struct log_channel *lc) { struct log_thread_syncer *lts = mb_allocz(log_pool, sizeof *lts); if ((rename(lc->filename, lc->backup) < 0) && (unlink(lc->filename) < 0)) return lts_request(lc, NULL, "Log Rotate Failed"); struct rfile *rf = rf_open(log_pool, lc->filename, RF_APPEND, lc->limit); if (!rf) return lts_request(lc, NULL, "Log Rotate Failed"); lts_request(NULL, atomic_load_explicit(&lc->rf, memory_order_relaxed), "Log Rotate Close Old File"); atomic_store_explicit(&lc->rf, rf, memory_order_release); } #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) { 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 for ( struct log_channel *l = atomic_load_explicit(&global_logs, memory_order_acquire); l; l = atomic_load_explicit(&l->next, memory_order_acquire) ) { uint mask = atomic_load_explicit(&l->mask, memory_order_acquire); if (!(mask & (1 << buf->class))) continue; struct rfile *rf = atomic_load_explicit(&l->rf, memory_order_acquire); if (rf && buf->tm_pos) { *buf->buf.pos = '\n'; byte *begin = l->terminal ? buf->buf.start : buf->tm_pos; off_t msg_len = buf->buf.pos - begin + 1; do { if (rf_write(rf, buf->tm_pos, msg_len)) break; log_lock(); rf = atomic_load_explicit(&l->rf, memory_order_acquire); if (rf_write(rf, buf->tm_pos, msg_len)) { log_unlock(); break; } log_rotate(l); log_unlock(); rf = atomic_load_explicit(&l->rf, memory_order_relaxed); } while (!rf_write(rf, buf->tm_pos, msg_len)); } #ifdef HAVE_SYSLOG_H else { *buf->buf.pos = '\0'; syslog(syslog_priorities[buf->class], "%s", buf->msg_pos); } #endif } 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, "