赞
踩
转载请注明出处:http://blog.csdn.net/bbld_/article/details/39757099
前言
众所周知,android.util.Log使我们调试程序的好帮手,但我们使用它时还是会发现一些问题,比如:我们一般都会在每个有需要log输出的类里加上以这个类为名字的TAG用于log信息的筛选;在app上线时,开发者可能一时疏忽遗漏了一些log信息的输出可能导致程序的log输出信息被人所利用...。那么怎么能避免这些问题呢,github上的DebugLog工具很方便的解决了这些问题。
其实这工具库很简单,只有一个类而已,下面是它的介绍说明
介绍
在传统的Log用法中,我们一般像下面这样使用
- public static final String TAG = MainActivity.class.getSimpleName();// "MainActivity"
-
- void myFunc()
- {
- android.util.Log.i(TAG, "my message");
- }
然后他的输出就如下:
通常,如果没有吧log的位置写到log输出信息里,我们可能过一会就会把这log的位置给忘了。但使用DebugLog之后,情况就会不同了
- void mySecondFunc()
- {
- DebugLog.v("my second message");
- }
输出如下
可以看到它会自动把我们的log输出所在 类、方法、甚至行号都打印出来了。确实为我们省下了许多不必要的麻烦。
源码
下面给出的是我稍稍修改后的源码,与原来的源码相比只是修改了输出显示:Tag的.java去掉了,不怎么需要,把行号也打印出来了。
- /***
- This is free and unencumbered software released into the public domain.
- Anyone is free to copy, modify, publish, use, compile, sell, or
- distribute this software, either in source code form or as a compiled
- binary, for any purpose, commercial or non-commercial, and by any
- means.
- For more information, please refer to <http://unlicense.org/>
- */
- package com.example.debuglog;
-
- import android.util.Log;
-
- /**
- * @author Mr.Zheng
- * @date 2014年10月3日 下午6:23:29
- */
- public class DebugLog
- {
- /**
- * Log输出所在类
- */
- private static String className;
- /**
- * Log输出所在方法
- */
- private static String methodName;
- /**
- * Log输出所行号
- */
- private static int lineNumber;
-
- /**
- * 是否可Debug状态
- *
- * @return
- */
- public static boolean isDebuggable()
- {
- return BuildConfig.DEBUG;
- }
-
- /**
- * 创建Log输出的基本信息
- *
- * @param log
- * @return
- */
- private static String createLog(String log)
- {
- StringBuffer buffer = new StringBuffer();
- buffer.append("[");
- buffer.append(methodName);
- buffer.append("()");
- buffer.append(" line:");
- buffer.append(lineNumber);
- buffer.append("] ");
- buffer.append(log);
-
- return buffer.toString();
- }
-
- /**
- * 取得输出所在位置的信息 className methodName lineNumber
- *
- * @param sElements
- */
- private static void getMethodNames(StackTraceElement[] sElements)
- {
- className = sElements[1].getFileName().split("\\.")[0];
- methodName = sElements[1].getMethodName();
- lineNumber = sElements[1].getLineNumber();
- }
-
- public static void e(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.e(className, createLog(message));
- }
-
- public static void i(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.i(className, createLog(message));
- }
-
- public static void d(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.d(className, createLog(message));
- }
-
- public static void v(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.v(className, createLog(message));
- }
-
- public static void w(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.w(className, createLog(message));
- }
-
- public static void wtf(String message)
- {
- if (!isDebuggable())
- return;
-
- getMethodNames(new Throwable().getStackTrace());
- Log.wtf(className, createLog(message));
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
其它
从这里我们可以扩展一下它的用处,比如,当我们查看源码时,有时一些方法在另一个类中被调用了我们可能一下子很难找出调用方法的所在位置,这是我们就可以根据这个原理自己打印出来。举个简单的例子,像我们的Activity中的onCreate方法,我们要想知道在父类中哪里调用了它,我们就可以使用这种方法来确定:
- package com.example.debuglog;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- public class MainActivity extends Activity
- {
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- getMethodNames(new Throwable().getStackTrace());
- DebugLog.i("onCreate的调用位置: " + className + "-" + methodName + "-" + lineNumber);
- }
-
- private static String className;
- private static String methodName;
- private static int lineNumber;
-
- /**
- * 取得输出所在位置的信息 className methodName lineNumber
- *
- * @param sElements
- */
- private void getMethodNames(StackTraceElement[] sElements)
- {
- className = sElements[1].getFileName().split("\\.")[0];
- methodName = sElements[1].getMethodName();
- lineNumber = sElements[1].getLineNumber();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
我们很清晰的就可以看到onCreate是在Activity类中的performCreate方法里第5008行调用的(在我的4.1的模拟器里得到的,API版本不同,所在位置即行号一般会不同),所以我们得知我们写的activity是在这里开始他的声明周期的。
源码下载:http://download.csdn.net/detail/bbld_/8000105
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。