neels has uploaded this change for review.
logging: add 'logging timezone (localtime|utc)'
Add ability to print logging timestamps in UTC.
I want to test logging timestamps in vty transcript tests. To get
deterministic epoch timestamps, I can use osmo_gettimeofday_override().
However, so far we log in the server's local time zone: the dates
printed after osmo_gettimeofday_override() still change according to the
locally configured timezone -- not good for regression testing. For UTC,
it is always the same.
The intended use is for regression testing of timestamps, but maybe
logging in UTC may be useful to some user out there, too.
Change-Id: I7f868b47bf8f8dfcf85e735f490ae69b18111af4
---
M configure.ac
M include/osmocom/core/logging.h
M src/core/libosmocore.map
M src/core/logging.c
M src/vty/logging_vty.c
M tests/logging/logging_vty_test.vty
6 files changed, 88 insertions(+), 12 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/43/32043/1
diff --git a/configure.ac b/configure.ac
index 5e17c7a..861b7b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -130,6 +130,7 @@
AC_SUBST(SYMBOL_VISIBILITY)
AC_CHECK_FUNCS(localtime_r)
+AC_CHECK_FUNCS(gmtime_r)
AC_DEFUN([CHECK_TM_INCLUDES_TM_GMTOFF], [
AC_CACHE_CHECK(
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 755564d..c12aaeb 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -295,6 +295,11 @@
LOG_FILENAME_POS_LINE_END,
};
+enum log_timezone {
+ LOG_TIMEZONE_LOCALTIME,
+ LOG_TIMEZONE_UTC,
+};
+
/*! structure representing a logging target */
struct log_target {
struct llist_head entry; /*!< linked list */
@@ -391,6 +396,10 @@
enum log_filename_type print_filename2;
/* Where on a log line to put the source file info. */
enum log_filename_pos print_filename_pos;
+
+ /* Set timezone for logging of timestamps: 0 for local time, 1 for UTC. Other values are reserved for future
+ * use. */
+ enum log_timezone timezone;
};
/* use the above macros */
@@ -412,6 +421,7 @@
void log_set_use_color(struct log_target *target, int);
void log_set_print_extended_timestamp(struct log_target *target, int);
void log_set_print_timestamp(struct log_target *target, int);
+void log_set_timezone(struct log_target *target, enum log_timezone timezone);
void log_set_print_tid(struct log_target *target, int);
void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead");
void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index bd1b3d9..642077f 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -83,6 +83,7 @@
log_set_print_tid;
log_set_print_timestamp;
log_set_use_color;
+log_set_timezone;
log_target_create;
log_target_create_file;
log_target_create_file_stream;
diff --git a/src/core/logging.c b/src/core/logging.c
index 3095f0d..757333d 100644
--- a/src/core/logging.c
+++ b/src/core/logging.c
@@ -464,6 +464,27 @@
return bn + 1;
}
+static int get_timestamp(struct timeval *tv, struct tm *tm, const struct log_target *target)
+{
+ osmo_gettimeofday(tv, NULL);
+ switch (target->timezone) {
+ case LOG_TIMEZONE_LOCALTIME:
+#ifdef HAVE_LOCALTIME_R
+ localtime_r(&tv->tv_sec, tm);
+ return 0;
+#endif
+ /* If there is no localtime_r() function, then maybe gmtime_r() is available instead? */
+ case LOG_TIMEZONE_UTC:
+#ifdef HAVE_GMTIME_R
+ gmtime_r(&tv->tv_sec, tm);
+ return 0;
+#endif
+ /* If there is no gmtime_r() function, then print no timestamp. */
+ default:
+ return -1;
+ }
+}
+
/*! main output formatting function for log lines.
* \param[out] buf caller-allocated output buffer for the generated string
* \param[in] buf_len number of bytes available in buf
@@ -494,12 +515,9 @@
}
}
if (!cont) {
- if (target->print_ext_timestamp) {
-#ifdef HAVE_LOCALTIME_R
- struct tm tm;
- struct timeval tv;
- osmo_gettimeofday(&tv, NULL);
- localtime_r(&tv.tv_sec, &tm);
+ struct timeval tv;
+ struct tm tm;
+ if (target->print_ext_timestamp && get_timestamp(&tv, &tm, target) == 0) {
ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
@@ -507,14 +525,10 @@
if (ret < 0)
goto err;
OSMO_SNPRINTF_RET(ret, rem, offset, len);
-#endif
- } else if (target->print_timestamp) {
- time_t tm;
- if ((tm = time(NULL)) == (time_t) -1)
- goto err;
+ } else if (target->print_timestamp && get_timestamp(&tv, &tm, target) == 0) {
/* Get human-readable representation of time.
man ctime: we need at least 26 bytes in buf */
- if (rem < 26 || !ctime_r(&tm, buf + offset))
+ if (rem < 26 || !asctime_r(&tm, buf + offset))
goto err;
ret = strlen(buf + offset);
if (ret <= 0)
@@ -851,6 +865,15 @@
target->print_timestamp = print_timestamp;
}
+/*! Switch logging timezone between UTC or local time.
+ * \param[in] target Log target to be affected.
+ * \param[in] timezone Timezone to set.
+ */
+void log_set_timezone(struct log_target *target, enum log_timezone timezone)
+{
+ target->timezone = timezone;
+}
+
/*! Enable or disable printing of extended timestamps while logging
* \param[in] target Log target to be affected
* \param[in] print_timestamp Enable (1) or disable (0) timestamps
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index 2a07422..26b8844 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -190,6 +190,22 @@
RET_WITH_UNLOCK(CMD_SUCCESS);
}
+DEFUN(logging_prnt_timezone,
+ logging_timezone_cmd,
+ "logging timezone (localtime|utc)",
+ LOGGING_STR "Configure time zone for log message timestamping\n"
+ "Log timestamps in the system's local time\n"
+ "Log timestamps in Coordinated Universal Time (UTC)\n")
+{
+ struct log_target *tgt;
+ ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
+ if (strcmp(argv[0], "utc") == 0)
+ log_set_timezone(tgt, LOG_TIMEZONE_UTC);
+ else
+ log_set_timezone(tgt, LOG_TIMEZONE_LOCALTIME);
+ RET_WITH_UNLOCK(CMD_SUCCESS);
+}
+
DEFUN(logging_prnt_timestamp,
logging_prnt_timestamp_cmd,
"logging timestamp (0|1)",
@@ -1204,6 +1220,7 @@
install_lib_element_ve(&logging_use_clr_cmd);
install_lib_element_ve(&logging_prnt_timestamp_cmd);
install_lib_element_ve(&logging_prnt_ext_timestamp_cmd);
+ install_lib_element_ve(&logging_timezone_cmd);
install_lib_element_ve(&logging_prnt_tid_cmd);
install_lib_element_ve(&logging_prnt_cat_cmd);
install_lib_element_ve(&logging_prnt_cat_hex_cmd);
@@ -1239,6 +1256,7 @@
install_lib_element(CFG_LOG_NODE, &logging_use_clr_cmd);
install_lib_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
install_lib_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
+ install_lib_element(CFG_LOG_NODE, &logging_timezone_cmd);
install_lib_element(CFG_LOG_NODE, &logging_prnt_tid_cmd);
install_lib_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
install_lib_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
diff --git a/tests/logging/logging_vty_test.vty b/tests/logging/logging_vty_test.vty
index f9b4e01..b9afc04 100644
--- a/tests/logging/logging_vty_test.vty
+++ b/tests/logging/logging_vty_test.vty
@@ -48,6 +48,7 @@
logging color (0|1)
logging timestamp (0|1)
logging print extended-timestamp (0|1)
+ logging timezone (localtime|utc)
logging print thread-id (0|1)
logging print category (0|1)
logging print category-hex (0|1)
@@ -67,6 +68,7 @@
color Configure color-printing for log messages
timestamp Configure log message timestamping
print Log output settings
+ timezone Configure time zone for log message timestamping
set-log-mask Set the logmask of this logging target
level Set the log level for a specified category
To view, visit change 32043. To unsubscribe, or for help writing mail filters, visit settings.