<commit_before>/**
 * Project: jlib 
 * Version: $Id$ 
 * License: SPL
 *
 * This file is best viewed with 110 columns.
 * 34567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
 * 
 * Authors (in chronological order): 
 *   <NAME> - http://dejan.lekic.org
 * Contributors (in chronological order): 
 *   -
 */

package com.areen.jlib.util;

import com.areen.jlib.gui.err.ExceptionDialog;
import com.areen.jlib.gui.err.ExceptionUtility;
import javax.swing.JFrame;
import org.apache.log4j.Logger;

/**
 * This simple implementation of the Thread.UncaughtExceptionHandler will popup a dialog whenever an
 * uncaught exception is thrown.
 * 
 * Example:
 **********

Thread.setDefaultUncaughtExceptionHandler(new ExceptionLogger("com.areen.jlib"));

 **********
 * @author dejan
 */
public class ExceptionLogger implements Thread.UncaughtExceptionHandler {
    private Logger logger;
    private ExceptionInfoSender exceptionInfoSender;

    public ExceptionLogger(String argPackage) {
        if (argPackage == null) {
            throw new NullPointerException("Package argument to the ExceptionLogger is a NULL.");
        } else {
            logger = Logger.getLogger(argPackage);
        }
    } // ExceptionLogger constructor

    public void setExceptionInfoSender(ExceptionInfoSender argExceptionInfoSender) {
        exceptionInfoSender = argExceptionInfoSender;
    }
    
    @Override
    public void uncaughtException(Thread argThread, Throwable argThrowable) {
        if (argThrowable instanceof Exception) {
            final String eof = System.getProperty("line.separator");
            Exception ex = (Exception) argThrowable;
            String header = ex + "  Thread: " + argThread.getName() + eof 
                        + ":::::::::::::::::::::::::::::::::" + eof;
            
            JFrame frame = null; // parent frame
            
            ExceptionDialog ed = new ExceptionDialog(frame, true);
            String exceptionString = ex + eof + ":::::::::::::::::::::::::::::::::" + eof;
            String lineInfo = argThrowable.toString() + " Thread: " + argThread.getName();
            ed.setInfo(lineInfo);
            ed.setExceptionText(exceptionString + ExceptionUtility.getStackTrace(ex.getStackTrace()));
            ed.setLocationRelativeTo(frame);
            ed.setExceptionInfoSender(exceptionInfoSender);
            ed.setVisible(true);
            
            if (logger.isDebugEnabled()) {
                logger.debug(header + ExceptionUtility.getStackTrace(ex.getStackTrace()));
            }
        }
    } // uncaughtException() method
    
} // ExceptionLogger class

// $Id$<commit_msg>ExceptionLogger changes related to this task... Related issue(s): APC-1045<commit_after>/**
 * Project: jlib 
 * Version: $Id$ 
 * License: SPL
 *
 * This file is best viewed with 110 columns.
 * 34567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
 * 
 * Authors (in chronological order): 
 *   <NAME> - http://dejan.lekic.org
 * Contributors (in chronological order): 
 *   -
 */

package com.areen.jlib.util;

import com.areen.jlib.gui.err.ExceptionDialog;
import com.areen.jlib.gui.err.ExceptionUtility;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 * This simple implementation of the Thread.UncaughtExceptionHandler will popup a dialog whenever an
 * uncaught exception is thrown.
 * 
 * Example:
 **********

Thread.setDefaultUncaughtExceptionHandler(new ExceptionLogger("com.areen.jlib"));

 **********
 * @author dejan
 */
public class ExceptionLogger implements Thread.UncaughtExceptionHandler {
    private Logger logger;
    private ExceptionInfoSender exceptionInfoSender;

    public ExceptionLogger(String argPackage) {
        if (argPackage == null) {
            throw new NullPointerException("Package argument to the ExceptionLogger is a NULL.");
        } else {
            logger = Logger.getLogger(argPackage);
        }
    } // ExceptionLogger constructor

    public void setExceptionInfoSender(ExceptionInfoSender argExceptionInfoSender) {
        exceptionInfoSender = argExceptionInfoSender;
    }
    
    @Override
    public void uncaughtException(Thread argThread, Throwable argThrowable) {
        if (argThrowable instanceof Exception) {
            
            final String eof = System.getProperty("line.separator");
            Exception ex = (Exception) argThrowable;
            String header = ex + "  Thread: " + argThread.getName() + eof 
                        + ":::::::::::::::::::::::::::::::::" + eof;

            JFrame frame = null; // parent frame
            
            // First we show a dialog
            Object[] options = { "More...", "OK"};
            int ret = JOptionPane.showOptionDialog(frame, 
                    "Unexpected error. \nClick on `More...` for more information.", 
                    "APC error", 
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.ERROR_MESSAGE, 
                    null, 
                    options, 
                    options[1]);
            
            if (ret == 0) {
                // User pressed "More..." 
                ExceptionDialog ed = new ExceptionDialog(frame, true);
                String exceptionString = ex + eof + ":::::::::::::::::::::::::::::::::" + eof;
                String lineInfo = argThrowable.toString() + " Thread: " + argThread.getName();
                ed.setInfo(lineInfo);
                ed.setExceptionText(exceptionString + ExceptionUtility.getStackTrace(ex.getStackTrace()));
                ed.setLocationRelativeTo(frame);
                ed.setExceptionInfoSender(exceptionInfoSender);
                ed.setVisible(true);

                if (logger.isDebugEnabled()) {
                    logger.debug(header + ExceptionUtility.getStackTrace(ex.getStackTrace()));
                } // if
            } // if
        } // if
    } // uncaughtException() method
    
} // ExceptionLogger class

// $Id$
{'fraction_non_alphanumeric': 0.12032173342087985, 'fraction_numerical': 0.03742613263296126, 'mean_word_length': 2.4856979405034325, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2399263', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1521, 'n_words': 357}