ASTERIXDB-1553: ClusterControllerService Dir/File Creation On-Demand

Change-Id: Ie2e65cf083b3eb2db3e716f2b34fb28fe01debca
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1029
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
index 7eb0674..8f95c35 100644
--- a/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
+++ b/hyracks-fullstack/hyracks/hyracks-control/hyracks-control-common/src/main/java/org/apache/hyracks/control/common/logs/LogFile.java
@@ -27,6 +27,7 @@
 public class LogFile {
     private final File root;
 
+    private long openTime;
     private PrintWriter out;
 
     public LogFile(File root) {
@@ -34,18 +35,23 @@
     }
 
     public void open() throws Exception {
-        root.mkdirs();
-        out = new PrintWriter(new FileOutputStream(new File(root, String.valueOf(System.currentTimeMillis()) + ".log"),
-                true));
+        openTime = System.currentTimeMillis();
     }
 
-    public void log(JSONObject object) throws Exception {
+    public synchronized void log(JSONObject object) throws Exception {
+        if (out == null) {
+            root.mkdirs();
+            out = new PrintWriter(new FileOutputStream(new File(root, openTime + ".log"), true));
+        }
         out.println(object.toString(1));
         out.flush();
     }
 
-    public void close() {
-        out.flush();
-        out.close();
+    public synchronized void close() {
+        if (out != null) {
+            out.flush();
+            out.close();
+            out = null;
+        }
     }
 }