New command line option: --log-file-qualifier=VAR.  When specified,
the contents of environment variable VAR (viz, $VAR) are incorporated
into logfile names.  This makes it easy to incorporate environmental
information into logfile names.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4180 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/README_XML_OUTPUT.txt b/README_XML_OUTPUT.txt
index bd90cf9..3ad2696 100644
--- a/README_XML_OUTPUT.txt
+++ b/README_XML_OUTPUT.txt
@@ -134,6 +134,14 @@
 
      <tool>TEXT</tool>
 
+* OPTIONALLY, if --log-file-qualifier=VAR flag was given:
+
+     <logfilequalifier> <var>VAR</var> <value>$VAR</value>
+     </logfilequalifier>
+
+  That is, both the name of the environment variable and its value
+  are given.
+
 * The program and args being run.
 
      <argv>
diff --git a/coregrind/m_main.c b/coregrind/m_main.c
index d4e2e45..da97ed5 100644
--- a/coregrind/m_main.c
+++ b/coregrind/m_main.c
@@ -1275,6 +1275,7 @@
 "    --log-fd=<number>         log messages to file descriptor [2=stderr]\n"
 "    --log-file=<file>         log messages to <file>.pid<pid>\n"
 "    --log-file-exactly=<file> log messages to <file>\n"
+"    --log-file-qualifier=<VAR> incorporate $VAR in logfile name [none]\n"
 "    --log-socket=ipaddr:port  log messages to socket ipaddr:port\n"
 "    --demangle=no|yes         automatically demangle C++ names? [yes]\n"
 "    --num-callers=<number>    show <num> callers in stack traces [12]\n"
@@ -1534,6 +1535,15 @@
          VG_(clo_log_name) = &arg[11];
       }
 
+      else if (VG_CLO_STREQN(11, arg, "--log-file=")) {
+         log_to            = VgLogTo_File;
+         VG_(clo_log_name) = &arg[11];
+      }
+
+      else if (VG_CLO_STREQN(21, arg, "--log-file-qualifier=")) {
+         VG_(clo_log_file_qualifier) = &arg[21];
+      }
+
       else if (VG_CLO_STREQN(19, arg, "--log-file-exactly=")) {
          log_to            = VgLogTo_FileExactly;
          VG_(clo_log_name) = &arg[19];
@@ -1696,20 +1706,29 @@
          break;
 
       case VgLogTo_File: {
-         Char logfilename[1000];
-	 Int seq = 0;
-	 Int pid = VG_(getpid)();
+         HChar  logfilename[1000];
+	 Int    seq  = 0;
+	 Int    pid  = VG_(getpid)();
+         HChar* qual = NULL;
 
          vg_assert(VG_(clo_log_name) != NULL);
          vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */
 
+	 if (VG_(clo_log_file_qualifier)) {
+            qual = VG_(getenv)(VG_(clo_log_file_qualifier));
+	 }
+
 	 for (;;) {
 	    if (seq == 0)
-	       VG_(sprintf)(logfilename, "%s.pid%d",
-			    VG_(clo_log_name), pid );
+	       VG_(sprintf)( logfilename, "%s%s%s.pid%d",
+                             VG_(clo_log_name), 
+                             qual ? "." : "", qual ? qual : "",
+                             pid );
 	    else
-	       VG_(sprintf)(logfilename, "%s.pid%d.%d",
-			    VG_(clo_log_name), pid, seq );
+	       VG_(sprintf)( logfilename, "%s%s%s.pid%d.%d",
+			     VG_(clo_log_name), 
+                             qual ? "." : "", qual ? qual : "",
+                             pid, seq );
 	    seq++;
 
             // EXCL: it will fail with EEXIST if the file already exists.
@@ -1879,6 +1898,13 @@
       VG_(message)(Vg_UserMsg, "<pid>%d</pid>", VG_(getpid)());
       VG_(message)(Vg_UserMsg, "<ppid>%d</ppid>", VG_(getppid)());
       VG_(message)(Vg_UserMsg, "<tool>%s</tool>", toolname);
+      if (VG_(clo_log_file_qualifier)) {
+         HChar* val = VG_(getenv)(VG_(clo_log_file_qualifier));
+         VG_(message)(Vg_UserMsg, "<logfilequalifier> <var>%s</var> "
+                                  "<value>%s</value> </logfilequalifier>",
+                                  VG_(clo_log_file_qualifier),
+                                  val ? val : "");
+      }
       VG_(message)(Vg_UserMsg, "");
       VG_(message)(Vg_UserMsg, "<argv>");   
       for (i = 0; i < VG_(client_argc); i++) {
diff --git a/coregrind/m_options.c b/coregrind/m_options.c
index 16f4a35..cf98924 100644
--- a/coregrind/m_options.c
+++ b/coregrind/m_options.c
@@ -48,6 +48,7 @@
 Bool   VG_(clo_trace_children) = False;
 Int    VG_(clo_log_fd)         = 2;
 Char*  VG_(clo_log_name)       = NULL;
+Char*  VG_(clo_log_file_qualifier) = NULL;
 Bool   VG_(clo_time_stamp)     = False;
 Int    VG_(clo_input_fd)       = 0; /* stdin */
 Int    VG_(clo_n_suppressions) = 0;
diff --git a/coregrind/pub_core_options.h b/coregrind/pub_core_options.h
index 14faef4..b24ad94 100644
--- a/coregrind/pub_core_options.h
+++ b/coregrind/pub_core_options.h
@@ -71,6 +71,12 @@
    made to hold the relevant file id, by opening clo_log_name
    (concatenated with the process ID) for writing.
 
+   With --log-file, there is an additional twist: if
+   clo_log_file_qualifier is non-NULL, the contents of the environment
+   variable specified by clo_log_file_qualifier is incorporated into
+   the logfile name.  This is useful in that it allows the logfile
+   name to incorporate environmental information.
+
    With --log-socket, clo_log_name holds the hostname:portnumber pair,
    and is taken from the command line.  clo_log_fd is then made to hold
    the relevant file handle, by opening a connection to that
@@ -78,8 +84,9 @@
 
    Global default is to set log_to == VgLogTo_Fd and log_fd == 2
    (stderr). */
-extern Int     VG_(clo_log_fd);
-extern Char*   VG_(clo_log_name);
+extern Int   VG_(clo_log_fd);
+extern Char* VG_(clo_log_name);
+extern Char* VG_(clo_log_file_qualifier);
 
 /* Add timestamps to log messages?  default: NO */
 extern Bool  VG_(clo_time_stamp);