Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Logger.java

00001 package edu.ksu.cis.bandera.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000  Roby Joehanes (robbyjo@cis.ksu.edu)           *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import java.io.*;
00036 import java.util.*;
00037 import edu.ksu.cis.bandera.bui.session.datastructure.*;
00038 
00039 /**
00040  * This is a logging and bug-reporting tool. Creates log files under (USER_HOME_DIR)/.bandera/log/*.log.
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 }

Generated at Thu Feb 7 06:49:59 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001