Browse Source

Clean up logging infrastructure

Ensure that service state notifications are logged at the correct level;
use a different level (WARN) for service failure due to dependency vs
immediate failure (ERROR).
Davin McCall 5 months ago
parent
commit
a76e4d5c17
3 changed files with 36 additions and 21 deletions
  1. 30 13
      src/dinit-log.cc
  2. 5 7
      src/includes/dinit-log.h
  3. 1 1
      src/service.cc

+ 30 - 13
src/dinit-log.cc

@@ -29,7 +29,7 @@ static service_set *services = nullptr;  // Reference to service set
 loglevel_t log_level[DLOG_NUM] = { loglevel_t::NOTICE, loglevel_t::WARN };
 static_assert(DLOG_NUM == 2, "number of log streams has changed");
 
-bool console_service_status = true;  // show service status messages to console?
+bool console_service_status = true;  // always show service status messages to console?
 
 
 dasynq::time_val release_time; // time the log was released
@@ -422,21 +422,31 @@ template <typename ... T> static void do_log(loglevel_t lvl, bool to_cons, T ...
     }
 }
 
-template <typename ... T> static void do_log_cons(T ... args) noexcept
+// Log to the console only (only for service status)
+template <typename ... T> static void do_log_cons(loglevel_t lvl, T ... args) noexcept
 {
-    if (console_service_status) {
+    if (console_service_status || lvl >= log_level[DLOG_CONS]) {
         log_current_line[DLOG_CONS] = true;
         log_current_line[DLOG_MAIN] = false;
         push_to_log(DLOG_CONS, args...);
     }
 }
 
-// Log to the main facility at NOTICE level
-template <typename ... T> static void do_log_main(T ... args) noexcept
+template <typename ... T> static void do_log_cons(T ... args) noexcept
 {
+    do_log_cons(loglevel_t::NOTICE, args...);
+}
+
+// Log to the main facility (only; not console)
+template <typename ... T> static void do_log_main(loglevel_t lvl, T ... args) noexcept
+{
+    if (lvl < log_level[DLOG_MAIN]) {
+        return;
+    }
+
     log_current_line[DLOG_CONS] = false;
     log_current_line[DLOG_MAIN] = true;
-    
+
     if (log_format_syslog[DLOG_MAIN]) {
         char svcbuf[10];
         snprintf(svcbuf, 10, "<%d>", LOG_DAEMON | LOG_NOTICE);
@@ -447,6 +457,12 @@ template <typename ... T> static void do_log_main(T ... args) noexcept
     }
 }
 
+// Log to the main facility at NOTICE level
+template <typename ... T> static void do_log_main(T ... args) noexcept
+{
+    do_log_main(loglevel_t::NOTICE, args...);
+}
+
 // Log a message. A newline will be appended.
 void log(loglevel_t lvl, const char *msg) noexcept
 {
@@ -523,18 +539,19 @@ void log_msg_end(const char *msg) noexcept
 
 void log_service_started(const char *service_name) noexcept
 {
-    do_log_cons("[  OK  ] ", service_name, "\n");
-    do_log_main("dinit: service ", service_name, " started.\n");
+    do_log_cons(loglevel_t::NOTICE, "[  OK  ] ", service_name, "\n");
+    do_log_main(loglevel_t::NOTICE, "dinit: service ", service_name, " started.\n");
 }
 
-void log_service_failed(const char *service_name) noexcept
+void log_service_failed(const char *service_name, bool dep_failed) noexcept
 {
-    do_log_cons("[FAILED] ", service_name, "\n");
-    do_log_main("dinit: service ", service_name, " failed to start.\n");
+    loglevel_t cons_lvl = dep_failed ? loglevel_t::WARN : loglevel_t::ERROR;
+    do_log_cons(cons_lvl, "[FAILED] ", service_name, "\n");
+    do_log_main(loglevel_t::ERROR, "dinit: service ", service_name, " failed to start.\n");
 }
 
 void log_service_stopped(const char *service_name) noexcept
 {
-    do_log_cons("[STOPPD] ", service_name, "\n");
-    do_log_main("dinit: service ", service_name, " stopped.\n");
+    do_log_cons(loglevel_t::NOTICE, "[STOPPD] ", service_name, "\n");
+    do_log_main(loglevel_t::NOTICE, "dinit: service ", service_name, " stopped.\n");
 }

+ 5 - 7
src/includes/dinit-log.h

@@ -76,14 +76,11 @@ void log_msg_begin(loglevel_t lvl, const char *msg) noexcept;
 void log_msg_part(const char *msg) noexcept;
 void log_msg_end(const char *msg) noexcept;
 
-// Defined below:
 void log_service_started(const char *service_name) noexcept;
-void log_service_failed(const char *service_name) noexcept;
+void log_service_failed(const char *service_name, bool dep_failed) noexcept;
 void log_service_stopped(const char *service_name) noexcept;
 
 // Convenience methods which perform type conversion of the argument.
-// There is some duplication here that could possibly be avoided, but
-// it doesn't seem like a big deal.
 static inline void log(loglevel_t lvl, const std::string &str) noexcept
 {
     log(lvl, str.c_str());
@@ -133,9 +130,9 @@ static inline void log_service_started(const std::string &str) noexcept
     log_service_started(str.c_str());
 }
 
-static inline void log_service_failed(const std::string &str) noexcept
+static inline void log_service_failed(const std::string &str, bool dep_failed) noexcept
 {
-    log_service_failed(str.c_str());
+    log_service_failed(str.c_str(), dep_failed);
 }
 
 static inline void log_service_stopped(const std::string &str) noexcept
@@ -143,7 +140,8 @@ static inline void log_service_stopped(const std::string &str) noexcept
     log_service_stopped(str.c_str());
 }
 
-// It's not intended that methods in this namespace be called directly:
+// It's not intended that methods in this namespace be called directly from outside the logging
+// subsystem:
 namespace dinit_log {
     template <typename A> static inline void log_parts(const A &a) noexcept
     {

+ 1 - 1
src/service.cc

@@ -546,7 +546,7 @@ void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexce
     }
 
     start_failed = true;
-    log_service_failed(get_name());
+    log_service_failed(get_name(), depfailed);
     notify_listeners(service_event_t::FAILEDSTART);
     pinned_started = false;