home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
Debug.java
< prev
next >
Wrap
Text File
|
1998-10-24
|
12KB
|
551 lines
package com.symantec.itools.lang;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.text.DateFormat;
import java.util.Calendar;
import com.symantec.itools.util.Properties;
/**
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public class Debug
{
/**
* @since VCafe 3.0
*/
public static final boolean DEBUG_OFF;
/**
* @since VCafe 3.0
*/
public static final boolean ASSERT_OFF;
/**
* @since VCafe 3.0
*/
public static final int INFORMATION = 0x0001;
/**
* @since VCafe 3.0
*/
public static final int WARNING = 0x0002;
/**
* @since VCafe 3.0
*/
public static final int ERROR = 0x0004;
/**
* @since VCafe 3.0
*/
public static final int EXCEPTION = 0x0008;
/**
* @since VCafe 3.0
*/
public static final int TRACE = 0x0016;
/**
* @since VCafe 3.0
*/
public static final int ASSERT = 0x0032;
/**
* @since VCafe 3.0
*/
protected static boolean informationOff;
protected static boolean warningOff;
protected static boolean errorOff;
protected static boolean exceptionOff;
protected static boolean traceOff;
protected static boolean assertOff;
/**
* @since VCafe 3.0
*/
protected static PrintWriter writer;
static
{
Properties properties;
try
{
properties = new Properties("/com/symantec/itools/lang/Debug.properties");
}
catch(Throwable ex)
{
properties = new Properties();
}
try
{
DEBUG_OFF = properties.getBoolean("debug.off", true);
}
catch(Throwable ex)
{
DEBUG_OFF = true;
}
try
{
ASSERT_OFF = properties.getBoolean("assert.off", true);
}
catch(Throwable ex)
{
ASSERT_OFF = true;
}
setInformationOff(properties.getBoolean("debug.information.off", false));
setWarningOff(properties.getBoolean("debug.warning.off", false));
setErrorOff(properties.getBoolean("debug.error.off", false));
setExceptionOff(properties.getBoolean("debug.excption.off", false));
setTraceOff(properties.getBoolean("debug.trace.off", true));
setAssertOff(properties.getBoolean("debug.assert.off", true));
}
protected Debug()
{
throw new IllegalInstantiationError(Debug.class);
}
/**
* @param str TODO
* @exception java.io.IOException
* @since VCafe 3.0
*/
public static void setWriter(String str)
throws IOException
{
if(writer == null)
{
if(str.equals("System.out"))
{
setWriter(System.out);
}
else if(str.equals("System.err"))
{
setWriter(System.err);
}
else
{
setWriter(new FileWriter(str));
}
}
}
/**
* @param w TODO
* @since VCafe 3.0
*/
public static void setWriter(Writer w)
{
if(writer == null)
{
writer = new PrintWriter(new BufferedWriter(w), true);
}
}
/**
* @param stream TODO
* @since VCafe 3.0
*/
public static void setWriter(OutputStream stream)
{
if(writer == null)
{
setWriter(new OutputStreamWriter(stream));
}
}
/**
* @param file TODO
* @exception java.io.IOException
* @since VCafe 3.0
*/
public static void setWriter(File file)
throws IOException
{
if(writer == null)
{
setWriter(new FileWriter(file));
}
}
/**
* @param url TODO
* @exception java.io.IOException
* @since VCafe 3.0
*/
public static void setWriter(URL url)
throws IOException
{
if(writer == null)
{
if(url.getProtocol().equals("file"))
{
setWriter(new FileWriter(url.getFile()));
}
else
{
throw new IOException("URL is not a local file");
}
}
}
/**
* @param descriptor TODO
* @since VCafe 3.0
*/
public static void setWriter(FileDescriptor descriptor)
{
if(writer == null)
{
setWriter(new FileWriter(descriptor));
}
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setInformationOff(boolean f)
{
informationOff = f;
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setWarningOff(boolean f)
{
warningOff = f;
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setErrorOff(boolean f)
{
errorOff = f;
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setExceptionOff(boolean f)
{
exceptionOff = f;
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setTraceOff(boolean f)
{
traceOff = f;
}
/**
* @param f TODO
* @since VCafe 3.0
*/
public static void setAssertOff(boolean f)
{
assertOff = f;
}
/**
* @since VCafe 3.0
*/
public static boolean isInformationOff()
{
return (informationOff);
}
/**
* @since VCafe 3.0
*/
public static boolean isWarningOff()
{
return (warningOff);
}
/**
* @since VCafe 3.0
*/
public static boolean isErrorOff()
{
return (errorOff);
}
/**
* @since VCafe 3.0
*/
public static boolean isExceptionOff()
{
return (exceptionOff);
}
/**
* @since VCafe 3.0
*/
public static boolean isTraceOff()
{
return (traceOff);
}
/**
* @since VCafe 3.0
*/
public static boolean isAssertOff()
{
return (assertOff);
}
/**
* @param clazz TODO
* @param version TODO
* @since VCafe 3.0
*/
public static void startLog(Class clazz, String version)
{
if(writer != null)
{
writer.println("Platform Information:");
writer.println(" - OS : " + System.getProperty("os.name"));
writer.println(" - Version : " + System.getProperty("os.version"));
writer.println(" - Platform : " + System.getProperty("os.arch"));
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.getName());
writer.println(" - Version : " + version);
writer.println();
writer.println("Start of debugging log - " +
DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
.format(Calendar.getInstance().getTime()));
}
}
/**
* @since VCafe 3.0
*/
public static void finishLog()
{
if(writer != null)
{
writer.println();
writer.println("End of debugging log - " +
DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
.format(Calendar.getInstance().getTime()));
writer.close();
writer = null;
}
}
/**
* @param msg TODO
* @since VCafe 3.0
*/
public static void logInformation(String msg)
{
if(!(isInformationOff()))
{
log(INFORMATION, getExecutionContext(1), msg);
}
}
/**
* @param msg TODO
* @since VCafe 3.0
*/
public static void logWarning(String msg)
{
if(!(isWarningOff()))
{
log(WARNING, getExecutionContext(1), msg);
}
}
/**
* @param msg TODO
* @since VCafe 3.0
*/
public static void logError(String msg)
{
if(!(isErrorOff()))
{
log(ERROR, getExecutionContext(1), msg);
}
}
/**
* @param ex TODO
* @since VCafe 3.0
*/
public static void logException(Throwable ex)
{
if(!(isExceptionOff()))
{
log(EXCEPTION, getExecutionContext(1), ex.getClass().getName() + " : " + ex.getMessage());
if(writer != null)
{
ex.printStackTrace(writer);
writer.flush();
}
}
}
/**
* @param action TODO
* @since VCafe 3.0
*/
public static void logTrace(String action)
{
if(!(isTraceOff()))
{
ExecutionContext context;
context = getExecutionContext(1);
log(TRACE, context, action + " " + context.getClazz() + " " + context.getMethod());
}
}
/**
* @param type TODO
* @param context TODO
* @param msg TODO
* @since VCafe 3.0
*/
protected static void log(int type, ExecutionContext context, String msg)
{
if(writer != null)
{
writer.print(typeToString(type) + " - ");
writer.print(context + " - ");
writer.println(msg);
writer.flush();
}
}
protected static String typeToString(int type)
{
switch(type)
{
case INFORMATION:
{
return ("INFORMATION");
}
case WARNING:
{
return ("WARNING");
}
case ERROR:
{
return ("ERROR");
}
case EXCEPTION:
{
return ("EXCEPTION");
}
case TRACE:
{
return ("TRACE");
}
}
return ("unknown");
}
/**
* @since VCafe 3.0
*/
protected static String getStackTrace()
{
Throwable stackTrace;
StringWriter writer;
stackTrace = new Throwable().fillInStackTrace();
writer = new StringWriter();
stackTrace.printStackTrace(new PrintWriter(writer));
return (writer.getBuffer().toString());
}
/**
* @param level TODO
* @since VCafe 3.0
*/
public static ExecutionContext getExecutionContext(int level)
{
return (new ExecutionContext(getStackTrace(), level));
}
/**
* @param f TODO
* @param msg TODO
* @since VCafe 3.0
*/
public static void assert(boolean f, String msg)
{
if(!(isAssertOff()))
{
if(!f)
{
throw new AssertionError(msg);
}
}
}
}