From ca6079ff5d5afa466a6c5469dc8c03c822d22040 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Tue, 15 May 2018 23:43:23 +0100 Subject: [PATCH] Set main log format earlier. This is necessary to avoid buffering messages in incorrect format (without the priority indicator). --- src/dinit-log.cc | 9 ++++++--- src/dinit.cc | 14 +++++++------- src/includes/dinit-log.h | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/dinit-log.cc b/src/dinit-log.cc index f9236fa..fe5dd73 100644 --- a/src/dinit-log.cc +++ b/src/dinit-log.cc @@ -254,20 +254,23 @@ rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexce // Initialise the logging subsystem // Potentially throws std::bad_alloc or std::system_error -void init_log(service_set *sset) +void init_log(service_set *sset, bool syslog_format) { services = sset; log_stream[DLOG_CONS].add_watch(event_loop, STDOUT_FILENO, dasynq::OUT_EVENTS, false); enable_console_log(true); + + // The main (non-console) log won't be active yet, but we set the format here so that we + // buffer messages in the correct format: + log_format_syslog[DLOG_MAIN] = syslog_format; } // Set up the main log to output to the given file descriptor. // Potentially throws std::bad_alloc or std::system_error -void setup_main_log(int fd, bool syslog_format) +void setup_main_log(int fd) { log_stream[DLOG_MAIN].init(fd); log_stream[DLOG_MAIN].add_watch(event_loop, fd, dasynq::OUT_EVENTS); - log_format_syslog[DLOG_MAIN] = syslog_format; } bool is_log_flushed() noexcept diff --git a/src/dinit.cc b/src/dinit.cc index 7297f98..bfcc9a5 100644 --- a/src/dinit.cc +++ b/src/dinit.cc @@ -350,10 +350,6 @@ int dinit_main(int argc, char **argv) // Try to open control socket (may fail due to readonly filesystem) open_control_socket(false); - // Only try to set up the external log now if we aren't the system init. (If we are the - // system init, wait until the log service starts). - if (! am_system_init) setup_external_log(); - #ifdef __linux__ if (am_system_init) { // Disable non-critical kernel output to console @@ -387,11 +383,15 @@ int dinit_main(int argc, char **argv) services->add_service_dir("/lib/dinit.d", false); } - init_log(services); + init_log(services, log_is_syslog); if (am_system_init) { log(loglevel_t::INFO, false, "starting system"); } + // Only try to set up the external log now if we aren't the system init. (If we are the + // system init, wait until the log service starts). + if (! am_system_init) setup_external_log(); + if (env_file != nullptr) { read_env_file(env_file); } @@ -696,7 +696,7 @@ void setup_external_log() noexcept // the file descriptor so we will be notified when it's ready. In other words we can // basically use it anyway. try { - setup_main_log(sockfd, true); + setup_main_log(sockfd); } catch (std::exception &e) { log(loglevel_t::ERROR, "Setting up log failed: ", e.what()); @@ -716,7 +716,7 @@ void setup_external_log() noexcept int log_fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK | O_CLOEXEC, 0644); if (log_fd >= 0) { try { - setup_main_log(log_fd, false); + setup_main_log(log_fd); } catch (std::exception &e) { log(loglevel_t::ERROR, "Setting up log failed: ", e.what()); diff --git a/src/includes/dinit-log.h b/src/includes/dinit-log.h index 93b1159..bcc3c18 100644 --- a/src/includes/dinit-log.h +++ b/src/includes/dinit-log.h @@ -28,8 +28,8 @@ enum class loglevel_t { // These are defined in dinit-log.cc: extern loglevel_t log_level[2]; void enable_console_log(bool do_enable) noexcept; -void init_log(service_set *sset); -void setup_main_log(int fd, bool syslog_format); +void init_log(service_set *sset, bool syslog_format); +void setup_main_log(int fd); bool is_log_flushed() noexcept; void discard_console_log_buffer() noexcept; -- 2.25.1