Commit | Line | Data |
---|---|---|
96c1b5fe SM |
1 | /* |
2 | * Copyright EfficiOS, Inc. | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
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 | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_OUTPUT_LEVEL log_level | |
24 | #define BT_LOG_TAG "COMMON/FORMAT-ERROR" | |
25 | #include <logging/log.h> | |
26 | ||
27 | #include "format-error.h" | |
28 | ||
29 | #include <stdint.h> | |
30 | #include <string-format/format-plugin-comp-cls-name.h> | |
31 | ||
32 | gchar *format_bt_error( | |
33 | const bt_error *error, | |
34 | unsigned int columns, | |
35 | bt_logging_level log_level, | |
36 | enum bt_common_color_when use_colors) | |
37 | { | |
38 | GString *str; | |
39 | int64_t i; | |
40 | GString *folded = NULL; | |
41 | gchar *comp_cls_str = NULL; | |
42 | struct bt_common_color_codes codes; | |
43 | ||
44 | BT_ASSERT(error); | |
45 | BT_ASSERT(bt_error_get_cause_count(error) > 0); | |
46 | ||
47 | str = g_string_new(NULL); | |
48 | BT_ASSERT(str); | |
49 | ||
50 | bt_common_color_get_codes(&codes, use_colors); | |
51 | ||
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 "; | |
59 | ||
60 | /* Print prefix */ | |
61 | g_string_append_printf(str, prefix_fmt, | |
62 | codes.bold, codes.fg_bright_red, codes.reset); | |
63 | ||
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", | |
69 | codes.bold, | |
70 | bt_error_cause_get_module_name(cause), | |
71 | codes.reset); | |
72 | break; | |
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), | |
78 | use_colors); | |
79 | BT_ASSERT(comp_cls_str); | |
80 | ||
81 | g_string_append_printf(str, "%s%s%s: %s", | |
82 | codes.bold, | |
83 | bt_error_cause_component_actor_get_component_name(cause), | |
84 | codes.reset, | |
85 | comp_cls_str); | |
86 | ||
87 | break; | |
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), | |
93 | use_colors); | |
94 | BT_ASSERT(comp_cls_str); | |
95 | ||
96 | g_string_append(str, comp_cls_str); | |
97 | break; | |
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), | |
103 | use_colors); | |
104 | BT_ASSERT(comp_cls_str); | |
105 | ||
106 | g_string_append_printf(str, "%s%s%s (%s%s%s): %s", | |
107 | codes.bold, | |
108 | bt_error_cause_message_iterator_actor_get_component_name(cause), | |
109 | codes.reset, | |
110 | codes.bold, | |
111 | bt_error_cause_message_iterator_actor_get_component_output_port_name(cause), | |
112 | codes.reset, | |
113 | comp_cls_str); | |
114 | ||
115 | break; | |
116 | default: | |
117 | bt_common_abort(); | |
118 | } | |
119 | ||
120 | /* Print file name and line number */ | |
121 | g_string_append_printf(str, "] (%s%s%s%s:%s%" PRIu64 "%s)\n", | |
122 | codes.bold, | |
123 | codes.fg_bright_magenta, | |
124 | bt_error_cause_get_file_name(cause), | |
125 | codes.reset, | |
126 | codes.fg_green, | |
127 | bt_error_cause_get_line_number(cause), | |
128 | codes.reset); | |
129 | ||
130 | /* Print message */ | |
131 | folded = bt_common_fold(bt_error_cause_get_message(cause), | |
132 | columns, 2); | |
133 | if (folded) { | |
134 | g_string_append(str, folded->str); | |
135 | g_string_free(folded, TRUE); | |
136 | folded = NULL; | |
137 | } else { | |
138 | BT_LOGE_STR("Could not fold string."); | |
139 | g_string_append(str, bt_error_cause_get_message(cause)); | |
140 | } | |
141 | ||
142 | /* | |
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 | |
145 | * at the end. | |
146 | */ | |
147 | if (i > 0) { | |
148 | g_string_append_c(str, '\n'); | |
149 | } | |
150 | } | |
151 | ||
152 | g_free(comp_cls_str); | |
153 | ||
154 | return g_string_free(str, FALSE); | |
155 | } |