Skip to content

Commit e4fd949

Browse files
committed
improvements to SLF4J-515
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent b6fb6e3 commit e4fd949

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import java.io.PrintStream;
2828
import java.util.Date;
29-
import java.util.concurrent.TimeUnit;
30-
import java.util.concurrent.locks.ReentrantLock;
3129

3230
import org.slf4j.Logger;
3331
import org.slf4j.event.LoggingEvent;
@@ -314,6 +312,13 @@ protected String renderLevel(int level) {
314312
throw new IllegalStateException("Unrecognized level [" + level + "]");
315313
}
316314

315+
/**
316+
* To avoid intermingling of log messages and associated stack traces, the two
317+
* operations are done in a synchronized block.
318+
*
319+
* @param buf
320+
* @param t
321+
*/
317322
void write(StringBuilder buf, Throwable t) {
318323
PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
319324

slf4j-simple/src/test/java/org/slf4j/simple/multiThreadedExecution/MultithereadedExecutionTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
public class MultithereadedExecutionTest {
4141

4242
private static int THREAD_COUNT = 2;
43+
private static long TEST_DURATION_IN_MILLIS = 100;
44+
4345
private Thread[] threads = new Thread[THREAD_COUNT];
4446

4547
private final PrintStream oldOut = System.out;
@@ -69,7 +71,7 @@ public void test() throws Throwable {
6971
threads[1] = new Thread(other);
7072
threads[0].start();
7173
threads[1].start();
72-
Thread.sleep(100);
74+
Thread.sleep(TEST_DURATION_IN_MILLIS);
7375
signal = true;
7476
threads[0].join();
7577
threads[1].join();
@@ -86,10 +88,9 @@ public void test() throws Throwable {
8688

8789
class WithException implements Runnable {
8890

89-
Throwable throwable;
91+
volatile Throwable throwable;
9092
Logger logger = LoggerFactory.getLogger(WithException.class);
9193

92-
@Override
9394
public void run() {
9495
int i = 0;
9596

@@ -108,10 +109,9 @@ public void run() {
108109
}
109110

110111
class Other implements Runnable {
111-
Throwable throwable;
112+
volatile Throwable throwable;
112113
Logger logger = LoggerFactory.getLogger(Other.class);
113114

114-
@Override
115115
public void run() {
116116
int i = 0;
117117
while (!signal) {
@@ -125,5 +125,4 @@ public void run() {
125125
}
126126
}
127127
}
128-
129128
}

slf4j-simple/src/test/java/org/slf4j/simple/multiThreadedExecution/StateCheckingPrintStream.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525
package org.slf4j.simple.multiThreadedExecution;
2626

2727
import java.io.PrintStream;
28-
import java.util.ArrayList;
29-
import java.util.Collections;
30-
import java.util.List;
3128
import java.util.regex.Pattern;
3229

30+
/**
31+
* This PrintStream checks that output lines are in an expected order.
32+
*
33+
* @author ceki
34+
*/
3335
public class StateCheckingPrintStream extends PrintStream {
3436

3537
enum State {
3638
INITIAL, UNKNOWN, HELLO, THROWABLE, AT1, AT2, OTHER;
3739
}
3840

39-
List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
40-
41-
State currentState = State.INITIAL;
41+
volatile State currentState = State.INITIAL;
4242

4343
public StateCheckingPrintStream(PrintStream ps) {
4444
super(ps);
@@ -57,6 +57,7 @@ public void println(String s) {
5757
break;
5858

5959
case UNKNOWN:
60+
// ignore garbage
6061
currentState = next;
6162
break;
6263

@@ -98,8 +99,6 @@ public void println(String s) {
9899
default:
99100
throw new IllegalStateException("Unreachable code");
100101
}
101-
102-
stringList.add(s);
103102
}
104103

105104
private IllegalStateException badState(String s, State currentState2, State next) {

0 commit comments

Comments
 (0)