001    package com.github.sarxos.webcam;
002    
003    import java.lang.Thread.UncaughtExceptionHandler;
004    
005    import org.slf4j.Logger;
006    import org.slf4j.LoggerFactory;
007    import org.slf4j.helpers.NOPLoggerFactory;
008    
009    
010    /**
011     * Used internally.
012     * 
013     * @author Bartosz Firyn (sarxos)
014     */
015    public class WebcamExceptionHandler implements UncaughtExceptionHandler {
016    
017            private static final Logger LOG = LoggerFactory.getLogger(WebcamExceptionHandler.class);
018    
019            private static final WebcamExceptionHandler INSTANCE = new WebcamExceptionHandler();
020    
021            private WebcamExceptionHandler() {
022                    // singleton
023            }
024    
025            @Override
026            public void uncaughtException(Thread t, Throwable e) {
027                    Object context = LoggerFactory.getILoggerFactory();
028                    if (context instanceof NOPLoggerFactory) {
029                            System.err.println(String.format("Exception in thread %s", t.getName()));
030                            e.printStackTrace();
031                    } else {
032                            LOG.error(String.format("Exception in thread %s", t.getName()), e);
033                    }
034            }
035    
036            public static void handle(Throwable e) {
037                    INSTANCE.uncaughtException(Thread.currentThread(), e);
038            }
039    
040            public static final WebcamExceptionHandler getInstance() {
041                    return INSTANCE;
042            }
043    }