安卓(Android) 怎么实现向logcat写入详细消息(Message)的方便方法

2019年10月19日11:02:01安卓(Android) 怎么实现向logcat写入详细消息(Message)的方便方法已关闭评论 224 1758字阅读5分51秒

1.简介

安卓(Android) 怎么实现向logcat写入详细消息(Message)的方便方法?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 

import java.lang.reflect.Method;
import java.util.Locale;

import android.util.Log;

public class Main {
    
    public static void log(String verbose, Object... args) {
        try {
            Method m = Log.class.getMethod("v", new Class<?>[] {
                    String.class, String.class });
            log(m, verbose, args);
        } catch (Exception e) {
            Log.w("AbLE", "Log Failed", e);
        }
    }

    
    private static void log(Method logMethod, String format, Object... args) {
        try {
            StackTraceElement[] trace = Thread.currentThread()
                    .getStackTrace();
            StringBuilder b = new StringBuilder();
            b.append(format).append(", ");
            int index = 1;
            String name = null;
            boolean loop = true;
            do {
                index++;
                if (trace != null && index < trace.length) {
                    name = trace[index].getClassName();

                    if (name != null) {
                        if (!name.contains("self.philbrown.AbLE.AbLEUtil")) {
                            loop = false;
                        }
                    }
                } else {
                    index = 1;
                    loop = false;
                }

            } while (loop);
            b.append(formatStackTrace(trace[index]));
            b.append(buildCommaSeparatedString(args));
            try {
                logMethod.invoke(null, "AbLE",
                        String.format(Locale.US, b.toString(), args));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Throwable t) {
            Log.w("AbLE", "Log Failed", t);
        }
    }

    
    public static String formatStackTrace(StackTraceElement element) {
        StringBuilder b = new StringBuilder();

        b.append(" at ");
        String clazz = element.getClassName();
        b.append(clazz).append(".");
        b.append(element.getMethodName()).append("(");
        b.append(clazz.substring(clazz.lastIndexOf(".") + 1)).append(
                ".java:");
        b.append(element.getLineNumber()).append(")").append(" , ##");
        return b.toString();
    }

    
    public static String buildCommaSeparatedString(Object... args) {
        if (args == null)
            return "";
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            b.append(args[i]);
            if (i != args.length - 1) {
                b.append(", ");
            }
        }
        return b.toString();
    }

    
    public static String format(String format, Object... args) {
        return String.format(Locale.US, format, args);
    }
}

 
继续阅读
编程十万个怎么办