]>
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 // ----------------------------------------------------------------------------
31 // custom debug reporting class
32 // ----------------------------------------------------------------------------
34 // this is your custom debug reporter, you will probably want to parse the XML
35 // document in OnServerReply() instead of just dumping it as I do
36 class MyDebugReport
: public wxDebugReportUpload
39 MyDebugReport() : wxDebugReportUpload
41 //_T("http://iml2.hitchcock.org/intranet/crashes/wxtest"),
42 _T("http://your.url.here/"),
50 virtual bool OnServerReply(const wxArrayString
& reply
)
52 if ( reply
.IsEmpty() )
54 wxLogError(_T("Didn't receive the expected server reply."));
58 wxString
s(_T("Server replied:\n"));
60 const size_t count
= reply
.GetCount();
61 for ( size_t n
= 0; n
< count
; n
++ )
63 s
<< _T('\t') << reply
[n
] << _T('\n');
66 wxLogMessage(_T("%s"), s
.c_str());
72 // another possibility would be to build email library from contrib and use
73 // this class, after uncommenting it:
76 #include "wx/net/email.h"
78 class MyDebugReport
: public wxDebugReportCompress
81 virtual bool DoProcess()
83 if ( !wxDebugReportCompress::DoProcess() )
85 wxMailMessage
msg(GetReportName() + _T(" crash report"),
86 _T("vadim@wxwindows.org"),
87 wxEmptyString
, // mail body
88 wxEmptyString
, // from address
89 GetCompressedFileName(),
90 _T("crashreport.zip"));
92 return wxEmail::Send(msg
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // just some functions to get a slightly deeper stack trace
103 static void bar(const wxChar
*p
)
108 printf("bar: %s\n", p
);
111 void baz(const wxString
& s
)
113 printf("baz: %s\n", s
.c_str());
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // this is a usual application class modified to work with debug reporter
130 // basically just 2 things are necessary: call wxHandleFatalExceptions() as
131 // early as possible and override OnFatalException() to create the report there
132 class MyApp
: public wxApp
135 virtual bool OnInit()
137 wxHandleFatalExceptions();
139 if ( !wxApp::OnInit() )
147 // a real program would be presumably be a bit harder to crash than
148 // just pressing "yes" in a dialog... but this is just an example
149 switch ( wxMessageBox
151 _T("Generate report for crash (or just current context)?"),
152 _T("wxDebugReport Test"),
157 // this call is going to crash
163 // example of manually generated report, this could be also
164 // used in wxApp::OnAssert()
165 GenerateReport(wxDebugReport::Context_Current
);
175 virtual void OnFatalException()
177 GenerateReport(wxDebugReport::Context_Exception
);
180 void GenerateReport(wxDebugReport::Context ctx
)
182 MyDebugReport report
;
184 // add all standard files: currently this means just a minidump and an
185 // XML file with system info and stack trace
188 // you can also call report.AddFile(...) with your own log files, files
189 // created using wxRegKey::Export() and so on, here we just add a test
190 // file containing the date of the crash
191 wxFileName
fn(report
.GetDirectory(), _T("timestamp.my"));
192 wxFFile
file(fn
.GetFullPath(), _T("w"));
193 if ( file
.IsOpened() )
195 wxDateTime dt
= wxDateTime::Now();
196 file
.Write(dt
.FormatISODate() + _T(' ') + dt
.FormatISOTime());
200 report
.AddFile(fn
.GetFullName(), _T("timestamp of this report"));
202 // calling Show() is not mandatory, but is more polite
203 if ( wxDebugReportPreviewStd().Show(report
) )
205 if ( report
.Process() )
207 // report successfully uploaded
210 //else: user cancelled the report