Skip to content

Commit

Permalink
Stack trace not available when no SLF4J binding in classpath, fixes #118
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 5, 2013
1 parent 65f7873 commit 6d77ba9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
14 changes: 3 additions & 11 deletions webcam-capture/src/example/java/WebcamPanelExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static void main(String[] args) throws InterruptedException {

JFrame window = new JFrame("Test webcam panel");

final WebcamPanel panel = new WebcamPanel(Webcam.getDefault());
Webcam webcam = Webcam.getDefault();

WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true); // display FPS on screen
panel.setFPSLimited(false); // no FPS limit
panel.setFillArea(true); // image will be resized with window
Expand All @@ -19,15 +21,5 @@ public static void main(String[] args) throws InterruptedException {
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Thread.sleep(5000);
panel.stop();
Thread.sleep(5000);
panel.start();
Thread.sleep(5000);
panel.pause();
Thread.sleep(5000);
panel.resume();

}
}
7 changes: 7 additions & 0 deletions webcam-capture/src/example/resources/simplelogger.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
org.slf4j.simpleLogger.defaultLogLevel=error
org.slf4j.simpleLogger.log.com.github.sarxos.webcam=warn
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss.SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.showLogName=true
org.slf4j.simpleLogger.showShortLogName=true
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory;


/**
Expand All @@ -23,9 +24,17 @@ private WebcamExceptionHandler() {

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
System.err.println(String.format("Exception in thread %s", t.getName()));
e.printStackTrace();
Object context = LoggerFactory.getILoggerFactory();
if (context instanceof NOPLoggerFactory) {
System.err.println(String.format("Exception in thread %s", t.getName()));
e.printStackTrace();
} else {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}
}

public static void handle(Throwable e) {
INSTANCE.uncaughtException(Thread.currentThread(), e);
}

public static final WebcamExceptionHandler getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,27 +285,23 @@ public void stop() {

@Override
public void run() {

if (!running.get()) {
return;
try {
update();
} catch (Throwable t) {
errored = true;
WebcamExceptionHandler.handle(t);
}
}

if (!webcam.isOpen()) {
return;
}
private void update() {

if (paused) {
if (!running.get() || !webcam.isOpen() || paused) {
return;
}

BufferedImage tmp = null;
try {
tmp = webcam.getImage();
} catch (Throwable t) {
LOG.error("Exception when getting image", t);
}

BufferedImage tmp = webcam.getImage();
if (tmp != null) {
errored = false;
image = tmp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ public void run() {
return;
}

try {
tick();
} catch (Throwable t) {
WebcamExceptionHandler.handle(t);
}

}

private void tick() {

long t1 = 0;
long t2 = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -35,32 +34,25 @@ public static void configure(InputStream is) {
try {

String[] names = {
"ch.qos.logback.classic.LoggerContext",
"ch.qos.logback.classic.joran.JoranConfigurator",
"ch.qos.logback.classic.LoggerContext",
"ch.qos.logback.classic.joran.JoranConfigurator",
};

for (String name : names) {
Class.forName(name, false, cl);
}

Object context = LoggerFactory.getILoggerFactory();

Class<?> cfgc = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
Object configurator = cfgc.newInstance();

Method setContext = cfgc.getMethod("setContext");
setContext.invoke(configurator, context);

Method reset = context.getClass().getMethod("reset");
reset.invoke(context, new Object[0]);

Method doConfigure = cfgc.getMethod("doConfigure");
doConfigure.invoke(configurator, is);
ch.qos.logback.classic.LoggerContext context = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.joran.JoranConfigurator configurator = new ch.qos.logback.classic.joran.JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(is);

} catch (ClassNotFoundException e) {
System.err.println("WLogC: Logback JARs are missing in classpath");
} catch (Exception e) {
LOG.error("Joran configuration exception", e);
} catch (NoClassDefFoundError e) {
System.err.println("WLogC: Logback JARs are missing in classpath");
} catch (Throwable e) {
e.printStackTrace();
}
}

Expand Down

0 comments on commit 6d77ba9

Please sign in to comment.