From 6d77ba9f772864e27d8410461d06a30f200cbac6 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Fri, 5 Jul 2013 21:23:01 +0200 Subject: [PATCH] Stack trace not available when no SLF4J binding in classpath, fixes #118 --- .../src/example/java/WebcamPanelExample.java | 14 ++------- .../example/resources/simplelogger.properties | 7 +++++ .../sarxos/webcam/WebcamExceptionHandler.java | 15 ++++++++-- .../com/github/sarxos/webcam/WebcamPanel.java | 24 +++++++-------- .../github/sarxos/webcam/WebcamUpdater.java | 10 +++++++ .../webcam/log/WebcamLogConfigurator.java | 30 +++++++------------ 6 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 webcam-capture/src/example/resources/simplelogger.properties diff --git a/webcam-capture/src/example/java/WebcamPanelExample.java b/webcam-capture/src/example/java/WebcamPanelExample.java index 924bf964..514325ca 100644 --- a/webcam-capture/src/example/java/WebcamPanelExample.java +++ b/webcam-capture/src/example/java/WebcamPanelExample.java @@ -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 @@ -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(); - } } diff --git a/webcam-capture/src/example/resources/simplelogger.properties b/webcam-capture/src/example/resources/simplelogger.properties new file mode 100644 index 00000000..56b9ce4f --- /dev/null +++ b/webcam-capture/src/example/resources/simplelogger.properties @@ -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 diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamExceptionHandler.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamExceptionHandler.java index 17dbb50e..9b222b30 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamExceptionHandler.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamExceptionHandler.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.helpers.NOPLoggerFactory; /** @@ -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() { diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamPanel.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamPanel.java index 9dd65244..5c912f89 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamPanel.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamPanel.java @@ -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; } diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamUpdater.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamUpdater.java index 09516e47..638726f5 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamUpdater.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamUpdater.java @@ -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; diff --git a/webcam-capture/src/main/java/com/github/sarxos/webcam/log/WebcamLogConfigurator.java b/webcam-capture/src/main/java/com/github/sarxos/webcam/log/WebcamLogConfigurator.java index 4978d26a..96aa39d1 100644 --- a/webcam-capture/src/main/java/com/github/sarxos/webcam/log/WebcamLogConfigurator.java +++ b/webcam-capture/src/main/java/com/github/sarxos/webcam/log/WebcamLogConfigurator.java @@ -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; @@ -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(); } }