package eu.qualimaster.common.logging;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import eu.qualimaster.file.Utils;

/**
 * Provide a data logger for writing logs into a file.
 * @author Cui Qin
 *
 */
public class DataLogger {
    /**
     * Returns a print writer to write the log in a given location.
     * @param filePath the full path of the file including the postfix ".log".
     * @return a print writer
     */
    public static PrintWriter getPrintWriter(String filePath) {
        PrintWriter writer = null;
        FileOutputStream fileOut = null;
        try {
            fileOut = Utils.createFileOutputStream(new File(filePath), false);
            writer = new PrintWriter(fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return writer;
    }
    
    /**
     * Returns a print writer to write the log in a given location.
     * @param fileName the log name
     * @param directory the location to write the log
     * @return a print writer
     */
    public static PrintWriter getPrintWriter(String directory, String fileName) {
        PrintWriter writer = null;
        FileOutputStream fileOut = null;
        try {
            File file = new File(directory);
            if (!file.exists()) {
                String userhome = System.getProperty("user.home");
                file = new File(userhome);
            }
            File logFile = new File(file, fileName);
            fileOut = Utils.createFileOutputStream(logFile, false);
            writer = new PrintWriter(fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return writer;
    }
}

@@ -663,32 +663,165 @@
 %0A        try %7B%0D%0A
+            File file = new File(filePath);%0D%0A            file.setReadable(true, false);%0D%0A            file.setWritable(true, false);%0D%0A
             file
@@ -855,34 +855,20 @@
 tStream(
-new File(filePath)
+file
 , false)
@@ -1689,24 +1689,120 @@
 fileName);%0D%0A
+            logFile.setReadable(true, false);%0D%0A            logFile.setExecutable(true, false);%0D%0A
             

set the log file to be writable