#include <assert.h>
#include <glib.h>
#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/common-internal.h>
#define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
#define HOME_ENV_VAR "HOME"
return ret;
}
-static bool supports_colors(void)
+BT_HIDDEN
+bool bt_common_colors_supported(void)
{
static bool supports_colors = false;
static bool supports_colors_set = false;
BT_HIDDEN
const char *bt_common_color_reset(void)
{
- return supports_colors() ? "\033[0m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
}
BT_HIDDEN
const char *bt_common_color_bold(void)
{
- return supports_colors() ? "\033[1m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
}
BT_HIDDEN
const char *bt_common_color_fg_default(void)
{
- return supports_colors() ? "\033[39m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
}
BT_HIDDEN
const char *bt_common_color_fg_red(void)
{
- return supports_colors() ? "\033[31m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
}
BT_HIDDEN
const char *bt_common_color_fg_green(void)
{
- return supports_colors() ? "\033[32m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
}
BT_HIDDEN
const char *bt_common_color_fg_yellow(void)
{
- return supports_colors() ? "\033[33m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
}
BT_HIDDEN
const char *bt_common_color_fg_blue(void)
{
- return supports_colors() ? "\033[34m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
}
BT_HIDDEN
const char *bt_common_color_fg_magenta(void)
{
- return supports_colors() ? "\033[35m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
}
BT_HIDDEN
const char *bt_common_color_fg_cyan(void)
{
- return supports_colors() ? "\033[36m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
}
BT_HIDDEN
const char *bt_common_color_fg_light_gray(void)
{
- return supports_colors() ? "\033[37m" : "";
+ return bt_common_colors_supported() ?
+ BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
}
BT_HIDDEN
const char *bt_common_color_bg_default(void)
{
- return supports_colors() ? "\033[49m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
}
BT_HIDDEN
const char *bt_common_color_bg_red(void)
{
- return supports_colors() ? "\033[41m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
}
BT_HIDDEN
const char *bt_common_color_bg_green(void)
{
- return supports_colors() ? "\033[42m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
}
BT_HIDDEN
const char *bt_common_color_bg_yellow(void)
{
- return supports_colors() ? "\033[43m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
}
BT_HIDDEN
const char *bt_common_color_bg_blue(void)
{
- return supports_colors() ? "\033[44m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
}
BT_HIDDEN
const char *bt_common_color_bg_magenta(void)
{
- return supports_colors() ? "\033[45m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
}
BT_HIDDEN
const char *bt_common_color_bg_cyan(void)
{
- return supports_colors() ? "\033[46m" : "";
+ return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
}
BT_HIDDEN
const char *bt_common_color_bg_light_gray(void)
{
- return supports_colors() ? "\033[47m" : "";
+ return bt_common_colors_supported() ?
+ BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
}
#include <babeltrace/ctf-ir/fields.h>
#include <babeltrace/ctf-ir/trace.h>
#include <babeltrace/bitfield.h>
+#include <babeltrace/common-internal.h>
#include <inttypes.h>
#include "text.h"
#define NSEC_PER_SEC 1000000000LL
+#define COLOR_NAME BT_COMMON_COLOR_BOLD
+#define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
+#define COLOR_RST BT_COMMON_COLOR_RESET
+#define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_GREEN
+#define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
+#define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_CYAN
+#define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
+#define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
+#define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
+
static inline
const char *rem_(const char *str)
{
enum bt_component_status print_field(struct text_component *text,
struct bt_ctf_field *field, bool print_names);
+static
+void print_name_equal(struct text_component *text, const char *name)
+{
+ if (text->use_colors) {
+ fprintf(text->out, "%s%s%s = ", COLOR_NAME, name, COLOR_RST);
+ } else {
+ fputs(name, text->out);
+ }
+}
+
+static
+void print_field_name_equal(struct text_component *text, const char *name)
+{
+ if (text->use_colors) {
+ fprintf(text->out, "%s%s%s = ", COLOR_FIELD_NAME, name,
+ COLOR_RST);
+ } else {
+ fputs(name, text->out);
+ }
+}
+
static
void print_timestamp_cycles(struct text_component *text,
struct bt_ctf_clock_class *clock_class,
goto end;
}
- fputs(print_names ? "timestamp = " : "[", out);
+ if (print_names) {
+ print_name_equal(text, "timestamp");
+ } else {
+ fputs("[", out);
+ }
+ if (text->use_colors) {
+ fputs(COLOR_TIMESTAMP, text->out);
+ }
if (text->options.print_timestamp_cycles) {
print_timestamp_cycles(text, clock_class, event);
} else {
print_timestamp_wall(text, clock_class, event);
}
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
if (!print_names)
fputs("] ", out);
if (text->options.print_delta_field) {
- if (print_names)
- fputs(", delta = ", text->out);
- else
+ if (print_names) {
+ fputs(", ", text->out);
+ print_name_equal(text, "delta");
+ } else {
fputs("(", text->out);
+ }
if (text->options.print_timestamp_cycles) {
if (text->delta_cycles == -1ULL) {
fputs("+??????????\?\?) ", text->out); /* Not a trigraph. */
}
text->start_line = false;
if (print_names) {
- fputs("trace = ", text->out);
+ print_name_equal(text, "trace");
}
fprintf(text->out, "%s", name);
}
}
text->start_line = false;
if (print_names) {
- fputs("trace:hostname = ", text->out);
+ print_name_equal(text, "trace:hostname");
}
if (bt_value_string_get(hostname_str, &str)
== BT_VALUE_STATUS_OK) {
}
text->start_line = false;
if (print_names) {
- fputs("trace:domain = ", text->out);
+ print_name_equal(text, "trace:domain");
}
if (bt_value_string_get(domain_str, &str)
== BT_VALUE_STATUS_OK) {
}
text->start_line = false;
if (print_names) {
- fputs("trace:procname = ", text->out);
+ print_name_equal(text, "trace:procname");
}
if (bt_value_string_get(procname_str, &str)
== BT_VALUE_STATUS_OK) {
}
text->start_line = false;
if (print_names) {
- fputs("trace:vpid = ", text->out);
+ print_name_equal(text, "trace:vpid");
}
if (bt_value_integer_get(vpid_value, &value)
== BT_VALUE_STATUS_OK) {
}
text->start_line = false;
if (print_names) {
- fputs("loglevel = ", text->out);
+ print_name_equal(text, "loglevel");
}
if (loglevel_str) {
const char *str;
}
text->start_line = false;
if (print_names) {
- fputs("model.emf.uri = ", text->out);
+ print_name_equal(text, "model.emf.uri");
}
if (uri_str) {
const char *str;
}
text->start_line = false;
if (print_names) {
- fputs("name = ", text->out);
+ print_name_equal(text, "name");
+ }
+ if (text->use_colors) {
+ fputs(COLOR_EVENT_NAME, text->out);
}
fputs(bt_ctf_event_class_get_name(event_class), text->out);
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
end:
bt_put(trace_class);
bt_put(stream_class);
uint64_t u;
int64_t s;
} v;
+ bool rst_color = false;
field_type = bt_ctf_field_get_type(field);
if (!field_type) {
goto end;
}
+ if (text->use_colors) {
+ fputs(COLOR_NUMBER_VALUE, text->out);
+ rst_color = true;
+ }
+
base = bt_ctf_field_type_integer_get_base(field_type);
switch (base) {
case BT_CTF_INTEGER_BASE_BINARY:
goto end;
}
end:
+ if (rst_color) {
+ fputs(COLOR_RST, text->out);
+ }
bt_put(field_type);
return ret;
}
}
if (nr_mappings++)
fprintf(text->out, ", ");
+ if (text->use_colors) {
+ fputs(COLOR_ENUM_MAPPING_NAME, text->out);
+ }
+ // TODO: escape string
fprintf(text->out, "\"%s\"", mapping_name);
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
break;
}
}
if (!nr_mappings) {
+ if (text->use_colors) {
+ fputs(COLOR_UNKNOWN, text->out);
+ }
fprintf(text->out, "<unknown>");
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
}
fprintf(text->out, " : container = ");
ret = print_integer(text, container_field);
fprintf(text->out, " ");
}
if (print_names) {
- fprintf(text->out, "%s = ", rem_(field_name));
+ print_field_name_equal(text, rem_(field_name));
}
ret = print_field(text, field, print_names);
end:
text->depth--;
if (is_string) {
+ if (text->use_colors) {
+ fputs(COLOR_STRING_VALUE, text->out);
+ }
+ // TODO: escape string
fprintf(text->out, "\"%s\"", text->string->str);
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
} else {
fprintf(text->out, " ]");
}
text->depth--;
if (is_string) {
+ if (text->use_colors) {
+ fputs(COLOR_STRING_VALUE, text->out);
+ }
+ // TODO: escape string
fprintf(text->out, "\"%s\"", text->string->str);
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
} else {
fprintf(text->out, " ]");
}
ret = BT_COMPONENT_STATUS_ERROR;
goto end;
}
- fprintf(text->out, "%s = ", rem_(tag_choice));
+ print_field_name_equal(text, rem_(tag_choice));
bt_put(tag_field);
bt_put(iter);
}
if (bt_ctf_field_floating_point_get_value(field, &v)) {
return BT_COMPONENT_STATUS_ERROR;
}
+ if (text->use_colors) {
+ fputs(COLOR_NUMBER_VALUE, text->out);
+ }
fprintf(text->out, "%g", v);
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
return BT_COMPONENT_STATUS_OK;
}
case CTF_TYPE_ENUM:
return print_enum(text, field);
case CTF_TYPE_STRING:
- fprintf(text->out, "\"%s\"", bt_ctf_field_string_get_value(field));
+ if (text->use_colors) {
+ fputs(COLOR_STRING_VALUE, text->out);
+ }
+ // TODO: escape the string value
+ fprintf(text->out, "\"%s\"",
+ bt_ctf_field_string_get_value(field));
+ if (text->use_colors) {
+ fputs(COLOR_RST, text->out);
+ }
return BT_COMPONENT_STATUS_OK;
case CTF_TYPE_STRUCT:
return print_struct(text, field, print_names);
}
text->start_line = false;
if (text->options.print_scope_field_names) {
- fputs("stream.packet.context = ", text->out);
+ print_name_equal(text, "stream.packet.context");
}
ret = print_field(text, main_field,
text->options.print_context_field_names);
}
text->start_line = false;
if (text->options.print_scope_field_names) {
- fputs("stream.event.header = ", text->out);
+ print_name_equal(text, "stream.event.header");
}
ret = print_field(text, main_field,
text->options.print_header_field_names);
}
text->start_line = false;
if (text->options.print_scope_field_names) {
- fputs("stream.event.context = ", text->out);
+ print_name_equal(text, "stream.event.context");
}
ret = print_field(text, main_field,
text->options.print_context_field_names);
}
text->start_line = false;
if (text->options.print_scope_field_names) {
- fputs("event.context = ", text->out);
+ print_name_equal(text, "event.context");
}
ret = print_field(text, main_field,
text->options.print_context_field_names);
}
text->start_line = false;
if (text->options.print_scope_field_names) {
- fputs("event.fields = ", text->out);
+ print_name_equal(text, "event.fields");
}
ret = print_field(text, main_field,
text->options.print_payload_field_names);
#include <babeltrace/component/notification/event.h>
#include <babeltrace/values.h>
#include <babeltrace/compiler.h>
+#include <babeltrace/common-internal.h>
#include <plugins-common.h>
#include <stdio.h>
#include <stdbool.h>
static
const char *plugin_options[] = {
+ "color",
"output-path",
"debug-info-dir",
"debug-info-target-prefix",
return ret;
}
+static
+void warn_wrong_color_param(struct text_component *text)
+{
+ fprintf(text->err,
+ "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
+}
+
static
enum bt_component_status apply_params(struct text_component *text,
struct bt_value *params)
goto end;
}
/* Known parameters. */
+ text->options.color = TEXT_COLOR_OPT_AUTO;
+ if (bt_value_map_has_key(params, "color")) {
+ struct bt_value *color_value;
+ const char *color;
+
+ color_value = bt_value_map_get(params, "color");
+ if (!color_value) {
+ goto end;
+ }
+
+ ret = bt_value_string_get(color_value, &color);
+ if (ret) {
+ warn_wrong_color_param(text);
+ } else {
+ if (strcmp(color, "never") == 0) {
+ text->options.color = TEXT_COLOR_OPT_NEVER;
+ } else if (strcmp(color, "auto") == 0) {
+ text->options.color = TEXT_COLOR_OPT_AUTO;
+ } else if (strcmp(color, "always") == 0) {
+ text->options.color = TEXT_COLOR_OPT_ALWAYS;
+ } else {
+ warn_wrong_color_param(text);
+ }
+ }
+
+ bt_put(color_value);
+ }
+
ret = apply_one_string("output-path",
params,
&text->options.output_path);
return ret;
}
+static
+void set_use_colors(struct text_component *text)
+{
+ switch (text->options.color) {
+ case TEXT_COLOR_OPT_ALWAYS:
+ text->use_colors = true;
+ break;
+ case TEXT_COLOR_OPT_AUTO:
+ text->use_colors = text->out == stdout &&
+ bt_common_colors_supported();
+ break;
+ case TEXT_COLOR_OPT_NEVER:
+ text->use_colors = false;
+ break;
+ }
+}
+
static
enum bt_component_status text_component_init(
struct bt_component *component, struct bt_value *params,
goto error;
}
+ set_use_colors(text);
+
ret = bt_component_set_private_data(component, text);
if (ret != BT_COMPONENT_STATUS_OK) {
goto error;