提交 2ce3b5bd 编写于 作者: Z zkq

add debug and trace method

上级 f3940563
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="KLog" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -32,6 +32,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'cz.msebera.android:httpclient:4.3.6'
compile 'com.github.zhaokaiqiang.klog:library:1.5.0'
// compile project(':library')
// compile 'com.github.zhaokaiqiang.klog:library:1.5.0'
compile project(':library')
}
......@@ -34,8 +34,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void handleMessage(Message msg) {
KLog.d("handleMessage");
handler.sendEmptyMessageDelayed(0, 3000);
KLog.d("Inner Class Test");
}
};
......@@ -64,6 +63,16 @@ public class MainActivity extends AppCompatActivity {
STRING_LONG = getString(R.string.string_long);
}
public void logTraceStack(View view) {
TestTraceUtil.testTrace();
}
public void logDebug(View view) {
KLog.debug();
KLog.debug("This is a debug message");
KLog.debug("DEBUG", "params1", "params2", this);
}
public void log(View view) {
KLog.v();
KLog.d();
......
package com.socks.sample;
import com.socks.library.KLog;
/**
* Created by zhao on 16/6/16.
*/
class TestTraceUtil {
static void testTrace() {
KLog.trace();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
......@@ -10,7 +10,7 @@
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:theme="@style/ToolbarTheme"/>
app:theme="@style/ToolbarTheme" />
<ScrollView
android:layout_width="match_parent"
......@@ -23,6 +23,12 @@
android:paddingLeft="16dp"
android:paddingRight="16dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logDebug"
android:text="Log.debug()" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
......@@ -95,6 +101,12 @@
android:onClick="logWithXmlFromNet"
android:text="Log.xml(FROM NET)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logTraceStack"
android:text="Log.trace()" />
</LinearLayout>
</ScrollView>
......
......@@ -5,9 +5,9 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha3'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
......
#Sun Jun 12 23:20:37 CST 2016
#Fri Sep 09 19:39:04 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
......@@ -10,6 +10,8 @@ import com.socks.library.klog.JsonLog;
import com.socks.library.klog.XmlLog;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* This is a Log tool,with this you can the following
......@@ -25,9 +27,11 @@ import java.io.File;
* 15/11/18 扩展功能,增加对XML的支持,修复BUG
* 15/12/8 扩展功能,添加对任意参数的支持
* 15/12/11 扩展功能,增加对无限长字符串支持
* 16/6/13 扩展功能,添加对自定义全局Tag的支持
* 16/6/13 扩展功能,添加对自定义全局Tag的支持,修复内部类不能点击跳转的BUG
* 16/6/15 扩展功能,添加不能关闭的KLog.debug(),用于发布版本的Log打印,优化部分代码
* 16/6/20 扩展功能,添加堆栈跟踪功能KLog.trace()
*/
public class KLog {
public final class KLog {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String NULL_TIPS = "Log with null object";
......@@ -39,8 +43,8 @@ public class KLog {
private static final String SUFFIX = ".java";
public static final int JSON_INDENT = 4;
public static final int V = 0x1;
public static final int V = 0x1;
public static final int D = 0x2;
public static final int I = 0x3;
public static final int W = 0x4;
......@@ -50,7 +54,8 @@ public class KLog {
private static final int JSON = 0x7;
private static final int XML = 0x8;
private static final int STACK_TRACE_INDEX = 5;
private static final int STACK_TRACE_INDEX_5 = 5;
private static final int STACK_TRACE_INDEX_4 = 4;
private static String mGlobalTag;
private static boolean mIsGlobalTagEmpty = true;
......@@ -166,13 +171,58 @@ public class KLog {
printFile(tag, targetDirectory, fileName, msg);
}
public static void debug() {
printDebug(null, DEFAULT_MESSAGE);
}
public static void debug(Object msg) {
printDebug(null, msg);
}
public static void debug(String tag, Object... objects) {
printDebug(tag, objects);
}
public static void trace() {
printStackTrace();
}
private static void printStackTrace() {
if (!IS_SHOW_LOG) {
return;
}
Throwable tr = new Throwable();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
pw.flush();
String message = sw.toString();
String traceString[] = message.split("\\n\\t");
StringBuilder sb = new StringBuilder();
sb.append("\n");
for (String trace : traceString) {
if (trace.contains("at com.socks.library.KLog")) {
continue;
}
sb.append(trace).append("\n");
}
String[] contents = wrapperContent(STACK_TRACE_INDEX_4, null, sb.toString());
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
BaseLog.printDefault(D, tag, headString + msg);
}
private static void printLog(int type, String tagStr, Object... objects) {
if (!IS_SHOW_LOG) {
return;
}
String[] contents = wrapperContent(tagStr, objects);
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
......@@ -193,6 +243,15 @@ public class KLog {
XmlLog.printXml(tag, msg, headString);
break;
}
}
private static void printDebug(String tagStr, Object... objects) {
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objects);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
BaseLog.printDefault(D, tag, headString + msg);
}
......@@ -202,7 +261,7 @@ public class KLog {
return;
}
String[] contents = wrapperContent(tagStr, objectMsg);
String[] contents = wrapperContent(STACK_TRACE_INDEX_5, tagStr, objectMsg);
String tag = contents[0];
String msg = contents[1];
String headString = contents[2];
......@@ -210,11 +269,11 @@ public class KLog {
FileLog.printFile(tag, targetDirectory, fileName, headString, msg);
}
private static String[] wrapperContent(String tagStr, Object... objects) {
private static String[] wrapperContent(int stackTraceIndex, String tagStr, Object... objects) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement targetElement = stackTrace[STACK_TRACE_INDEX];
StackTraceElement targetElement = stackTrace[stackTraceIndex];
String className = targetElement.getClassName();
String[] classNameInfo = className.split("\\.");
if (classNameInfo.length > 0) {
......@@ -232,8 +291,6 @@ public class KLog {
lineNumber = 0;
}
String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
String tag = (tagStr == null ? className : tagStr);
if (mIsGlobalTagEmpty && TextUtils.isEmpty(tag)) {
......@@ -243,7 +300,7 @@ public class KLog {
}
String msg = (objects == null) ? NULL_TIPS : getObjectsString(objects);
String headString = "[ (" + className + ":" + lineNumber + ")#" + methodNameShort + " ] ";
String headString = "[ (" + className + ":" + lineNumber + ")#" + methodName + " ] ";
return new String[]{tag, msg, headString};
}
......
......@@ -6,7 +6,7 @@ import android.util.Log;
/**
* Created by zhaokaiqiang on 15/12/11.
*/
public class Util {
public class KLogUtil {
public static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || line.equals("\n") || line.equals("\t") || TextUtils.isEmpty(line.trim());
......
......@@ -9,19 +9,21 @@ import com.socks.library.KLog;
*/
public class BaseLog {
private static final int MAX_LENGTH = 4000;
public static void printDefault(int type, String tag, String msg) {
int index = 0;
int maxLength = 4000;
int countOfSub = msg.length() / maxLength;
int length = msg.length();
int countOfSub = length / MAX_LENGTH;
if (countOfSub > 0) {
for (int i = 0; i < countOfSub; i++) {
String sub = msg.substring(index, index + maxLength);
String sub = msg.substring(index, index + MAX_LENGTH);
printSub(type, tag, sub);
index += maxLength;
index += MAX_LENGTH;
}
printSub(type, tag, msg.substring(index, msg.length()));
printSub(type, tag, msg.substring(index, length));
} else {
printSub(type, tag, msg);
}
......
package com.socks.library.klog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.File;
......@@ -16,7 +18,10 @@ import java.util.Random;
*/
public class FileLog {
public static void printFile(String tag, File targetDirectory, String fileName, String headString, String msg) {
private static final String FILE_PREFIX = "KLog_";
private static final String FILE_FORMAT = ".log";
public static void printFile(String tag, File targetDirectory, @Nullable String fileName, String headString, String msg) {
fileName = (fileName == null) ? getFileName() : fileName;
if (save(targetDirectory, fileName, msg)) {
......@@ -26,7 +31,7 @@ public class FileLog {
}
}
private static boolean save(File dic, String fileName, String msg) {
private static boolean save(File dic, @NonNull String fileName, String msg) {
File file = new File(dic, fileName);
......@@ -36,6 +41,7 @@ public class FileLog {
outputStreamWriter.write(msg);
outputStreamWriter.flush();
outputStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
......@@ -50,12 +56,11 @@ public class FileLog {
return false;
}
return true;
}
private static String getFileName() {
Random random = new Random();
return "KLog_" + Long.toString(System.currentTimeMillis() + random.nextInt(10000)).substring(4) + ".txt";
return FILE_PREFIX + Long.toString(System.currentTimeMillis() + random.nextInt(10000)).substring(4) + FILE_FORMAT;
}
}
......@@ -3,7 +3,7 @@ package com.socks.library.klog;
import android.util.Log;
import com.socks.library.KLog;
import com.socks.library.Util;
import com.socks.library.KLogUtil;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -32,12 +32,12 @@ public class JsonLog {
message = msg;
}
Util.printLine(tag, true);
KLogUtil.printLine(tag, true);
message = headString + KLog.LINE_SEPARATOR + message;
String[] lines = message.split(KLog.LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "║ " + line);
}
Util.printLine(tag, false);
KLogUtil.printLine(tag, false);
}
}
......@@ -4,7 +4,7 @@ import android.util.Log;
import com.socks.library.KLog;
import com.socks.library.Util;
import com.socks.library.KLogUtil;
import java.io.StringReader;
import java.io.StringWriter;
......@@ -30,17 +30,17 @@ public class XmlLog {
xml = headString + KLog.NULL_TIPS;
}
Util.printLine(tag, true);
KLogUtil.printLine(tag, true);
String[] lines = xml.split(KLog.LINE_SEPARATOR);
for (String line : lines) {
if (!Util.isEmpty(line)) {
if (!KLogUtil.isEmpty(line)) {
Log.d(tag, "║ " + line);
}
}
Util.printLine(tag, false);
KLogUtil.printLine(tag, false);
}
public static String formatXML(String inputXML) {
private static String formatXML(String inputXML) {
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StreamResult xmlOutput = new StreamResult(new StringWriter());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册