From: Simon Marchi Date: Thu, 2 Nov 2023 19:58:28 +0000 (+0000) Subject: lib: add logging format specifier for bt_field_location X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=59e62923c4edf11cea73c64499d836340f829961;p=babeltrace.git lib: add logging format specifier for bt_field_location Add the `format_field_location` function, to format a bt_field_location object in logging messages. Add the format specifier `L`, to format bt_field_location objects in lib-logging format strings. Change-Id: Ia874bea6a110e051cc19a0250e5cd831888f849f Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/7317 Tested-by: jenkins Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/7296 --- diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index 171fcb14..9225518b 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -631,6 +631,10 @@ The available format specifiers are: |Trace IR field path |`+const struct bt_field_path *+` +|`L` +|Trace IR field location +|`+const struct bt_field_location *+` + |`E` |Trace IR event class |`+const struct bt_event_class *+` diff --git a/src/common/common.h b/src/common/common.h index 63b01cc9..39a4fd77 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -461,7 +461,7 @@ const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_ } static inline -const char *bt_common_scope_string(enum bt_field_path_scope scope) +const char *bt_common_field_path_scope_string(enum bt_field_path_scope scope) { switch (scope) { case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: @@ -477,6 +477,23 @@ const char *bt_common_scope_string(enum bt_field_path_scope scope) bt_common_abort(); } +static inline +const char *bt_common_field_location_scope_string(enum bt_field_location_scope scope) +{ + switch (scope) { + case BT_FIELD_LOCATION_SCOPE_PACKET_CONTEXT: + return "PACKET_CONTEXT"; + case BT_FIELD_LOCATION_SCOPE_EVENT_COMMON_CONTEXT: + return "EVENT_COMMON_CONTEXT"; + case BT_FIELD_LOCATION_SCOPE_EVENT_SPECIFIC_CONTEXT: + return "EVENT_SPECIFIC_CONTEXT"; + case BT_FIELD_LOCATION_SCOPE_EVENT_PAYLOAD: + return "EVENT_PAYLOAD"; + } + + bt_common_abort(); +} + static inline const char *bt_common_event_class_log_level_string( enum bt_event_class_log_level level) @@ -544,6 +561,44 @@ const char *bt_common_value_type_string(enum bt_value_type type) bt_common_abort(); }; +static inline +GString *bt_common_field_path_string(struct bt_field_path *path) +{ + GString *str = g_string_new(NULL); + uint64_t i; + + BT_ASSERT_DBG(path); + + if (!str) { + goto end; + } + + g_string_append_printf(str, "[%s", bt_common_field_path_scope_string( + bt_field_path_get_root_scope(path))); + + for (i = 0; i < bt_field_path_get_item_count(path); i++) { + const struct bt_field_path_item *fp_item = + bt_field_path_borrow_item_by_index_const(path, i); + + switch (bt_field_path_item_get_type(fp_item)) { + case BT_FIELD_PATH_ITEM_TYPE_INDEX: + g_string_append_printf(str, ", %" PRIu64, + bt_field_path_item_index_get_index(fp_item)); + break; + case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT: + g_string_append(str, ", "); + break; + default: + bt_common_abort(); + } + } + + g_string_append(str, "]"); + +end: + return str; +} + static inline const char *bt_common_logging_level_string( enum bt_logging_level level) diff --git a/src/lib/lib-logging.c b/src/lib/lib-logging.c index 6d98a0ce..9de07800 100644 --- a/src/lib/lib-logging.c +++ b/src/lib/lib-logging.c @@ -44,6 +44,7 @@ #include "trace-ir/event.h" #include "trace-ir/field-class.h" #include "trace-ir/field.h" +#include "trace-ir/field-location.h" #include "trace-ir/field-path.h" #include "trace-ir/packet.h" #include "trace-ir/stream-class.h" @@ -108,6 +109,9 @@ static inline void format_clock_snapshot(char **buf_ch, bool extended, static inline void format_field_path(char **buf_ch, bool extended, const char *prefix, const struct bt_field_path *field_path); +static inline void format_field_location(char **buf_ch, bool extended , + const char *prefix, const struct bt_field_location *field_location); + static inline void format_object(char **buf_ch, const char *prefix, const struct bt_object *obj) { @@ -452,7 +456,7 @@ static inline void format_field_path(char **buf_ch, bool extended, } BUF_APPEND(", %spath=[%s", - PRFIELD(bt_common_scope_string(field_path->root))); + PRFIELD(bt_common_field_path_scope_string(field_path->root))); for (i = 0; i < bt_field_path_get_item_count(field_path); i++) { const struct bt_field_path_item *fp_item = @@ -474,6 +478,33 @@ static inline void format_field_path(char **buf_ch, bool extended, BUF_APPEND("%s", "]"); } +static inline void format_field_location(char **buf_ch, bool extended, + const char *prefix, const struct bt_field_location *field_location) +{ + uint64_t i; + + BT_ASSERT(field_location->items); + + BUF_APPEND(", %sitem-count=%u", PRFIELD(field_location->items->len)); + + if (!extended) { + return; + } + + BUF_APPEND(", %sloc=[%s", + PRFIELD(bt_common_field_location_scope_string(field_location->scope))); + + for (i = 0; i < bt_field_location_get_item_count(field_location); i++) { + const char *item = + bt_field_location_get_item_by_index (field_location, i); + + BUF_APPEND(", %s", item); + break; + } + + BUF_APPEND("%s", "]"); +} + static inline void format_trace_class(char **buf_ch, bool extended, const char *prefix, const struct bt_trace_class *trace_class) { @@ -1385,6 +1416,9 @@ static inline void handle_conversion_specifier_bt( case 'P': format_field_path(buf_ch, extended, prefix, obj); break; + case 'L': + format_field_location(buf_ch, extended, prefix, obj); + break; case 'E': format_event_class(buf_ch, extended, prefix, obj); break;