Permalink
Browse files

增加了log tag的测试用例(集成测试)

  • Loading branch information...
tianzhijiexian committed Jul 24, 2018
1 parent 78c0106 commit d1df21bdd5c3aa6fb74912d6569d561bbb3d52df
@@ -3,33 +3,53 @@
import android.util.Log;
import com.orhanobut.logger.helper.LogDelegate;
import com.orhanobut.logger.helper.ILogPrinter;
import com.orhanobut.logger.helper.LogSettings;
import com.orhanobut.loggersample.format.PrettyFormatter;
import com.orhanobut.logger.helper.formatter.DefaultLogFormatter;
/**
* @author Kale
* @date 2017/9/28
*/
public class LogUtils implements ILogPrinter {
public final class LogUtils {
private LogDelegate mDelegate;
private static LogDelegate sDelegate;
public LogUtils(LogSettings settings) {
mDelegate = new LogDelegate(settings, new PrettyFormatter(), this);
public static void init(LogSettings settings) {
sDelegate = new LogDelegate(settings, new DefaultLogFormatter(),
(priority, tag, message, throwable) -> Log.println(priority, tag, message));
}
public boolean isLoggable(int priority, String tag) {
return mDelegate.isLoggable(priority, tag);
public static boolean isLoggable(int priority, String tag) {
return sDelegate.isLoggable(priority, tag);
}
public void log(int priority, String tag, String message) {
tag = mDelegate.getAutoTag(null);
mDelegate.printLog(priority, tag, message, null);
public static void d(String tag, String message) {
if (isLoggable(Log.DEBUG, tag)) {
print(Log.DEBUG, tag, message);
}
}
@Override
public void println(int priority, String tag, String message, Throwable throwable) {
Log.println(priority, tag, message);
public static void d(String message) {
String tag = sDelegate.getAutoTag(null);
d(tag, message);
}
public static void e(String tag, String message) {
if (isLoggable(Log.ERROR, tag)) {
print(Log.ERROR, tag, message);
}
}
public static void openLog(boolean b) {
if (b) {
sDelegate.getSettings().changeLogLev(Log.VERBOSE);
} else {
sDelegate.getSettings().closeLog();
}
}
private static void print(int priority, String tag, String message) {
sDelegate.printLog(priority, tag, message, null);
}
}
@@ -36,7 +36,7 @@
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fabric.with(this, new Crashlytics());
CrashHandler.getInstance().init();
setContentView(R.layout.activity_main);
@@ -70,6 +70,12 @@ protected void onCreate(Bundle savedInstanceState) {
new XLogTree(getApplicationContext())
);
LogUtils.init(settings);
LogUtils.d("kale-tag", "my message");
LogUtils.d("my message");
LogUtils.e("kale-tag", "my error message");
levTest();
largeDataTest();
@@ -1,11 +1,13 @@
package com.orhanobut.logger.helper;
import android.support.annotation.Nullable;
/**
* @author Kale
* @date 2018/7/19
*/
public interface ILogPrinter {
void println(int priority, String tag, String message, Throwable throwable);
void println(int priority, String tag, String message, @Nullable Throwable throwable);
}
@@ -5,7 +5,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
/**
* @author Kale
@@ -15,7 +14,7 @@
public static final int BASE_STACK_OFFSET = 10;
private static final int MIN_STACK_OFFSET = 5;
public static final int MIN_STACK_OFFSET = 5;
private static final int MAX_LOG_LENGTH = 4000;
@@ -40,7 +39,8 @@ public LogDelegate(LogSettings logSettings, AbsLogFormatter logFormatter, ILogPr
format = logFormatter;
printer = logPrinter;
sb = new StringBuilder();
logFormatter.setDelegate(this);
format.setDelegate(this);
}
/**
@@ -50,7 +50,7 @@ public String getAutoTag(@Nullable StackTraceElement element) {
hasCustomTag = false;
final String tag;
if (settings.globalTag != null) {
if (!isEmpty(settings.globalTag)) {
// force tag
tag = settings.globalTag;
} else {
@@ -62,7 +62,11 @@ public String getAutoTag(@Nullable StackTraceElement element) {
tag = getTagByStack(element);
}
}
return TextUtils.isEmpty(settings.tagPrefix) ? tag : settings.tagPrefix + "-" + tag;
return isEmpty(settings.tagPrefix) ? tag : settings.tagPrefix + "-" + tag;
}
private static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
private static String getTagByStack(StackTraceElement element) {
@@ -83,7 +87,7 @@ private int getStackOffsetByClz(StackTraceElement[] trace, String clzName) {
StackTraceElement e = trace[i];
String name = e.getClassName();
if (!name.equals(clzName)) {
return --i;
return --i; // 一般情况下是相等的,这里处理tag的问题所以-1
}
}
return -1;
@@ -0,0 +1,80 @@
package com.orhanobut.logger.helper;
import com.orhanobut.logger.helper.formatter.DefaultLogFormatter;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Kale
* @date 2018/7/24
*/
public class LogDelegateTest {
@Test
public void testGetAutoTag_return_TestClass() {
LogSettings settings = new LogSettings.Builder()
.methodOffset(-LogDelegate.MIN_STACK_OFFSET)
.build();
LogDelegate delegate = new LogDelegate(settings, new DefaultLogFormatter(), null);
String tag = delegate.getAutoTag(null); // 直接调用此方法,所以方法的offset要特殊设置,LogDelegate.MIN_STACK_OFFSET
Assert.assertEquals(LogDelegateTest.class.getSimpleName(), tag);
}
@Test
public void testGetAutoTag_return_MainActivity() {
LogDelegate delegate = new LogDelegate(LogSettings.create(), new DefaultLogFormatter(), null);
StackTraceElement element = new StackTraceElement("com.abc.kale.MainActivity", "test()", "ddd", 31);
String tag = delegate.getAutoTag(element);
Assert.assertEquals("MainActivity", tag);
}
@Test
public void testGetAutoTag_return_globalTag() {
LogSettings settings = new LogSettings.Builder()
.globalTag("GLOBAL_TAG")
.build();
LogDelegate delegate = new LogDelegate(settings, new DefaultLogFormatter(), null);
String tag = delegate.getAutoTag(null); // 直接调用此方法,所以方法的offset要特殊设置,LogDelegate.MIN_STACK_OFFSET
Assert.assertEquals("GLOBAL_TAG", tag);
}
@Test
public void testGetAutoTag_setEmptyTag_return_globalTag() {
LogSettings settings = new LogSettings.Builder()
.globalTag("") // empty global tag
.build();
LogDelegate delegate = new LogDelegate(settings, new DefaultLogFormatter(), null);
StackTraceElement element = new StackTraceElement("com.abc.kale.MainActivity", "test()", "ddd", 31);
String tag = delegate.getAutoTag(element);
Assert.assertEquals("MainActivity", tag);
}
@Test
public void testGetAutoTag_seTagPrefix() {
LogSettings settings = new LogSettings.Builder()
.tagPrefix("kale")
.build();
LogDelegate delegate = new LogDelegate(settings, new DefaultLogFormatter(), null);
StackTraceElement element = new StackTraceElement("com.abc.kale.MainActivity", "test()", "ddd", 31);
String tag = delegate.getAutoTag(element);
Assert.assertEquals("kale-MainActivity", tag);
}
}

0 comments on commit d1df21b

Please sign in to comment.