Set main log format earlier.
authorDavin McCall <davmac@davmac.org>
Tue, 15 May 2018 22:43:23 +0000 (23:43 +0100)
committerDavin McCall <davmac@davmac.org>
Mon, 21 May 2018 17:29:45 +0000 (18:29 +0100)
This is necessary to avoid buffering messages in incorrect format
(without the priority indicator).

src/dinit-log.cc
src/dinit.cc
src/includes/dinit-log.h

index f9236faaa757861fde6e9b169a5e2cc941e315c2..fe5dd73187425a5fc4ae0bd15b08b47671fd4a70 100644 (file)
@@ -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
index 7297f9895fcace4408323d313015be3c23797a82..bfcc9a56e19571d08816de3aca3c13f3af45859f 100644 (file)
@@ -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());
index 93b11596bf41d8c0c9e597dc077a6300532a92cd..bcc3c1841fdd8c94343349984980bec73f60e44b 100644 (file)
@@ -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;