wxWidgets/samples/debugrpt/debugrpt.cpp
Vadim Zeitlin 985604c166 don't use private URL in the sample
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32758 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2005-03-11 15:44:37 +00:00

188 lines
5.2 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// 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"
#include "wx/datetime.h"
#include "wx/ffile.h"
#include "wx/filename.h"
#include "wx/dynlib.h"
#include "wx/debugrpt.h"
#include "wx/msgdlg.h"
#if !wxUSE_DEBUGREPORT
#error "This sample can't be built without wxUSE_DEBUGREPORT"
#endif // wxUSE_DEBUGREPORT
// ----------------------------------------------------------------------------
// custom debug reporting class
// ----------------------------------------------------------------------------
// this is your custom debug reporter, you will probably want to parse the XML
// document in OnServerReply() instead of just dumping it as I do
class MyDebugReport : public wxDebugReportUpload
{
public:
MyDebugReport() : wxDebugReportUpload
(
_T("http://your.url.here/"),
_T("report:file"),
_T("action")
)
{
}
protected:
virtual bool OnServerReply(const wxArrayString& reply)
{
if ( reply.IsEmpty() )
{
wxLogError(_T("Didn't receive the expected server reply."));
return false;
}
wxString s(_T("Server replied:\n"));
const size_t count = reply.GetCount();
for ( size_t n = 0; n < count; n++ )
{
s << _T('\t') << reply[n] << _T('\n');
}
wxLogMessage(_T("%s"), s.c_str());
return true;
}
};
// ----------------------------------------------------------------------------
// helper functions
// ----------------------------------------------------------------------------
// just some functions to get a slightly deeper stack trace
static void bar(const wxChar *p)
{
char *pc = 0;
*pc = *p;
printf("bar: %s\n", p);
}
void baz(const wxString& s)
{
printf("baz: %s\n", s.c_str());
}
void foo(int n)
{
if ( n % 2 )
baz(wxT("odd"));
else
bar(wxT("even"));
}
// ----------------------------------------------------------------------------
// 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:
virtual bool OnInit()
{
wxHandleFatalExceptions();
if ( !wxApp::OnInit() )
return false;
return true;
}
virtual int OnRun()
{
// a real program would be presumably be a bit harder to crash than
// just pressing "yes" in a dialog... but this is just an example
switch ( wxMessageBox
(
_T("Generate report for crash (or just current context)?"),
_T("wxDebugReport Test"),
wxYES_NO | wxCANCEL
) )
{
case wxYES:
// this call is going to crash
foo(32);
foo(17);
break;
case wxNO:
// example of manually generated report, this could be also
// used in wxApp::OnAssert()
GenerateReport(wxDebugReport::Context_Curent);
break;
case wxCANCEL:
break;
}
return 0;
}
virtual void OnFatalException()
{
GenerateReport(wxDebugReport::Context_Exception);
}
void GenerateReport(wxDebugReport::Context ctx)
{
MyDebugReport report;
// 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
wxFileName fn(report.GetDirectory(), _T("timestamp.my"));
wxFFile file(fn.GetFullPath(), _T("w"));
if ( file.IsOpened() )
{
wxDateTime dt = wxDateTime::Now();
file.Write(dt.FormatISODate() + _T(' ') + dt.FormatISOTime());
file.Close();
}
report.AddFile(fn.GetFullName(), _T("timestamp of this report"));
// calling Show() is not mandatory, but is more polite
if ( wxDebugReportPreviewStd().Show(report) )
{
if ( report.Process() )
{
// report successfully uploaded
}
}
//else: user cancelled the report
}
};
IMPLEMENT_APP(MyApp)