2 * SPDX-License-Identifier: MIT
4 * Copyright EfficiOS, Inc.
7 #define BT_LOG_OUTPUT_LEVEL ((enum bt_log_level) log_level)
8 #define BT_LOG_TAG "COMMON/FORMAT-ERROR"
9 #include <logging/log.h>
11 #include "format-error.h"
14 #include <string-format/format-plugin-comp-cls-name.h>
16 gchar
*format_bt_error_cause(
17 const bt_error_cause
*error_cause
,
19 bt_logging_level log_level
,
20 enum bt_common_color_when use_colors
)
23 gchar
*comp_cls_str
= NULL
;
24 GString
*folded
= NULL
;
25 struct bt_common_color_codes codes
;
27 str
= g_string_new(NULL
);
30 bt_common_color_get_codes(&codes
, use_colors
);
32 /* Print actor name */
33 g_string_append_c(str
, '[');
34 switch (bt_error_cause_get_actor_type(error_cause
)) {
35 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
36 g_string_append_printf(str
, "%s%s%s",
38 bt_error_cause_get_module_name(error_cause
),
41 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
42 comp_cls_str
= format_plugin_comp_cls_opt(
43 bt_error_cause_component_actor_get_plugin_name(error_cause
),
44 bt_error_cause_component_actor_get_component_class_name(error_cause
),
45 bt_error_cause_component_actor_get_component_class_type(error_cause
),
47 BT_ASSERT(comp_cls_str
);
49 g_string_append_printf(str
, "%s%s%s: %s",
51 bt_error_cause_component_actor_get_component_name(error_cause
),
56 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
57 comp_cls_str
= format_plugin_comp_cls_opt(
58 bt_error_cause_component_class_actor_get_plugin_name(error_cause
),
59 bt_error_cause_component_class_actor_get_component_class_name(error_cause
),
60 bt_error_cause_component_class_actor_get_component_class_type(error_cause
),
62 BT_ASSERT(comp_cls_str
);
64 g_string_append(str
, comp_cls_str
);
66 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
67 comp_cls_str
= format_plugin_comp_cls_opt(
68 bt_error_cause_message_iterator_actor_get_plugin_name(error_cause
),
69 bt_error_cause_message_iterator_actor_get_component_class_name(error_cause
),
70 bt_error_cause_message_iterator_actor_get_component_class_type(error_cause
),
72 BT_ASSERT(comp_cls_str
);
74 g_string_append_printf(str
, "%s%s%s (%s%s%s): %s",
76 bt_error_cause_message_iterator_actor_get_component_name(error_cause
),
79 bt_error_cause_message_iterator_actor_get_component_output_port_name(error_cause
),
88 /* Print file name and line number */
89 g_string_append_printf(str
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
91 codes
.fg_bright_magenta
,
92 bt_error_cause_get_file_name(error_cause
),
95 bt_error_cause_get_line_number(error_cause
),
99 folded
= bt_common_fold(bt_error_cause_get_message(error_cause
),
102 g_string_append(str
, folded
->str
);
103 g_string_free(folded
, TRUE
);
106 BT_LOGE_STR("Could not fold string.");
107 g_string_append(str
, bt_error_cause_get_message(error_cause
));
110 g_free(comp_cls_str
);
112 return g_string_free(str
, FALSE
);
115 gchar
*format_bt_error(
116 const bt_error
*error
,
117 unsigned int columns
,
118 bt_logging_level log_level
,
119 enum bt_common_color_when use_colors
)
123 gchar
*error_cause_str
= NULL
;
124 struct bt_common_color_codes codes
;
127 BT_ASSERT(bt_error_get_cause_count(error
) > 0);
129 str
= g_string_new(NULL
);
132 bt_common_color_get_codes(&codes
, use_colors
);
134 /* Reverse order: deepest (root) cause printed at the end */
135 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
136 const bt_error_cause
*cause
=
137 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
138 const char *prefix_fmt
=
139 i
== bt_error_get_cause_count(error
) - 1 ?
140 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
142 #pragma GCC diagnostic push
143 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
145 g_string_append_printf(str
, prefix_fmt
,
146 codes
.bold
, codes
.fg_bright_red
, codes
.reset
);
147 #pragma GCC diagnostic pop
149 g_free(error_cause_str
);
150 error_cause_str
= format_bt_error_cause(cause
, columns
,
151 log_level
, use_colors
);
152 BT_ASSERT(error_cause_str
);
154 g_string_append(str
, error_cause_str
);
157 * Don't append a newline at the end, since that is used to
158 * generate the Python __str__, which doesn't need a newline
162 g_string_append_c(str
, '\n');
166 g_free(error_cause_str
);
168 return g_string_free(str
, FALSE
);