FileDocCategorySizeDatePackage
Print.javaAPI DocGlassfish v2 API23657Fri May 04 22:32:08 BST 2007com.sun.enterprise.util

Print

public class Print extends Object

Fields Summary
public static final String
SYSTEM_DEBUG_MODE
public static final String
SYSTEM_STACK_FRAME
private static Throwable
stackTrace
private static boolean
showStackFrameSource
private static PrintStream
printStackTrace_Stream
private static File
printStackTrace_LogFile
Constructors Summary
Methods Summary
private static void_printStackTrace(java.io.PrintStream out, int frame, java.lang.String title, java.lang.String msg, java.lang.Throwable excp)


	/* header vars */
	final String _dash = "----------------------------------------"; // 40
	final String dashLine = _dash + _dash + "\n"; // 80
	final String _ttl = " " + title + " ";
	final String errTitle = dashLine.substring(0, 16) + _ttl + dashLine.substring(16 + _ttl.length());

	/* default PrintStream */
	if (out == null) {
	    out = System.out;
	}

	/* header */
	Print.print(out, "\n");
	Print.print(out, dashLine + errTitle);
	Print.print(out, "[" + Print.getStackFrameString(frame + 1) + "]\n");
	if ((msg != null) && !msg.equals("")) { Print.print(out, msg + "\n"); }

	/* stack trace */
	if (excp != null) {
	    Print.print(out, excp.toString() + "\n");
	    Print.print(out, dashLine);
	    excp.printStackTrace(out);
	} else {
	    Print.print(out, dashLine);
	    Print.print(out, "Stack Trace:\n");
	    Throwable t = new Throwable();
	    t.fillInStackTrace();
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    PrintStream ps = new PrintStream(bos);
	    t.printStackTrace(ps);
	    StringTokenizer st = new StringTokenizer(bos.toString(), "\n");
	    st.nextToken(); // "java.lang.Throwable"
	    for (int i = 0; i < frame + 1; i++) { st.nextToken(); } // discard superfluous stack frames
	    for (;st.hasMoreTokens();) { Print.println(out, st.nextToken()); }
	}

	/* final dashed line */
	Print.print(out, dashLine);
	Print.print(out, "\n");

    
protected static void_println(java.io.PrintStream out, int frameNum, java.lang.String msg)

	String m = (frameNum >= 0)? "[" + getStackFrameString(frameNum + 1) + "] " + msg : msg;
	print(out, m + "\n");
    
public static voiddprint(java.io.PrintStream out, java.lang.String msg)

	if (isDebugMode()) {
	    print(out, msg);
	}
    
public static voiddprint(java.lang.String msg)

	if (isDebugMode()) {
	    print(null, msg);
	}
    
public static voiddprintStackTrace(java.io.PrintStream out, int frame, java.lang.String title, java.lang.String msg, java.lang.Throwable excp)

	if (isDebugMode()) {
	    if (out == null) { out = printStackTrace_Stream; }
	    if ((out != null) && (out != System.out) && (out != System.err)) {
	        String m = (excp != null)? (msg + " (" + excp + ")") : msg;
	        Print.println(null, frame + 1, "{log} " + m);
	    }
	    _printStackTrace(out, frame + 1, title, msg, excp); 
	}
    
public static voiddprintStackTrace(java.lang.String title, java.lang.String msg, java.lang.Throwable excp)

	if (isDebugMode()) {
	    dprintStackTrace(null, 1, title, msg, excp);
	}
    
public static voiddprintStackTrace(java.lang.String msg, java.lang.Throwable excp)

	if (isDebugMode()) {
	    String title = (excp != null)? "(DEBUG) Exception" : "(DEBUG) StackTrace";
	    dprintStackTrace(null, 1, title, msg, excp);
	}
    
public static voiddprintStackTrace(java.lang.String msg)

	if (isDebugMode()) {
	    dprintStackTrace(null, 1, "(DEBUG) StackTrace", msg, null);
	}
    
public static voiddprintStackTrace(int frame, java.lang.String msg)

	if (isDebugMode()) {
	    dprintStackTrace(null, frame + 1, "(DEBUG) StackTrace", msg, null);
	}
    
public static voiddprintln(java.io.PrintStream out, int frameNum, java.lang.String msg)

	if (isDebugMode()) {
	    int f = (frameNum >= 0)? (frameNum + 1) : Math.abs(frameNum);
	    _println(out, f, msg);
	}
    
public static voiddprintln(int frameNum, java.lang.String msg)

	if (isDebugMode()) {
	    int f = (frameNum >= 0)? (frameNum + 1) : Math.abs(frameNum);
	    _println(null, f, msg);
	}
    
public static voiddprintln(java.io.PrintStream out, java.lang.String msg)

	if (isDebugMode()) {
	    _println(out, 1, msg);
	}
    
public static voiddprintln(java.lang.String msg)

	if (isDebugMode()) {
	    _println(null, 1, msg);
	}
    
public static java.lang.StackTraceElementgetStackFrame(int frameNum)

	if (stackTrace == null) { stackTrace = new Throwable(); }
	stackTrace.fillInStackTrace();
	return stackTrace.getStackTrace()[frameNum + 1];
    
