changeset 16128:66e3fc56e85f

support adding pid to LogFile name
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Tue, 17 Jun 2014 13:24:20 -0700
parents 7c9cf1697845
children 9410f831fefa
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java
diffstat 2 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Tue Jun 17 12:50:22 2014 -0700
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Tue Jun 17 13:24:20 2014 -0700
@@ -169,7 +169,7 @@
         @Option(help = "The runtime configuration to use")
         static final OptionValue<String> GraalRuntime = new OptionValue<>("");
 
-        @Option(help = "File to which logging is sent")
+        @Option(help = "File to which logging is sent.  %p in the name will be replaced with a string the process, usually the process id.")
         public static final PrintStreamOption LogFile = new PrintStreamOption();
         // @formatter:on
     }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java	Tue Jun 17 12:50:22 2014 -0700
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java	Tue Jun 17 13:24:20 2014 -0700
@@ -23,6 +23,7 @@
 package com.oracle.graal.hotspot;
 
 import java.io.*;
+import java.lang.management.*;
 
 import com.oracle.graal.options.*;
 
@@ -44,6 +45,30 @@
     private volatile PrintStream ps;
 
     /**
+     * Replace any instance of %p with a an identifying name. Try to get it from the RuntimeMXBean
+     * name.
+     * 
+     * @return the name of the file to log to
+     */
+    private String getFilename() {
+        String name = getValue();
+        if (name.contains("%p")) {
+            String runtimeName = ManagementFactory.getRuntimeMXBean().getName();
+            try {
+                int index = runtimeName.indexOf('@');
+                if (index != -1) {
+                    long pid = Long.parseLong(runtimeName.substring(0, index));
+                    runtimeName = Long.toString(pid);
+                }
+                name = name.replaceAll("%p", runtimeName);
+            } catch (NumberFormatException e) {
+
+            }
+        }
+        return name;
+    }
+
+    /**
      * Gets the print stream configured by this option.
      */
     public PrintStream getStream() {
@@ -53,7 +78,7 @@
                     if (ps == null) {
                         try {
                             final boolean enableAutoflush = true;
-                            ps = new PrintStream(new FileOutputStream(getValue()), enableAutoflush);
+                            ps = new PrintStream(new FileOutputStream(getFilename()), enableAutoflush);
                         } catch (FileNotFoundException e) {
                             throw new RuntimeException("couldn't open file: " + getValue(), e);
                         }