]>
git.saurik.com Git - wxWidgets.git/blob - samples/debugrpt/debugrpt.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: minimal sample showing wxDebugReport and related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
20 #include "wx/msgdlg.h"
21 #include "wx/button.h"
22 #include "wx/dcclient.h"
24 #include "wx/datetime.h"
26 #include "wx/filename.h"
27 #include "wx/debugrpt.h"
29 #if !wxUSE_DEBUGREPORT
30 #error "This sample can't be built without wxUSE_DEBUGREPORT"
31 #endif // wxUSE_DEBUGREPORT
33 #if !wxUSE_ON_FATAL_EXCEPTION
34 #error "This sample can't be built without wxUSE_ON_FATAL_EXCEPTION"
35 #endif // wxUSE_ON_FATAL_EXCEPTION
37 #if !defined(__WXMSW__) && !defined(__WXPM__)
38 #include "../sample.xpm"
41 // ----------------------------------------------------------------------------
42 // custom debug reporting class
43 // ----------------------------------------------------------------------------
45 // this is your custom debug reporter: it will use curl program (which should
46 // be available) to upload the crash report to the given URL (which should be
48 class MyDebugReport
: public wxDebugReportUpload
51 MyDebugReport() : wxDebugReportUpload
53 _T("http://your.url.here/"),
61 // this is called with the contents of the server response: you will
62 // probably want to parse the XML document in OnServerReply() instead of
63 // just dumping it as I do
64 virtual bool OnServerReply(const wxArrayString
& reply
)
66 if ( reply
.IsEmpty() )
68 wxLogError(_T("Didn't receive the expected server reply."));
72 wxString
s(_T("Server replied:\n"));
74 const size_t count
= reply
.GetCount();
75 for ( size_t n
= 0; n
< count
; n
++ )
77 s
<< _T('\t') << reply
[n
] << _T('\n');
80 wxLogMessage(_T("%s"), s
.c_str());
86 // another possibility would be to build email library from contrib and use
87 // this class, after uncommenting it:
90 #include "wx/net/email.h"
92 class MyDebugReport
: public wxDebugReportCompress
95 virtual bool DoProcess()
97 if ( !wxDebugReportCompress::DoProcess() )
99 wxMailMessage
msg(GetReportName() + _T(" crash report"),
100 _T("vadim@wxwindows.org"),
101 wxEmptyString
, // mail body
102 wxEmptyString
, // from address
103 GetCompressedFileName(),
104 _T("crashreport.zip"));
106 return wxEmail::Send(msg
);
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // just some functions to get a slightly deeper stack trace
117 static void bar(const wxChar
*p
)
122 printf("bar: %s\n", p
);
125 void baz(const wxString
& s
)
127 printf("baz: %s\n", s
.c_str());
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
144 DebugRpt_Quit
= wxID_EXIT
,
145 DebugRpt_Crash
= 100,
149 DebugRpt_About
= wxID_ABOUT
152 class MyFrame
: public wxFrame
158 void OnQuit(wxCommandEvent
& event
);
159 void OnReportForCrash(wxCommandEvent
& event
);
160 void OnReportForCurrent(wxCommandEvent
& event
);
161 void OnReportPaint(wxCommandEvent
& event
);
162 void OnReportUpload(wxCommandEvent
& event
);
163 void OnAbout(wxCommandEvent
& event
);
165 void OnPaint(wxPaintEvent
& event
);
168 // number of lines drawn in OnPaint()
171 DECLARE_NO_COPY_CLASS(MyFrame
)
172 DECLARE_EVENT_TABLE()
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 // this is a usual application class modified to work with debug reporter
181 // basically just 2 things are necessary: call wxHandleFatalExceptions() as
182 // early as possible and override OnFatalException() to create the report there
183 class MyApp
: public wxApp
186 // call wxHandleFatalExceptions here
189 // create our main window here
190 virtual bool OnInit();
192 // called when a crash occurs in this application
193 virtual void OnFatalException();
195 // this is where we really generate the debug report
196 void GenerateReport(wxDebugReport::Context ctx
);
198 // if this function is called, we'll use MyDebugReport which would try to
199 // upload the (next) generated debug report to its URL, otherwise we just
200 // generate the debug report and leave it in a local file
201 void UploadReport(bool doIt
) { m_uploadReport
= doIt
; }
206 DECLARE_NO_COPY_CLASS(MyApp
)
211 // ============================================================================
213 // ============================================================================
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
220 EVT_MENU(DebugRpt_Quit
, MyFrame::OnQuit
)
221 EVT_MENU(DebugRpt_Crash
, MyFrame::OnReportForCrash
)
222 EVT_MENU(DebugRpt_Current
, MyFrame::OnReportForCurrent
)
223 EVT_MENU(DebugRpt_Paint
, MyFrame::OnReportPaint
)
224 EVT_MENU(DebugRpt_Upload
, MyFrame::OnReportUpload
)
225 EVT_MENU(DebugRpt_About
, MyFrame::OnAbout
)
227 EVT_PAINT(MyFrame::OnPaint
)
231 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets Debug Report Sample"))
235 SetIcon(wxICON(sample
));
237 wxMenu
*menuFile
= new wxMenu
;
238 menuFile
->Append(DebugRpt_Quit
, _T("E&xit\tAlt-X"));
240 wxMenu
*menuReport
= new wxMenu
;
241 menuReport
->Append(DebugRpt_Crash
, _T("Report for &crash\tCtrl-C"),
242 _T("Provoke a crash inside the program and create report for it"));
243 menuReport
->Append(DebugRpt_Current
, _T("Report for c&urrent context\tCtrl-U"),
244 _T("Create report for the current program context"));
245 menuReport
->Append(DebugRpt_Paint
, _T("Report for &paint handler\tCtrl-P"),
246 _T("Provoke a repeatable crash in wxEVT_PAINT handler"));
247 menuReport
->AppendSeparator();
248 menuReport
->AppendCheckItem(DebugRpt_Upload
, _T("Up&load debug report"),
249 _T("You need to configure a web server accepting debug report uploads to use this function"));
251 wxMenu
*menuHelp
= new wxMenu
;
252 menuHelp
->Append(DebugRpt_About
, _T("&About...\tF1"));
254 wxMenuBar
*mbar
= new wxMenuBar();
255 mbar
->Append(menuFile
, _T("&File"));
256 mbar
->Append(menuReport
, _T("&Report"));
257 mbar
->Append(menuHelp
, _T("&Help"));
265 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
270 void MyFrame::OnReportForCrash(wxCommandEvent
& WXUNUSED(event
))
272 // this call is going to crash
277 void MyFrame::OnReportForCurrent(wxCommandEvent
& WXUNUSED(event
))
279 // example of manually generated report, this could be also
280 // used in wxApp::OnAssert()
281 wxGetApp().GenerateReport(wxDebugReport::Context_Current
);
284 void MyFrame::OnReportPaint(wxCommandEvent
& WXUNUSED(event
))
286 // this will result in a crash in OnPaint()
289 // ensure it's called immediately
293 void MyFrame::OnReportUpload(wxCommandEvent
& event
)
295 wxGetApp().UploadReport(event
.IsChecked());
298 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
302 _T("wxDebugReport sample\n(c) 2005 Vadim Zeitlin <vadim@wxwindows.org>"),
303 _T("wxWidgets Debug Report Sample"),
304 wxOK
| wxICON_INFORMATION
,
309 void MyFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
312 const wxSize size
= GetClientSize();
313 for ( wxCoord x
= 0; x
< size
.x
; x
+= size
.x
/m_numLines
)
314 dc
.DrawLine(x
, 0, x
, size
.y
);
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
323 // user needs to explicitely enable this
324 m_uploadReport
= false;
326 // call this to tell the library to call our OnFatalException()
327 wxHandleFatalExceptions();
332 if ( !wxApp::OnInit() )
340 void MyApp::OnFatalException()
342 GenerateReport(wxDebugReport::Context_Exception
);
345 void MyApp::GenerateReport(wxDebugReport::Context ctx
)
347 wxDebugReportCompress
*report
= m_uploadReport
? new MyDebugReport
348 : new wxDebugReportCompress
;
350 // add all standard files: currently this means just a minidump and an
351 // XML file with system info and stack trace
354 // you can also call report->AddFile(...) with your own log files, files
355 // created using wxRegKey::Export() and so on, here we just add a test
356 // file containing the date of the crash
357 wxFileName
fn(report
->GetDirectory(), _T("timestamp.my"));
358 wxFFile
file(fn
.GetFullPath(), _T("w"));
359 if ( file
.IsOpened() )
361 wxDateTime dt
= wxDateTime::Now();
362 file
.Write(dt
.FormatISODate() + _T(' ') + dt
.FormatISOTime());
366 report
->AddFile(fn
.GetFullName(), _T("timestamp of this report"));
368 // can also add an existing file directly, it will be copied
371 report
->AddFile(_T("c:\\autoexec.bat"), _T("DOS startup file"));
373 report
->AddFile(_T("/etc/motd"), _T("Message of the day"));
376 // calling Show() is not mandatory, but is more polite
377 if ( wxDebugReportPreviewStd().Show(*report
) )
379 if ( report
->Process() )
381 if ( m_uploadReport
)
383 wxLogMessage(_T("Report successfully uploaded."));
387 wxLogMessage(_T("Report generated in \"%s\"."),
388 report
->GetCompressedFileName().c_str());
393 //else: user cancelled the report