mirror of
https://github.com/systemd/systemd.git
synced 2024-11-01 17:51:22 +03:00
shared: Extract common LogControl CLI code to verb-log-control.h
Let's reduce duplication by sharing common logic between all log-target/log-level verbs.
This commit is contained in:
parent
627191c285
commit
a87b151ac0
@ -53,6 +53,7 @@
|
||||
#include "time-util.h"
|
||||
#include "unit-name.h"
|
||||
#include "util.h"
|
||||
#include "verb-log-control.h"
|
||||
#include "verbs.h"
|
||||
#include "version.h"
|
||||
|
||||
@ -1405,86 +1406,17 @@ static int cat_config(int argc, char *argv[], void *userdata) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int set_log_level(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
static int verb_log_control(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
int r;
|
||||
|
||||
assert(argc == 2);
|
||||
assert(argv);
|
||||
assert(argc == 1 || argc == 2);
|
||||
|
||||
r = acquire_bus(&bus, NULL);
|
||||
if (r < 0)
|
||||
return bus_log_connect_error(r);
|
||||
|
||||
r = bus_set_property(bus, bus_systemd_mgr, "LogLevel", &error, "s", argv[1]);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_log_level(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
_cleanup_free_ char *level = NULL;
|
||||
int r;
|
||||
|
||||
r = acquire_bus(&bus, NULL);
|
||||
if (r < 0)
|
||||
return bus_log_connect_error(r);
|
||||
|
||||
r = bus_get_property_string(bus, bus_systemd_mgr, "LogLevel", &error, &level);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r));
|
||||
|
||||
puts(level);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_or_set_log_level(int argc, char *argv[], void *userdata) {
|
||||
return (argc == 1) ? get_log_level(argc, argv, userdata) : set_log_level(argc, argv, userdata);
|
||||
}
|
||||
|
||||
static int set_log_target(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
int r;
|
||||
|
||||
assert(argc == 2);
|
||||
assert(argv);
|
||||
|
||||
r = acquire_bus(&bus, NULL);
|
||||
if (r < 0)
|
||||
return bus_log_connect_error(r);
|
||||
|
||||
r = bus_set_property(bus, bus_systemd_mgr, "LogTarget", &error, "s", argv[1]);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_log_target(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
||||
_cleanup_free_ char *target = NULL;
|
||||
int r;
|
||||
|
||||
r = acquire_bus(&bus, NULL);
|
||||
if (r < 0)
|
||||
return bus_log_connect_error(r);
|
||||
|
||||
r = bus_get_property_string(bus, bus_systemd_mgr, "LogTarget", &error, &target);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get log target: %s", bus_error_message(&error, r));
|
||||
|
||||
puts(target);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_or_set_log_target(int argc, char *argv[], void *userdata) {
|
||||
return (argc == 1) ? get_log_target(argc, argv, userdata) : set_log_target(argc, argv, userdata);
|
||||
return verb_log_control_common(bus, "org.freedesktop.systemd1", argv[0], argc == 2 ? argv[1] : NULL);
|
||||
}
|
||||
|
||||
static bool strv_fnmatch_strv_or_empty(char* const* patterns, char **strv, int flags) {
|
||||
@ -2557,12 +2489,12 @@ static int run(int argc, char *argv[]) {
|
||||
{ "plot", VERB_ANY, 1, 0, analyze_plot },
|
||||
{ "dot", VERB_ANY, VERB_ANY, 0, dot },
|
||||
/* The following seven verbs are deprecated */
|
||||
{ "log-level", VERB_ANY, 2, 0, get_or_set_log_level },
|
||||
{ "log-target", VERB_ANY, 2, 0, get_or_set_log_target },
|
||||
{ "set-log-level", 2, 2, 0, set_log_level },
|
||||
{ "get-log-level", VERB_ANY, 1, 0, get_log_level },
|
||||
{ "set-log-target", 2, 2, 0, set_log_target },
|
||||
{ "get-log-target", VERB_ANY, 1, 0, get_log_target },
|
||||
{ "log-level", VERB_ANY, 2, 0, verb_log_control },
|
||||
{ "log-target", VERB_ANY, 2, 0, verb_log_control },
|
||||
{ "set-log-level", 2, 2, 0, verb_log_control },
|
||||
{ "get-log-level", VERB_ANY, 1, 0, verb_log_control },
|
||||
{ "set-log-target", 2, 2, 0, verb_log_control },
|
||||
{ "get-log-target", VERB_ANY, 1, 0, verb_log_control },
|
||||
{ "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
|
||||
{ "dump", VERB_ANY, 1, 0, dump },
|
||||
{ "cat-config", 2, VERB_ANY, 0, cat_config },
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
#include "utf8.h"
|
||||
#include "verb-log-control.h"
|
||||
#include "verbs.h"
|
||||
|
||||
static int arg_family = AF_UNSPEC;
|
||||
@ -2533,45 +2534,12 @@ static int verb_revert_link(int argc, char **argv, void *userdata) {
|
||||
}
|
||||
|
||||
static int verb_log_level(int argc, char *argv[], void *userdata) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
sd_bus *bus = userdata;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(argc == 1 || argc == 2);
|
||||
|
||||
if (argc == 1) {
|
||||
_cleanup_free_ char *level = NULL;
|
||||
|
||||
r = sd_bus_get_property_string(
|
||||
bus,
|
||||
"org.freedesktop.resolve1",
|
||||
"/org/freedesktop/LogControl1",
|
||||
"org.freedesktop.LogControl1",
|
||||
"LogLevel",
|
||||
&error,
|
||||
&level);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r));
|
||||
|
||||
puts(level);
|
||||
|
||||
} else {
|
||||
assert(argc == 2);
|
||||
|
||||
r = sd_bus_set_property(
|
||||
bus,
|
||||
"org.freedesktop.resolve1",
|
||||
"/org/freedesktop/LogControl1",
|
||||
"org.freedesktop.LogControl1",
|
||||
"LogLevel",
|
||||
&error,
|
||||
"s",
|
||||
argv[1]);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to set log level: %s", bus_error_message(&error, r));
|
||||
}
|
||||
|
||||
return 0;
|
||||
return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL);
|
||||
}
|
||||
|
||||
static void help_protocol_types(void) {
|
||||
|
@ -301,6 +301,8 @@ shared_sources = files('''
|
||||
utmp-wtmp.h
|
||||
varlink.c
|
||||
varlink.h
|
||||
verb-log-control.c
|
||||
verb-log-control.h
|
||||
verbs.c
|
||||
verbs.h
|
||||
vlan-util.c
|
||||
|
56
src/shared/verb-log-control.c
Normal file
56
src/shared/verb-log-control.c
Normal file
@ -0,0 +1,56 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-error.h"
|
||||
#include "log.h"
|
||||
#include "strv.h"
|
||||
#include "syslog-util.h"
|
||||
#include "verb-log-control.h"
|
||||
|
||||
int verb_log_control_common(sd_bus *bus, const char *destination, const char *verb, const char *value) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
bool level = endswith(verb, "log-level");
|
||||
const BusLocator bloc = {
|
||||
.destination = destination,
|
||||
.path = "/org/freedesktop/LogControl1",
|
||||
.interface = "org.freedesktop.LogControl1",
|
||||
};
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(endswith(verb, "log-level") || endswith(verb, "log-target"));
|
||||
|
||||
if (value) {
|
||||
if (level) {
|
||||
r = log_level_from_string(value);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "\"%s\" is not a valid log level.", value);
|
||||
}
|
||||
|
||||
r = bus_set_property(bus, &bloc,
|
||||
level ? "LogLevel" : "LogTarget",
|
||||
&error, "s", value);
|
||||
if (r >= 0)
|
||||
return 0;
|
||||
|
||||
log_error_errno(r, "Failed to set log %s of %s to %s: %s",
|
||||
level ? "level" : "target",
|
||||
bloc.destination, value, bus_error_message(&error, r));
|
||||
} else {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
|
||||
r = bus_get_property_string(bus, &bloc,
|
||||
level ? "LogLevel" : "LogTarget",
|
||||
&error, &t);
|
||||
if (r >= 0) {
|
||||
puts(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
log_error_errno(r, "Failed to get log %s of %s: %s",
|
||||
level ? "level" : "target",
|
||||
bloc.destination, bus_error_message(&error, r));
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
8
src/shared/verb-log-control.h
Normal file
8
src/shared/verb-log-control.h
Normal file
@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "bus-locator.h"
|
||||
|
||||
int verb_log_control_common(sd_bus *bus, const char *destination, const char *verb, const char *value);
|
@ -7,6 +7,7 @@
|
||||
#include "systemctl-log-setting.h"
|
||||
#include "systemctl-util.h"
|
||||
#include "systemctl.h"
|
||||
#include "verb-log-control.h"
|
||||
|
||||
static void give_log_control1_hint(const char *name) {
|
||||
_cleanup_free_ char *link = NULL;
|
||||
@ -20,54 +21,6 @@ static void give_log_control1_hint(const char *name) {
|
||||
" See the %s for details.", link ?: "org.freedesktop.LogControl1(5) man page");
|
||||
}
|
||||
|
||||
static int log_setting_internal(sd_bus *bus, const BusLocator* bloc, const char *verb, const char *value) {
|
||||
assert(bus);
|
||||
assert(STR_IN_SET(verb, "log-level", "log-target", "service-log-level", "service-log-target"));
|
||||
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
bool level = endswith(verb, "log-level");
|
||||
int r;
|
||||
|
||||
if (value) {
|
||||
if (level) {
|
||||
r = log_level_from_string(value);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "\"%s\" is not a valid log level.", value);
|
||||
}
|
||||
|
||||
r = bus_set_property(bus, bloc,
|
||||
level ? "LogLevel" : "LogTarget",
|
||||
&error, "s", value);
|
||||
if (r >= 0)
|
||||
return 0;
|
||||
|
||||
log_error_errno(r, "Failed to set log %s of %s to %s: %s",
|
||||
level ? "level" : "target",
|
||||
bloc->destination, value, bus_error_message(&error, r));
|
||||
} else {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
|
||||
r = bus_get_property_string(bus, bloc,
|
||||
level ? "LogLevel" : "LogTarget",
|
||||
&error, &t);
|
||||
if (r >= 0) {
|
||||
puts(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
log_error_errno(r, "Failed to get log %s of %s: %s",
|
||||
level ? "level" : "target",
|
||||
bloc->destination, bus_error_message(&error, r));
|
||||
}
|
||||
|
||||
if (sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_METHOD,
|
||||
SD_BUS_ERROR_UNKNOWN_OBJECT,
|
||||
SD_BUS_ERROR_UNKNOWN_INTERFACE,
|
||||
SD_BUS_ERROR_UNKNOWN_PROPERTY))
|
||||
give_log_control1_hint(bloc->destination);
|
||||
return r;
|
||||
}
|
||||
|
||||
int log_setting(int argc, char *argv[], void *userdata) {
|
||||
sd_bus *bus;
|
||||
int r;
|
||||
@ -78,7 +31,7 @@ int log_setting(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return log_setting_internal(bus, bus_systemd_mgr, argv[0], argv[1]);
|
||||
return verb_log_control_common(bus, "org.freedesktop.systemd1", argv[0], argv[1]);
|
||||
}
|
||||
|
||||
static int service_name_to_dbus(sd_bus *bus, const char *name, char **ret_dbus_name) {
|
||||
@ -134,11 +87,10 @@ int service_log_setting(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
const BusLocator bloc = {
|
||||
.destination = dbus_name,
|
||||
.path = "/org/freedesktop/LogControl1",
|
||||
.interface = "org.freedesktop.LogControl1",
|
||||
};
|
||||
r = verb_log_control_common(bus, dbus_name, argv[0], argv[2]);
|
||||
|
||||
return log_setting_internal(bus, &bloc, argv[0], argv[2]);
|
||||
if (r == -EBADR)
|
||||
give_log_control1_hint(dbus_name);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user