Commit | Line | Data |
---|---|---|
96c1b5fe | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
96c1b5fe | 3 | * |
0235b0db | 4 | * Copyright EfficiOS, Inc. |
96c1b5fe SM |
5 | */ |
6 | ||
71436ae4 | 7 | #define BT_LOG_OUTPUT_LEVEL ((enum bt_log_level) log_level) |
96c1b5fe SM |
8 | #define BT_LOG_TAG "COMMON/FORMAT-ERROR" |
9 | #include <logging/log.h> | |
10 | ||
11 | #include "format-error.h" | |
12 | ||
13 | #include <stdint.h> | |
14 | #include <string-format/format-plugin-comp-cls-name.h> | |
15 | ||
30b828d2 SM |
16 | gchar *format_bt_error_cause( |
17 | const bt_error_cause *error_cause, | |
18 | unsigned int columns, | |
19 | bt_logging_level log_level, | |
20 | enum bt_common_color_when use_colors) | |
21 | { | |
22 | GString *str; | |
23 | gchar *comp_cls_str = NULL; | |
24 | GString *folded = NULL; | |
25 | struct bt_common_color_codes codes; | |
26 | ||
27 | str = g_string_new(NULL); | |
28 | BT_ASSERT(str); | |
29 | ||
30 | bt_common_color_get_codes(&codes, use_colors); | |
31 | ||
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", | |
37 | codes.bold, | |
38 | bt_error_cause_get_module_name(error_cause), | |
39 | codes.reset); | |
40 | break; | |
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), | |
46 | use_colors); | |
47 | BT_ASSERT(comp_cls_str); | |
48 | ||
49 | g_string_append_printf(str, "%s%s%s: %s", | |
50 | codes.bold, | |
51 | bt_error_cause_component_actor_get_component_name(error_cause), | |
52 | codes.reset, | |
53 | comp_cls_str); | |
54 | ||
55 | break; | |
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), | |
61 | use_colors); | |
62 | BT_ASSERT(comp_cls_str); | |
63 | ||
64 | g_string_append(str, comp_cls_str); | |
65 | break; | |
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), | |
71 | use_colors); | |
72 | BT_ASSERT(comp_cls_str); | |
73 | ||
74 | g_string_append_printf(str, "%s%s%s (%s%s%s): %s", | |
75 | codes.bold, | |
76 | bt_error_cause_message_iterator_actor_get_component_name(error_cause), | |
77 | codes.reset, | |
78 | codes.bold, | |
79 | bt_error_cause_message_iterator_actor_get_component_output_port_name(error_cause), | |
80 | codes.reset, | |
81 | comp_cls_str); | |
82 | ||
83 | break; | |
84 | default: | |
85 | bt_common_abort(); | |
86 | } | |
87 | ||
88 | /* Print file name and line number */ | |
89 | g_string_append_printf(str, "] (%s%s%s%s:%s%" PRIu64 "%s)\n", | |
90 | codes.bold, | |
91 | codes.fg_bright_magenta, | |
92 | bt_error_cause_get_file_name(error_cause), | |
93 | codes.reset, | |
94 | codes.fg_green, | |
95 | bt_error_cause_get_line_number(error_cause), | |
96 | codes.reset); | |
97 | ||
98 | /* Print message */ | |
99 | folded = bt_common_fold(bt_error_cause_get_message(error_cause), | |
100 | columns, 2); | |
101 | if (folded) { | |
102 | g_string_append(str, folded->str); | |
103 | g_string_free(folded, TRUE); | |
104 | folded = NULL; | |
105 | } else { | |
106 | BT_LOGE_STR("Could not fold string."); | |
107 | g_string_append(str, bt_error_cause_get_message(error_cause)); | |
108 | } | |
109 | ||
110 | g_free(comp_cls_str); | |
111 | ||
112 | return g_string_free(str, FALSE); | |
113 | } | |
114 | ||
96c1b5fe SM |
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) | |
120 | { | |
121 | GString *str; | |
122 | int64_t i; | |
30b828d2 | 123 | gchar *error_cause_str = NULL; |
96c1b5fe SM |
124 | struct bt_common_color_codes codes; |
125 | ||
126 | BT_ASSERT(error); | |
127 | BT_ASSERT(bt_error_get_cause_count(error) > 0); | |
128 | ||
129 | str = g_string_new(NULL); | |
130 | BT_ASSERT(str); | |
131 | ||
132 | bt_common_color_get_codes(&codes, use_colors); | |
133 | ||
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 "; | |
141 | ||
900eef73 SM |
142 | #pragma GCC diagnostic push |
143 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" | |
96c1b5fe SM |
144 | /* Print prefix */ |
145 | g_string_append_printf(str, prefix_fmt, | |
146 | codes.bold, codes.fg_bright_red, codes.reset); | |
900eef73 | 147 | #pragma GCC diagnostic pop |
96c1b5fe | 148 | |
30b828d2 SM |
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); | |
96c1b5fe | 153 | |
30b828d2 | 154 | g_string_append(str, error_cause_str); |
96c1b5fe SM |
155 | |
156 | /* | |
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 | |
159 | * at the end. | |
160 | */ | |
161 | if (i > 0) { | |
162 | g_string_append_c(str, '\n'); | |
163 | } | |
164 | } | |
165 | ||
30b828d2 | 166 | g_free(error_cause_str); |
96c1b5fe SM |
167 | |
168 | return g_string_free(str, FALSE); | |
169 | } |