diff options
Diffstat (limited to 'sysdep/unix/log.c')
-rw-r--r-- | sysdep/unix/log.c | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c index 68a04e78..be7f8adf 100644 --- a/sysdep/unix/log.c +++ b/sysdep/unix/log.c @@ -32,14 +32,15 @@ #include "lib/lists.h" #include "sysdep/unix/unix.h" +static int dbg_fd = -1; static FILE *dbgf; static list *current_log_list; static char *current_syslog_name; /* NULL -> syslog closed */ -static _Atomic uint max_coro_id = ATOMIC_VAR_INIT(1); -static _Thread_local uint this_coro_id; +static _Atomic uint max_thread_id = ATOMIC_VAR_INIT(1); +static _Thread_local uint this_thread_id; -#define THIS_CORO_ID (this_coro_id ?: (this_coro_id = atomic_fetch_add_explicit(&max_coro_id, 1, memory_order_acq_rel))) +#define THIS_THREAD_ID (this_thread_id ?: (this_thread_id = atomic_fetch_add_explicit(&max_thread_id, 1, memory_order_acq_rel))) #include <pthread.h> @@ -183,7 +184,7 @@ log_commit(int class, buffer *buf) l->pos += msg_len; } - fprintf(l->fh, "%s [%04x] <%s> ", tbuf, THIS_CORO_ID, class_names[class]); + fprintf(l->fh, "%s [%04x] <%s> ", tbuf, THIS_THREAD_ID, class_names[class]); } fputs(buf->start, l->fh); fputc('\n', l->fh); @@ -313,6 +314,7 @@ debug(const char *msg, ...) va_start(args, msg); if (dbgf) { +#if 0 struct timespec dbg_time; clock_gettime(CLOCK_MONOTONIC, &dbg_time); uint nsec; @@ -329,10 +331,10 @@ debug(const char *msg, ...) sec = dbg_time.tv_sec - dbg_time_start.tv_sec - 1; } - int n = bsnprintf(pos, max, "%u.%09u: [%04x] ", sec, nsec, THIS_CORO_ID); + int n = bsnprintf(pos, max, "%u.%09u: [%04x] ", sec, nsec, THIS_THREAD_ID); pos += n; max -= n; - +#endif if (bvsnprintf(pos, max, msg, args) < 0) bug("Extremely long debug output, split it."); @@ -341,6 +343,21 @@ debug(const char *msg, ...) va_end(args); } +/** + * debug_safe - async-safe write to debug output + * @msg: a string message + * + * This function prints the message @msg to the debugging output in a + * way that is async safe and can be used in signal handlers. No newline + * character is appended. + */ +void +debug_safe(const char *msg) +{ + if (dbg_fd >= 0) + write(dbg_fd, msg, strlen(msg)); +} + static list * default_log_list(int initial, const char **syslog_name) { @@ -388,21 +405,6 @@ default_log_list(int initial, const char **syslog_name) } void -log_cleanup(int syslog) -{ - struct log_config *l; - - if (current_log_list) - WALK_LIST(l, *current_log_list) - if (l->rf) - log_close(l); - - if (syslog && current_syslog_name) - closelog(); -} - - -void log_switch(int initial, list *logs, const char *new_syslog_name) { struct log_config *l; @@ -414,7 +416,10 @@ log_switch(int initial, list *logs, const char *new_syslog_name) logs = default_log_list(initial, &new_syslog_name); /* Close the logs to avoid pinning them on disk when deleted */ - log_cleanup(0); + if (current_log_list) + WALK_LIST(l, *current_log_list) + if (l->rf) + log_close(l); /* Reopen the logs, needed for 'configure undo' */ if (logs) @@ -453,8 +458,10 @@ log_init_debug(char *f) { clock_gettime(CLOCK_MONOTONIC, &dbg_time_start); + dbg_fd = -1; if (dbgf && dbgf != stderr) fclose(dbgf); + if (!f) dbgf = NULL; else if (!*f) @@ -465,6 +472,10 @@ log_init_debug(char *f) fprintf(stderr, "bird: Unable to open debug file %s: %s\n", f, strerror(errno)); exit(1); } + if (dbgf) + { setvbuf(dbgf, NULL, _IONBF, 0); + dbg_fd = fileno(dbgf); + } } |