android Log控制小技巧--的使用

时间:2025-03-23 08:59:05

log控制输出,有个小技巧:

ADT(r17)中添加了一个新功能可以允许开发者只在debug模式下允许某些代码,Build系统会自动生成一个BuilldConfig的类,里面包含一个DEBUG静态常量,该常量可以根据Build类型自动设置值,利用此常量可以编写debug模式下跑的代码,而不再需要手动配置变量,避免忘记修改在release版本泄露debug信息。日志类可以这么写:

import ;
import static ;;

public final class L {

	public static void d(String tag, String str) {
		if (DEBUG) {
			(tag, str);
		}
	}

	public static void v(String tag, String str) {
		if (DEBUG) {
			(tag, str);
		}
	}

	public static void i(String tag, String str) {
		if (DEBUG) {
			(tag, str);
		}
	}

	public static void e(String tag, String str) {
		if (DEBUG) {
			(tag, str);
		}
	}
	
	/**
	 * 打印当前方法的调用栈
	 * @param Tag
	 * @param printDepth 打印的最大调用层数,若为0,只打印当前方法信息
	 */
	public static void printMethodCallStack(String Tag, int printDepth){ 
		if (DEBUG) {
			StackTraceElement stack[] = (new Throwable()).getStackTrace();
			StackTraceElement stackTraceElement = null;
			int depth = (, printDepth + 2);
			for (int i = 1; i < depth; i++) {
				stackTraceElement = stack[i];
				(Tag, "["+(i-1)+"]"+()+"."+()+"(...)");
				(Tag, "	--"+()+"#"+()); 
			}
		}
	}
}

情怀