]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Implement a new function VMTools_GetTimeAsString.
authorOliver Kurth <okurth@vmware.com>
Tue, 6 Mar 2018 18:38:43 +0000 (10:38 -0800)
committerOliver Kurth <okurth@vmware.com>
Tue, 6 Mar 2018 18:38:43 +0000 (10:38 -0800)
* Implemented a new function VMTools_GetTimeAsString in
vmtools library which returns a properly formatted UTC timestamp
information. This function can be used by different modules / plugins
in 'VMware Tools' to add the timestamp information in log files.

Example of UTC timestamp information: "2018-02-22T21:17:38.517Z"

* Modified vmtools logging module to call the new function and
prepend the UTC timestamp information in every log message.

* Modified deployPkgLog.c to use the new function.

* Removed references / definition of System_GetTimeAsString since
it is no longer used anywhere in the code.

open-vm-tools/lib/include/system.h
open-vm-tools/lib/include/vmware/tools/utils.h
open-vm-tools/lib/system/systemLinux.c
open-vm-tools/libvmtools/vmtoolsLog.c
open-vm-tools/services/plugins/deployPkg/deployPkgLog.c

index c186fa7dc7c9a6becb32cfba62a83c9deeebc8b6..f1119cefcebf8fa8978d43700d1db7d713db39e2 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2016,2018 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -36,7 +36,6 @@
 
 uint64 System_GetTimeMonotonic(void);
 uint64 System_Uptime(void);
-char *System_GetTimeAsString(void);
 void System_Shutdown(Bool reboot);
 Bool System_GetNodeName(size_t outBufSize, char *outBuf);
 
index c0bfd3e215d28613ba0479bd1c4cf5215fe23b5c..127a49fb6b1ca005e1b0204fec279349711c2892 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 2008-2017 VMware, Inc. All rights reserved.
+ * Copyright (C) 2008-2018 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -182,6 +182,9 @@ VMTools_StopLogging(void);
 void
 VMTools_RestartLogging(void);
 
+gchar *
+VMTools_GetTimeAsString(void);
+
 void
 VMTools_SuspendLogIO(void);
 
