Merge pull request #23309 from DaanDeMeyer/log-context

basic: Add log context
This commit is contained in:
Luca Boccassi
2023-01-20 15:01:03 +00:00
committed by GitHub
22 changed files with 528 additions and 96 deletions
+23
View File
@@ -4667,3 +4667,26 @@ _public_ int sd_bus_message_sensitive(sd_bus_message *m) {
m->sensitive = true;
return 0;
}
char** bus_message_make_log_fields(sd_bus_message *m) {
_cleanup_strv_free_ char **strv = NULL;
assert(m);
(void) strv_extend_assignment(&strv, "DBUS_MESSAGE_TYPE", bus_message_type_to_string(m->header->type));
(void) strv_extend_assignment(&strv, "DBUS_SENDER", sd_bus_message_get_sender(m));
(void) strv_extend_assignment(&strv, "DBUS_DESTINATION", sd_bus_message_get_destination(m));
(void) strv_extend_assignment(&strv, "DBUS_PATH", sd_bus_message_get_path(m));
(void) strv_extend_assignment(&strv, "DBUS_INTERFACE", sd_bus_message_get_interface(m));
(void) strv_extend_assignment(&strv, "DBUS_MEMBER", sd_bus_message_get_member(m));
(void) strv_extendf(&strv, "DBUS_MESSAGE_COOKIE=%" PRIu64, BUS_MESSAGE_COOKIE(m));
if (m->reply_cookie != 0)
(void) strv_extendf(&strv, "DBUS_MESSAGE_REPLY_COOKIE=%" PRIu64, m->reply_cookie);
(void) strv_extend_assignment(&strv, "DBUS_SIGNATURE", m->root_container.signature);
(void) strv_extend_assignment(&strv, "DBUS_ERROR_NAME", m->error.name);
(void) strv_extend_assignment(&strv, "DBUS_ERROR_MESSAGE", m->error.message);
return TAKE_PTR(strv);
}
+2
View File
@@ -187,3 +187,5 @@ void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m);
sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus);
sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus);
char** bus_message_make_log_fields(sd_bus_message *m);
+4
View File
@@ -2928,6 +2928,7 @@ static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
}
static int process_message(sd_bus *bus, sd_bus_message *m) {
_unused_ _cleanup_(log_context_freep) LogContext *c = NULL;
int r;
assert(bus);
@@ -2936,6 +2937,9 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
bus->current_message = m;
bus->iteration_counter++;
if (log_context_enabled())
c = log_context_new_consume(bus_message_make_log_fields(m));
log_debug_bus_message(m);
r = process_hello(bus, m);
@@ -242,11 +242,15 @@ _public_ int sd_device_monitor_stop(sd_device_monitor *m) {
static int device_monitor_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
_unused_ _cleanup_(log_context_freep) LogContext *c = NULL;
sd_device_monitor *m = ASSERT_PTR(userdata);
if (device_monitor_receive_device(m, &device) <= 0)
return 0;
if (log_context_enabled())
c = log_context_new_consume(device_make_log_fields(device));
if (m->callback)
return m->callback(m, device, m->userdata);
+76
View File
@@ -5,6 +5,7 @@
#include "devnum-util.h"
#include "fd-util.h"
#include "string-util.h"
#include "strv.h"
int devname_from_devnum(mode_t mode, dev_t devnum, char **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
@@ -63,3 +64,78 @@ int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
return TAKE_FD(fd);
}
static int add_string_field(
sd_device *device,
const char *field,
int (*func)(sd_device *dev, const char **s),
char ***strv) {
const char *s;
int r;
assert(device);
assert(field);
assert(func);
assert(strv);
r = func(device, &s);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", field);
if (r >= 0)
(void) strv_extend_assignment(strv, field, s);
return 0;
}
char** device_make_log_fields(sd_device *device) {
_cleanup_strv_free_ char **strv = NULL;
dev_t devnum;
int ifindex;
sd_device_action_t action;
uint64_t seqnum, diskseq;
int r;
assert(device);
(void) add_string_field(device, "SYSPATH", sd_device_get_syspath, &strv);
(void) add_string_field(device, "SUBSYSTEM", sd_device_get_subsystem, &strv);
(void) add_string_field(device, "DEVTYPE", sd_device_get_devtype, &strv);
(void) add_string_field(device, "DRIVER", sd_device_get_driver, &strv);
(void) add_string_field(device, "DEVPATH", sd_device_get_devpath, &strv);
(void) add_string_field(device, "DEVNAME", sd_device_get_devname, &strv);
(void) add_string_field(device, "SYSNAME", sd_device_get_sysname, &strv);
(void) add_string_field(device, "SYSNUM", sd_device_get_sysnum, &strv);
r = sd_device_get_devnum(device, &devnum);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"DEVNUM\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "DEVNUM="DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(devnum));
r = sd_device_get_ifindex(device, &ifindex);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"IFINDEX\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "IFINDEX=%i", ifindex);
r = sd_device_get_action(device, &action);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"ACTION\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "ACTION=%s", device_action_to_string(action));
r = sd_device_get_seqnum(device, &seqnum);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"SEQNUM\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "SEQNUM=%"PRIu64, seqnum);
r = sd_device_get_diskseq(device, &diskseq);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"DISKSEQ\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "DISKSEQ=%"PRIu64, diskseq);
return TAKE_PTR(strv);
}
+2
View File
@@ -99,3 +99,5 @@ static inline int devname_from_stat_rdev(const struct stat *st, char **ret) {
return devname_from_devnum(st->st_mode, st->st_rdev, ret);
}
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret);
char** device_make_log_fields(sd_device *device);