⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logger.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 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 + -