index ee069168cd37acd9f6690d0b8a3ca6833df34309..5b067bef1ccce7d31d3978fbd516e1875d8e857a 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998-2017 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2018 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -283,82 +283,6 @@ System_Uptime(void)
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *
- * System_GetTimeAsString --
- *
- *      Returns the current time as a formatted string, useful for prepending
- *      to debugging output.
- *
- *      For example: "Oct 05 18:03:24.948"
- *
- * Results:
- *      On success, allocates and returns a string containing the formatted
- *      time.
- *      On failure, returns NULL.
- *
- * Side effects:
- *      None.
- *
- *-----------------------------------------------------------------------------
- */
-
-char *
-System_GetTimeAsString(void)
-{
-   struct timeval tv;
-   time_t sec;
-   int msec;
-   size_t charsWritten;
-   size_t bufSize = 8; // Multiplied by 2 for the initial allocation.
-   char *buf = NULL;
-   char *dateTime = NULL;
-   char *output = NULL;
-
-   if (gettimeofday(&tv, NULL)) {
-      goto out;
-   }
-   sec = tv.tv_sec;
-   msec = tv.tv_usec / 1000;
-
-   /*
-    * Loop repeatedly trying to format the time into a buffer, doubling the
-    * buffer with each failure. This should be safe as the manpage for
-    * strftime(3) seems to suggest that it only fails if the buffer isn't large
-    * enough.
-    *
-    * The resultant string is encoded according to the current locale.
-    */
-   do {
-      char *newBuf;
-      bufSize *= 2;
-
-      newBuf = realloc(buf, bufSize);
-      if (newBuf == NULL) {
-         goto out;
-      }
-      buf = newBuf;
-      charsWritten = strftime(buf, bufSize, "%b %d %H:%M:%S", localtime(&sec));
-   } while (charsWritten == 0);
-
-   /*
-    * Append the milliseconds field, but only after converting the date/time
-    * string from encoding specified in the current locale to an opaque type.
-    */
-   dateTime = Unicode_Alloc(buf, STRING_ENCODING_DEFAULT);
-   if (dateTime == NULL) {
-      goto out;
-   }
-   output = Unicode_Format("%s.%03d", dateTime, msec);
-
-  out:
-   free(buf);
-   free(dateTime);
-   return output;
-}
-
-
 /*
  *-----------------------------------------------------------------------------
  *
index fdafcd58237a2a14943701dbbd4199c9d6e06240..c0e4324292c25975ae59edf5cd4e6fb0268eb4e6 100644 (file)
@@ -231,6 +231,44 @@ VMToolsAsprintf(gchar **string,
 }
 
 
+/**
+ * VMTools_GetTimeAsString --
+ *
+ *    Returns the current UTC timestamp information
+ *
+ *    Ex: "2018-02-22T21:17:38.517Z"
+ *
+ *    The caller must free the return value using g_free
+ *
+ * @return Properly formatted string that contains the timestamp.
+ *         NULL if the timestamp cannot be retrieved.
+ */
+
+gchar *
+VMTools_GetTimeAsString(void)
+{
+   gchar *timePrefix = NULL;
+   GDateTime *utcTime = g_date_time_new_now_utc();
+
+   if (utcTime != NULL) {
+      gchar *dateFormat = g_date_time_format(utcTime, "%FT%T");
+
+      if (dateFormat != NULL) {
+         gint msec = g_date_time_get_microsecond(utcTime) / 1000;
+
+         timePrefix = g_strdup_printf("%s.%03dZ", dateFormat, msec);
+
+         g_free(dateFormat);
+         dateFormat = NULL;
+      }
+
+      g_date_time_unref(utcTime);
+   }
+
+   return timePrefix;
+}
+
+
 /**
  * Creates a formatted message to be logged. The format of the message will be:
  *
@@ -258,7 +296,7 @@ VMToolsLogFormat(const gchar *message,
    size_t len = 0;
    gboolean shared = TRUE;
    gboolean addsTimestamp = TRUE;
-   char *tstamp;
+   gchar *tstamp;
 
    if (domain == NULL) {
       domain = gLogDomain;
@@ -306,7 +344,7 @@ VMToolsLogFormat(const gchar *message,
       addsTimestamp = data->logger->addsTimestamp;
    }
 
-   tstamp = System_GetTimeAsString();
+   tstamp = VMTools_GetTimeAsString();
 
    if (!addsTimestamp) {
       if (shared) {
@@ -339,7 +377,7 @@ VMToolsLogFormat(const gchar *message,
       }
    }
 
-   free(tstamp);
+   g_free(tstamp);
 
    /*
     * The log messages from glib itself (and probably other libraries based
index 9a7039524c1679e76a98ca2e55d8e7094defa14e..b0990e6c2ef3abf7ee466836415f35f40b6bfe0b 100644 (file)
 
 #include "deployPkgInt.h"
 #include "imgcust-common/log.h"
-#include <glib.h>
 #include "util.h"
 #include "file.h"
 #include "str.h"
+#include "vmware/tools/utils.h"
 
 #include <stdio.h>
 
@@ -113,53 +113,6 @@ DeployPkgLog_Close()
 }
 
 
-/*
- *----------------------------------------------------------------------
- *
- * DeployPkgLogGetTimeAsString --
- *
- *    Returns the current UTC timestamp information that should be printed
- *    at the beginning of each log message.
- *
- *    Ex: "2018-02-22T21:17:38.517Z"
- *
- *    The caller must free the return value using g_free
- *
- * Results:
- *    Properly formatted string that contains the timestamp.
- *    NULL if the timestamp cannot be retrieved.
- *
- * Side effects:
- *    None
- *
- *----------------------------------------------------------------------
- */
-
-static gchar*
-DeployPkgLogGetTimeAsString()
-{
-   gchar *timePrefix = NULL;
-   GDateTime *utcTime = g_date_time_new_now_utc();
-
-   if (utcTime != NULL) {
-      gchar *dateFormat = g_date_time_format(utcTime, "%FT%T");
-
-      if (dateFormat != NULL) {
-         gint msec = g_date_time_get_microsecond(utcTime) / 1000;
-
-         timePrefix = g_strdup_printf("%s.%03dZ", dateFormat, msec);
-
-         g_free(dateFormat);
-         dateFormat = NULL;
-      }
-
-      g_date_time_unref(utcTime);
-   }
-
-   return timePrefix;
-}
-
-
 /*
  *----------------------------------------------------------------------
  *
@@ -209,7 +162,7 @@ DeployPkgLog_Log(int level,          // IN
    }
 
    va_start(args, fmtstr);
-   tstamp = DeployPkgLogGetTimeAsString();
+   tstamp = VMTools_GetTimeAsString();
    fprintf(_file, "[%s] [%8s] ",
            (tstamp != NULL) ? tstamp : "no time", logLevel);
    vfprintf(_file, fmtstr, args);