string-format: introduce function to format component class name
[babeltrace.git] / src / string-format / format-plugin-comp-cls-name.c
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-PLUGIN-COMP-CLS-NAME"
25 #include <logging/log.h>
26
27 #include "format-plugin-comp-cls-name.h"
28
29 #include <common/common.h>
30
31
32 static
33 const char *component_type_str(bt_component_class_type type)
34 {
35 switch (type) {
36 case BT_COMPONENT_CLASS_TYPE_SOURCE:
37 return "source";
38 case BT_COMPONENT_CLASS_TYPE_SINK:
39 return "sink";
40 case BT_COMPONENT_CLASS_TYPE_FILTER:
41 return "filter";
42 default:
43 return "(unknown)";
44 }
45 }
46
47 gchar *format_plugin_comp_cls_opt(const char *plugin_name,
48 const char *comp_cls_name, bt_component_class_type type,
49 enum bt_common_color_when use_colors)
50 {
51 GString *str;
52 GString *shell_plugin_name = NULL;
53 GString *shell_comp_cls_name = NULL;
54 gchar *ret;
55 struct bt_common_color_codes codes;
56
57 str = g_string_new(NULL);
58 if (!str) {
59 goto end;
60 }
61
62 if (plugin_name) {
63 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
64 if (!shell_plugin_name) {
65 goto end;
66 }
67 }
68
69 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
70 if (!shell_comp_cls_name) {
71 goto end;
72 }
73
74 bt_common_color_get_codes(&codes, use_colors);
75
76 g_string_append_printf(str, "'%s%s%s%s",
77 codes.bold,
78 codes.fg_bright_cyan,
79 component_type_str(type),
80 codes.fg_default);
81
82 if (shell_plugin_name) {
83 g_string_append_printf(str, ".%s%s%s",
84 codes.fg_blue,
85 shell_plugin_name->str,
86 codes.fg_default);
87 }
88
89 g_string_append_printf(str, ".%s%s%s'",
90 codes.fg_yellow,
91 shell_comp_cls_name->str,
92 codes.reset);
93
94 end:
95 if (shell_plugin_name) {
96 g_string_free(shell_plugin_name, TRUE);
97 }
98
99 if (shell_comp_cls_name) {
100 g_string_free(shell_comp_cls_name, TRUE);
101 }
102
103 if (str) {
104 ret = g_string_free(str, FALSE);
105 } else {
106 ret = NULL;
107 }
108
109 return ret;
110 }
This page took 0.031989 seconds and 5 git commands to generate.