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