一个自己写的Log的包括类,便于在一行中打印调用堆栈:

package com.android.util;

/**
 * 这是一个打印堆栈的Log包裹类。
 * <p>使用方法是传入一个参数(必须为String),那么verbose方法不会打印堆栈,debug会打印2层,info打印3层,依次下去;
 * <p>如果传入两个参数,则第2个参数为int类型,属于自己控制打印的层数。
 *
 * @author  geduosishisi
 * @version 1.0
 */

public class CamLog {

    private static final int TRACE_DEPTH = 4;
    private static final int NUM8 = 8;
    private static final int NUM7 = 7;
    private static final int NUM6 = 6;
    private static final int NUM5 = 5;
    private static final int NUM4 = 4;
    private static final int NUM3 = 3;
    private static final int NUM2 = 2;
    private static final int NUM1 = 1;

    /**
     * 打印verbose log.
     *
     * @param message 所有参数
     */
    @SuppressWarnings("unused")
    public static void verbose(Object... message) {
        if (message != null && message.length == 1) {
            android.util.Log.v(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], NUM1));
        } else if (message != null && message.length == 2) {
            android.util.Log.v(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], (Integer)message[1]));
        }
    }

    /**
     * 打印debug log.
     *
     * @param message 所有参数
     */
    @SuppressWarnings("unused")
    public static void debug(Object... message) {
        if (message != null && message.length == 1) {
            android.util.Log.d(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], NUM2));
        } else if (message != null && message.length == 2) {
            android.util.Log.d(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], (Integer)message[1]));
        }
    }

    /**
     * 打印info log.
     *
     * @param message 所有参数
     */
    @SuppressWarnings("unused")
    public static void info(Object... message) {
        if (message != null && message.length == 1) {
            android.util.Log.i(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], NUM3));
        } else if (message != null && message.length == 2) {
            android.util.Log.i(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], (Integer)message[1]));
        }
    }

    /**
     * 打印warning log.
     *
     * @param message 所有参数
     */
    @SuppressWarnings("unused")
    public static void warning(Object... message) {
        if (message != null && message.length == 1) {
            android.util.Log.w(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], NUM4));
        } else if (message != null && message.length == 2) {
            android.util.Log.w(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], (Integer)message[1]));
        }
    }

    /**
     * 打印error log.
     *
     * @param message 所有参数
     */
    @SuppressWarnings("unused")
    public static void error(Object... message) {
        if (message != null && message.length == 1) {
            android.util.Log.e(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], NUM5));
        } else if (message != null && message.length == 2) {
            android.util.Log.e(
                    suppressFileExtension(Thread.currentThread().getStackTrace()[TRACE_DEPTH - 1].getFileName()),
                    makeLogStringWithLongInfo((String)message[0], (Integer)message[1]));
        }
    }

    private static String makeLogStringWithLongInfo(String messages, int depth) {
        StackTraceElement stackTrace = Thread.currentThread().getStackTrace()[TRACE_DEPTH];
        StringBuilder builder = new StringBuilder();

        appendMethodLineInfo(builder, stackTrace);
        builder.append(" ");
        builder.append(messages);

        StackTraceElement stackTraceCall5 = null;
        StackTraceElement stackTraceCall6 = null;
        StackTraceElement stackTraceCall7 = null;
        StackTraceElement stackTraceCall8 = null;
        StackTraceElement[] stackTraceArray = Thread.currentThread().getStackTrace();

        if (depth >= NUM2 && stackTraceArray.length > NUM5) {
            stackTraceCall5 = Thread.currentThread().getStackTrace()[NUM5];
        }
        if (depth >= NUM3 && stackTraceArray.length > NUM6) {
            stackTraceCall6 = Thread.currentThread().getStackTrace()[NUM6];
        }
        if (depth >= NUM4 && stackTraceArray.length > NUM7) {
            stackTraceCall7 = Thread.currentThread().getStackTrace()[NUM7];
        }
        if (depth >= NUM5 && stackTraceArray.length > NUM8) {
            stackTraceCall8 = Thread.currentThread().getStackTrace()[NUM8];
        }
        StringBuilder subBuilder = new StringBuilder();
        if (stackTraceCall5 != null) {
            appendFileMethodLineInfo(subBuilder, stackTraceCall5);
        }
        if (stackTraceCall6 != null) {
            appendFileMethodLineInfo(subBuilder, stackTraceCall6);
        }
        if (stackTraceCall7 != null) {
            appendFileMethodLineInfo(subBuilder, stackTraceCall7);
        }
        if (stackTraceCall8 != null) {
            appendFileMethodLineInfo(subBuilder, stackTraceCall8);
        }
        builder.append(subBuilder);
        return builder.toString();
    }

    private static String suppressFileExtension(String filename) {
        int extensionPosition = filename.lastIndexOf('.');
        if (extensionPosition > 0) {
            return filename.substring(0, extensionPosition);
        } else {
            return filename;
        }
    }

    private static void appendMethodLineInfo(StringBuilder builder, StackTraceElement stackTrace) {
        builder.append(stackTrace.getMethodName());
        builder.append("():");
        builder.append(stackTrace.getLineNumber());
        builder.append(" ");
    }

    private static void appendFileMethodLineInfo(StringBuilder builder, StackTraceElement stackTrace) {
        builder.append("  <- ");
        String file = stackTrace.getFileName();
        if (file != null) {
            String fileSub = file.substring(0, file.length() - NUM5);
            builder.append(fileSub);
            builder.append(":");
        }
        builder.append(stackTrace.getMethodName());
        builder.append("():");
        builder.append(stackTrace.getLineNumber());
    }
}