]>
git.saurik.com Git - wxWidgets.git/blob - samples/debugrpt/debugrpt.cpp
1cec0dfa13bba665c89e0052fc9d8aa84b45ebb6
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 // ----------------------------------------------------------------------------
17 #include "wx/datetime.h"
19 #include "wx/filename.h"
20 #include "wx/dynlib.h"
21 #include "wx/debugrpt.h"
23 #include "wx/msgdlg.h"
25 #if !wxUSE_DEBUGREPORT
26 #error "This sample can't be built without wxUSE_DEBUGREPORT"
27 #endif // wxUSE_DEBUGREPORT
29 // ----------------------------------------------------------------------------
30 // custom debug reporting class
31 // ----------------------------------------------------------------------------
33 // this is your custom debug reporter, you will probably want to parse the XML
34 // document in OnServerReply() instead of just dumping it as I do
35 class MyDebugReport
: public wxDebugReportUpload
38 MyDebugReport() : wxDebugReportUpload
40 _T("http://iml2.hitchcock.org/intranet/crashes/wxtest"),
48 virtual bool OnServerReply(const wxArrayString
& reply
)
50 if ( reply
.IsEmpty() )
52 wxLogError(_T("Didn't receive the expected server reply."));
56 wxString
s(_T("Server replied:\n"));
58 const size_t count
= reply
.GetCount();
59 for ( size_t n
= 0; n
< count
; n
++ )
61 s
<< _T('\t') << reply
[n
] << _T('\n');
64 wxLogMessage(_T("%s"), s
.c_str());
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // just some functions to get a slightly deeper stack trace
75 static void bar(const wxChar
*p
)
80 printf("bar: %s\n", p
);
83 void baz(const wxString
& s
)
85 printf("baz: %s\n", s
.c_str());
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // this is a usual application class modified to work with debug reporter
102 // basically just 2 things are necessary: call wxHandleFatalExceptions() as
103 // early as possible and override OnFatalException() to create the report there
104 class MyApp
: public wxApp
107 virtual bool OnInit()
109 wxHandleFatalExceptions();
111 if ( !wxApp::OnInit() )
119 // a real program would be presumably be a bit harder to crash than
120 // just pressing "yes" in a dialog... but this is just an example
121 switch ( wxMessageBox
123 _T("Generate report for crash (or just current context)?"),
124 _T("wxDebugReport Test"),
129 // this call is going to crash
135 // example of manually generated report, this could be also
136 // used in wxApp::OnAssert()
137 GenerateReport(wxDebugReport::Context_Curent
);
147 virtual void OnFatalException()
149 GenerateReport(wxDebugReport::Context_Exception
);
152 void GenerateReport(wxDebugReport::Context ctx
)
154 MyDebugReport report
;
156 // add all standard files: currently this means just a minidump and an
157 // XML file with system info and stack trace
160 // you can also call report.AddFile(...) with your own log files, files
161 // created using wxRegKey::Export() and so on, here we just add a test
162 // file containing the date of the crash
163 wxFileName
fn(report
.GetDirectory(), _T("timestamp.my"));
164 wxFFile
file(fn
.GetFullPath(), _T("w"));
165 if ( file
.IsOpened() )
167 wxDateTime dt
= wxDateTime::Now();
168 file
.Write(dt
.FormatISODate() + _T(' ') + dt
.FormatISOTime());
172 report
.AddFile(fn
.GetFullName(), _T("timestamp of this report"));
174 // calling Show() is not mandatory, but is more polite
175 if ( wxDebugReportPreviewStd().Show(report
) )
177 if ( report
.Process() )
179 // report successfully uploaded
182 //else: user cancelled the report