From e9fe66a91c75d6bb856fe4910be57b89b53a7c2f Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Wed, 9 Mar 2022 01:29:55 +0200 Subject: [PATCH] weston-log: Extract helper for generating a time stamp As we might be needing it for other scopes extract the time stamp genration into a helper. Signed-off-by: Marius Vlad --- compositor/main.c | 36 ++---------------------- include/libweston/weston-log.h | 2 ++ libweston/weston-log.c | 50 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/compositor/main.c b/compositor/main.c index 6fda035a..3b15a959 100644 --- a/compositor/main.c +++ b/compositor/main.c @@ -134,37 +134,6 @@ static struct weston_log_scope *log_scope; static struct weston_log_scope *protocol_scope; static int cached_tm_mday = -1; -static char * -weston_log_timestamp(char *buf, size_t len) -{ - struct timeval tv; - struct tm *brokendown_time; - char datestr[128]; - char timestr[128]; - - gettimeofday(&tv, NULL); - - brokendown_time = localtime(&tv.tv_sec); - if (brokendown_time == NULL) { - snprintf(buf, len, "%s", "[(NULL)localtime] "); - return buf; - } - - memset(datestr, 0, sizeof(datestr)); - if (brokendown_time->tm_mday != cached_tm_mday) { - strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n", - brokendown_time); - cached_tm_mday = brokendown_time->tm_mday; - } - - strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time); - /* if datestr is empty it prints only timestr*/ - snprintf(buf, len, "%s[%s.%03li]", datestr, - timestr, (tv.tv_usec / 1000)); - - return buf; -} - static void custom_handler(const char *fmt, va_list arg) { @@ -172,7 +141,7 @@ custom_handler(const char *fmt, va_list arg) weston_log_scope_printf(log_scope, "%s libwayland: ", weston_log_timestamp(timestr, - sizeof(timestr))); + sizeof(timestr), &cached_tm_mday)); weston_log_scope_vprintf(log_scope, fmt, arg); } @@ -218,7 +187,8 @@ vlog(const char *fmt, va_list ap) if (weston_log_scope_is_enabled(log_scope)) { int len_va; char *log_timestamp = weston_log_timestamp(timestr, - sizeof(timestr)); + sizeof(timestr), + &cached_tm_mday); len_va = vasprintf(&str, fmt, ap); if (len_va >= 0) { len = weston_log_scope_printf(log_scope, "%s %s", diff --git a/include/libweston/weston-log.h b/include/libweston/weston-log.h index aeb7768b..1786dea0 100644 --- a/include/libweston/weston-log.h +++ b/include/libweston/weston-log.h @@ -109,6 +109,8 @@ weston_log_subscription_complete(struct weston_log_subscription *sub); char * weston_log_scope_timestamp(struct weston_log_scope *scope, char *buf, size_t len); +char * +weston_log_timestamp(char *buf, size_t len, int *cached_tm_mday); void weston_log_subscriber_destroy(struct weston_log_subscriber *subscriber); diff --git a/libweston/weston-log.c b/libweston/weston-log.c index 276fde26..5e7f3521 100644 --- a/libweston/weston-log.c +++ b/libweston/weston-log.c @@ -913,6 +913,56 @@ weston_log_scope_timestamp(struct weston_log_scope *scope, return buf; } +/** Returns a timestamp useful for adding it to a log scope. + * + * @example + * char timestr[128]; + * static int cached_dm = -1; + * char *time_buff = weston_log_timestamp(timestr, sizeof(timestr), &cached_dm); + * weston_log_scope_printf(log_scope, "%s %s", time_buff, other_data); + * + * @param buf a user-supplied buffer + * @param len user-supplied length of the buffer + * @param cached_tm_mday a cached day of the month, as an integer. Setting this + * pointer different from NULL, to an integer value other than was retrieved as + * current day of the month, would add an additional line under the form of + * 'Date: Y-m-d Z\n'. Setting the pointer to NULL would not print any date, nor + * if the value matches the current day of month. Helps identify logs that + * spawn multiple days, while still having a shorter time stamp format. + * @ingroup log + */ +WL_EXPORT char * +weston_log_timestamp(char *buf, size_t len, int *cached_tm_mday) +{ + struct timeval tv; + struct tm *brokendown_time; + char datestr[128]; + char timestr[128]; + + gettimeofday(&tv, NULL); + + brokendown_time = localtime(&tv.tv_sec); + if (brokendown_time == NULL) { + snprintf(buf, len, "%s", "[(NULL)localtime] "); + return buf; + } + + memset(datestr, 0, sizeof(datestr)); + if (cached_tm_mday && brokendown_time->tm_mday != *cached_tm_mday) { + strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n", + brokendown_time); + *cached_tm_mday = brokendown_time->tm_mday; + } + + strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time); + /* if datestr is empty it prints only timestr*/ + snprintf(buf, len, "%s[%s.%03li]", datestr, + timestr, (tv.tv_usec / 1000)); + + return buf; +} + + void weston_log_subscriber_release(struct weston_log_subscriber *subscriber) {