Skip to content

Commit

Permalink
修改日志工具类,增加调用方法所在的文件行号显示,在AndroidStudio的logcat处可点击定位
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed Jun 30, 2016
1 parent c9bdc7f commit 8efcf22
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ dependencies {
也可以直接远程加载jcenter里的,如:
```groovy
dependencies {
compile 'cn.qqtheme.framework:WheelPicker:1.1.3'
compile 'cn.qqtheme.framework:FilePicker:1.1.3'
compile 'cn.qqtheme.framework:ColorPicker:1.1.3'
compile 'cn.qqtheme.framework:WheelPicker:latest.release'
compile 'cn.qqtheme.framework:FilePicker:latest.release'
compile 'cn.qqtheme.framework:ColorPicker:latest.release'
}
```
*注:*
Expand Down
12 changes: 3 additions & 9 deletions library/Common/src/main/java/cn/qqtheme/framework/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@
* ************************************************************************
*
* @author 李玉江[QQ :1032694760]
* @version 2014-09-05 11:49
* @since 2014-09-05 11:49
*/
public class AppConfig {
/**
* The constant DEBUG_ENABLE.
*/
public static final boolean DEBUG_ENABLE = BuildConfig.DEBUG;// 是否调试模式
/**
* The constant DEBUG_TAG.
*/
public static final String DEBUG_TAG = "liyujiang";// LogCat的标记
public static boolean DEBUG_ENABLE = BuildConfig.DEBUG;// 是否调试模式
public static String DEBUG_TAG = "liyujiang";// LogCat的标记
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@
*
* @param <V> the type parameter
* @author 李玉江[QQ :1023694760]
* @version 2015/7/19
* @since 2015/7/19
*/
public abstract class BottomPopup<V extends View> implements DialogInterface.OnKeyListener {
/**
* The constant MATCH_PARENT.
*/
public static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
/**
* The constant WRAP_CONTENT.
*/
public static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
protected Activity activity;
protected int screenWidthPixels;
Expand All @@ -36,6 +30,7 @@ public abstract class BottomPopup<V extends View> implements DialogInterface.OnK
private int width = 0, height = 0;
private boolean isFillScreen = false;
private boolean isHalfScreen = false;
private boolean isPrepared = false;

/**
* Instantiates a new Bottom popup.
Expand All @@ -62,6 +57,9 @@ public BottomPopup(Activity activity) {
* 弹出窗显示之前调用
*/
private void onShowPrepare() {
if (isPrepared) {
return;
}
setContentViewBefore();
V view = makeContentView();
popup.setContentView(view);// 设置弹出窗体的布局
Expand All @@ -79,6 +77,7 @@ private void onShowPrepare() {
}
}
popup.setSize(width, height);
isPrepared = true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* 弹窗
*
* @author 李玉江[QQ :1023694760]
* @version 2015 -10-19
* @see android.widget.PopupWindow
* @since 2015-10-19
*/
public class Popup {
private android.app.Dialog dialog;
Expand Down
108 changes: 90 additions & 18 deletions library/Common/src/main/java/cn/qqtheme/framework/util/LogUtils.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package cn.qqtheme.framework.util;

import android.os.Debug;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;

import android.os.Debug;
import android.os.Environment;
import android.util.Log;

import cn.qqtheme.framework.AppConfig;

/**
* 如果用于android平台,将信息记录到“LogCat”。如果用于java平台,将信息记录到“Console”
* 将信息记录到“LogCat”,显示调用方法及所在的文件、行号,方便调试查错。
* 在Debug状态下开启,在Release状态下关闭以提高程序性能
*
* @author 李玉江[QQ :1023694760]
* @version 2013 -11-2
* @author 李玉江[QQ:1023694760]
* @since 2013/11/2
*/
public class LogUtils {
public final class LogUtils {
private static final int MIN_STACK_OFFSET = 3;// starts at this class after two native calls
private static final int MAX_STACK_TRACE_SIZE = 131071; //128 KB - 1
// 是否开启日志输出,在Debug状态下开启,在Release状态下关闭以提高程序性能,避免日志被人抓取
private static boolean isDebug = AppConfig.DEBUG_ENABLE;
private static String debugTag = AppConfig.DEBUG_TAG;
private static final int METHOD_COUNT = 2; // show method count in trace
private static boolean isDebug = AppConfig.DEBUG_ENABLE;// 是否调试模式
private static String debugTag = AppConfig.DEBUG_TAG;// LogCat的标记

/**
* Debug.
Expand Down Expand Up @@ -49,10 +51,12 @@ public static void debug(Object object, String message) {
*/
public static void debug(String tag, String message) {
if (isDebug) {
tag = debugTag + "-" + tag;
String msg = message + getTraceElement();
try {
Log.d(debugTag + "-" + tag, message);
Log.d(tag, msg);
} catch (Exception e) {
System.out.println(tag + ">>>" + message);
System.out.println(tag + ">>>" + msg);
}
}
}
Expand Down Expand Up @@ -103,10 +107,12 @@ public static void warn(Object object, Throwable e) {
*/
public static void warn(String tag, String message) {
if (isDebug) {
tag = debugTag + "-" + tag;
String msg = message + getTraceElement();
try {
Log.w(debugTag + tag, message);
Log.w(tag, msg);
} catch (Exception e) {
System.out.println(debugTag + ">>>" + message);
System.out.println(tag + ">>>" + msg);
}
}
}
Expand Down Expand Up @@ -157,18 +163,20 @@ public static void error(Object object, Throwable e) {
*/
public static void error(String tag, String message) {
if (isDebug) {
tag = debugTag + "-" + tag;
String msg = message + getTraceElement();
try {
Log.e(debugTag + tag, message);
Log.e(tag, msg);
} catch (Exception e) {
System.out.println(debugTag + ">>>" + message);
System.err.println(tag + ">>>" + msg);
}
}
}

/**
* 在某个方法中调用生成.trace文件。然后拿到电脑上用DDMS工具打开分析
*
* @see #stopMethodTracing() #stopMethodTracing()#stopMethodTracing()
* @see #stopMethodTracing() #stopMethodTracing()
*/
public static void startMethodTracing() {
if (isDebug) {
Expand All @@ -187,6 +195,8 @@ public static void stopMethodTracing() {

/**
* To stack trace string string.
* <p/>
* 此方法参见:https://github.com/Ereza/CustomActivityOnCrash
*
* @param throwable the throwable
* @return the string
Expand All @@ -207,4 +217,66 @@ public static String toStackTraceString(Throwable throwable) {
return stackTraceString;
}

/**
* 此方法参考:https://github.com/orhanobut/logger
*/
private static String getTraceElement() {
try {
int methodCount = METHOD_COUNT;
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int stackOffset = _getStackOffset(trace);

//corresponding method count with the current stack may exceeds the stack trace. Trims the count
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}

String level = " ";
StringBuilder builder = new StringBuilder();
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
builder.append("\n")
.append(level)
.append(_getSimpleClassName(trace[stackIndex].getClassName()))
.append(".")
.append(trace[stackIndex].getMethodName())
.append(" ")
.append("(")
.append(trace[stackIndex].getFileName())
.append(":")
.append(trace[stackIndex].getLineNumber())
.append(")");
level += " ";
}
return builder.toString();
} catch (Exception e) {
return "";
}
}

/**
* Determines the starting index of the stack trace, after method calls made by this class.
*
* @param trace the stack trace
* @return the stack offset
*/
private static int _getStackOffset(StackTraceElement[] trace) {
for (int i = MIN_STACK_OFFSET; i < trace.length; i++) {
StackTraceElement e = trace[i];
String name = e.getClassName();
if (!name.equals(LogUtils.class.getName())) {
return --i;
}
}
return -1;
}

private static String _getSimpleClassName(String name) {
int lastIndex = name.lastIndexOf(".");
return name.substring(lastIndex + 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/**
* 数字选择器
*
* @author 李玉江[QQ :1032694760]
* @version 2015 /10/24
* @author 李玉江[QQ:1032694760]
* @since 2015/10/24
*/
public class NumberPicker extends OptionPicker {

Expand Down

0 comments on commit 8efcf22

Please sign in to comment.