?? logger.java
字號:
package com.jmobilecore.support;
import java.util.Calendar;
/**
* Logs messages to the system output stream.
* <p/>
* All methods are wrapped in {@link Debug#ON} checks;
* If {@link Debug#ON} is false then all methods simply return doing nothing.
* However, for optimizaton purpose all calles should be wrapped into {@link Debug#ON} checks
* Example:
* <pre><code>
* if ( Debug.ON ) {
* Logger.development( "Some message" );
* }
* </code></pre>
* or:
* <pre><code>
* if ( Debug.ON ) {
* Logger.OUTPUT_ON = false;
* System.err.println( Logger.development( "Some message" ) );
* }
* </code></pre>
*
* @author Greg Gridin
*/
public class Logger {
/**
* Separator for the message
*/
protected final static String SEPARATOR = " | ";
/**
* Tabulation for message alighment
*/
protected final static String TABULATION = " ";
/**
* Exception indicator
*/
protected final static String EXCEPTION = "EXCEPTION ";
/**
* Development indicator
*/
protected final static String DEVELOPMENT = "DEVELOPMENT";
/**
* Enables or disables output of time
*/
public static boolean TIME_ON = false;
/**
* Enables or disables output of severity
*/
public static boolean SEVERITY_ON = false;
/**
* Enables or disables output of stack trace
*/
public static boolean STACK_TRACE_ON = true;
/**
* Enables or disables output to standard output stream
*/
public static boolean OUTPUT_ON = true;
/**
* Creates a new instance of <code>Logger</code> class.
*/
private Logger() {
}
/**
* Logging of an exception and stack trace.
*
* @param throwable the exception
* @see #exception(Throwable throwable, int tabs)
* @return the logging message
*/
public final static String exception(Throwable throwable) {
if (Debug.ON) {
return exception(throwable, 0);
} else
return null;
}
/**
* Logging of an exception and stack trace.
*
* @param throwable the exception
* @param tabs the number of tabulations for the message alignment
* @return the logging message
*/
public final static String exception(Throwable throwable, int tabs) {
if (Debug.ON) {
if (STACK_TRACE_ON) throwable.printStackTrace();
return logMessage(throwable.toString(), EXCEPTION, tabs);
} else
return null;
}
/**
* Logging of an exception and stack trace.
*
* @param throwable the exception
* @param msg the additional message
* @return the logging message
*/
public final static String exception(Throwable throwable, String msg) {
if (Debug.ON) {
StringBuffer logBuffer = new StringBuffer();
logBuffer.append(throwable.getMessage());
if (msg != null) logBuffer.append(" : ").append(msg);
if (STACK_TRACE_ON) throwable.printStackTrace();
return logMessage(logBuffer.toString(), EXCEPTION, 0);
} else
return null;
}
/**
* Logging of development message.
*
* @param msg the logging message.
* @return the logging message
*/
public final static String development(String msg) {
if (Debug.ON) {
return development(msg, 0);
} else
return null;
}
/**
* Logging of development message.
*
* @param msg the logging message.
* @return the logging message
*/
public final static String development(String msg, int tabs) {
if (Debug.ON) {
return logMessage(msg, DEVELOPMENT, tabs);
} else
return null;
}
/**
* Logging of memory message.
*
* @param msg the additional message
* @return the logging message
*/
public static String logMemory(String msg) {
if (Debug.ON) {
StringBuffer logBuffer = new StringBuffer();
if (msg != null) {
logBuffer.append(msg).append('\n');
}
logBuffer.append("Total: ").append(Runtime.getRuntime().totalMemory()).append('\n');
logBuffer.append("Free : ").append(Runtime.getRuntime().freeMemory()).append('\n');
return logMessage(logBuffer.toString(), DEVELOPMENT, 0);
} else
return null;
}
/**
* Logging of the message, adding the timestamp and severity indicator.
*
* @param msg the logging message.
* @param severity the message severity.
* @param tabs the number of tabulations for the message alignment
* @return the logging message
*/
protected final static String logMessage(String msg, String severity, int tabs) {
StringBuffer logBuffer = new StringBuffer();
if (TIME_ON) {
Calendar cal = Calendar.getInstance();
logBuffer.append(cal.get(Calendar.HOUR)).append(':').
append(cal.get(Calendar.MINUTE)).append(':').
append(cal.get(Calendar.SECOND)).append(':').
append(cal.get(Calendar.MILLISECOND)).
append(SEPARATOR);
}
if (SEVERITY_ON) {
logBuffer.append(severity).append(SEPARATOR);
}
for (int i = 0; i < tabs; i++) {
logBuffer.append(TABULATION);
}
if (msg != null) {
logBuffer.append(msg);
}
String rslt = logBuffer.toString();
if (OUTPUT_ON) {
System.out.println(rslt);
}
return rslt;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -