All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclarion.clarion.crash.Crash Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2010, by Andrew Barnham
 *
 * The contents of this file are subject to
 * GNU Lesser General Public License (LGPL), v.3
 * http://www.gnu.org/licenses/lgpl.txt
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 */
package org.jclarion.clarion.crash;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import org.jclarion.clarion.runtime.CRun;
import org.jclarion.clarion.runtime.CWin;


public class Crash implements Runnable 
{
    public static void main(String args[]) {
        try {
            throw new RuntimeException("Hello");
        } catch (Throwable t) {
            getInstance().log(t);
        }
        getInstance().crash();
    }
    
    private static Crash        instance;
    
    public static Crash getInstance()
    {
        if (instance==null) {
            synchronized(Crash.class) {
                if (instance==null) {
                    instance=new Crash();
                }
            }
        }
        return instance;
    }
    
    private StringBuilder log;
    private CrashStream  stream;
    private List contributors=new ArrayList();
    private Properties      crashResources;
    
    public Crash()
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream("resources/crash.properties");
        crashResources=new Properties();
        if (is!=null) {
            try {
                crashResources.load(is);
            } catch (IOException ex) { }
            try {
                is.close();
            } catch (IOException ex) { }
        }
        
        log=new StringBuilder();
        CharArrayWriter caw = new CharArrayWriter();
        try {
            System.getProperties().store(caw,"Crash Log");
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.append(caw.toString());
        log.append("\n==============\n");
        
        CRun.addShutdownHook(new Runnable() { 
            public void run()
            {
                instance=null;
            }
        } );
    }
    
    public void registerContributor(CrashContributor contributor)
    {
        synchronized(contributors) {
            contributors.add(contributor);
        }
    }
    
    public void setCrashStream(CrashStream stream)
    {
        this.stream=stream;
    }
    
    public void crashDetailed()
    {
        log(CWin.getInstance().getDebugMetaData());
        crash();
    }
    
    public void log(String entry)
    {
        synchronized(log) {
            log.append(entry).append('\n');
        }
    }

    public void log(int b) {
        synchronized(log) {
            log.append((char)b);
        }
    }

    public void log(byte b[],int ofs,int len) {
        synchronized(log) {
            for (int scan=0;scan=0) {
            }
            is.close();
            labelb.setText(getResourceString("label.uploaded","A report of this crash has been uploaded for analysis"));
        } catch (IOException e) {
            labelb.setText(getResourceString("label.uploaded","A report of this crash could not be uploaded. Please contact your software vendor with below details."));
            labelb.setForeground(Color.red);
            e.printStackTrace();
        }
    }

    public void logAllThreads() {
        for (Entry e : Thread.getAllStackTraces().entrySet() ) {
            log(e.getKey().toString());
            for (StackTraceElement line : e.getValue()) {
                log(" "+line.toString());
            }
        }
    }

    public void threadCrash() {
        Thread t = new Thread("crash") {
            public void run()
            {
                crash();
            }
        };
        t.start();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy