Skip to content

Commit 6c14879

Browse files
author
Eric Denman
committed
Merge pull request SquareSquash#6 from square/jw/thread-stuff
Grab actual thread name. Thread local style tweak.
2 parents 15bbf52 + 5470d18 commit 6c14879

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

src/main/java/com/squareup/squash/SquashBacktrace.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
/** Creates the Squash stacktrace format for serialization by gson. */
1212
public final class SquashBacktrace {
1313

14-
// TODO get the actual thread identifier if we can.
15-
public static final String THREAD_0 = "Thread 0";
1614
// This lets Squash know that the stacktrace is java-style and can contain obfuscated classes.
1715
public static final String JAVA_PREFIX = "_JAVA_";
1816

@@ -25,11 +23,11 @@ public static List<List<Object>> getBacktraces(Throwable error) {
2523
return null;
2624
}
2725
final ArrayList<List<Object>> threadList = new ArrayList<List<Object>>();
28-
final ArrayList<Object> thread0 = new ArrayList<Object>();
29-
thread0.add(THREAD_0);
30-
thread0.add(true);
31-
thread0.add(getStacktraceArray(error));
32-
threadList.add(thread0);
26+
final ArrayList<Object> currentThread = new ArrayList<Object>();
27+
currentThread.add(Thread.currentThread().getName());
28+
currentThread.add(true);
29+
currentThread.add(getStacktraceArray(error));
30+
threadList.add(currentThread);
3331
return threadList;
3432
}
3533

src/main/java/com/squareup/squash/SquashEntry.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
@SuppressWarnings({ "FieldCanBeLocal", "UnusedDeclaration" })
1414
public class SquashEntry {
1515
private static final String DATE_RFC_2822 = "EEE, dd MMM yyyy HH:mm:ss Z";
16-
private static final ThreadLocal<DateFormat> DATE_FORMAT_THREAD_LOCAL =
17-
new ThreadLocal<DateFormat>();
16+
private static final ThreadLocal<DateFormat> DATE_FORMAT_THREAD_LOCAL = new ThreadLocal<DateFormat>() {
17+
@Override protected DateFormat initialValue() {
18+
return new SimpleDateFormat(DATE_RFC_2822);
19+
}
20+
};
1821

1922
// Things that do not change per entry but should still be gson'd.
2023
private final String client;
@@ -55,11 +58,6 @@ public SquashEntry(String client, String apiKey, String message, Throwable error
5558
this.message = error == null ? null : error.getMessage();
5659
this.api_key = apiKey;
5760
this.user_id = userId;
58-
DateFormat dateFormat = DATE_FORMAT_THREAD_LOCAL.get();
59-
if (dateFormat == null) {
60-
dateFormat = new SimpleDateFormat(DATE_RFC_2822);
61-
DATE_FORMAT_THREAD_LOCAL.set(dateFormat);
62-
}
63-
this.occurred_at = dateFormat.format(new Date());
61+
this.occurred_at = DATE_FORMAT_THREAD_LOCAL.get().format(new Date());
6462
}
6563
}

src/test/java/com/squareup/squash/SquashEntryTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
package com.squareup.squash;
33

44
import com.google.gson.Gson;
5+
import org.junit.Test;
6+
57
import java.io.IOException;
68
import java.util.List;
7-
import org.junit.Test;
89

910
import static org.fest.assertions.Assertions.assertThat;
1011
import static org.mockito.Mockito.mock;
@@ -48,7 +49,7 @@ private SquashEntry serializeAndDeserialize(SquashEntry logEntry) throws IOExcep
4849
SquashEntry deserialized = serializeAndDeserialize(logEntry);
4950
assertThat(deserialized.backtraces).isNotEmpty();
5051
final List<Object> backtrace = deserialized.backtraces.get(0);
51-
assertThat(backtrace.get(0)).isEqualTo(SquashBacktrace.THREAD_0);
52+
assertThat(backtrace.get(0)).isEqualTo(Thread.currentThread().getName());
5253
assertThat(backtrace.get(1)).isEqualTo(true);
5354
List<List<Object>> stackElements = (List<List<Object>>) backtrace.get(2);
5455
assertBacktracesMatch(myLittleStackTrace, stackElements);
@@ -121,7 +122,7 @@ private void assertBacktracesMatch(StackTraceElement[] myLittleStackTrace,
121122
SquashEntry deserialized = serializeAndDeserialize(logEntry);
122123
assertThat(deserialized.backtraces).isNotEmpty();
123124
List<Object> backtrace = deserialized.backtraces.get(0);
124-
assertThat(backtrace.get(0)).isEqualTo(SquashBacktrace.THREAD_0);
125+
assertThat(backtrace.get(0)).isEqualTo(Thread.currentThread().getName());
125126
assertThat(backtrace.get(1)).isEqualTo(true);
126127
List<List<Object>> stackElements = (List<List<Object>>) backtrace.get(2);
127128
assertBacktracesMatch(myLittleStackTrace, stackElements);
@@ -136,7 +137,7 @@ private void assertBacktracesMatch(StackTraceElement[] myLittleStackTrace,
136137
assertThat(nested1.ivars).isEmpty();
137138
assertThat(nested1.message).isEqualTo(nestedExceptionMessage);
138139
backtrace = nested1.backtraces.get(0);
139-
assertThat(backtrace.get(0)).isEqualTo(SquashBacktrace.THREAD_0);
140+
assertThat(backtrace.get(0)).isEqualTo(Thread.currentThread().getName());
140141
assertThat(backtrace.get(1)).isEqualTo(true);
141142
assertBacktracesMatch(nestedStackTrace, (List<List<Object>>) backtrace.get(2));
142143

@@ -145,7 +146,7 @@ private void assertBacktracesMatch(StackTraceElement[] myLittleStackTrace,
145146
assertThat(nested2.ivars).isEmpty();
146147
assertThat(nested2.message).isEqualTo(doublyNestedExceptionMessage);
147148
backtrace = nested1.backtraces.get(0);
148-
assertThat(backtrace.get(0)).isEqualTo(SquashBacktrace.THREAD_0);
149+
assertThat(backtrace.get(0)).isEqualTo(Thread.currentThread().getName());
149150
assertThat(backtrace.get(1)).isEqualTo(true);
150151
assertBacktracesMatch(nestedStackTrace, (List<List<Object>>) backtrace.get(2));
151152
}

0 commit comments

Comments
 (0)