2 * Copyright EfficiOS, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_OUTPUT_LEVEL log_level
24 #define BT_LOG_TAG "COMMON/FORMAT-ERROR"
25 #include <logging/log.h>
27 #include "format-error.h"
30 #include <string-format/format-plugin-comp-cls-name.h>
32 gchar
*format_bt_error(
33 const bt_error
*error
,
35 bt_logging_level log_level
,
36 enum bt_common_color_when use_colors
)
40 GString
*folded
= NULL
;
41 gchar
*comp_cls_str
= NULL
;
42 struct bt_common_color_codes codes
;
45 BT_ASSERT(bt_error_get_cause_count(error
) > 0);
47 str
= g_string_new(NULL
);
50 bt_common_color_get_codes(&codes
, use_colors
);
52 /* Reverse order: deepest (root) cause printed at the end */
53 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
54 const bt_error_cause
*cause
=
55 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
56 const char *prefix_fmt
=
57 i
== bt_error_get_cause_count(error
) - 1 ?
58 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
61 g_string_append_printf(str
, prefix_fmt
,
62 codes
.bold
, codes
.fg_bright_red
, codes
.reset
);
64 /* Print actor name */
65 g_string_append_c(str
, '[');
66 switch (bt_error_cause_get_actor_type(cause
)) {
67 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
68 g_string_append_printf(str
, "%s%s%s",
70 bt_error_cause_get_module_name(cause
),
73 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
74 comp_cls_str
= format_plugin_comp_cls_opt(
75 bt_error_cause_component_actor_get_plugin_name(cause
),
76 bt_error_cause_component_actor_get_component_class_name(cause
),
77 bt_error_cause_component_actor_get_component_class_type(cause
),
79 BT_ASSERT(comp_cls_str
);
81 g_string_append_printf(str
, "%s%s%s: %s",
83 bt_error_cause_component_actor_get_component_name(cause
),
88 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
89 comp_cls_str
= format_plugin_comp_cls_opt(
90 bt_error_cause_component_class_actor_get_plugin_name(cause
),
91 bt_error_cause_component_class_actor_get_component_class_name(cause
),
92 bt_error_cause_component_class_actor_get_component_class_type(cause
),
94 BT_ASSERT(comp_cls_str
);
96 g_string_append(str
, comp_cls_str
);
98 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
99 comp_cls_str
= format_plugin_comp_cls_opt(
100 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
101 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
102 bt_error_cause_message_iterator_actor_get_component_class_type(cause
),
104 BT_ASSERT(comp_cls_str
);
106 g_string_append_printf(str
, "%s%s%s (%s%s%s): %s",
108 bt_error_cause_message_iterator_actor_get_component_name(cause
),
111 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
120 /* Print file name and line number */
121 g_string_append_printf(str
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
123 codes
.fg_bright_magenta
,
124 bt_error_cause_get_file_name(cause
),
127 bt_error_cause_get_line_number(cause
),
131 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
134 g_string_append(str
, folded
->str
);
135 g_string_free(folded
, TRUE
);
138 BT_LOGE_STR("Could not fold string.");
139 g_string_append(str
, bt_error_cause_get_message(cause
));
143 * Don't append a newline at the end, since that is used to
144 * generate the Python __str__, which doesn't need a newline
148 g_string_append_c(str
, '\n');
152 g_free(comp_cls_str
);
154 return g_string_free(str
, FALSE
);