00001 package edu.ksu.cis.bandera.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.io.*;
00036 import java.util.*;
00037 import edu.ksu.cis.bandera.bui.session.datastructure.*;
00038
00039
00040
00041
00042
00043 public class Logger {
00044 private static PrintStream stdout = null;
00045 private static PrintStream stderr = null;
00046 private static boolean keepLog = true;
00047 private static BanderaLog theLog = null;
00048 private static File logDir = new File(Preferences.getUserPrefDir() + File.separator + "log");
00049 private static String logPath = logDir.getAbsolutePath() + File.separator;
00050 public static void dump(Session s)
00051 {
00052 theLog.setInternal(true);
00053 System.out.println("User select the following session: N/A");
00054 //String st = s.getStringRepresentation();
00055 //if (st!=null) System.out.println(st);
00056 theLog.setInternal(false);
00057 }
00058 public static void keep() { keepLog = true; }
00059 public static void off()
00060 {
00061 // Return system's standard out and standard error
00062 System.setOut(stdout);
00063 System.setErr(stderr);
00064
00065 // Flush and close the interceptor
00066 theLog.flush();
00067 theLog.close();
00068
00069 // If we don't keep the log, delete it.
00070 if (!keepLog)
00071 {
00072 try
00073 {
00074 File logFile = new File(logPath + "current.log");
00075 if (logFile.exists()) { logFile.delete(); }
00076 } catch (Exception e)
00077 {
00078 System.out.println("WARNING: Cannot delete unused log data! You may want to delete current.log manually.");
00079 }
00080 } else renameCurrentLog();
00081 }
00082 public static void on()
00083 {
00084 try
00085 {
00086 if (!logDir.exists() && !logDir.mkdir()) throw new Exception("Error: Can't setup log directory");
00087 renameCurrentLog();
00088
00089 // Save system's output and errors.
00090 stdout = System.out;
00091 stderr = System.err;
00092
00093 PrintStream ps = new PrintStream(new FileOutputStream(logPath + "current.log"));
00094
00095 theLog = new BanderaLog(stdout, ps);
00096
00097 // Set the marks
00098 ps.println("This log file is created at " + Calendar.getInstance().getTime().toString());
00099 ps.println("------------------------------------------------------------------------");
00100 ps.println("System properties are:\n");
00101 System.getProperties().list(ps);
00102 ps.println("------------------------------------------------------------------------");
00103 ps.println("User's preferences are:\n");
00104 Preferences.getProperties().list(ps);
00105 ps.println("------------------------------------------------------------------------");
00106 ps.println("The log begins here:\n");
00107
00108 // Intercept system's output.
00109 System.setOut(theLog);
00110 System.setErr(theLog);
00111 } catch (Exception e)
00112 {
00113 System.out.println(e.getMessage());
00114 System.out.println("Error: Unable to setup Bandera logging system! Aborting...");
00115 System.exit(0);
00116 }
00117 }
00118 private static void renameCurrentLog() throws RuntimeException
00119 {
00120 File logFile = new File(logPath + "current.log");
00121
00122 // If the log file exists, it may be the remnant of previous run-time failure. So, keep it.
00123 if (logFile.exists())
00124 {
00125 int i = 0;
00126 File f = null;
00127 do {
00128 f = new File(logPath + String.valueOf(i) + ".log");
00129 i++;
00130 } while (f.exists());
00131 if (f != null)
00132 logFile.renameTo(f);
00133 else
00134 throw new RuntimeException("Error: Unable to rename previous log file. Try renaming current.log!");
00135 }
00136 }
00137 }