misc: Add cr_append_pid_and_datetime()
authorTomas Mlcoch <tmlcoch@redhat.com>
Mon, 27 Jan 2014 13:58:53 +0000 (14:58 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Mon, 27 Jan 2014 13:58:53 +0000 (14:58 +0100)
src/misc.c
src/misc.h

index 86f6eea..8e3b6c8 100644 (file)
@@ -1232,3 +1232,19 @@ cr_rm(const char *path,
 
     return ret;
 }
+
+gchar *
+cr_append_pid_and_datetime(const char *str, const char *suffix)
+{
+    GDateTime *cur_datetime = g_date_time_new_now_local();
+    gchar *datetime = g_date_time_format(cur_datetime, "%Y%m%d%H%M%S");
+    gchar *result = g_strdup_printf("%s%jd.%s.%d%s",
+                                    str ? str : "",
+                                    (intmax_t) getpid(),
+                                    datetime,
+                                    g_date_time_get_microsecond(cur_datetime),
+                                    suffix ? suffix : "");
+    g_free(datetime);
+    g_date_time_unref(cur_datetime);
+    return result;
+}
index ceb3911..a9a1ad8 100644 (file)
@@ -427,7 +427,7 @@ typedef enum {
 } cr_RmFlags;
 
 /** Wrapper over rm command
- * @param path                  Path (supports wildcards)
+ * @param path          Path (supports wildcards)
  * @param flags         Flags
  * @param working_dir   Working directory
  * @param err           GError **
@@ -437,6 +437,14 @@ cr_rm(const char *path,
       cr_RmFlags flags,
       const char *working_dir,
       GError **err);
+
+/** Append "YYYYmmddHHMMSS.MICROSECONDS.PID" suffix to the str.
+ * @param str       String or NULL
+ * @param suffix    Another string that will be appended or NULL
+ * @param return    Newly allocated string
+ */
+gchar *
+cr_append_pid_and_datetime(const char *str, const char *suffix);
 /** @} */
 
 #ifdef __cplusplus