From 637310cf1903f9072a391074a65855fc1c41ae2b Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Fri, 15 Apr 2022 09:28:15 +0800
Subject: [PATCH] core: add OptionalLog to allow users change log level.
This adds log_optional* log_unit_optional* to log messages in LOG_INFO
or LOG_DEBUG. Set "OptionalLog=yes" to log in LOG_INFO. Defaults to no.
---
 src/basic/log.h         | 2 ++
 src/core/dbus-manager.c | 1 +
 src/core/main.c         | 1 +
 src/core/manager.c      | 2 ++
 src/core/manager.h      | 1 +
 src/core/mount.c        | 2 +-
 src/core/system.conf.in | 1 +
 src/core/unit.h         | 2 ++
 8 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/basic/log.h b/src/basic/log.h
index 9008d47..bf6aa8e 100644
--- a/src/basic/log.h
+++ b/src/basic/log.h
@@ -245,6 +245,7 @@ int log_emergency_level(void);
 #define log_warning(...)   log_full(LOG_WARNING, __VA_ARGS__)
 #define log_error(...)     log_full(LOG_ERR,     __VA_ARGS__)
 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
+#define log_optional(use_info, ...) log_full(((use_info) ? LOG_INFO : LOG_DEBUG), __VA_ARGS__)
 
 /* Logging triggered by an errno-like error */
 #define log_debug_errno(error, ...)     log_full_errno(LOG_DEBUG,   error, __VA_ARGS__)
@@ -253,6 +254,7 @@ int log_emergency_level(void);
 #define log_warning_errno(error, ...)   log_full_errno(LOG_WARNING, error, __VA_ARGS__)
 #define log_error_errno(error, ...)     log_full_errno(LOG_ERR,     error, __VA_ARGS__)
 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
+#define log_optional_errno(error, use_info, ...) log_full_errno(((use_info) ? LOG_INFO : LOG_DEBUG), error, __VA_ARGS__)
 
 /* This logs at the specified level the first time it is called, and then
  * logs at debug. If the specified level is debug, this logs only the first
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index 0f9d4e8..a644e86 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -2963,6 +2963,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDUnitsLoadFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", bus_property_get_log_level, property_set_log_level, 0, 0),
         SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", bus_property_get_log_target, property_set_log_target, 0, 0),
+        SD_BUS_PROPERTY("OptionalLog", "b", bus_property_get_bool, offsetof(Manager, defaults.optional_log), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("NNames", "u", property_get_hashmap_size, offsetof(Manager, units), 0),
         SD_BUS_PROPERTY("NFailedUnits", "u", property_get_set_size, offsetof(Manager, failed_units), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("NJobs", "u", property_get_hashmap_size, offsetof(Manager, jobs), 0),
diff --git a/src/core/main.c b/src/core/main.c
index 96b0a11..c4379cf 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -617,6 +617,7 @@ static int parse_config_file(void) {
                 { "Manager", "LogColor",                     config_parse_color,                 0,                        NULL                              },
                 { "Manager", "LogLocation",                  config_parse_location,              0,                        NULL                              },
                 { "Manager", "LogTime",                      config_parse_time,                  0,                        NULL                              },
+                { "Manager", "OptionalLog",                  config_parse_bool,                  0,                        &arg_defaults.optional_log        },
                 { "Manager", "DumpCore",                     config_parse_bool,                  0,                        &arg_dump_core                    },
                 { "Manager", "CrashChVT", /* legacy */       config_parse_crash_chvt,            0,                        &arg_crash_chvt                   },
                 { "Manager", "CrashChangeVT",                config_parse_crash_chvt,            0,                        &arg_crash_chvt                   },
diff --git a/src/core/manager.c b/src/core/manager.c
index 3d14ea1..59170af 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -4200,6 +4200,7 @@ int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
         m->defaults.ip_accounting = defaults->ip_accounting;
 
         m->defaults.tasks_max = defaults->tasks_max;
+        m->defaults.optional_log = defaults->optional_log;
         m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
 
         m->defaults.oom_policy = defaults->oom_policy;
@@ -4971,6 +4972,7 @@ void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
                 .ip_accounting = false,
 
                 .tasks_max = DEFAULT_TASKS_MAX,
+                .optional_log = false,
                 .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
 
                 .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
diff --git a/src/core/manager.h b/src/core/manager.h
index 93e9d2a..6dd1a18 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -181,6 +181,7 @@ typedef struct UnitDefaults {
         usec_t memory_pressure_threshold_usec;
 
         char *smack_process_label;
+        bool optional_log;
 
         struct rlimit *rlimit[_RLIMIT_MAX];
 } UnitDefaults;
diff --git a/src/core/mount.c b/src/core/mount.c
index 52bd53e..26cade1 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -781,7 +781,7 @@ static void mount_set_state(Mount *m, MountState state) {
         }
 
         if (state != old_state)
-                log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
+                log_unit_optional(UNIT(m), UNIT(m)->manager->defaults.optional_log, "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
 
         unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
 }
diff --git a/src/core/system.conf.in b/src/core/system.conf.in
index dbdc47c..a55106c 100644
--- a/src/core/system.conf.in
+++ b/src/core/system.conf.in
@@ -22,6 +22,7 @@
 #LogColor=yes
 #LogLocation=no
 #LogTime=no
+#OptionalLog=no
 #DumpCore=yes
 #ShowStatus=yes
 #CrashChangeVT=no
diff --git a/src/core/unit.h b/src/core/unit.h
index 60bc2e3..afa4387 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -1132,12 +1132,14 @@ int unit_compare_priority(Unit *a, Unit *b);
 #define log_unit_notice(unit, ...)  log_unit_full(unit, LOG_NOTICE, __VA_ARGS__)
 #define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, __VA_ARGS__)
 #define log_unit_error(unit, ...)   log_unit_full(unit, LOG_ERR, __VA_ARGS__)
+#define log_unit_optional(unit, use_info, ...) log_unit_full(unit, ((use_info) ? LOG_INFO : LOG_DEBUG), __VA_ARGS__)
 
 #define log_unit_debug_errno(unit, error, ...)   log_unit_full_errno(unit, LOG_DEBUG, error, __VA_ARGS__)
 #define log_unit_info_errno(unit, error, ...)    log_unit_full_errno(unit, LOG_INFO, error, __VA_ARGS__)
 #define log_unit_notice_errno(unit, error, ...)  log_unit_full_errno(unit, LOG_NOTICE, error, __VA_ARGS__)
 #define log_unit_warning_errno(unit, error, ...) log_unit_full_errno(unit, LOG_WARNING, error, __VA_ARGS__)
 #define log_unit_error_errno(unit, error, ...)   log_unit_full_errno(unit, LOG_ERR, error, __VA_ARGS__)
+#define log_unit_optional_errno(unit, use_info, error, ...) log_unit_full_errno(unit, ((use_info) ? LOG_INFO : LOG_DEBUG), error, __VA_ARGS__)
 
 #if LOG_TRACE
 #  define log_unit_trace(...)          log_unit_debug(__VA_ARGS__)
-- 
2.33.0

