Skip to content
Permalink
Browse files

fixed rendering message attributes

  • Loading branch information...
maciejmikosik committed Apr 11, 2018
2 parents 5f67658 + 5cfce52 commit 35ad2a1d325205ad045f40fd29d0418605b6c945
@@ -31,12 +31,14 @@ Quick example.

prints

Thread(main) 2017-07-10T08:19:15.195Z App.start()
Thread(main) 2017-07-10T08:19:15.204Z Service#1521118594.serve()
Thread(main) 2017-07-10T08:19:15.205Z returned
Thread(main) 2017-07-10T08:19:15.205Z Service#1682463303.serve()
Thread(main) 2017-07-10T08:19:15.205Z returned
Thread(main) 2017-07-10T08:19:15.205Z returned
```
main 2018-03-27T11:57:27.493Z App.start()
main 2018-03-27T11:57:27.502Z Service#1404928347.serve()
main 2018-03-27T11:57:27.502Z returned
main 2018-03-27T11:57:27.502Z Service#949057310.serve()
main 2018-03-27T11:57:27.502Z returned
main 2018-03-27T11:57:27.503Z returned
```

Logbuddy offers possibility to generate html logs that contain images, graphs etc.

@@ -48,7 +48,7 @@

public class Documentation {
public static void main(String[] args) {
readme_html();
readme_text();
}

public static void readme_text() {
@@ -25,14 +25,17 @@
import org.logbuddy.logger.wire.BrowserLogger;
import org.logbuddy.logger.wire.ConsoleLogger;
import org.logbuddy.logger.wire.FileLogger;
import org.logbuddy.message.Attribute;
import org.logbuddy.renderer.HtmlRenderer;
import org.logbuddy.renderer.TextRenderer;
import org.logbuddy.renderer.chart.LineChartRenderer;
import org.logbuddy.renderer.chart.SaturationChartRenderer;
import org.logbuddy.renderer.gallery.Gallery;

public class Build {
public List<Class<?>> model = asList(Message.class);
public List<Class<?>> message = asList(
Message.class,
Attribute.class);

public List<Class<?>> loggers = asList(
InvocationDepthLogger.class,
@@ -5,9 +5,9 @@
import static org.logbuddy.LogBuddyException.check;
import static org.logbuddy.Message.message;
import static org.logbuddy.common.Classes.makeAccessible;
import static org.logbuddy.model.Completed.returned;
import static org.logbuddy.model.Completed.thrown;
import static org.logbuddy.model.Invoked.invoked;
import static org.logbuddy.message.Completed.returned;
import static org.logbuddy.message.Completed.thrown;
import static org.logbuddy.message.Invoked.invoked;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -4,9 +4,9 @@

import org.logbuddy.Logger;
import org.logbuddy.Message;
import org.logbuddy.model.Completed;
import org.logbuddy.model.InvocationDepth;
import org.logbuddy.model.Invoked;
import org.logbuddy.message.Completed;
import org.logbuddy.message.InvocationDepth;
import org.logbuddy.message.Invoked;

public class InvocationDepthLogger implements Logger {
private final Logger logger;
@@ -0,0 +1,34 @@
package org.logbuddy.message;

import static java.lang.String.format;
import static java.util.Objects.hash;

import java.util.Objects;

public class Attribute {
public final Object model;

private Attribute(Object model) {
this.model = model;
}

public static Attribute attribute(Object model) {
return new Attribute(model);
}

public boolean equals(Object object) {
return object instanceof Attribute && equals((Attribute) object);
}

private boolean equals(Attribute attribute) {
return Objects.equals(model, attribute.model);
}

public int hashCode() {
return hash(model);
}

public String toString() {
return format("attribute(%s)", model);
}
}
@@ -1,4 +1,4 @@
package org.logbuddy.model;
package org.logbuddy.message;

import static java.lang.String.format;
import static java.util.Objects.hash;
@@ -7,15 +7,15 @@
import java.util.Objects;

public class Completed {
public static ReturnedObject returned(Object object) {
public static Completed returned(Object object) {
return new ReturnedObject(object);
}

public static ReturnedVoid returned() {
public static Completed returned() {
return new ReturnedVoid();
}

public static Thrown thrown(Throwable throwable) {
public static Completed thrown(Throwable throwable) {
return new Thrown(throwable);
}

@@ -1,4 +1,4 @@
package org.logbuddy.model;
package org.logbuddy.message;

import static java.lang.String.format;
import static java.util.Objects.hash;
@@ -1,4 +1,4 @@
package org.logbuddy.model;
package org.logbuddy.message;

import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
@@ -9,7 +9,7 @@
import java.awt.image.RenderedImage;

import org.logbuddy.Message;
import org.logbuddy.model.Completed.Thrown;
import org.logbuddy.message.Completed.Thrown;

public class HtmlRenderer extends TextRenderer {
public HtmlRenderer() {}
@@ -10,6 +10,7 @@
import static org.logbuddy.common.Collections.arrayToList;
import static org.logbuddy.common.Strings.times;
import static org.logbuddy.common.Throwables.stackTrace;
import static org.logbuddy.message.Attribute.attribute;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -18,11 +19,12 @@

import org.logbuddy.Message;
import org.logbuddy.Renderer;
import org.logbuddy.model.Completed.ReturnedObject;
import org.logbuddy.model.Completed.ReturnedVoid;
import org.logbuddy.model.Completed.Thrown;
import org.logbuddy.model.InvocationDepth;
import org.logbuddy.model.Invoked;
import org.logbuddy.message.Attribute;
import org.logbuddy.message.Completed.ReturnedObject;
import org.logbuddy.message.Completed.ReturnedVoid;
import org.logbuddy.message.Completed.Thrown;
import org.logbuddy.message.InvocationDepth;
import org.logbuddy.message.Invoked;

public class TextRenderer implements Renderer<String> {
private final DateTimeFormatter dateTimeFormatter;
@@ -46,9 +48,11 @@ public String render(Object model) {
if (model == null) {
return "null";
} else if (model instanceof String) {
return escape((String) model);
return renderImpl((String) model);
} else if (model instanceof Message) {
return renderImpl((Message) model);
} else if (model instanceof Attribute) {
return renderImpl((Attribute) model);
} else if (model instanceof Invoked) {
return renderImpl((Invoked) model);
} else if (model instanceof ReturnedObject) {
@@ -78,15 +82,27 @@ protected String escape(String string) {
return string;
}

private String renderImpl(String string) {
return escape(string);
}

private String renderImpl(Message message) {
StringBuilder builder = new StringBuilder();
for (Object attribute : message.attributes()) {
builder.append(render(attribute)).append(escape(" "));
builder.append(render(attribute(attribute))).append(escape(" "));
}
builder.append(render(message.content()));
return builder.toString();
}

private String renderImpl(Attribute attribute) {
if (attribute.model instanceof Thread) {
return render(((Thread) attribute.model).getName());
} else {
return render(attribute.model);
}
}

private String renderImpl(Invoked invoked) {
return ""
+ render(invoked.instance)
@@ -5,9 +5,9 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.logbuddy.Message.message;
import static org.logbuddy.decorator.InvocationDecorator.invocationDecorator;
import static org.logbuddy.model.Completed.returned;
import static org.logbuddy.model.Completed.thrown;
import static org.logbuddy.model.Invoked.invoked;
import static org.logbuddy.message.Completed.returned;
import static org.logbuddy.message.Completed.thrown;
import static org.logbuddy.message.Invoked.invoked;
import static org.logbuddy.testing.Matchers.withContent;
import static org.logbuddy.testing.TestingAnonymous.anonymousAbstractList;
import static org.logbuddy.testing.TestingAnonymous.anonymousArrayList;
@@ -28,7 +28,7 @@
import org.logbuddy.LogBuddyException;
import org.logbuddy.Logger;
import org.logbuddy.Message;
import org.logbuddy.model.Invoked;
import org.logbuddy.message.Invoked;

public class TestInvocationDecorator {
private Decorator decorator;
@@ -5,7 +5,7 @@
import static java.util.Collections.synchronizedList;
import static org.logbuddy.Message.message;
import static org.logbuddy.logger.InvocationDepthLogger.invocationDepth;
import static org.logbuddy.model.InvocationDepth.invocationDepth;
import static org.logbuddy.message.InvocationDepth.invocationDepth;
import static org.testory.Testory.given;
import static org.testory.Testory.givenTest;
import static org.testory.Testory.thenCalled;
@@ -20,9 +20,9 @@
import org.junit.Test;
import org.logbuddy.Logger;
import org.logbuddy.Message;
import org.logbuddy.model.Completed.ReturnedObject;
import org.logbuddy.model.Completed.Thrown;
import org.logbuddy.model.Invoked;
import org.logbuddy.message.Completed.ReturnedObject;
import org.logbuddy.message.Completed.Thrown;
import org.logbuddy.message.Invoked;

public class TestInvocationDepthLogger {
private Logger logger, invocationDepthLogger;
@@ -1,10 +1,10 @@
package org.logbuddy.model;
package org.logbuddy.message;

import static java.lang.String.format;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.sameInstance;
import static org.logbuddy.model.Completed.returned;
import static org.logbuddy.model.Completed.thrown;
import static org.logbuddy.message.Completed.returned;
import static org.logbuddy.message.Completed.thrown;
import static org.testory.Testory.given;
import static org.testory.Testory.givenTest;
import static org.testory.Testory.then;
@@ -15,16 +15,14 @@
import org.junit.Before;
import org.junit.Test;
import org.logbuddy.LogBuddyException;
import org.logbuddy.model.Completed.ReturnedObject;
import org.logbuddy.model.Completed.ReturnedVoid;
import org.logbuddy.model.Completed.Thrown;
import org.logbuddy.message.Completed.ReturnedObject;
import org.logbuddy.message.Completed.ReturnedVoid;
import org.logbuddy.message.Completed.Thrown;

public class TestCompleted {
private Object object, otherObject;
private Throwable throwable, otherThrowable;
private ReturnedObject returned;
private ReturnedVoid returnedVoid;
private Thrown thrown;
private Completed returned, returnedVoid, thrown;

@Before
public void before() {
@@ -34,14 +32,14 @@ public void before() {
@Test
public void creates_returned_object() {
given(returned = returned(object));
when(returned.object);
when(((ReturnedObject) returned).object);
thenReturned(sameInstance(object));
}

@Test
public void creates_returned_null() {
given(returned = returned(null));
when(returned.object);
when(((ReturnedObject) returned).object);
thenReturned(null);
}

@@ -54,7 +52,7 @@ public void creates_returned_void() {
@Test
public void creates_thrown_throwable() {
given(thrown = thrown(throwable));
when(thrown.throwable);
when(((Thrown) thrown).throwable);
thenReturned(throwable);
}

@@ -3,10 +3,10 @@
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.logbuddy.Message.message;
import static org.logbuddy.model.Completed.returned;
import static org.logbuddy.model.Completed.thrown;
import static org.logbuddy.model.InvocationDepth.invocationDepth;
import static org.logbuddy.model.Invoked.invoked;
import static org.logbuddy.message.Completed.returned;
import static org.logbuddy.message.Completed.thrown;
import static org.logbuddy.message.InvocationDepth.invocationDepth;
import static org.logbuddy.message.Invoked.invoked;
import static org.testory.Testory.given;
import static org.testory.Testory.givenTest;
import static org.testory.Testory.thenReturned;

0 comments on commit 35ad2a1

Please sign in to comment.
You can’t perform that action at this time.