systemd/core-add-OptionalLog-to-allow-users-change-log-level.patch
2023-07-27 00:50:15 +08:00

145 lines
7.8 KiB
Diff

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 | 4 ++++
src/core/manager.h | 1 +
src/core/mount.c | 2 +-
src/core/system.conf.in | 1 +
src/core/unit.h | 2 ++
7 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/basic/log.h b/src/basic/log.h
index f73d4c4..d341681 100644
--- a/src/basic/log.h
+++ b/src/basic/log.h
@@ -243,6 +243,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__)
@@ -251,6 +252,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 7e57a32..9ca392b 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -2870,6 +2870,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, 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 eaae658..809ed76 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -119,6 +119,7 @@ static const char *arg_bus_introspect = NULL;
* defaults are assigned in reset_arguments() below. */
static char *arg_default_unit;
static bool arg_system;
+static bool arg_optional_log;
bool arg_dump_core;
int arg_crash_chvt;
bool arg_crash_shell;
@@ -626,6 +627,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_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 },
@@ -745,6 +747,7 @@ static void set_manager_defaults(Manager *m) {
* affect the manager itself, but are just what newly allocated units will have set if they haven't set
* anything else. (Also see set_manager_settings() for the settings that affect the manager's own behaviour) */
+ m->optional_log = arg_optional_log;
m->default_timer_accuracy_usec = arg_default_timer_accuracy_usec;
m->default_std_output = arg_default_std_output;
m->default_std_error = arg_default_std_error;
@@ -2423,6 +2426,7 @@ static void reset_arguments(void) {
/* arg_system — ignore */
+ arg_optional_log = false;
arg_dump_core = true;
arg_crash_chvt = -1;
arg_crash_shell = false;
diff --git a/src/core/manager.h b/src/core/manager.h
index d3f6aa2..814421f 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -385,6 +385,7 @@ struct Manager {
LogTarget original_log_target;
bool log_level_overridden;
bool log_target_overridden;
+ bool optional_log;
struct rlimit *rlimit[_RLIMIT_MAX];
diff --git a/src/core/mount.c b/src/core/mount.c
index af0eae6..3751cb4 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -756,7 +756,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->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 ? 0 : UNIT_NOTIFY_RELOAD_FAILURE);
diff --git a/src/core/system.conf.in b/src/core/system.conf.in
index 066a9a7..564d146 100644
--- a/src/core/system.conf.in
+++ b/src/core/system.conf.in
@@ -20,6 +20,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 58417eb..cc65d93 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -1097,12 +1097,14 @@ Condition *unit_find_failed_condition(Unit *u);
#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