From: Jérémie Galarneau Date: Fri, 14 Dec 2018 02:24:35 +0000 (-0500) Subject: Print UTF-8 SI suffix only when allowed by the locale X-Git-Tag: v2.12.0-rc1~681 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=2a1135fa25ad1fcad3b395e795aa6d41afc90c54;p=lttng-tools.git Print UTF-8 SI suffix only when allowed by the locale Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng-sessiond/timer.c b/src/bin/lttng-sessiond/timer.c index 991576765..c65df5c2d 100644 --- a/src/bin/lttng-sessiond/timer.c +++ b/src/bin/lttng-sessiond/timer.c @@ -279,8 +279,8 @@ int timer_session_rotation_schedule_timer_start(struct ltt_session *session, ret = -1; goto end; } - DBG("Enabling scheduled rotation timer on session \"%s\" (%ui µs)", session->name, - interval_us); + DBG("Enabling scheduled rotation timer on session \"%s\" (%ui %s)", session->name, + interval_us, USEC_UNIT); ret = timer_start(&session->rotation_schedule_timer, session, interval_us, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION, /* one-shot */ false); diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 60ca1f5d4..d2741c373 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -442,7 +442,7 @@ static int create_session(void) MSG("Traces will be written in %s", print_str_url); if (opt_live_timer) { - MSG("Live timer set to %u usec", opt_live_timer); + MSG("Live timer set to %u %s", opt_live_timer, USEC_UNIT); } } else if (opt_snapshot) { if (print_str_url) { diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c index 3314bde0d..b4e2942c3 100644 --- a/src/bin/lttng/commands/enable_channels.c +++ b/src/bin/lttng/commands/enable_channels.c @@ -510,7 +510,9 @@ int cmd_enable_channels(int argc, const char **argv) goto end; } chan_opts.attr.switch_timer_interval = (uint32_t) v; - DBG("Channel switch timer interval set to %d", chan_opts.attr.switch_timer_interval); + DBG("Channel switch timer interval set to %d %s", + chan_opts.attr.switch_timer_interval, + USEC_UNIT); break; } case OPT_READ_TIMER: @@ -532,7 +534,9 @@ int cmd_enable_channels(int argc, const char **argv) goto end; } chan_opts.attr.read_timer_interval = (uint32_t) v; - DBG("Channel read timer interval set to %d", chan_opts.attr.read_timer_interval); + DBG("Channel read timer interval set to %d %s", + chan_opts.attr.read_timer_interval, + USEC_UNIT); break; } case OPT_MONITOR_TIMER: @@ -549,7 +553,9 @@ int cmd_enable_channels(int argc, const char **argv) } opt_monitor_timer.interval = (uint64_t) v; opt_monitor_timer.set = true; - DBG("Channel monitor timer interval set to %" PRIu64" (µs)", opt_monitor_timer.interval); + DBG("Channel monitor timer interval set to %" PRIu64 " %s", + opt_monitor_timer.interval, + USEC_UNIT); break; } case OPT_BLOCKING_TIMEOUT: @@ -597,8 +603,9 @@ int cmd_enable_channels(int argc, const char **argv) opt_blocking_timeout.value = (int64_t) v; opt_blocking_timeout.set = true; - DBG("Channel blocking timeout set to %" PRId64 " µs%s", + DBG("Channel blocking timeout set to %" PRId64 " %s%s", opt_blocking_timeout.value, + USEC_UNIT, opt_blocking_timeout.value == 0 ? " (non-blocking)" : ""); break; diff --git a/src/bin/lttng/commands/enable_rotation.c b/src/bin/lttng/commands/enable_rotation.c index 85dae5681..0023de5b7 100644 --- a/src/bin/lttng/commands/enable_rotation.c +++ b/src/bin/lttng/commands/enable_rotation.c @@ -118,8 +118,8 @@ static enum cmd_error_code add_schedule(const char *session_name, ret = CMD_SUCCESS; switch (schedule_type) { case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: - MSG("Enabled %s rotations every %" PRIu64 " µs on session %s", - schedule_type_name, value, session_name); + MSG("Enabled %s rotations every %" PRIu64 " %s on session %s", + schedule_type_name, value, USEC_UNIT, session_name); break; case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: MSG("Enabled %s rotations every %" PRIu64 " bytes written on session %s", diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c index eabeb8132..d43127d1d 100644 --- a/src/bin/lttng/commands/list.c +++ b/src/bin/lttng/commands/list.c @@ -1261,7 +1261,7 @@ void print_timer(const char *timer_name, uint32_t space_count, int64_t value) } if (value) { - MSG("%" PRId64 " µs", value); + MSG("%" PRId64 " %s", value, USEC_UNIT); } else { MSG("inactive"); } @@ -1318,7 +1318,8 @@ static void print_channel(struct lttng_channel *channel) if (blocking_timeout == -1) { MSG("%sBlocking timeout: infinite", indent6); } else { - MSG("%sBlocking timeout: %" PRId64 " µs", indent6, blocking_timeout); + MSG("%sBlocking timeout: %" PRId64 " %s", indent6, + blocking_timeout, USEC_UNIT); } } @@ -1616,7 +1617,7 @@ static enum cmd_error_code print_periodic_rotation_schedule( goto end; } - MSG(" timer period: %" PRIu64" µs", value); + MSG(" timer period: %" PRIu64" %s", value, USEC_UNIT); ret = CMD_SUCCESS; end: return ret; diff --git a/src/common/time.c b/src/common/time.c index 5c5594584..5519e3ab4 100644 --- a/src/common/time.c +++ b/src/common/time.c @@ -22,6 +22,16 @@ #include #include #include +#include +#include + +static bool utf8_output_supported; + +LTTNG_HIDDEN +bool locale_supports_utf8(void) +{ + return utf8_output_supported; +} LTTNG_HIDDEN int timespec_to_ms(struct timespec ts, unsigned long *ms) @@ -57,3 +67,16 @@ struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; return res; } + +static +void __attribute__((constructor)) init_locale_utf8_support(void) +{ + const char *program_locale = setlocale(LC_ALL, NULL); + const char *lang = getenv("LANG"); + + if (program_locale && strstr(program_locale, "utf8")) { + utf8_output_supported = true; + } else if (strstr(lang, "utf8")) { + utf8_output_supported = true; + } +} diff --git a/src/common/time.h b/src/common/time.h index 8a7dd958f..894ab9259 100644 --- a/src/common/time.h +++ b/src/common/time.h @@ -20,12 +20,22 @@ #define LTTNG_TIME_H #include +#include #define MSEC_PER_SEC 1000ULL #define NSEC_PER_SEC 1000000000ULL #define NSEC_PER_MSEC 1000000ULL #define NSEC_PER_USEC 1000ULL +bool locale_supports_utf8(void); + +#define NSEC_UNIT "ns" +#define USEC_UNIT (locale_supports_utf8() ? "µs" : "us") +#define MSEC_UNIT "ms" +#define SEC_UNIT "s" +#define MIN_UNIT "m" +#define HR_UNIT "h" + /* * timespec_to_ms: Convert timespec to milliseconds. *