📄 debug.java
字号:
error("Debug: can't set up <" + file + "> for log file! \n" + ioe); return; } } /** * Provide a file to log output. This can be in conjunction with * the ouput stream, or instead of it. * * @param filename the file to use for the error log. * @param append if true, log the output at the end of the file, * instead of the beginning. * @param alsoToOutStream true if the out stream should still * provide output, in addition to logging the ouput. */ public static void directOutput(String filename, boolean append, boolean alsoToOutStream) { try { directOutput(new FileOutputStream(filename, append), alsoToOutStream); } catch (IOException ioe) { // If something goes wrong, set the output to the // System.out only, and hope someone sees it. notifyOut = true; out = System.out; error("Debug: can't set up <" + filename + "> for log file! \n" + ioe); return; } } /** * Provide a DataOutputStream to log output. This can be in * conjunction with the ouput stream, or instead of it. * * @param os the OutputStream that's handling outputlogging. * @param alsoToOutStream true if the out stream should still * provide output, in addition to logging the ouput. */ public static void directOutput(OutputStream os, boolean alsoToOutStream) { outputLog = new DataOutputStream(os); notifyOut = alsoToOutStream; } /** * Sets the error output stream to the named stream. * * @param err the desired error output stream */ public static void setErrorStream(PrintStream err) { Debug.err = err; } /** * Accessor for the current error output stream. * * @return the current error output stream. */ public static PrintStream getErrorStream() { return err; } /** * Provide a file to log errors. This can be in conjunction with * the errorstream, or instead of it. * * @param file the file to use for the error log. * @param alsoToErrStream true if the err stream should still * provide output, in addition to logging the errors. */ public static void directErrors(File file, boolean alsoToErrStream) { errorFile = file; notifyErr = alsoToErrStream; } /** * Provide a file to log errors. This can be in conjunction with * the errorstream, or instead of it. * * @param filename the file to use for the error log. * @param append if true, log the output at the end of the file, * instead of the beginning. * @param alsoToErrStream true if the err stream should still * provide output, in addition to logging the errors. */ public static void directErrors(String filename, boolean append, boolean alsoToErrStream) { errorAppend = append; errorFile = new File(filename); notifyErr = alsoToErrStream; } /** * Provide a DataOutputStream to log errors. This can be in * conjunction with the errorstream, or instead of it. * * @param os the DataOutputStream handling error logging. * @param alsoToErrStream true if the err stream should still * provide output, in addition to logging the errors. */ public static void directErrors(OutputStream os, boolean alsoToErrStream) { errorLog = new DataOutputStream(os); notifyErr = alsoToErrStream; } /** * Handle error messages, buy writing them to an error log, if * that has been set up, and/or to the error stream, if requested. * The special thing about error output is that the error is * framed with a header and a tail, hopefully to make it easier * for someone to spot in the log. * * @param errorString the string to write as an error. */ public static void error(String errorString) { try { if (errorLog == null) { // If no errors have happened yet, this will get run. if (errorFile != null) { FileOutputStream log = new FileOutputStream(errorFile.getPath(), errorAppend); errorLog = new DataOutputStream(log); // Lets make introductions into the error file, // shall we? errorLog.writeBytes("\n"); errorLog.writeBytes(getMapBeanMessage()); errorLog.writeBytes("\n"); errorLog.writeBytes("ERROR log file - " + java.util.Calendar.getInstance().getTime()); errorLog.writeBytes("\n"); errorLog.writeBytes(ERROR_TAIL); errorLog.writeBytes(ERROR_TAIL); errorLog.writeBytes(ERROR_TAIL); errorLog.writeBytes("\n"); ///////////////////// errorLog.writeBytes(ERROR_HEADER); errorLog.writeBytes(errorString); errorLog.writeBytes("\n"); errorLog.writeBytes(ERROR_TAIL); errorLog.writeBytes("\n"); } } else { // With the log already set up, this should get run. errorLog.writeBytes(ERROR_HEADER); errorLog.writeBytes("\n"); errorLog.writeBytes(errorString); errorLog.writeBytes("\n"); errorLog.writeBytes(ERROR_TAIL); errorLog.writeBytes("\n"); } } catch (IOException ioe) { // If something goes wrong, set the output to the // System.err only, and hope someone sees it. errorFile = null; notifyErr = true; err = System.err; err.println(ERROR_HEADER); err.println("Debug: error writing <" + errorString + "> to log! \n" + ioe); err.println(ERROR_TAIL); return; } // Write to the error stream if required. if (notifyErr) { err.println(ERROR_HEADER); err.println(errorString); err.println(ERROR_TAIL); } } /** * A reflective method to get the copyright message from the * MapBean without having to actually compile the MapBean when * Debug is compiled. */ public static String getMapBeanMessage() { String message = ""; try { Class mbClass = Class.forName("com.bbn.openmap.MapBean"); java.lang.reflect.Method crMessage = mbClass.getDeclaredMethod("getCopyrightMessage", (Class[])null); message = (String) crMessage.invoke(mbClass, (Object[])null); } catch (java.lang.reflect.InvocationTargetException ite) { System.out.println(ite.getMessage()); } catch (IllegalArgumentException iae) { System.out.println(iae.getMessage()); } catch (IllegalAccessException iae2) { System.out.println(iae2.getMessage()); } catch (NoSuchMethodException nme) { System.out.println(nme.getMessage()); } catch (NullPointerException npe) { System.out.println(npe.getMessage()); } catch (SecurityException se) { System.out.println(se.getMessage()); } catch (ClassNotFoundException cnfe) { System.out.println(cnfe.getMessage()); } return message; } /** println to output. */ public static void output() { Debug.output(""); } /** * Handle output messages, buy writing them to an output log, if * that has been set up, and/or to the output stream, if * requested. * * @param outputString the string to write as output. */ public static void output(String outputString) { try { if (outputLog != null) { outputLog.writeBytes(outputString); outputLog.writeBytes("\n"); } } catch (IOException ioe) { // If something goes wrong, set the output to the // System.out only, and hope someone sees it. notifyOut = true; out = System.out; error("Debug: output writing <" + outputString + "> to log! \n" + ioe); return; } // Write to the output stream if required. if (notifyOut) { out.println(outputString); } } /** * Provide a file to log output. This can be in conjunction with * the streams, or instead of them. This basically sets the output * log and the error log to be the same thing. * * @param file the file to use for the error log. * @param alsoToStreams true if the streams should still provide * output, in addition to logging the output. */ public static void setLog(File file, boolean alsoToStreams) { try { FileOutputStream logStream = new FileOutputStream(file); setLog(logStream, alsoToStreams); } catch (IOException ioe) { // If something goes wrong, set the output to the // System streams only, and hope someone sees it. resetOutput(); error("IOException trying to create a log file.\n" + ioe); } } /** * Provide an output stream to log output. This can be in * conjunction with the streams, or instead of them. This * basically sets the output log and the error log to be the same * thing. * * @param logStream the output stream for output. * @param alsoToStreams true if the streams should still provide * output, in addition to logging the output. */ public static void setLog(OutputStream logStream, boolean alsoToStreams) { DataOutputStream dos = new DataOutputStream(logStream); outputLog = dos; errorLog = dos; // Wrap up loose ends - if something happens to the // logStream, we'll just shut down logging, and force // everything to the output streams. errorFile = null; notifyErr = alsoToStreams; notifyOut = alsoToStreams; } /** * Reset the logging to the output. */ public static void resetOutput() { // If something goes wrong, set the output to the // System streams only, and hope someone sees it. notifyOut = true; errorFile = null; notifyErr = true; err = System.err; out = System.out; } /** * Dummy function to illustrate usage of the debugging class. */ public static void sampleUsage() { if (Debug.On && Debug.debugging("debug")) { Debug.output("debug message"); } else { Debug.output("try again"); } } /** * <code>main</code> routine used in unit testing. */ public static void main(String args[]) { Debug.init(System.getProperties()); Debug.sampleUsage(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -