string-format: introduce function to format a bt_error_cause
[babeltrace.git] / src / string-format / format-error.c
CommitLineData
baa9c5f0
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
2efcc711
SM
32gchar *format_bt_error_cause(
33 const bt_error_cause *error_cause,
34 unsigned int columns,
35 bt_logging_level log_level,
36 enum bt_common_color_when use_colors)
37{
38 GString *str;
39 gchar *comp_cls_str = NULL;
40 GString *folded = NULL;
41 struct bt_common_color_codes codes;
42
43 str = g_string_new(NULL);
44 BT_ASSERT(str);
45
46 bt_common_color_get_codes(&codes, use_colors);
47
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",
53 codes.bold,
54 bt_error_cause_get_module_name(error_cause),
55 codes.reset);
56 break;
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),
62 use_colors);
63 BT_ASSERT(comp_cls_str);
64
65 g_string_append_printf(str, "%s%s%s: %s",
66 codes.bold,
67 bt_error_cause_component_actor_get_component_name(error_cause),
68 codes.reset,
69 comp_cls_str);
70
71 break;
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),
77 use_colors);
78 BT_ASSERT(comp_cls_str);
79
80 g_string_append(str, comp_cls_str);
81 break;
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),
87 use_colors);
88 BT_ASSERT(comp_cls_str);
89
90 g_string_append_printf(str, "%s%s%s (%s%s%s): %s",
91 codes.bold,
92 bt_error_cause_message_iterator_actor_get_component_name(error_cause),
93 codes.reset,
94 codes.bold,
95 bt_error_cause_message_iterator_actor_get_component_output_port_name(error_cause),
96 codes.reset,
97 comp_cls_str);
98
99 break;
100 default:
101 bt_common_abort();
102 }
103
104 /* Print file name and line number */
105 g_string_append_printf(str, "] (%s%s%s%s:%s%" PRIu64 "%s)\n",
106 codes.bold,
107 codes.fg_bright_magenta,
108 bt_error_cause_get_file_name(error_cause),
109 codes.reset,
110 codes.fg_green,
111 bt_error_cause_get_line_number(error_cause),
112 codes.reset);
113
114 /* Print message */
115 folded = bt_common_fold(bt_error_cause_get_message(error_cause),
116 columns, 2);
117 if (folded) {
118 g_string_append(str, folded->str);
119 g_string_free(folded, TRUE);
120 folded = NULL;
121 } else {
122 BT_LOGE_STR("Could not fold string.");
123 g_string_append(str, bt_error_cause_get_message(error_cause));
124 }
125
126 g_free(comp_cls_str);
127
128 return g_string_free(str, FALSE);
129}
130
baa9c5f0
SM
131gchar *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)
136{
137 GString *str;
138 int64_t i;
2efcc711 139 gchar *error_cause_str = NULL;
baa9c5f0
SM
140 struct bt_common_color_codes codes;
141
142 BT_ASSERT(error);
143 BT_ASSERT(bt_error_get_cause_count(error) > 0);
144
145 str = g_string_new(NULL);
146 BT_ASSERT(str);
147
148 bt_common_color_get_codes(&codes, use_colors);
149
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 ";
157
158 /* Print prefix */
159 g_string_append_printf(str, prefix_fmt,
160 codes.bold, codes.fg_bright_red, codes.reset);
161
2efcc711
SM
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);
baa9c5f0 166
2efcc711 167 g_string_append(str, error_cause_str);
baa9c5f0
SM
168
169 /*
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
172 * at the end.
173 */
174 if (i > 0) {
175 g_string_append_c(str, '\n');
176 }
177 }
178
2efcc711 179 g_free(error_cause_str);
baa9c5f0
SM
180
181 return g_string_free(str, FALSE);
182}
This page took 0.030021 seconds and 4 git commands to generate.