home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 9.3 KB | 290 lines |
- package symantec.itools.lang;
-
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.DateFormat;
- import java.util.Calendar;
-
-
- /**
- * Provides a single debug log file to be used by any number of
- * classes.
- *
- * @author D'Arcy Smith
- * @version 1.0, 07/01/1998
- * @since VCafe 2.1a
- */
-
- public class Debug
- {
- /**
- * The PrintWriter used for output.
- * @see #setWriter(java.lang.String, java.lang.Object, java.lang.String)
- * @see #setWriter(java.net.URL, java.lang.Object, java.lang.String)
- * @see #setWriter(java.io.File, java.lang.Object, java.lang.String)
- * @see #setWriter(java.io.OutputStream, java.lang.Object, java.lang.String)
- * @see #setWriter(java.io.Writer, java.lang.Object, java.lang.String)
- */
- protected static PrintWriter writer;
- /**
- * The value of the system property "os.name";
- */
- protected static String OSName;
- /**
- * The value of the system property "os.version";
- */
- protected static String OSVersion;
- /**
- * The value of the system property "os.arch";
- */
- protected static String OSPlatform;
-
- static
- {
- OSName = System.getProperty("os.name");
- OSVersion = System.getProperty("os.version");
- OSPlatform = System.getProperty("os.arch");
- }
-
- /**
- * Constructor - this should not be called... it is only protected
- * so that Debug can be subclassed.
- */
- protected Debug()
- {
- }
-
- /**
- * Set the file name to write the debug log to
- *
- * @param fileName the file name of the debug log
- * @param clazz the class that had "main" called
- * @param version the version of clazz
- * @return true if the writer was set
- * false if the writer already has a value
- * @exception IOException if any error occured attaching the log file
- */
- public static synchronized boolean setWriter(String fileName, Object clazz, String version)
- throws IOException
- {
- return (setWriter(new File(fileName), clazz, version));
- }
-
- /**
- * Set the url to write the debug log to
- *
- * @param url the url of the debug log
- * @param clazz the class that had "main" called
- * @param version the version of clazz
- * @return true if the writer was set
- * false if the writer already has a value
- * @exception IOException if any error occured attaching the log file
- */
- public static synchronized boolean setWriter(URL url, Object clazz, String version)
- throws IOException
- {
- return (setWriter(url.getFile(), clazz, version));
- }
-
- /**
- * Set the file to write the debug log to
- *
- * @param file the file of the debug log
- * @param clazz the class that had "main" called
- * @param version the version of clazz
- * @return true if the writer was set
- * false if the writer already has a value
- * @exception IOException if any error occured attaching the log file
- */
- public static synchronized boolean setWriter(File file, Object clazz, String version)
- throws IOException
- {
- return (setWriter(new FileWriter(file), clazz, version));
- }
-
- /**
- * Set the stream to write the debug log to
- *
- * @param stream the stream of the debug log
- * @param clazz the class that had "main" called
- * @param version the version of clazz
- * @return true if the writer was set
- * false if the writer already has a value
- * @exception IOException if any error occured attaching the log file
- */
- public static synchronized boolean setWriter(OutputStream stream, Object clazz, String version)
- {
- return (setWriter(new OutputStreamWriter(stream), clazz, version));
- }
-
- /**
- * Set the writer to write the debug log to.
- * If the writer (of the class - not the param)
- * is null then the log will be assigned.
- *
- * @param writer the writer of the debug log
- * @param clazz the class that had "main" called
- * @param version the version of clazz
- * @return true if the writer was set
- * false if the writer already has a value
- * @exception IOException if any error occured attaching the log file
- */
- public static synchronized boolean setWriter(Writer w, Object clazz, String version)
- {
- if(writer == null)
- {
- writer = new PrintWriter(w, true);
-
- writer.println("OS Information:");
- writer.println(" - OS : " + OSName);
- writer.println(" - Version : " + OSVersion);
- writer.println(" - Platform : " + OSPlatform);
- writer.println(" - JDK Version : " + System.getProperty("java.version"));
- writer.println(" - JDK Vendor : " + System.getProperty("java.vendor"));
- writer.println();
- writer.println("Program Information:");
- writer.println(" - Main Class : " + clazz.getClass().getName());
- writer.println(" - Version : " + version);
- writer.println();
- writer.println("Start of debugging log - Time: " +
- DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(Calendar.getInstance().getTime()));
- writer.println();
-
- return (true);
- }
-
- return (false);
- }
-
- /**
- * Write out a debug message
- *
- * @param msg the message to write
- */
- public static synchronized void write(String msg)
- {
- if(writer != null)
- {
- writer.println(msg);
- }
- }
-
- /**
- * Write out a debug message
- *
- * @param src an instance of the class where the message is coming from
- * @param msg the message to write
- */
- public static synchronized void write(Object src, long line, String msg)
- {
- if(writer != null)
- {
- writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + msg);
- }
- }
-
- /**
- * Write out a debug message
- *
- * @param src an instance of the class where the message is coming from
- * @param action the action that is being reported
- * @param result the reult of the action
- */
- public static synchronized void write(Object src, long line, String action, String result)
- {
- if(writer != null)
- {
- writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + action + " :: " + result);
- }
- }
-
- /**
- * Write out a debug message
- *
- * @param src an instance of the class where the message is coming from
- * @param ex the exception that was thrown
- */
- public static synchronized void write(Object src, long line, Throwable ex)
- {
- if(writer != null)
- {
- writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + ex.getMessage());
- ex.printStackTrace(writer);
- }
- }
-
- /**
- * Finish writing the debug log.
- */
- public static synchronized void finish()
- {
- writer.println();
- writer.println("End of debugging log - Time: " +
- DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(Calendar.getInstance().getTime()));
- writer.close();
- }
-
- /**
- * Get the current line number
- *
- * This has only been tested with the Sun JDK 1.1.5 and VCafe JDK 1.1.4
- * on Windows. I need to make use of the OS class & Java Vendor
- * to make it work for others (assuming they display stack traces
- * differently from Sun and Symantec).
- */
- public static synchronized long getLineNumber()
- {
- Throwable stackTrace;
- StringWriter writer;
- BufferedReader reader;
-
- stackTrace = new Throwable().fillInStackTrace();
- writer = new StringWriter();
- stackTrace.printStackTrace(new PrintWriter(writer));
- reader = new BufferedReader(new StringReader(writer.toString()));
-
- try
- {
- String line;
-
- reader.readLine();
- reader.readLine();
- line = reader.readLine();
-
- if(line != null)
- {
- line = line.substring(line.lastIndexOf(':') + 1, line.length() - 1);
- return (Long.parseLong(line));
- }
- }
- catch(IOException ex)
- {
- }
- catch(NumberFormatException ex)
- {
- }
-
- return (-1L);
- }
-
- private static String convertLineNumber(long line)
- {
- if(line == -1)
- {
- return ("Unknown line");
- }
-
- return (Long.toString(line));
- }
- }