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_cause(
33 const bt_error_cause
*error_cause
,
35 bt_logging_level log_level
,
36 enum bt_common_color_when use_colors
)
39 gchar
*comp_cls_str
= NULL
;
40 GString
*folded
= NULL
;
41 struct bt_common_color_codes codes
;
43 str
= g_string_new(NULL
);
46 bt_common_color_get_codes(&codes
, use_colors
);
48 /* Print actor name */
49 g_string_append_c(str
, '[');
50 switch (bt_error_cause_get_actor_type(error_cause
)) {
51 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
52 g_string_append_printf(str
, "%s%s%s",
54 bt_error_cause_get_module_name(error_cause
),
57 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
58 comp_cls_str
= format_plugin_comp_cls_opt(
59 bt_error_cause_component_actor_get_plugin_name(error_cause
),
60 bt_error_cause_component_actor_get_component_class_name(error_cause
),
61 bt_error_cause_component_actor_get_component_class_type(error_cause
),
63 BT_ASSERT(comp_cls_str
);
65 g_string_append_printf(str
, "%s%s%s: %s",
67 bt_error_cause_component_actor_get_component_name(error_cause
),
72 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
73 comp_cls_str
= format_plugin_comp_cls_opt(
74 bt_error_cause_component_class_actor_get_plugin_name(error_cause
),
75 bt_error_cause_component_class_actor_get_component_class_name(error_cause
),
76 bt_error_cause_component_class_actor_get_component_class_type(error_cause
),
78 BT_ASSERT(comp_cls_str
);
80 g_string_append(str
, comp_cls_str
);
82 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
83 comp_cls_str
= format_plugin_comp_cls_opt(
84 bt_error_cause_message_iterator_actor_get_plugin_name(error_cause
),
85 bt_error_cause_message_iterator_actor_get_component_class_name(error_cause
),
86 bt_error_cause_message_iterator_actor_get_component_class_type(error_cause
),
88 BT_ASSERT(comp_cls_str
);
90 g_string_append_printf(str
, "%s%s%s (%s%s%s): %s",
92 bt_error_cause_message_iterator_actor_get_component_name(error_cause
),
95 bt_error_cause_message_iterator_actor_get_component_output_port_name(error_cause
),
104 /* Print file name and line number */
105 g_string_append_printf(str
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
107 codes
.fg_bright_magenta
,
108 bt_error_cause_get_file_name(error_cause
),
111 bt_error_cause_get_line_number(error_cause
),
115 folded
= bt_common_fold(bt_error_cause_get_message(error_cause
),
118 g_string_append(str
, folded
->str
);
119 g_string_free(folded
, TRUE
);
122 BT_LOGE_STR("Could not fold string.");
123 g_string_append(str
, bt_error_cause_get_message(error_cause
));
126 g_free(comp_cls_str
);
128 return g_string_free(str
, FALSE
);
131 gchar
*format_bt_error(
132 const bt_error
*error
,
133 unsigned int columns
,
134 bt_logging_level log_level
,
135 enum bt_common_color_when use_colors
)
139 gchar
*error_cause_str
= NULL
;
140 struct bt_common_color_codes codes
;
143 BT_ASSERT(bt_error_get_cause_count(error
) > 0);
145 str
= g_string_new(NULL
);
148 bt_common_color_get_codes(&codes
, use_colors
);
150 /* Reverse order: deepest (root) cause printed at the end */
151 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
152 const bt_error_cause
*cause
=
153 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
154 const char *prefix_fmt
=
155 i
== bt_error_get_cause_count(error
) - 1 ?
156 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
159 g_string_append_printf(str
, prefix_fmt
,
160 codes
.bold
, codes
.fg_bright_red
, codes
.reset
);
162 g_free(error_cause_str
);
163 error_cause_str
= format_bt_error_cause(cause
, columns
,
164 log_level
, use_colors
);
165 BT_ASSERT(error_cause_str
);
167 g_string_append(str
, error_cause_str
);
170 * Don't append a newline at the end, since that is used to
171 * generate the Python __str__, which doesn't need a newline
175 g_string_append_c(str
, '\n');
179 g_free(error_cause_str
);
181 return g_string_free(str
, FALSE
);