Skip to content

Commit

Permalink
Merge pull request #126 from orhanobut/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
orhanobut authored May 28, 2017
2 parents 16baaab + 6ae0414 commit a5bcc3f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Logger-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/1658) [![](https://img.shields.io/badge/AndroidWeekly-%23147-blue.svg)](http://androidweekly.net/issues/issue-147)
[![Join the chat at https://gitter.im/orhanobut/logger](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/orhanobut/logger?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) <a href="http://www.methodscount.com/?lib=com.orhanobut%3Alogger%3A1.15"><img src="https://img.shields.io/badge/Methods and size-165 | 12 KB-e91e63.svg"/></a> [![Build Status](https://travis-ci.org/orhanobut/logger.svg?branch=master)](https://travis-ci.org/orhanobut/logger)
[![Join the chat at https://gitter.im/orhanobut/logger](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/orhanobut/logger?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) <a href="http://www.methodscount.com/?lib=com.orhanobut%3Alogger%3A2.0.0"><img src="https://img.shields.io/badge/Methods and size-198 | 18 KB-e91e63.svg"/></a> [![Build Status](https://travis-ci.org/orhanobut/logger.svg?branch=master)](https://travis-ci.org/orhanobut/logger)

<img align="right" src='https://github.com/orhanobut/logger/blob/master/art/logger-logo.png' width='128' height='128'/>

Expand All @@ -9,7 +9,7 @@ Simple, pretty and powerful logger for android
### Setup
Download
```groovy
compile 'com.orhanobut:logger:2.0.0'
compile 'com.orhanobut:logger:2.1.0'
```

Initialize
Expand Down Expand Up @@ -61,7 +61,7 @@ FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(7) // (Optional) Hides internal method calls up to offset. Default 5
.logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
.tag("My custom tag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
.tag("My custom tag") // (Optional) Global tag for every log. Default PRETTY_LOGGER
.build();

Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
Expand Down
Binary file modified art/logger_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=2.0.0
VERSION_NAME=2.1.0
GROUP=com.orhanobut

POM_DESCRIPTION=Simple, Pretty and Advanced Logger
Expand Down
9 changes: 1 addition & 8 deletions logger/src/main/java/com/orhanobut/logger/LoggerPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.transform.OutputKeys;
Expand Down Expand Up @@ -51,13 +50,7 @@ class LoggerPrinter implements Printer {
}

@Override public void d(Object object) {
String message;
if (object.getClass().isArray()) {
message = Arrays.deepToString((Object[]) object);
} else {
message = object.toString();
}
log(DEBUG, null, message);
log(DEBUG, null, Utils.toString(object));
}

@Override public void e(String message, Object... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class PrettyFormatStrategy implements FormatStrategy {
/**
* Drawing toolbox
*/
private static final char TOP_LEFT_CORNER = '';
private static final char BOTTOM_LEFT_CORNER = '';
private static final char MIDDLE_CORNER = '';
private static final char HORIZONTAL_DOUBLE_LINE = '';
private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════════════════";
private static final String SINGLE_DIVIDER = "────────────────────────────────────────────────────────";
private static final char TOP_LEFT_CORNER = '';
private static final char BOTTOM_LEFT_CORNER = '';
private static final char MIDDLE_CORNER = '';
private static final char HORIZONTAL_LINE = '';
private static final String DOUBLE_DIVIDER = "────────────────────────────────────────────────────────";
private static final String SINGLE_DIVIDER = "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄";
private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER;
Expand Down Expand Up @@ -81,7 +81,7 @@ private void logTopBorder(int logType, String tag) {
private void logHeaderContent(int logType, String tag, int methodCount) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
if (showThreadInfo) {
logChunk(logType, tag, HORIZONTAL_DOUBLE_LINE + " Thread: " + Thread.currentThread().getName());
logChunk(logType, tag, HORIZONTAL_LINE + " Thread: " + Thread.currentThread().getName());
logDivider(logType, tag);
}
String level = "";
Expand All @@ -99,7 +99,8 @@ private void logHeaderContent(int logType, String tag, int methodCount) {
continue;
}
StringBuilder builder = new StringBuilder();
builder.append("║ ")
builder.append(HORIZONTAL_LINE)
.append(' ')
.append(level)
.append(getSimpleClassName(trace[stackIndex].getClassName()))
.append(".")
Expand All @@ -126,7 +127,7 @@ private void logDivider(int logType, String tag) {
private void logContent(int logType, String tag, String chunk) {
String[] lines = chunk.split(System.getProperty("line.separator"));
for (String line : lines) {
logChunk(logType, tag, HORIZONTAL_DOUBLE_LINE + " " + line);
logChunk(logType, tag, HORIZONTAL_LINE + " " + line);
}
}

Expand Down
35 changes: 35 additions & 0 deletions logger/src/main/java/com/orhanobut/logger/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.Arrays;

import static com.orhanobut.logger.Logger.ASSERT;
import static com.orhanobut.logger.Logger.DEBUG;
Expand Down Expand Up @@ -106,4 +107,38 @@ static String logLevel(int value) {
return "UNKNOWN";
}
}

public static String toString(Object object) {
if (!object.getClass().isArray()) {
return object.toString();
}
if (object instanceof boolean[]) {
return Arrays.toString((boolean[]) object);
}
if (object instanceof byte[]) {
return Arrays.toString((byte[]) object);
}
if (object instanceof char[]) {
return Arrays.toString((char[]) object);
}
if (object instanceof short[]) {
return Arrays.toString((short[]) object);
}
if (object instanceof int[]) {
return Arrays.toString((int[]) object);
}
if (object instanceof long[]) {
return Arrays.toString((long[]) object);
}
if (object instanceof float[]) {
return Arrays.toString((float[]) object);
}
if (object instanceof double[]) {
return Arrays.toString((double[]) object);
}
if (object instanceof Object[]) {
return Arrays.deepToString((Object[]) object);
}
return "Couldn't find a correct type for the object";
}
}
18 changes: 9 additions & 9 deletions logger/src/test/java/com.orhanobut.logger/LogAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
final class LogAssert {
private static final String DEFAULT_TAG = "PRETTY_LOGGER";

private static final char TOP_LEFT_CORNER = '';
private static final char BOTTOM_LEFT_CORNER = '';
private static final char MIDDLE_CORNER = '';
private static final char HORIZONTAL_DOUBLE_LINE = '';
private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════════════════";
private static final String SINGLE_DIVIDER = "────────────────────────────────────────────────────────";
private static final char TOP_LEFT_CORNER = '';
private static final char BOTTOM_LEFT_CORNER = '';
private static final char MIDDLE_CORNER = '';
private static final char HORIZONTAL_LINE = '';
private static final String DOUBLE_DIVIDER = "────────────────────────────────────────────────────────";
private static final String SINGLE_DIVIDER = "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄";
private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER;
Expand Down Expand Up @@ -43,15 +43,15 @@ LogAssert hasMiddleBorder() {
}

LogAssert hasThread(String threadName) {
return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + "Thread: " + threadName);
return hasLog(priority, tag, HORIZONTAL_LINE + " " + "Thread: " + threadName);
}

LogAssert hasMethodInfo(String methodInfo) {
return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + methodInfo);
return hasLog(priority, tag, HORIZONTAL_LINE + " " + methodInfo);
}

LogAssert hasMessage(String message) {
return hasLog(priority, tag, HORIZONTAL_DOUBLE_LINE + " " + message);
return hasLog(priority, tag, HORIZONTAL_LINE + " " + message);
}

private LogAssert hasLog(int priority, String tag, String message) {
Expand Down
32 changes: 27 additions & 5 deletions logger/src/test/java/com.orhanobut.logger/LoggerPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,39 @@ public class LoggerPrinterTest {
verify(adapter).log(DEBUG, "tag", "message");
}

@Test public void logObject() {
Object object = "Test";

printer.d(object);

verify(adapter).log(DEBUG, null, "Test");
}

@Test public void logArray() {
Object object = new int[]{1, 6, 7, 30, 33};

printer.d(object);

verify(adapter).log(DEBUG, null, "[1, 6, 7, 30, 33]");
}

@Test public void logStringArray() {
Object object = new String[]{"a", "b", "c"};

printer.d(object);

verify(adapter).log(DEBUG, null, "[a, b, c]");
}

@Test public void logMultiDimensionArray() {
double[][] doubles = {
{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33},
{1.2, 1.6, 1.7, 30, 33}
{1, 6},
{1.2, 33},
};

printer.d(doubles);

verify(adapter).log(DEBUG, null, Arrays.deepToString(doubles));
verify(adapter).log(DEBUG, null, "[[1.0, 6.0], [1.2, 33.0]]");
}

@Test public void logList() {
Expand Down
41 changes: 41 additions & 0 deletions logger/src/test/java/com.orhanobut.logger/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,45 @@ public class UtilsTest {
assertThat(Utils.logLevel(Logger.ERROR)).isEqualTo("ERROR");
assertThat(Utils.logLevel(100)).isEqualTo("UNKNOWN");
}

@Test public void objectToString() {
Object object = "Test";

assertThat(Utils.toString(object)).isEqualTo("Test");
}

@Test public void primitiveArrayToString() {
Object booleanArray = new boolean[]{true, false, true};
assertThat(Utils.toString(booleanArray)).isEqualTo("[true, false, true]");

Object byteArray = new byte[]{1, 0, 1};
assertThat(Utils.toString(byteArray)).isEqualTo("[1, 0, 1]");

Object charArray = new char[]{'a', 'b', 'c'};
assertThat(Utils.toString(charArray)).isEqualTo("[a, b, c]");

Object shortArray = new short[]{1, 3, 5};
assertThat(Utils.toString(shortArray)).isEqualTo("[1, 3, 5]");

Object intArray = new int[]{1, 3, 5};
assertThat(Utils.toString(intArray)).isEqualTo("[1, 3, 5]");

Object longArray = new long[]{1, 3, 5};
assertThat(Utils.toString(longArray)).isEqualTo("[1, 3, 5]");

Object floatArray = new float[]{1, 3, 5};
assertThat(Utils.toString(floatArray)).isEqualTo("[1.0, 3.0, 5.0]");

Object doubleArray = new double[]{1, 3, 5};
assertThat(Utils.toString(doubleArray)).isEqualTo("[1.0, 3.0, 5.0]");
}

@Test public void multiDimensionArrayToString() {
Object object = new int[][]{
{1, 2, 3},
{4, 5, 6}
};

assertThat(Utils.toString(object)).isEqualTo("[[1, 2, 3], [4, 5, 6]]");
}
}

0 comments on commit a5bcc3f

Please sign in to comment.