现在的位置: 首页 > 综合 > 正文

[小代码]简化android Log,显示调用类和函数名

2013年03月14日 ⁄ 综合 ⁄ 共 847字 ⁄ 字号 评论关闭

用更简单的方式打LOG,日志显示类和方法。

打日志:L.e("Test日志");
将显示:com.yourpackage.ClassName: [Method] Test日志

import android.util.Log;

public class L
{

private enum LogState
{
    INFO, ERROR, ALL;
}

public static final LogState CURRENT_STATE = LogState.ALL;

public static void e(final String msg)
{

    switch (CURRENT_STATE)
    {
        case ALL:
        case ERROR:

            final Throwable t = new Throwable();
            final StackTraceElement[] elements = t.getStackTrace();

            final String callerClassName = elements[1].getClassName();
            final String callerMethodName = elements[1].getMethodName();

            Log.e(callerClassName, "[" + callerMethodName + "] " + msg);
    }
}

public static void i(final String msg)
{

    switch (CURRENT_STATE)
    {
        case ALL:
        case INFO:

            final Throwable t = new Throwable();
            final StackTraceElement[] elements = t.getStackTrace();

            final String callerClassName = elements[1].getClassName();
            final String callerMethodName = elements[1].getMethodName();

            Log.d(callerClassName, "[" + callerMethodName + "] " + msg);
    }
}

}

抱歉!评论已关闭.