[ovs-dev] [PATCH 2/2] vlog: Change the default timestamp structure.

Gurucharan Shetty gshetty at nicira.com
Fri Jan 27 19:39:15 UTC 2012


Change the default timestamp for console and file logs to
UTC in a format that satisfies timestamp requirements in RFC 5424.

Also, add the ability for ovs-appctl to log timestamps in UTC.

Bug #9052.

Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/dynamic-string.c      |   13 +++++++++++--
 lib/dynamic-string.h      |    2 +-
 lib/vlog.c                |    6 +++++-
 lib/vlog.h                |    4 ++--
 utilities/ovs-appctl.8.in |   11 +++++++++--
 5 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c
index dbb33a3..257a4ac 100644
--- a/lib/dynamic-string.c
+++ b/lib/dynamic-string.c
@@ -173,12 +173,21 @@ ds_put_printable(struct ds *ds, const char *s, size_t n)
     }
 }
 
+/*
+ * Writes the time 'tm' to 'string' based on 'template'. A Null 'tm'
+ * writes the current utc time or localtime to 'string' based on 'utc'
+ */
 void
-ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
+ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm,
+                    bool utc)
 {
     if (!tm) {
         time_t now = time_wall();
-        tm = localtime(&now);
+        if (utc) {
+            tm = gmtime(&now);
+        } else {
+            tm = localtime(&now);
+        }
     }
     for (;;) {
         size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
diff --git a/lib/dynamic-string.h b/lib/dynamic-string.h
index 2961a01..1c76fcf 100644
--- a/lib/dynamic-string.h
+++ b/lib/dynamic-string.h
@@ -49,7 +49,7 @@ void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3);
 void ds_put_format_valist(struct ds *, const char *, va_list)
     PRINTF_FORMAT(2, 0);
 void ds_put_printable(struct ds *, const char *, size_t);
-void ds_put_strftime(struct ds *, const char *, const struct tm *)
+void ds_put_strftime(struct ds *, const char *, const struct tm *, bool utc)
     STRFTIME_FORMAT(2);
 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
                      uintptr_t ofs, bool ascii);
diff --git a/lib/vlog.c b/lib/vlog.c
index 0d7f4d1..4b8bc6b 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -610,7 +610,11 @@ format_log_message(const struct vlog_module *module, enum vlog_level level,
             break;
         case 'd':
             p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
-            ds_put_strftime(s, tmp, NULL);
+            ds_put_strftime(s, tmp, NULL, false);
+            break;
+        case 'D':
+            p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
+            ds_put_strftime(s, tmp, NULL, true);
             break;
         case 'm':
             /* Format user-supplied log message and trim trailing new-lines. */
diff --git a/lib/vlog.h b/lib/vlog.h
index 6fa007b..5954d21 100644
--- a/lib/vlog.h
+++ b/lib/vlog.h
@@ -51,8 +51,8 @@ enum vlog_level vlog_get_level_val(const char *name);
 /* Facilities that we can log to. */
 #define VLOG_FACILITIES                                         \
     VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m")                      \
-    VLOG_FACILITY(CONSOLE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")  \
-    VLOG_FACILITY(FILE, "%d{%b %d %H:%M:%S}|%05N|%c|%p|%m")
+    VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c|%p|%m")  \
+    VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c|%p|%m")
 enum vlog_facility {
 #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME,
     VLOG_FACILITIES
diff --git a/utilities/ovs-appctl.8.in b/utilities/ovs-appctl.8.in
index d851ea1..6a61732 100644
--- a/utilities/ovs-appctl.8.in
+++ b/utilities/ovs-appctl.8.in
@@ -139,6 +139,13 @@ The current date and time in ISO 8601 format (YYYY\-MM\-DD HH:MM:SS).
 The current date and time in the specified \fIformat\fR, which takes
 the same format as the \fItemplate\fR argument to \fBstrftime\fR(3).
 .
+.IP \fB%D\fR
+The current UTC date and time in ISO 8601 format (YYYY\-MM\-DD HH:MM:SS).
+.
+.IP \fB%D{\fIformat\fB}\fR
+The current UTC date and time in the specified \fIformat\fR, which takes
+the same format as the \fItemplate\fR argument to \fBstrftime\fR(3).
+.
 .IP \fB%m\fR
 The message being logged.
 .
@@ -184,8 +191,8 @@ width.  (A field wider than \fIwidth\fR is not truncated to fit.)
 .RE
 .
 .IP
-The default pattern for console and file output is \fB%d{%b %d
-%H:%M:%S}|%05N|%c|%p|%m\fR; for syslog output, \fB%05N|%c|%p|%m\fR.
+The default pattern for console and file output is \fB%D{%Y-%m-%dT
+%H:%M:%SZ}|%05N|%c|%p|%m\fR; for syslog output, \fB%05N|%c|%p|%m\fR.
 .
 .IP "\fBvlog/reopen\fR"
 Causes the daemon to close and reopen its log file.  (This
-- 
1.7.2.5




More information about the dev mailing list