Initial commit from SVN.
This commit is contained in:
60
Common/src/com/common/debug/ConsoleLog.java
Normal file
60
Common/src/com/common/debug/ConsoleLog.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.debug;
|
||||
|
||||
/**
|
||||
* This should be the base class for application specific logs.
|
||||
*/
|
||||
public class ConsoleLog implements ILog {
|
||||
private String lineSeparator = System.getProperty("line.separator");
|
||||
/**
|
||||
* DefaultLog constructor.
|
||||
*/
|
||||
public ConsoleLog() {
|
||||
super();
|
||||
}//ConsoleLog()//
|
||||
/**
|
||||
* Allows for common application specific handling of some exceptions.
|
||||
* The default log's behavior is to rethrow ThreadDeath exceptions and InterruptedExceptions so that threads can properly terminate.
|
||||
*/
|
||||
public void handle(Throwable exception) {
|
||||
if(exception instanceof ThreadDeath) {
|
||||
throw (ThreadDeath) exception;
|
||||
}//if//
|
||||
else if(exception instanceof InterruptedException) {
|
||||
Thread.currentThread().interrupt();
|
||||
}//else if//
|
||||
}//handle()//
|
||||
/**
|
||||
* The default is to print the stack trace to the standard output stream.
|
||||
*/
|
||||
public synchronized void log(String note, Throwable exception, int type, boolean display) {
|
||||
if(note != null && note.trim().length() > 0 && !note.equals("null")) {
|
||||
System.out.print(note + lineSeparator);
|
||||
}//if//
|
||||
|
||||
if(exception != null) {
|
||||
printException(exception);
|
||||
}//if//
|
||||
}//log()//
|
||||
/**
|
||||
* Recursively prints the exceptions in order of occurance.
|
||||
* @param exception The exception to print.
|
||||
*/
|
||||
private void printException(Throwable exception) {
|
||||
if(exception.getCause() != null) {
|
||||
printException(exception.getCause());
|
||||
}//if//
|
||||
|
||||
// if(exception.getMessage() != null && exception.getMessage().trim().length() > 0) {
|
||||
// System.out.println(exception.getMessage());
|
||||
// }//if//
|
||||
|
||||
exception.printStackTrace();
|
||||
}//printException()//
|
||||
}//DefaultLog//
|
||||
144
Common/src/com/common/debug/Debug.java
Normal file
144
Common/src/com/common/debug/Debug.java
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 2009,2007 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */
|
||||
package com.common.debug;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* This is where all the orb debug information gets sent to.
|
||||
* A custom log can be specified by overriding DefaultDebugLog or implementing IDebugLog & calling OrbDebugLog.getSingleton().setDebugLog(myDebugLog);
|
||||
*/
|
||||
public final class Debug {
|
||||
public static final int TYPE_ERROR = ILog.TYPE_ERROR;
|
||||
public static final int TYPE_WARNING = ILog.TYPE_WARNING;
|
||||
public static final int TYPE_INFORMATION = ILog.TYPE_INFORMATION;
|
||||
|
||||
private static ILog log = null;
|
||||
/**
|
||||
* OrbDebugLog constructor.
|
||||
*/
|
||||
private Debug() {
|
||||
super();
|
||||
}//OrbDebugLog()//
|
||||
/**
|
||||
* Creates a string containing the full stack for the exception.
|
||||
* @param e The exception.
|
||||
* @return The stack text.
|
||||
*/
|
||||
public static String getExceptionStack(Throwable e) {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
|
||||
return stringWriter.toString();
|
||||
}//getExceptionStack()//
|
||||
/**
|
||||
* Gets the current debug log.
|
||||
*/
|
||||
public static ILog getLog() {
|
||||
if(log == null) {
|
||||
//setLog(new FrameLog());
|
||||
//((com.common.debug.FrameLog) getLog()).setVisible(true);
|
||||
setLog(new DefaultLog());
|
||||
}//if//
|
||||
|
||||
return log;
|
||||
}//getLog()//
|
||||
public static void halt() {
|
||||
try {
|
||||
Class debugSupportClass = Class.forName("com.ibm.uvm.tools.DebugSupport");
|
||||
Method haltMethod = debugSupportClass.getMethod("halt", new Class[0]);
|
||||
|
||||
haltMethod.invoke(null, new Object[0]);
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
}//catch//
|
||||
}//halt()//
|
||||
/**
|
||||
* Allows for common application specific handling of some exceptions.
|
||||
* The default log's behavior is to rethrow ThreadDeath exceptions and InterruptedExceptions so that threads can properly terminate.
|
||||
*/
|
||||
public static void handle(Throwable exception) {
|
||||
getLog().handle(exception);
|
||||
}//handle()//
|
||||
/**
|
||||
* Determines if a log has been set yet.
|
||||
*/
|
||||
public static boolean hasLog() {
|
||||
return log != null;
|
||||
}//hasLog()//
|
||||
public static void log(String note) {
|
||||
getLog().log(note, null, ILog.TYPE_INFORMATION, false);
|
||||
}//log()//
|
||||
public static void log(String note, boolean forceDisplay) {
|
||||
getLog().log(note, null, ILog.TYPE_INFORMATION, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(Throwable exception) {
|
||||
getLog().log(null, exception, ILog.TYPE_ERROR, true);
|
||||
}//log()//
|
||||
public static void log(String note, Throwable exception) {
|
||||
getLog().log(note, exception, exception == null ? ILog.TYPE_INFORMATION : ILog.TYPE_ERROR, true);
|
||||
}//log()//
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public static void log(Throwable exception, String note) {
|
||||
getLog().log(note, exception, exception == null ? ILog.TYPE_INFORMATION : ILog.TYPE_ERROR, true);
|
||||
}//log()//
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public static void log(Throwable exception, String note, boolean forceDisplay) {
|
||||
getLog().log(note, exception, exception == null ? ILog.TYPE_INFORMATION : ILog.TYPE_ERROR, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(String note, Throwable exception, boolean forceDisplay) {
|
||||
getLog().log(note, exception, exception == null ? ILog.TYPE_INFORMATION : ILog.TYPE_ERROR, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(Throwable exception, boolean forceDisplay) {
|
||||
getLog().log(null, exception, ILog.TYPE_ERROR, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(Object location, String note) {
|
||||
log(ILog.TYPE_INFORMATION, location, note);
|
||||
}//log()//
|
||||
public static void log(int type, String note) {
|
||||
getLog().log(note, null, type, false);
|
||||
}//log()//
|
||||
public static void log(int type, String note, boolean forceDisplay) {
|
||||
getLog().log(note, null, type, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(int type, Throwable exception) {
|
||||
getLog().log(null, exception, type, true);
|
||||
}//log()//
|
||||
public static void log(int type, Throwable exception, boolean forceDisplay) {
|
||||
getLog().log(null, exception, type, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(int type, String note, Throwable exception) {
|
||||
getLog().log(note, exception, type, true);
|
||||
}//log()//
|
||||
public static void log(int type, String note, Throwable exception, boolean forceDisplay) {
|
||||
getLog().log(note, exception, type, forceDisplay);
|
||||
}//log()//
|
||||
public static void log(int type, Object location, String note) {
|
||||
if(location != null) {
|
||||
String completeNote = "\r\n" + location.toString() + "\r\n" + note;
|
||||
|
||||
getLog().log(completeNote, null, type, true);
|
||||
}//if//
|
||||
else {
|
||||
log(note);
|
||||
}//else//
|
||||
}//log()//
|
||||
/**
|
||||
* Sets an application defined debug log object.
|
||||
* @param log ILog A log object that handles debug logging for the application.
|
||||
*/
|
||||
public static void setLog(ILog _log) {
|
||||
log = _log;
|
||||
}//setLog()//
|
||||
}//DebugSupport//
|
||||
20
Common/src/com/common/debug/DefaultLog.java
Normal file
20
Common/src/com/common/debug/DefaultLog.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.debug;
|
||||
|
||||
/**
|
||||
* This should be the base class for application specific logs.
|
||||
*/
|
||||
public class DefaultLog extends ConsoleLog {
|
||||
/**
|
||||
* DefaultLog constructor.
|
||||
*/
|
||||
public DefaultLog() {
|
||||
super();
|
||||
}//DefaultLog()//
|
||||
}//DefaultLog//
|
||||
110
Common/src/com/common/debug/FileLog.java
Normal file
110
Common/src/com/common/debug/FileLog.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2003,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.debug;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* Logs all output to a single log file.
|
||||
*/
|
||||
public class FileLog extends ConsoleLog {
|
||||
private DateFormat dateFormat = new SimpleDateFormat("MM/dd hh:mm:ss:SSS a");
|
||||
private File file = null;
|
||||
private File path = null;
|
||||
private String lineSeparator = System.getProperty("line.separator");
|
||||
/**
|
||||
* DefaultLog constructor.
|
||||
*/
|
||||
public FileLog() {
|
||||
this(new File('.' + System.getProperty("path.separator") + "log.txt"));
|
||||
}//FileLog()//
|
||||
/**
|
||||
* DefaultLog constructor.
|
||||
*/
|
||||
public FileLog(File file) {
|
||||
super();
|
||||
|
||||
this.path = new File(file.getParent());
|
||||
this.file = file;
|
||||
|
||||
//Clear previous log data.//
|
||||
if(file.exists()) {
|
||||
try {
|
||||
file.delete();
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
//Ignore.//
|
||||
}//catch//
|
||||
}//if//
|
||||
}//FileLog()//
|
||||
/**
|
||||
* The default is to print the stack trace to the standard output stream.
|
||||
* @param note The note to be logged.
|
||||
* @param exception The exception to be logged.
|
||||
* @param display Whether to force the log output. This is currently ignored.
|
||||
*/
|
||||
public synchronized void log(String note, Throwable exception, int type, boolean display) {
|
||||
try {
|
||||
PrintWriter writer = null;
|
||||
|
||||
if(!path.exists()) {
|
||||
if(!path.mkdirs()) {
|
||||
System.out.println("Unable to create the required log directory.");
|
||||
}//if//
|
||||
}//if//
|
||||
|
||||
if(path.exists()) {
|
||||
writer = new PrintWriter(new FileWriter(file.toString(), file.exists()));
|
||||
}//if//
|
||||
|
||||
writer.write(dateFormat.format(new Date()));
|
||||
writer.write('\t');
|
||||
|
||||
//Write the note if there is one.//
|
||||
if(note != null && note.trim().length() > 0) {
|
||||
writer.write(note);
|
||||
}//if//
|
||||
|
||||
//Write the exception if there is one.//
|
||||
if(exception != null) {
|
||||
if(note != null) {
|
||||
writer.write(lineSeparator);
|
||||
}//if//
|
||||
|
||||
printException(writer, exception);
|
||||
}//if//
|
||||
|
||||
writer.write(lineSeparator);
|
||||
writer.close();
|
||||
}//try//
|
||||
catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}//catch//
|
||||
}//log()//
|
||||
/**
|
||||
* Recursively prints the exceptions in order of occurance.
|
||||
* @param writer The writer to use for printing.
|
||||
* @param exception The exception to print.
|
||||
*/
|
||||
private void printException(PrintWriter writer, Throwable exception) {
|
||||
if(exception.getCause() != null) {
|
||||
printException(writer, exception.getCause());
|
||||
}//if//
|
||||
|
||||
if(exception.getMessage() != null && exception.getMessage().trim().length() > 0) {
|
||||
writer.write(exception.getMessage());
|
||||
writer.write(lineSeparator);
|
||||
}//if//
|
||||
|
||||
exception.printStackTrace(writer);
|
||||
writer.write(lineSeparator);
|
||||
}//printException()//
|
||||
}//FileLog//
|
||||
31
Common/src/com/common/debug/ILog.java
Normal file
31
Common/src/com/common/debug/ILog.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2005 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.debug;
|
||||
|
||||
/**
|
||||
* The ILog interface defines the basic structure of an application specific logging class.
|
||||
* This class will handle exceptions and debug notes for the application in an application specific way.
|
||||
*/
|
||||
public interface ILog {
|
||||
public static final int TYPE_ERROR = 0;
|
||||
public static final int TYPE_WARNING = 1;
|
||||
public static final int TYPE_INFORMATION = 2;
|
||||
/**
|
||||
* Allows for common application specific handling of some exceptions.
|
||||
* The default log's behavior is to rethrow ThreadDeath exceptions and InterruptedExceptions so that threads can properly terminate.
|
||||
*/
|
||||
public void handle(Throwable exception);
|
||||
/**
|
||||
* Handles logging a note and/or exception.
|
||||
* @param note String An optional note to be logged. If an exception is also specified, then the note should be bundled with the exception in the log output.
|
||||
* @param exception Throwable An optional exception to be logged.
|
||||
* @param type The type of log entry this is. One of the type codes.
|
||||
* @param forceDisplay boolean A flag indicating whether the note/exception should be displayed. The log may treat this how ever it desires.
|
||||
*/
|
||||
public void log(String note, Throwable exception, int type, boolean forceDisplay);
|
||||
}//ILog//
|
||||
171
Common/src/com/common/debug/QueuedLog.java
Normal file
171
Common/src/com/common/debug/QueuedLog.java
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 1999,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.common.debug;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import com.common.util.IReversableIterator;
|
||||
import com.common.util.CircularQueue;
|
||||
|
||||
/**
|
||||
* Queues log entries and writes log files containing the queued entries when a display is requested.
|
||||
*/
|
||||
public class QueuedLog extends ConsoleLog {
|
||||
private DateFormat dateFormat = new SimpleDateFormat("MM/dd hh:mm:ss:SSS a");
|
||||
private File path = null;
|
||||
private String logFileNamePrefix = null;
|
||||
private String countFileName = null;
|
||||
private String lineSeparator = System.getProperty("line.separator");
|
||||
private CircularQueue queue = null;
|
||||
private boolean useTimestamps = false;
|
||||
private int fileIndex = 0;
|
||||
/** Whether to display the log content in order of descending time (newest to oldest log entry). */
|
||||
private boolean descendingTimeOrder = true;
|
||||
/**
|
||||
* QueuedLog constructor.
|
||||
*/
|
||||
public QueuedLog() {
|
||||
this(new File("."), 100, true);
|
||||
}//QueuedLog()//
|
||||
/**
|
||||
* QueuedLog constructor.
|
||||
* @param path The path where log files will be located.
|
||||
* @param queueSize The size of the circular queue.
|
||||
*/
|
||||
public QueuedLog(File path, int queueSize) {
|
||||
this(path, queueSize, true);
|
||||
}//QueuedLog()//
|
||||
/**
|
||||
* QueuedLog constructor.
|
||||
* @param path The path where log files will be located.
|
||||
* @param queueSize The size of the circular queue.
|
||||
* @param descendingTimeOrder Whether to display the log content in order of descending time (newest to oldest log entry).
|
||||
*/
|
||||
public QueuedLog(File path, int queueSize, boolean descendingTimeOrder) {
|
||||
super();
|
||||
|
||||
this.queue = new CircularQueue(queueSize);
|
||||
this.path = path == null ? new File(".") : path;
|
||||
this.countFileName = "count.txt";
|
||||
this.logFileNamePrefix = "log";
|
||||
this.descendingTimeOrder = descendingTimeOrder;
|
||||
}//QueuedLog()//
|
||||
/**
|
||||
* The default is to print the stack trace to the standard output stream.
|
||||
*/
|
||||
public synchronized void log(String note, Throwable exception, int type, boolean display) {
|
||||
StringWriter writer = new StringWriter((useTimestamps ? 9 : 0) + (note != null ? note.length() : 0) + (exception != null ? 400 : 0));
|
||||
|
||||
if(useTimestamps) {
|
||||
writer.write(dateFormat.format(new Date()));
|
||||
writer.write('\t');
|
||||
}//if//
|
||||
|
||||
if(note != null && note.trim().length() > 0) {
|
||||
writer.write(note);
|
||||
}//if//
|
||||
|
||||
if(exception != null) {
|
||||
PrintWriter printWriter = new PrintWriter(writer, true);
|
||||
|
||||
printException(printWriter, exception);
|
||||
}//if//
|
||||
|
||||
queue.enqueue(writer.getBuffer().toString());
|
||||
|
||||
if(display) {
|
||||
writeLogFile();
|
||||
}//if//
|
||||
}//log()//
|
||||
/**
|
||||
* Writes the a log file.
|
||||
*/
|
||||
public synchronized void writeLogFile() {
|
||||
try {
|
||||
PrintWriter writer = null;
|
||||
|
||||
if(!path.exists()) {
|
||||
if(!path.mkdirs()) {
|
||||
System.out.println("Unable to create the required log directory.");
|
||||
}//if//
|
||||
}//if//
|
||||
|
||||
if(path.exists()) {
|
||||
StringBuffer buffer = new StringBuffer(logFileNamePrefix.length() + 4);
|
||||
IReversableIterator iterator = null;
|
||||
|
||||
//Create the log file name.//
|
||||
buffer.append(logFileNamePrefix);
|
||||
|
||||
//Prefix the number with zero padding.//
|
||||
for(int count = Integer.toString(fileIndex).length(); count < 4; count++) {
|
||||
buffer.append('0');
|
||||
}//for//
|
||||
|
||||
buffer.append(fileIndex);
|
||||
buffer.append(".txt");
|
||||
//Write the log file.//
|
||||
writer = new PrintWriter(new FileWriter(new File(path, buffer.toString()), false));
|
||||
|
||||
writer.write("Below are the queued log entries beginning with the " + (descendingTimeOrder ? "most recent log and ending with the least recent" : "least recent log and ending with the most recent") + "...");
|
||||
writer.write(lineSeparator);
|
||||
writer.write("_______________________________________________________________________________________________________");
|
||||
writer.write(lineSeparator);
|
||||
iterator = queue.iterator();
|
||||
|
||||
if(descendingTimeOrder) {
|
||||
iterator.resetToBack();
|
||||
|
||||
//Write the log entries.//
|
||||
while(iterator.hasPrevious()) {
|
||||
writer.write(iterator.previous().toString());
|
||||
writer.write(lineSeparator);
|
||||
}//while//
|
||||
}//if//
|
||||
else {
|
||||
iterator.resetToFront();
|
||||
|
||||
//Write the log entries.//
|
||||
while(iterator.hasNext()) {
|
||||
writer.write(iterator.next().toString());
|
||||
writer.write(lineSeparator);
|
||||
}//while//
|
||||
}//else//
|
||||
|
||||
writer.close();
|
||||
//Write the count file.//
|
||||
writer = new PrintWriter(new FileWriter(new File(path, countFileName), false));
|
||||
writer.write("" + ++fileIndex);
|
||||
writer.close();
|
||||
}//if//
|
||||
}//try//
|
||||
catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}//catch//
|
||||
}//writeLogFile()//
|
||||
/**
|
||||
* Recursively prints the exceptions in order of occurance.
|
||||
* @param writer The writer to use for printing.
|
||||
* @param exception The exception to print.
|
||||
*/
|
||||
private void printException(PrintWriter writer, Throwable exception) {
|
||||
if(exception.getCause() != null) {
|
||||
printException(writer, exception.getCause());
|
||||
}//if//
|
||||
|
||||
if(exception.getMessage() != null && exception.getMessage().trim().length() > 0) {
|
||||
writer.write(exception.getMessage());
|
||||
writer.write(lineSeparator);
|
||||
}//if//
|
||||
|
||||
exception.printStackTrace(writer);
|
||||
writer.write(lineSeparator);
|
||||
}//printException()//
|
||||
}//QueuedLog//
|
||||
Reference in New Issue
Block a user