Skip to content

Commit a335e18

Browse files
committed
F!! SimpleLogger logToString now handles multi threaded tests by default
1 parent 1c2946e commit a335e18

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

approvaltests-tests/src/test/java/org/approvaltests/StoryBoardTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ void gameOfLife()
2626
@Test
2727
void gameOfLifeWithDescription()
2828
{
29-
try (VerifiableStoryBoard storyboard = StoryBoardApprovals.verifyStoryboard()) {
29+
try (VerifiableStoryBoard storyboard = StoryBoardApprovals.verifyStoryboard())
30+
{
3031
GameOfLife gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
3132
storyboard.addDescription("Game of Life");
3233
storyboard.add(gameOfLife);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.spun.util;
2+
3+
public class SingleWrapper<T> implements Wrapper<T>
4+
{
5+
private final T instance;
6+
public SingleWrapper(T instance)
7+
{
8+
this.instance = instance;
9+
}
10+
@Override
11+
public T get()
12+
{
13+
return instance;
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.spun.util;
2+
3+
import java.util.function.Supplier;
4+
5+
public class ThreadedWrapper<T> implements Wrapper<T>
6+
{
7+
private final ThreadLocal<T> instance;
8+
public ThreadedWrapper(Supplier<T> lambda)
9+
{
10+
this.instance = ThreadLocal.withInitial(lambda);
11+
}
12+
@Override
13+
public T get()
14+
{
15+
return instance.get();
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.spun.util;
2+
3+
public interface Wrapper<T>
4+
{
5+
T get();
6+
}
Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
11
package com.spun.util.logger;
22

3+
import com.spun.util.SingleWrapper;
4+
import com.spun.util.ThreadedWrapper;
5+
import com.spun.util.Wrapper;
6+
37
public class SimpleLogger
48
{
5-
private static final SimpleLoggerInstance log = new SimpleLoggerInstance(1);
9+
private static Wrapper<SimpleLoggerInstance> wrapper = new SingleWrapper<>(new SimpleLoggerInstance(1));
610
public static void toggleAll(boolean t)
711
{
8-
log.toggleAll(t);
12+
wrapper.get().toggleAll(t);
913
}
1014
public static void setHourGlassWrap(int numberOfDots)
1115
{
12-
log.setHourGlassWrap(numberOfDots);
16+
wrapper.get().setHourGlassWrap(numberOfDots);
1317
}
1418
public static void hourGlass()
1519
{
16-
log.hourGlass();
20+
wrapper.get().hourGlass();
1721
}
1822
@Deprecated
1923
//use useMarkers
2024
public static void markerIn(String statement)
2125
{
22-
log.markerIn(statement);
26+
wrapper.get().markerIn(statement);
2327
}
2428
@Deprecated
2529
//use useMarkers
2630
public synchronized static void markerOut(String text)
2731
{
28-
log.markerOut(text);
32+
wrapper.get().markerOut(text);
2933
}
3034
public synchronized static void query(String sqlQuery)
3135
{
32-
log.query(sqlQuery);
36+
wrapper.get().query(sqlQuery);
3337
}
3438
/**
3539
* Prints to screen any variable information to be viewed.
3640
**/
3741
public synchronized static void query(String queryName, Object sqlQuery)
3842
{
39-
log.query(queryName, sqlQuery);
43+
wrapper.get().query(queryName, sqlQuery);
4044
}
4145
public static void variableFormated(String string, Object... parameters)
4246
{
43-
log.variableFormated(string, parameters);
47+
wrapper.get().variableFormated(string, parameters);
4448
}
4549
public synchronized static void variable(String statement)
4650
{
47-
log.variable(statement);
51+
wrapper.get().variable(statement);
4852
}
4953
public static void variable(String name, Object value, boolean showTypes)
5054
{
51-
log.variable(name, value, showTypes);
55+
wrapper.get().variable(name, value, showTypes);
5256
}
5357
/**
5458
* Prints to screen any variable information to be viewed.
@@ -59,39 +63,39 @@ public synchronized static void variable(String name, Object value)
5963
}
6064
public synchronized static void variable(String name, Object[] array)
6165
{
62-
log.variable(name, array);
66+
wrapper.get().variable(name, array);
6367
}
6468
public synchronized static <T> void variable(T[] array)
6569
{
66-
log.variable(array);
70+
wrapper.get().variable(array);
6771
}
6872
public synchronized static void message(String Statement)
6973
{
70-
log.message(Statement);
74+
wrapper.get().message(Statement);
7175
}
7276
public static void event(String Statement)
7377
{
74-
log.event(Statement);
78+
wrapper.get().event(Statement);
7579
}
7680
public synchronized static void warning(String statement)
7781
{
78-
log.warning(statement);
82+
wrapper.get().warning(statement);
7983
}
8084
public synchronized static void warning(Throwable throwable)
8185
{
82-
log.warning(throwable);
86+
wrapper.get().warning(throwable);
8387
}
8488
public synchronized static void warning(String statement, Throwable throwable)
8589
{
86-
log.warning(statement, throwable);
90+
wrapper.get().warning(statement, throwable);
8791
}
8892
/**
8993
* Logs the current memory status [total, used, free].
9094
* This forces garbage collection to run first.
9195
**/
9296
public static void logMemoryStatus()
9397
{
94-
log.logMemoryStatus();
98+
wrapper.get().logMemoryStatus();
9599
}
96100
/**
97101
* {@code
@@ -102,31 +106,42 @@ public static void logMemoryStatus()
102106
*/
103107
public static Markers useMarkers()
104108
{
105-
return log.useMarkers();
109+
return wrapper.get().useMarkers();
106110
}
107111
public static StringBuffer logToString()
108112
{
109-
return log.logToString();
113+
return logToString(true);
114+
}
115+
public static StringBuffer logToString(boolean threadSafe)
116+
{
117+
synchronized (SimpleLogger.wrapper)
118+
{
119+
if (threadSafe && !(wrapper instanceof ThreadedWrapper))
120+
{
121+
wrapper = new ThreadedWrapper<>(() -> new SimpleLoggerInstance(1));
122+
}
123+
return wrapper.get().logToString();
124+
}
110125
}
111126
public static Appendable logToNothing()
112127
{
113128
return logTo(new NullLogger());
114129
}
115130
public static void useOutputFile(String file, boolean addDateStamp)
116131
{
117-
log.useOutputFile(file, addDateStamp);
132+
wrapper.get().useOutputFile(file, addDateStamp);
118133
}
119134
public static Appendable logTo(Appendable writer)
120135
{
121-
log.logTo(writer);
136+
wrapper.get().logTo(writer);
122137
return writer;
123138
}
124139
public static Appendable getLogTo()
125140
{
126-
return log.getLogTo();
141+
return wrapper.get().getLogTo();
127142
}
128143
public static SimpleLoggerInstance get()
129144
{
130-
return log;
145+
return wrapper.get();
131146
}
132147
}

0 commit comments

Comments
 (0)