📄 print.java
字号:
protected static void _logStackTrace(int level, int frame, String msg, Throwable t) { /* log stack trace */ Print._log(level, frame + 1, msg); try { PrintStream out = Print.openLogFile(); _printStackTrace(out, frame + 1, null, t); } catch (Throwable loge) { _printStackTrace(null, frame + 1, null, t); } finally { Print.closeLogFile(); } /* email */ if (_emailExceptions()) { Print.sysPrintln("EMailing error..."); ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bos); String host = Print.getHostName(); /* include hostname */ out.println("From host: " + host); /* include stacktrace */ _printStackTrace(out, frame + 1, msg, t); /* close and send email */ out.close(); // may not be necessary //Print.emailError("[" + host + "] " + msg, bos.toString()); } } protected static void _printStackTrace(PrintStream out, int frame, String msg, Throwable t) { /* get default print stream */ if (out == null) { /* first try default stdout */ out = Print.getSysStdout(); if (out == null) { // failing that, set to stderr out = Print.getSystemErr(); } } /* print stack trace */ if (msg != null) { boolean printExceptionFrame = true; Print._println(out, frame + 1, printExceptionFrame, msg); } if (t == null) { t = new Throwable(); t.fillInStackTrace(); StackTraceElement oldst[] = t.getStackTrace(); StackTraceElement newst[] = new StackTraceElement[oldst.length - (frame + 1)]; System.arraycopy(oldst, frame + 1, newst, 0, newst.length); t.setStackTrace(newst); } t.printStackTrace(out); if (t instanceof SQLException) { SQLException sqe = ((SQLException)t).getNextException(); for (; (sqe != null); sqe = sqe.getNextException()) { sqe.printStackTrace(out); } } } // ------------------------------------------------------------------------ public static void logNotImplemented(String msg) { Print._logStackTrace(LOG_ERROR, 1, "Feature Not Implemented: " + msg, null); } public static void logException(String msg, Throwable t) { Print._logStackTrace(LOG_ERROR, 1, "Exception: " + msg, t); } public static void logStackTrace(String msg, Throwable t) { Print._logStackTrace(LOG_INFO, 1, "Stacktrace: " + msg, t); } public static void logStackTrace(Throwable t) { Print._logStackTrace(LOG_INFO, 1, "Stacktrace: ", t); } public static void logStackTrace(String msg) { Print._logStackTrace(LOG_INFO, 1, "Stacktrace: " + msg, null); } // ------------------------------------------------------------------------ public static void logSQLError(int frame, String msg, SQLException sqe) { PrintStream ps = null; Print._log(LOG_ERROR, frame + 1, "==> SQLException: " + msg); while (sqe != null) { Print._log(LOG_ERROR, frame + 1, "Message: " + sqe.getMessage()); Print._log(LOG_ERROR, frame + 1, "SQLState: " + sqe.getSQLState()); Print._log(LOG_ERROR, frame + 1, "ErrorCode: " + sqe.getErrorCode()); //if (sqe.getErrorCode() != DBFactory.SQLERR_DUPLICATE_KEY) { Print._printStackTrace(ps, frame + 1, sqe.toString(), sqe); //} sqe = sqe.getNextException(); } } public static void logSQLError(SQLException sqe) { Print.logSQLError(1, "", sqe); } public static void logSQLError(String msg, SQLException sqe) { Print.logSQLError(1, msg, sqe); } // ------------------------------------------------------------------------ private static Object logLock = new Object(); private static PrintStream logOutput = null; private static long logRefCount = 0L; public static void setLogFile(File file) { printLogFile = file; } public static File getLogFile() { return printLogFile; } private static int openLogFile_recursion = 0; protected static PrintStream openLogFile() { // Do not make calls to "logXXXXXX" from within this method (infinite recursion could result) // Calls to 'println' and 'sysPrintln' are ok. /* check to see if this has been called before RTConfig has completed initialization */ if (!RTConfig.isInitialized()) { return Print.getSystemErr(); } /* return log PrintStream */ PrintStream out = null; synchronized (Print.logLock) { if (openLogFile_recursion > 0) { Print.sysPrintln("[Print.openLogFile] Recursive call to 'openLogFile'!!!"); Throwable t = new Throwable(); t.fillInStackTrace(); t.printStackTrace(); return Print.getSystemErr(); } else { openLogFile_recursion++; /* increment log counter */ Print.logRefCount++; /* get log file */ if (Print.logOutput != null) { /* already open */ out = Print.logOutput; } else { /* get/return log file */ File file = Print.getLogFile(); if ((file == null) || file.toString().equals("")) { out = Print.getSystemErr(); } else if (file.isDirectory()) { Print.setLogFile(null); Print.sysPrintln("ERROR: Invalid Print log file specification: " + file); out = Print.getSystemErr(); } else { if (file.exists()) { long maxLogFileSize = printMaxLogFileSize; if ((maxLogFileSize > 8000L) && (file.length() > maxLogFileSize)) { String bkuName = file.getAbsolutePath() + "." + Print.formatDate("yyMMdd"); File bkuFile = new File(bkuName); for (int i = 1; bkuFile.exists(); i++) { bkuFile = new File(bkuName + "." + i); } file.renameTo(bkuFile); } } try { out = new PrintStream(new FileOutputStream(file,true)); } catch (IOException ioe) { Print.setLogFile(null); Print.sysPrintln("ERROR: Unable to open Print log file: " + file); out = Print.getSystemErr(); } } Print.logOutput = out; } openLogFile_recursion--; } } return out; } protected static void closeLogFile() { synchronized (Print.logLock) { /* decrement log counter */ Print.logRefCount--; if (Print.logRefCount < 0) { Print.logRefCount = 0L; } /* close */ if ((Print.logRefCount == 0L) && (Print.logOutput != null)) { // don't close if stderr or stdout if ((Print.logOutput != Print.getSystemOut()) && (Print.logOutput != Print.getSystemErr()) ) { try { Print.logOutput.close(); } catch (Throwable t) { Print.sysPrintln("Unable to close log file: " + t); } } Print.logOutput = null; } } } // ------------------------------------------------------------------------ public static void setLogLevel(int level, boolean inclDate, boolean inclFrame) { Print.setLogLevel(level); printLogIncludeDate = inclDate; printLogIncludeFrame = inclFrame; } public static void setLogLevel(int level) { if (level < LOG_OFF) { level = LOG_OFF; } else if (level > LOG_ALL) { level = LOG_ALL; } printLogLevel = level; } public static int getLogLevel() { return printLogLevel; } public static boolean isDebugLoggingLevel() { return (Print.getLogLevel() >= Print.LOG_DEBUG); } public static void setLogHeaderLevel(int level) { if (level < LOG_OFF) { level = LOG_OFF; } else if (level > LOG_ALL) { level = LOG_ALL; } printLogHeaderLevel = level; } public static int getLogHeaderLevel() { return printLogHeaderLevel; } public static String getLogLevelString(int level) { if (level <= LOG_OFF) { return "OFF"; } switch (level) { case LOG_FATAL: return "FATAL"; case LOG_ERROR: return "ERROR"; case LOG_WARN : return "WARN "; case LOG_INFO : return "INFO "; case LOG_DEBUG: return "DEBUG"; } return "ALL"; } public static int parseLogLevel(String val) { String v = (val != null)? val.toUpperCase() : null; if ((v == null) || v.equals("")) { return LOG_OFF; } else if (Character.isDigit(v.charAt(0))) { int lvl = StringTools.parseInt(v.substring(0,1),LOG_ALL); if (lvl < LOG_OFF) { return LOG_OFF; } else if (lvl > LOG_ALL) { return LOG_ALL; } else { return lvl; } } else if (v.startsWith("OFF")) { return LOG_OFF; } else if (v.startsWith("FAT")) { return LOG_FATAL; } else if (v.startsWith("ERR")) { return LOG_ERROR; } else if (v.startsWith("WAR")) { return LOG_WARN; } else if (v.startsWith("INF")) { return LOG_INFO; } else if (v.startsWith("DEB")) { return LOG_DEBUG; } else { return LOG_ALL; } } // ------------------------------------------------------------------------ public static void log(int level, String msg) { Print._log(level, 1, msg); } public static void logFatal(String msg) { Print._log(LOG_FATAL, 1, msg); } public static void logError(String msg) { Print._log(LOG_ERROR, 1, msg); } public static void logWarn(String msg) { Print._log(LOG_WARN, 1, msg); } public static void logInfo(String msg) { Print._log(LOG_INFO, 1, msg); } public static void logDebug(String msg) { Print._log(LOG_DEBUG, 1, msg); } protected static void _log(int level, int frame, String msg) { /* pertinent level? */ if (level > Print.getLogLevel()) { return; } /* message accumulator */ StringBuffer logMsg = new StringBuffer(); /* log message */ if (level <= Print.getLogHeaderLevel()) { // Print this 'header' info for logged messages with a level < 'headerLevel' // ie. print header for errors/warnings, but not for info/debug logMsg.append("["); logMsg.append(Print.getLogLevelString(level)); if (Print._includeDate()) { logMsg.append("|"); logMsg.append(Print.formatDate("MM/dd HH:mm:ss")); // "yyyy/MM/dd HH:mm:ss" } if (Print._includeStackFrame()) { logMsg.append("|"); logMsg.append(_getStackFrame(frame + 1)); } logMsg.append("] "); } /* message */ logMsg.append(msg); if (!msg.endsWith("\n")) { logMsg.append("\n"); } /* print message */ try { PrintStream out = RTConfig.isInitialized()? Print.openLogFile() : Print.getSystemErr(); if (out != null) { out.write(StringTools.getBytes(logMsg.toString())); out.flush(); } else { Print._print(null, frame + 1, false, logMsg.toString()); } } catch (IOException ioe) { Print.setLogFile(null); Print.logError("Unable to open/write log file: " + ioe); Print._print(null, frame + 1, false, logMsg.toString()); } finally { Print.closeLogFile(); } } // ------------------------------------------------------------------------ public static class NullOutputStream extends OutputStream { public NullOutputStream() {} public void write(int b) throws IOException {} public void write(byte[] b) throws IOException {} public void write(byte[] b, int off, int len) throws IOException {} public void flush() throws IOException {} public void close() throws IOException {} } public static class NullPrintStream extends PrintStream { public NullPrintStream() { super(new NullOutputStream()); } } // ------------------------------------------------------------------------}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -