[libvirt] [PATCH 1/2] util: Add helper function to build timestamp string

Osier Yang jyang at redhat.com
Mon Nov 1 04:17:14 UTC 2010


* src/util/util.h
* src/util/util.c
* src/libvirt_private.syms
---
 src/libvirt_private.syms |    1 +
 src/util/util.c          |   20 ++++++++++++++++++++
 src/util/util.h          |    3 +++
 3 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index cf64bd3..bd77a34 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -779,6 +779,7 @@ virStrToLong_ui;
 virStrToLong_ull;
 virStrcpy;
 virStrncpy;
+virTimestamp;


 # uuid.h
diff --git a/src/util/util.c b/src/util/util.c
index 2632fe7..6f83730 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -38,6 +38,7 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <sys/time.h>
 #if HAVE_MMAP
 # include <sys/mman.h>
 #endif
@@ -2912,3 +2913,22 @@ int virBuildPathInternal(char **path, ...)

     return ret;
 }
+
+char *virTimestamp(void) {
+    struct timeval cur_time;
+    struct tm time_info;
+    char timestr[100];
+    char *timestamp;
+
+    gettimeofday(&cur_time, NULL);
+    localtime_r(&cur_time.tv_sec, &time_info);
+
+    strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", &time_info);
+
+    if (virAsprintf(&timestamp, "%s.%3d: ",
+                    timestr, (int) cur_time.tv_usec / 1000) < 0) {
+        return NULL;
+    }
+
+    return timestamp;
+}
diff --git a/src/util/util.h b/src/util/util.h
index 5de4fd6..a240d87 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -160,6 +160,7 @@ int virFileOpenTtyAt(const char *ptmx,

 char* virFilePid(const char *dir,
                  const char *name);
+
 int virFileWritePidPath(const char *path,
                         pid_t pid) ATTRIBUTE_RETURN_CHECK;
 int virFileWritePid(const char *dir,
@@ -277,4 +278,6 @@ void virFileWaitForDevices(void);
 # define virBuildPath(path, ...) virBuildPathInternal(path, __VA_ARGS__, NULL)
 int virBuildPathInternal(char **path, ...) ATTRIBUTE_SENTINEL;

+char *virTimestamp(void);
+
 #endif /* __VIR_UTIL_H__ */
--
1.7.2.3




More information about the libvir-list mailing list