Commit 1d3fef81 by Christian Brauner Committed by Stéphane Graber

log: use lxc_unix_epoch_to_utc()

This allows us to generate nice timestamps in a thread-safe manner without relying on locale touching functions from any libc. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 2bfafb07
...@@ -136,10 +136,9 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -136,10 +136,9 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
struct lxc_log_event *event) struct lxc_log_event *event)
{ {
char buffer[LXC_LOG_BUFFER_SIZE]; char buffer[LXC_LOG_BUFFER_SIZE];
struct timespec zero_timespec = {0}; char date_time[LXC_LOG_TIME_SIZE];
int ret, n; int n;
int fd_to_use = -1; int fd_to_use = -1;
char nanosec[__LXC_NUMSTRLEN];
#ifndef NO_LXC_CONF #ifndef NO_LXC_CONF
if (!lxc_log_use_global_fd && current_config) if (!lxc_log_use_global_fd && current_config)
...@@ -152,19 +151,15 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -152,19 +151,15 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
if (fd_to_use == -1) if (fd_to_use == -1)
return 0; return 0;
ret = snprintf(nanosec, __LXC_NUMSTRLEN, "%ld", event->timestamp.tv_nsec); if (lxc_unix_epoch_to_utc(date_time, LXC_LOG_TIME_SIZE, &event->timestamp) < 0)
if (ret < 0 || ret >= LXC_LOG_TIME_SIZE)
return 0; return 0;
n = snprintf(buffer, sizeof(buffer), n = snprintf(buffer, sizeof(buffer),
"%15s%s%s [ %.0f.%.6s ] %-8s %s - %s:%s:%d - ", "%15s%s%s %s %-8s %s - %s:%s:%d - ",
log_prefix, log_prefix,
log_vmname ? " " : "", log_vmname ? " " : "",
log_vmname ? log_vmname : "", log_vmname ? log_vmname : "",
/* sec_since_epoch: Safely convert time_t type to date_time,
* double. */
difftime(event->timestamp.tv_sec, zero_timespec.tv_sec),
nanosec,
lxc_log_priority_to_string(event->priority), lxc_log_priority_to_string(event->priority),
event->category, event->category,
event->locinfo->file, event->locinfo->func, event->locinfo->file, event->locinfo->func,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment