2005-03-10 21:55:21 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: debugrpt.cpp
|
|
|
|
// Purpose: minimal sample showing wxDebugReport and related classes
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 2005-01-20
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// License: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "wx/app.h"
|
2005-07-16 08:28:08 -04:00
|
|
|
#include "wx/log.h"
|
2005-07-15 17:05:48 -04:00
|
|
|
#include "wx/frame.h"
|
2005-08-30 13:30:43 -04:00
|
|
|
#include "wx/icon.h"
|
2005-07-15 17:05:48 -04:00
|
|
|
#include "wx/menu.h"
|
|
|
|
#include "wx/msgdlg.h"
|
|
|
|
#include "wx/button.h"
|
2005-07-15 18:54:51 -04:00
|
|
|
#include "wx/dcclient.h"
|
2005-07-15 17:05:48 -04:00
|
|
|
|
2005-03-10 21:55:21 -05:00
|
|
|
#include "wx/datetime.h"
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/filename.h"
|
|
|
|
#include "wx/debugrpt.h"
|
|
|
|
|
|
|
|
#if !wxUSE_DEBUGREPORT
|
|
|
|
#error "This sample can't be built without wxUSE_DEBUGREPORT"
|
|
|
|
#endif // wxUSE_DEBUGREPORT
|
|
|
|
|
2005-04-16 18:10:55 -04:00
|
|
|
#if !wxUSE_ON_FATAL_EXCEPTION
|
|
|
|
#error "This sample can't be built without wxUSE_ON_FATAL_EXCEPTION"
|
|
|
|
#endif // wxUSE_ON_FATAL_EXCEPTION
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
#if !defined(__WXMSW__) && !defined(__WXPM__)
|
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
2005-03-10 21:55:21 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// custom debug reporting class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2005-04-10 18:37:43 -04:00
|
|
|
// this is your custom debug reporter: it will use curl program (which should
|
|
|
|
// be available) to upload the crash report to the given URL (which should be
|
|
|
|
// set up by you)
|
2005-03-10 21:55:21 -05:00
|
|
|
class MyDebugReport : public wxDebugReportUpload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyDebugReport() : wxDebugReportUpload
|
|
|
|
(
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("http://your.url.here/"),
|
|
|
|
wxT("report:file"),
|
|
|
|
wxT("action")
|
2005-03-10 21:55:21 -05:00
|
|
|
)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2005-04-10 18:37:43 -04:00
|
|
|
// this is called with the contents of the server response: you will
|
|
|
|
// probably want to parse the XML document in OnServerReply() instead of
|
|
|
|
// just dumping it as I do
|
2005-03-10 21:55:21 -05:00
|
|
|
virtual bool OnServerReply(const wxArrayString& reply)
|
|
|
|
{
|
|
|
|
if ( reply.IsEmpty() )
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogError(wxT("Didn't receive the expected server reply."));
|
2005-03-10 21:55:21 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxString s(wxT("Server replied:\n"));
|
2005-03-10 21:55:21 -05:00
|
|
|
|
|
|
|
const size_t count = reply.GetCount();
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
s << wxT('\t') << reply[n] << wxT('\n');
|
2005-03-10 21:55:21 -05:00
|
|
|
}
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("%s"), s.c_str());
|
2005-03-10 21:55:21 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-04-02 18:32:12 -05:00
|
|
|
// another possibility would be to build email library from contrib and use
|
|
|
|
// this class, after uncommenting it:
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
#include "wx/net/email.h"
|
|
|
|
|
|
|
|
class MyDebugReport : public wxDebugReportCompress
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool DoProcess()
|
|
|
|
{
|
|
|
|
if ( !wxDebugReportCompress::DoProcess() )
|
|
|
|
return false;
|
2009-07-23 16:30:22 -04:00
|
|
|
wxMailMessage msg(GetReportName() + wxT(" crash report"),
|
|
|
|
wxT("vadim@wxwindows.org"),
|
2005-04-02 18:32:12 -05:00
|
|
|
wxEmptyString, // mail body
|
|
|
|
wxEmptyString, // from address
|
|
|
|
GetCompressedFileName(),
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("crashreport.zip"));
|
2005-04-02 18:32:12 -05:00
|
|
|
|
|
|
|
return wxEmail::Send(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // 0
|
|
|
|
|
2005-03-10 21:55:21 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// helper functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// just some functions to get a slightly deeper stack trace
|
2009-02-05 12:42:50 -05:00
|
|
|
static void bar(const char *p)
|
2005-03-10 21:55:21 -05:00
|
|
|
{
|
|
|
|
char *pc = 0;
|
|
|
|
*pc = *p;
|
|
|
|
|
|
|
|
printf("bar: %s\n", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void baz(const wxString& s)
|
|
|
|
{
|
2007-06-27 16:49:19 -04:00
|
|
|
printf("baz: %s\n", (const char*)s.c_str());
|
2005-03-10 21:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void foo(int n)
|
|
|
|
{
|
|
|
|
if ( n % 2 )
|
2009-02-05 12:42:50 -05:00
|
|
|
baz("odd");
|
2005-03-10 21:55:21 -05:00
|
|
|
else
|
2009-02-05 12:42:50 -05:00
|
|
|
bar("even");
|
2005-03-10 21:55:21 -05:00
|
|
|
}
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main window
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
DebugRpt_Quit = wxID_EXIT,
|
|
|
|
DebugRpt_Crash = 100,
|
|
|
|
DebugRpt_Current,
|
2005-07-15 18:54:51 -04:00
|
|
|
DebugRpt_Paint,
|
2005-07-15 17:05:48 -04:00
|
|
|
DebugRpt_Upload,
|
|
|
|
DebugRpt_About = wxID_ABOUT
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnReportForCrash(wxCommandEvent& event);
|
|
|
|
void OnReportForCurrent(wxCommandEvent& event);
|
2005-07-15 18:54:51 -04:00
|
|
|
void OnReportPaint(wxCommandEvent& event);
|
2005-07-15 17:05:48 -04:00
|
|
|
void OnReportUpload(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
2005-07-15 18:54:51 -04:00
|
|
|
void OnPaint(wxPaintEvent& event);
|
|
|
|
|
|
|
|
|
|
|
|
// number of lines drawn in OnPaint()
|
|
|
|
int m_numLines;
|
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(MyFrame);
|
2005-07-15 17:05:48 -04:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
2005-03-10 21:55:21 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this is a usual application class modified to work with debug reporter
|
|
|
|
//
|
|
|
|
// basically just 2 things are necessary: call wxHandleFatalExceptions() as
|
|
|
|
// early as possible and override OnFatalException() to create the report there
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2005-07-15 17:05:48 -04:00
|
|
|
// call wxHandleFatalExceptions here
|
|
|
|
MyApp();
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// create our main window here
|
|
|
|
virtual bool OnInit();
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// called when a crash occurs in this application
|
|
|
|
virtual void OnFatalException();
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// this is where we really generate the debug report
|
|
|
|
void GenerateReport(wxDebugReport::Context ctx);
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// if this function is called, we'll use MyDebugReport which would try to
|
|
|
|
// upload the (next) generated debug report to its URL, otherwise we just
|
|
|
|
// generate the debug report and leave it in a local file
|
|
|
|
void UploadReport(bool doIt) { m_uploadReport = doIt; }
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
private:
|
|
|
|
bool m_uploadReport;
|
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(MyApp);
|
2005-07-15 17:05:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyFrame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU(DebugRpt_Quit, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(DebugRpt_Crash, MyFrame::OnReportForCrash)
|
|
|
|
EVT_MENU(DebugRpt_Current, MyFrame::OnReportForCurrent)
|
2005-07-15 18:54:51 -04:00
|
|
|
EVT_MENU(DebugRpt_Paint, MyFrame::OnReportPaint)
|
2005-07-15 17:05:48 -04:00
|
|
|
EVT_MENU(DebugRpt_Upload, MyFrame::OnReportUpload)
|
|
|
|
EVT_MENU(DebugRpt_About, MyFrame::OnAbout)
|
2005-07-15 18:54:51 -04:00
|
|
|
|
|
|
|
EVT_PAINT(MyFrame::OnPaint)
|
2005-07-15 17:05:48 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
MyFrame::MyFrame()
|
2009-07-23 16:30:22 -04:00
|
|
|
: wxFrame(NULL, wxID_ANY, wxT("wxWidgets Debug Report Sample"),
|
2009-02-07 17:30:56 -05:00
|
|
|
wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE|wxDEFAULT_FRAME_STYLE)
|
2005-07-15 17:05:48 -04:00
|
|
|
{
|
2005-07-15 18:54:51 -04:00
|
|
|
m_numLines = 10;
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
2009-07-23 16:30:22 -04:00
|
|
|
menuFile->Append(DebugRpt_Quit, wxT("E&xit\tAlt-X"));
|
2005-07-15 17:05:48 -04:00
|
|
|
|
|
|
|
wxMenu *menuReport = new wxMenu;
|
2009-07-23 16:30:22 -04:00
|
|
|
menuReport->Append(DebugRpt_Crash, wxT("Report for &crash\tCtrl-C"),
|
|
|
|
wxT("Provoke a crash inside the program and create report for it"));
|
|
|
|
menuReport->Append(DebugRpt_Current, wxT("Report for c&urrent context\tCtrl-U"),
|
|
|
|
wxT("Create report for the current program context"));
|
|
|
|
menuReport->Append(DebugRpt_Paint, wxT("Report for &paint handler\tCtrl-P"),
|
|
|
|
wxT("Provoke a repeatable crash in wxEVT_PAINT handler"));
|
2005-07-15 17:05:48 -04:00
|
|
|
menuReport->AppendSeparator();
|
2009-07-23 16:30:22 -04:00
|
|
|
menuReport->AppendCheckItem(DebugRpt_Upload, wxT("Up&load debug report"),
|
|
|
|
wxT("You need to configure a web server accepting debug report uploads to use this function"));
|
2005-07-15 17:05:48 -04:00
|
|
|
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
2009-07-23 16:30:22 -04:00
|
|
|
menuHelp->Append(DebugRpt_About, wxT("&About...\tF1"));
|
2005-07-15 17:05:48 -04:00
|
|
|
|
|
|
|
wxMenuBar *mbar = new wxMenuBar();
|
2009-07-23 16:30:22 -04:00
|
|
|
mbar->Append(menuFile, wxT("&File"));
|
|
|
|
mbar->Append(menuReport, wxT("&Report"));
|
|
|
|
mbar->Append(menuHelp, wxT("&Help"));
|
2005-07-15 17:05:48 -04:00
|
|
|
SetMenuBar(mbar);
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
CreateStatusBar();
|
|
|
|
|
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
Close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnReportForCrash(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// this call is going to crash
|
|
|
|
foo(32);
|
|
|
|
foo(17);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnReportForCurrent(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// example of manually generated report, this could be also
|
|
|
|
// used in wxApp::OnAssert()
|
|
|
|
wxGetApp().GenerateReport(wxDebugReport::Context_Current);
|
|
|
|
}
|
|
|
|
|
2005-07-15 18:54:51 -04:00
|
|
|
void MyFrame::OnReportPaint(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// this will result in a crash in OnPaint()
|
|
|
|
m_numLines = 0;
|
|
|
|
|
|
|
|
// ensure it's called immediately
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
void MyFrame::OnReportUpload(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
wxGetApp().UploadReport(event.IsChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxMessageBox
|
|
|
|
(
|
2009-07-23 16:30:22 -04:00
|
|
|
wxT("wxDebugReport sample\n(c) 2005 Vadim Zeitlin <vadim@wxwindows.org>"),
|
|
|
|
wxT("wxWidgets Debug Report Sample"),
|
2005-07-15 17:05:48 -04:00
|
|
|
wxOK | wxICON_INFORMATION,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-07-15 18:54:51 -04:00
|
|
|
void MyFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
const wxSize size = GetClientSize();
|
|
|
|
for ( wxCoord x = 0; x < size.x; x += size.x/m_numLines )
|
|
|
|
dc.DrawLine(x, 0, x, size.y);
|
|
|
|
}
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
MyApp::MyApp()
|
|
|
|
{
|
|
|
|
// user needs to explicitely enable this
|
|
|
|
m_uploadReport = false;
|
|
|
|
|
|
|
|
// call this to tell the library to call our OnFatalException()
|
|
|
|
wxHandleFatalExceptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
new MyFrame;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyApp::OnFatalException()
|
|
|
|
{
|
|
|
|
GenerateReport(wxDebugReport::Context_Exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyApp::GenerateReport(wxDebugReport::Context ctx)
|
|
|
|
{
|
|
|
|
wxDebugReportCompress *report = m_uploadReport ? new MyDebugReport
|
|
|
|
: new wxDebugReportCompress;
|
|
|
|
|
|
|
|
// add all standard files: currently this means just a minidump and an
|
|
|
|
// XML file with system info and stack trace
|
|
|
|
report->AddAll(ctx);
|
|
|
|
|
|
|
|
// you can also call report->AddFile(...) with your own log files, files
|
|
|
|
// created using wxRegKey::Export() and so on, here we just add a test
|
|
|
|
// file containing the date of the crash
|
2009-07-23 16:30:22 -04:00
|
|
|
wxFileName fn(report->GetDirectory(), wxT("timestamp.my"));
|
|
|
|
wxFFile file(fn.GetFullPath(), wxT("w"));
|
2005-07-15 17:05:48 -04:00
|
|
|
if ( file.IsOpened() )
|
2005-03-10 21:55:21 -05:00
|
|
|
{
|
2005-07-15 17:05:48 -04:00
|
|
|
wxDateTime dt = wxDateTime::Now();
|
2009-07-23 16:30:22 -04:00
|
|
|
file.Write(dt.FormatISODate() + wxT(' ') + dt.FormatISOTime());
|
2005-07-15 17:05:48 -04:00
|
|
|
file.Close();
|
|
|
|
}
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
report->AddFile(fn.GetFullName(), wxT("timestamp of this report"));
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// can also add an existing file directly, it will be copied
|
|
|
|
// automatically
|
2005-04-10 18:37:43 -04:00
|
|
|
#ifdef __WXMSW__
|
2009-07-23 16:30:22 -04:00
|
|
|
report->AddFile(wxT("c:\\autoexec.bat"), wxT("DOS startup file"));
|
2005-04-10 18:37:43 -04:00
|
|
|
#else
|
2009-07-23 16:30:22 -04:00
|
|
|
report->AddFile(wxT("/etc/motd"), wxT("Message of the day"));
|
2005-04-10 18:37:43 -04:00
|
|
|
#endif
|
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
// calling Show() is not mandatory, but is more polite
|
|
|
|
if ( wxDebugReportPreviewStd().Show(*report) )
|
|
|
|
{
|
|
|
|
if ( report->Process() )
|
2005-03-10 21:55:21 -05:00
|
|
|
{
|
2005-07-15 17:05:48 -04:00
|
|
|
if ( m_uploadReport )
|
2005-03-10 21:55:21 -05:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("Report successfully uploaded."));
|
2005-07-15 17:05:48 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxLogMessage(wxT("Report generated in \"%s\"."),
|
2005-07-15 17:05:48 -04:00
|
|
|
report->GetCompressedFileName().c_str());
|
|
|
|
report->Reset();
|
2005-03-10 21:55:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-15 17:05:48 -04:00
|
|
|
//else: user cancelled the report
|
2005-03-10 21:55:21 -05:00
|
|
|
|
2005-07-15 17:05:48 -04:00
|
|
|
delete report;
|
|
|
|
}
|
2005-03-10 21:55:21 -05:00
|
|
|
|