info: Replace %p and %r in GST_DEBUG_FILE
authorStian Selnes <stian@pexip.com>
Thu, 29 Oct 2015 21:51:18 +0000 (22:51 +0100)
committerSebastian Dröge <sebastian@centricular.com>
Tue, 1 Nov 2016 18:32:46 +0000 (20:32 +0200)
It's useful to be able to set a name pattern for GST_DEBUG_FILE so that
the same environment variable can be used for multiple processes and
still write to different files. Especially useful if these processes
run simultaneously.

%p: Replaced with PID
%r: Replaced with random number

%p is obviously useful. %r is useful when for instance running two
processes with same PID but in different containers.

https://bugzilla.gnome.org/show_bug.cgi?id=773092

gst/gstinfo.c

index 75b5e4c..ba2c407 100644 (file)
@@ -292,6 +292,33 @@ _priv_gst_in_valgrind (void)
   return (in_valgrind == GST_VG_INSIDE);
 }
 
+static gchar *
+_replace_pattern_in_gst_debug_file_name (gchar * name, const char * token, guint val)
+{
+  gchar * token_start;
+  if ((token_start = strstr (name, token))) {
+    gsize token_len = strlen (token);
+    gchar * name_prefix = name;
+    gchar * name_suffix = token_start + token_len;
+    token_start[0] = '\0';
+    name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
+    g_free (name_prefix);
+  }
+  return name;
+}
+
+static gchar *
+_priv_gst_debug_file_name (const gchar * env)
+{
+  gchar *name;
+
+  name = g_strdup (env);
+  name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
+  name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
+
+  return name;
+}
+
 /* Initialize the debugging system */
 void
 _priv_gst_debug_init (void)
@@ -305,7 +332,9 @@ _priv_gst_debug_init (void)
       if (strcmp (env, "-") == 0) {
         log_file = stdout;
       } else {
-        log_file = g_fopen (env, "w");
+        gchar *name = _priv_gst_debug_file_name (env);
+        log_file = g_fopen (name, "w");
+        g_free (name);
         if (log_file == NULL) {
           g_printerr ("Could not open log file '%s' for writing: %s\n", env,
               g_strerror (errno));