1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bus-error.h"
5 #include "log.h"
6 #include "strv.h"
7 #include "syslog-util.h"
8 #include "verb-log-control.h"
9
verb_log_control_common(sd_bus * bus,const char * destination,const char * verb,const char * value)10 int verb_log_control_common(sd_bus *bus, const char *destination, const char *verb, const char *value) {
11 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
12 bool level = endswith(verb, "log-level");
13 const BusLocator bloc = {
14 .destination = destination,
15 .path = "/org/freedesktop/LogControl1",
16 .interface = "org.freedesktop.LogControl1",
17 };
18 int r;
19
20 assert(bus);
21 assert(endswith(verb, "log-level") || endswith(verb, "log-target"));
22
23 if (value) {
24 if (level) {
25 r = log_level_from_string(value);
26 if (r < 0)
27 return log_error_errno(r, "\"%s\" is not a valid log level.", value);
28 }
29
30 r = bus_set_property(bus, &bloc,
31 level ? "LogLevel" : "LogTarget",
32 &error, "s", value);
33 if (r >= 0)
34 return 0;
35
36 log_error_errno(r, "Failed to set log %s of %s to %s: %s",
37 level ? "level" : "target",
38 bloc.destination, value, bus_error_message(&error, r));
39 } else {
40 _cleanup_free_ char *t = NULL;
41
42 r = bus_get_property_string(bus, &bloc,
43 level ? "LogLevel" : "LogTarget",
44 &error, &t);
45 if (r >= 0) {
46 puts(t);
47 return 0;
48 }
49
50 log_error_errno(r, "Failed to get log %s of %s: %s",
51 level ? "level" : "target",
52 bloc.destination, bus_error_message(&error, r));
53 }
54
55 return r;
56 }
57