]>
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 // ----------------------------------------------------------------------------
18 #include "wx/datetime.h"
20 #include "wx/filename.h"
21 #include "wx/dynlib.h"
22 #include "wx/debugrpt.h"
24 #include "wx/msgdlg.h"
26 #if !wxUSE_DEBUGREPORT
27 #error "This sample can't be built without wxUSE_DEBUGREPORT"
28 #endif // wxUSE_DEBUGREPORT
30 #if !wxUSE_ON_FATAL_EXCEPTION
31 #error "This sample can't be built without wxUSE_ON_FATAL_EXCEPTION"
32 #endif // wxUSE_ON_FATAL_EXCEPTION
34 // ----------------------------------------------------------------------------
35 // custom debug reporting class
36 // ----------------------------------------------------------------------------
38 // this is your custom debug reporter: it will use curl program (which should
39 // be available) to upload the crash report to the given URL (which should be
41 class MyDebugReport
: public wxDebugReportUpload
44 MyDebugReport() : wxDebugReportUpload
46 _T("http://your.url.here/"),
54 // this is called with the contents of the server response: you will
55 // probably want to parse the XML document in OnServerReply() instead of
56 // just dumping it as I do
57 virtual bool OnServerReply(const wxArrayString
& reply
)
59 if ( reply
.IsEmpty() )
61 wxLogError(_T("Didn't receive the expected server reply."));
65 wxString
s(_T("Server replied:\n"));
67 const size_t count
= reply
.GetCount();
68 for ( size_t n
= 0; n
< count
; n
++ )
70 s
<< _T('\t') << reply
[n
] << _T('\n');
73 wxLogMessage(_T("%s"), s
.c_str());
79 // another possibility would be to build email library from contrib and use
80 // this class, after uncommenting it:
83 #include "wx/net/email.h"
85 class MyDebugReport
: public wxDebugReportCompress
88 virtual bool DoProcess()
90 if ( !wxDebugReportCompress::DoProcess() )
92 wxMailMessage
msg(GetReportName() + _T(" crash report"),
93 _T("vadim@wxwindows.org"),
94 wxEmptyString
, // mail body
95 wxEmptyString
, // from address
96 GetCompressedFileName(),
97 _T("crashreport.zip"));
99 return wxEmail::Send(msg
);
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // just some functions to get a slightly deeper stack trace
110 static void bar(const wxChar
*p
)
115 printf("bar: %s\n", p
);
118 void baz(const wxString
& s
)
120 printf("baz: %s\n", s
.c_str());
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 // this is a usual application class modified to work with debug reporter
137 // basically just 2 things are necessary: call wxHandleFatalExceptions() as
138 // early as possible and override OnFatalException() to create the report there
139 class MyApp
: public wxApp
142 virtual bool OnInit()
144 wxHandleFatalExceptions();
146 if ( !wxApp::OnInit() )
154 // a real program would be presumably be a bit harder to crash than
155 // just pressing "yes" in a dialog... but this is just an example
156 switch ( wxMessageBox
158 _T("Generate report for crash (or just current context)?"),
159 _T("wxDebugReport Test"),
164 // this call is going to crash
170 // example of manually generated report, this could be also
171 // used in wxApp::OnAssert()
172 GenerateReport(wxDebugReport::Context_Current
);
182 virtual void OnFatalException()
184 GenerateReport(wxDebugReport::Context_Exception
);
187 void GenerateReport(wxDebugReport::Context ctx
)
189 MyDebugReport report
;
191 // add all standard files: currently this means just a minidump and an
192 // XML file with system info and stack trace
195 // you can also call report.AddFile(...) with your own log files, files
196 // created using wxRegKey::Export() and so on, here we just add a test
197 // file containing the date of the crash
198 wxFileName
fn(report
.GetDirectory(), _T("timestamp.my"));
199 wxFFile
file(fn
.GetFullPath(), _T("w"));
200 if ( file
.IsOpened() )
202 wxDateTime dt
= wxDateTime::Now();
203 file
.Write(dt
.FormatISODate() + _T(' ') + dt
.FormatISOTime());
207 report
.AddFile(fn
.GetFullName(), _T("timestamp of this report"));
209 // can also add an existing file directly, it will be copied
212 report
.AddFile(_T("c:\\autoexec.bat"), _T("DOS startup file"));
214 report
.AddFile(_T("/etc/motd"), _T("Message of the day"));
217 // calling Show() is not mandatory, but is more polite
218 if ( wxDebugReportPreviewStd().Show(report
) )
220 if ( report
.Process() )
222 // report successfully uploaded
225 //else: user cancelled the report