]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Add helper function to build timestamp string
authorOsier Yang <jyang@redhat.com>
Mon, 1 Nov 2010 04:17:14 +0000 (12:17 +0800)
committerEric Blake <eblake@redhat.com>
Mon, 1 Nov 2010 23:05:11 +0000 (17:05 -0600)
* src/util/util.h
* src/util/util.c
* src/libvirt_private.syms

src/libvirt_private.syms
src/util/util.c
src/util/util.h

index cf64bd3661254e1ce6629cb872dfb94f06452f62..bd77a34ef90496a2617bba17e98fc34bc881211e 100644 (file)
@@ -779,6 +779,7 @@ virStrToLong_ui;
 virStrToLong_ull;
 virStrcpy;
 virStrncpy;
+virTimestamp;
 
 
 # uuid.h
index 2632fe701f6aec3df8917f39dabe52f6564c326f..f7c7d3253ce8b0b632f2f447206a7f85109c7fdf 100644 (file)
@@ -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,30 @@ int virBuildPathInternal(char **path, ...)
 
     return ret;
 }
+
+/**
+ * virTimestamp:
+ *
+ * Return an allocated string containing the current date and time,
+ * followed by ": ".  Return NULL on allocation failure.
+ */
+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.%03d: ",
+                    timestr, (int) cur_time.tv_usec / 1000) < 0) {
+        return NULL;
+    }
+
+    return timestamp;
+}
index 5de4fd67d4825da3b5ac0820ae721dd6d9a92333..a240d8791c7f228160887655d7c8a761f38c4f7d 100644 (file)
@@ -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__ */