public static java.lang.StringgetStackFrameString(java.lang.StackTraceElement stackFrame, boolean showSrc)

	StringBuffer sb = new StringBuffer();
	if (showSrc) {
	    String s = stackFrame.getFileName();
	    if (s != null) {
		int p = s.lastIndexOf(File.separator);
	        sb.append((p >= 0)? s.substring(p + 1) : s);
	    } else {
	        String c = stackFrame.getClassName();
	        int p = c.lastIndexOf(".");
	        sb.append("<").append((p >= 0)? c.substring(p + 1) : c).append(">");
	    }
	} else {
	    String c = stackFrame.getClassName();
	    int p = c.lastIndexOf(".");
	    sb.append((p >= 0)? c.substring(p + 1) : c);
	}
	sb.append(".").append(stackFrame.getMethodName());
	if (stackFrame.getLineNumber() >= 0) {
	    sb.append(":").append(stackFrame.getLineNumber());
	}
	return sb.toString();
    
public static java.lang.StringgetStackFrameString(int frameNum)

	return getStackFrameString(frameNum + 1, showStackFrameSource);
    
public static java.lang.StringgetStackFrameString(java.lang.StackTraceElement stackFrame)

	return getStackFrameString(stackFrame, showStackFrameSource);
    
public static java.lang.StringgetStackFrameString(int frameNum, boolean showSrc)

	return getStackFrameString(getStackFrame(frameNum + 1), showSrc);
    
public static booleanisDebugMode()

	return Boolean.getBoolean(SYSTEM_DEBUG_MODE);
    
public static voidprint(java.io.PrintStream out, java.lang.String msg)

	((out != null)? out : System.out).print(msg);
    
public static voidprint(java.lang.String msg)

	print(null, msg);
    
public static booleanprintStackFrame()

	return Boolean.getBoolean(SYSTEM_STACK_FRAME);
    
public static voidprintStackTrace(java.io.PrintStream out, int frame, java.lang.String title, java.lang.String msg, java.lang.Throwable excp)

	if (out == null) { out = printStackTrace_Stream; }
	if ((out != null) && (out != System.out) && (out != System.err)) {
	    Print.print("\n");
	    if (excp != null) {
	        String m = ((msg != null) && !msg.equals(""))? (msg + " - ") : "";
		Print.println(null, frame + 1, "Error: " + m + excp + "");
		StackTraceElement ste[] = excp.getStackTrace();
		if (ste.length > 0) {
	            Print.print("  ==> at " + getStackFrameString(ste[0]) + "\n");
		}
	    } else {
	        Print.println(null, frame + 1, msg);
	    }
	    if (printStackTrace_LogFile != null) {
	        Print.print("(Stack trace logged to '" + printStackTrace_LogFile + "')\n");
	    }
	    Print.print("\n");
	}
	_printStackTrace(out, frame + 1, title, msg, excp);
    
public static voidprintStackTrace(java.lang.String title, java.lang.String msg, java.lang.Throwable excp)

	printStackTrace(null, 1, title, msg, excp);
    
public static voidprintStackTrace(java.lang.String msg, java.lang.Throwable excp)

	String title = (excp != null)? "Exception" : "StackTrace";
	printStackTrace(null, 1, title, msg, excp);
    
public static voidprintStackTrace(java.lang.String msg)

	printStackTrace(null, 1, "StackTrace", msg, null);
    
public static voidprintStackTrace(int frame, java.lang.String msg)

	printStackTrace(null, frame + 1, "StackTrace", msg, null);
    
public static java.lang.StringprintStackTraceToString()

        Throwable t = new Throwable("printStackTraceToString");
        t.fillInStackTrace();

        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        t.printStackTrace(printWriter);
        String stackTrace = stringWriter.toString();

        printWriter.close();

        return stackTrace;
    
public static voidprintln(java.io.PrintStream out, int frameNum, java.lang.String msg)

	int f = (frameNum >= 0)? (frameNum + 1) : (printStackFrame()? Math.abs(frameNum) : -1);
	_println(out, f, msg);
    
public static voidprintln(int frameNum, java.lang.String msg)

	int f = (frameNum >= 0)? (frameNum + 1) : (printStackFrame()? Math.abs(frameNum) : -1);
	_println(null, f, msg);
    
public static voidprintln(java.io.PrintStream out, java.lang.String msg)

	_println(out, (printStackFrame()? 1 : -1), msg);
    
public static voidprintln(java.lang.String msg)

	_println(null, (printStackFrame()? 1 : -1), msg);
    
public static voidsetDebugMode(boolean state)


    /* -------------------------------------------------------------------------
    */

    /* Set the debug mode
    ** @param specified debug state
    */
        
    
	System.setProperty(SYSTEM_DEBUG_MODE, String.valueOf(state));
    
public static voidsetPrintStackFrame(boolean state)

	System.setProperty(SYSTEM_STACK_FRAME, String.valueOf(state));
    
public static voidsetShowStackFrameSource(boolean flag)

	showStackFrameSource = flag;
    
public static voidsetStackTraceLogFile(java.io.File logFile)


    /* Set default output file for logging stack traces (logging defaults to System.out)
    ** @param the output file to log stack traces
    */
        
    

	/* close old log file */
	if (printStackTrace_Stream != null) {
	    printStackTrace_Stream.close();
	    printStackTrace_Stream = null;
	    printStackTrace_LogFile = null;
	}

	/* open new log file */
	if ((logFile != null) && logFile.isAbsolute()) {
	    try {
	        printStackTrace_Stream = new PrintStream(new FileOutputStream(logFile), true);
		printStackTrace_LogFile = logFile;
	    } catch (IOException ioe) {
		Print.println(0, "Unable to open StackTrace log file: " + logFile);
	    }
	}