RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package zephyr.plugin.core.api.internal.monitoring.fileloggers; 00002 00003 import java.io.BufferedOutputStream; 00004 import java.io.File; 00005 import java.io.FileOutputStream; 00006 import java.io.IOException; 00007 import java.io.OutputStream; 00008 import java.io.PrintWriter; 00009 import java.io.Writer; 00010 import java.util.zip.GZIPOutputStream; 00011 00012 import zephyr.plugin.core.api.internal.monitoring.helpers.Loggers; 00013 00014 public class AbstractFileLogger { 00015 public static final String TEMP = ".tmp"; 00016 public static final String GZEXT = ".gz"; 00017 public final String filepath; 00018 protected PrintWriter file; 00019 protected final boolean temporaryFile; 00020 00021 public AbstractFileLogger(String filepath, boolean temporaryFile) throws IOException { 00022 this.filepath = filepath; 00023 checkFolders(); 00024 this.temporaryFile = temporaryFile; 00025 String fileCreatedPath = temporaryFile ? this.filepath + TEMP : this.filepath; 00026 OutputStream outputStream = createOutputStream(fileCreatedPath); 00027 file = new PrintWriter(outputStream, true); 00028 } 00029 00030 protected OutputStream createOutputStream(String fileCreatedPath) throws IOException { 00031 OutputStream out = new FileOutputStream(fileCreatedPath); 00032 if (fileCreatedPath.endsWith(GZEXT)) 00033 out = new GZIPOutputStream(out); 00034 return new BufferedOutputStream(out); 00035 } 00036 00037 private void checkFolders() { 00038 File logFile = new File(filepath); 00039 File folder = logFile.getParentFile(); 00040 if (!folder.isDirectory() && !folder.mkdirs()) 00041 throw new RuntimeException("Could not create folder"); 00042 } 00043 00044 public AbstractFileLogger(Writer writer) { 00045 file = new PrintWriter(writer); 00046 temporaryFile = false; 00047 filepath = null; 00048 } 00049 00050 public void close() { 00051 file.flush(); 00052 file.close(); 00053 file = null; 00054 if (temporaryFile) { 00055 finaliseFile(filepath); 00056 } 00057 } 00058 00059 static private File currentFilename(String filepath) { 00060 String[] possibilities = new String[] { filepath + TEMP, filepath }; 00061 for (String possibility : possibilities) { 00062 File possibleFile = new File(possibility); 00063 if (possibleFile.canRead()) 00064 return possibleFile; 00065 } 00066 return null; 00067 } 00068 00069 static public boolean exist(String filepath) { 00070 return currentFilename(filepath) != null; 00071 } 00072 00073 static public boolean isTemporary(String filepath) { 00074 File possibleFile = currentFilename(filepath); 00075 if (possibleFile == null) 00076 return false; 00077 return possibleFile.getAbsolutePath().endsWith(TEMP); 00078 } 00079 00080 public static String makeTemporary(String filepath) throws IOException { 00081 assert !isTemporary(filepath); 00082 File currentFileName = currentFilename(filepath); 00083 if (currentFileName == null) { 00084 String temporaryFilepath = filepath + TEMP; 00085 File temporaryFile = new File(temporaryFilepath); 00086 File parentFolder = new File(temporaryFile.getParent()); 00087 if (!parentFolder.canRead()) 00088 parentFolder.mkdirs(); 00089 if (!temporaryFile.createNewFile()) 00090 throw new RuntimeException("Error creating " + temporaryFilepath); 00091 return temporaryFilepath; 00092 } 00093 File resultFile = new File(currentFileName.getAbsolutePath() + TEMP); 00094 Loggers.copyFile(currentFileName, resultFile); 00095 return resultFile.getAbsolutePath(); 00096 } 00097 00098 static public void finaliseFile(String filepath) { 00099 File currentFile = currentFilename(filepath); 00100 String path = currentFile.getAbsolutePath(); 00101 assert path.endsWith(TEMP); 00102 File finalFile = new File(path.substring(0, path.length() - TEMP.length())); 00103 if (!currentFile.renameTo(finalFile)) 00104 throw new RuntimeException("Error renaming to " + filepath); 00105 } 00106 }