Skip to content

Commit

Permalink
improvements to SLF4J-515
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jul 9, 2021
1 parent b6fb6e3 commit e4fd949
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
9 changes: 7 additions & 2 deletions slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java
Expand Up @@ -26,8 +26,6 @@

import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

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

/**
* To avoid intermingling of log messages and associated stack traces, the two
* operations are done in a synchronized block.
*
* @param buf
* @param t
*/
void write(StringBuilder buf, Throwable t) {
PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();

Expand Down
Expand Up @@ -40,6 +40,8 @@
public class MultithereadedExecutionTest {

private static int THREAD_COUNT = 2;
private static long TEST_DURATION_IN_MILLIS = 100;

private Thread[] threads = new Thread[THREAD_COUNT];

private final PrintStream oldOut = System.out;
Expand Down Expand Up @@ -69,7 +71,7 @@ public void test() throws Throwable {
threads[1] = new Thread(other);
threads[0].start();
threads[1].start();
Thread.sleep(100);
Thread.sleep(TEST_DURATION_IN_MILLIS);
signal = true;
threads[0].join();
threads[1].join();
Expand All @@ -86,10 +88,9 @@ public void test() throws Throwable {

class WithException implements Runnable {

Throwable throwable;
volatile Throwable throwable;
Logger logger = LoggerFactory.getLogger(WithException.class);

@Override
public void run() {
int i = 0;

Expand All @@ -108,10 +109,9 @@ public void run() {
}

class Other implements Runnable {
Throwable throwable;
volatile Throwable throwable;
Logger logger = LoggerFactory.getLogger(Other.class);

@Override
public void run() {
int i = 0;
while (!signal) {
Expand All @@ -125,5 +125,4 @@ public void run() {
}
}
}

}
Expand Up @@ -25,20 +25,20 @@
package org.slf4j.simple.multiThreadedExecution;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
* This PrintStream checks that output lines are in an expected order.
*
* @author ceki
*/
public class StateCheckingPrintStream extends PrintStream {

enum State {
INITIAL, UNKNOWN, HELLO, THROWABLE, AT1, AT2, OTHER;
}

List<String> stringList = Collections.synchronizedList(new ArrayList<String>());

State currentState = State.INITIAL;
volatile State currentState = State.INITIAL;

public StateCheckingPrintStream(PrintStream ps) {
super(ps);
Expand All @@ -57,6 +57,7 @@ public void println(String s) {
break;

case UNKNOWN:
// ignore garbage
currentState = next;
break;

Expand Down Expand Up @@ -98,8 +99,6 @@ public void println(String s) {
default:
throw new IllegalStateException("Unreachable code");
}

stringList.add(s);
}

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

0 comments on commit e4fd949

Please sign in to comment.