added wxDebugReport

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32744 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2005-03-11 02:55:21 +00:00
parent 1de1196ab3
commit ce4fd7b577
15 changed files with 1816 additions and 2 deletions

639
src/common/debugrpt.cpp Normal file
View File

@@ -0,0 +1,639 @@
///////////////////////////////////////////////////////////////////////////////
// Name: src/common/debugrpt.cpp
// Purpose: wxDebugReport and related classes implementation
// Author: Vadim Zeitlin
// Modified by:
// Created: 2005-01-17
// RCS-ID: $Id$
// Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/log.h"
#include "wx/intl.h"
#endif // WX_PRECOMP
#if wxUSE_DEBUGREPORT
#include "wx/debugrpt.h"
#include "wx/filename.h"
#include "wx/dir.h"
#include "wx/dynlib.h"
#include "wx/xml/xml.h"
#if wxUSE_STACKWALKER
#include "wx/stackwalk.h"
#endif
#if wxUSE_CRASHREPORT
#include "wx/msw/crashrpt.h"
#endif
#if wxUSE_ZIPSTREAM
#include "wx/wfstream.h"
#include "wx/zipstrm.h"
#endif // wxUSE_ZIPSTREAM
#if wxUSE_STACKWALKER
// ----------------------------------------------------------------------------
// XmlStackWalker: stack walker specialization which dumps stack in XML
// ----------------------------------------------------------------------------
class XmlStackWalker : public wxStackWalker
{
public:
XmlStackWalker(wxXmlNode *nodeStack)
{
m_isOk = false;
m_nodeStack = nodeStack;
}
bool IsOk() const { return m_isOk; }
protected:
virtual void OnStackFrame(const wxStackFrame& frame);
wxXmlNode *m_nodeStack;
bool m_isOk;
};
#endif // wxUSE_STACKWALKER
// ----------------------------------------------------------------------------
// local functions
// ----------------------------------------------------------------------------
static inline void
HexProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
{
node->AddProperty(name, wxString::Format(_T("%08x"), value));
}
static inline void
NumProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
{
node->AddProperty(name, wxString::Format(_T("%lu"), value));
}
static inline void
TextElement(wxXmlNode *node, const wxChar *name, const wxString& value)
{
wxXmlNode *nodeChild = new wxXmlNode(wxXML_ELEMENT_NODE, name);
node->AddChild(nodeChild);
nodeChild->AddChild(new wxXmlNode(wxXML_TEXT_NODE, _T(""), value));
}
static inline void
HexElement(wxXmlNode *node, const wxChar *name, unsigned long value)
{
TextElement(node, name, wxString::Format(_T("%08x"), value));
}
#if wxUSE_STACKWALKER
// ============================================================================
// XmlStackWalker implementation
// ============================================================================
void XmlStackWalker::OnStackFrame(const wxStackFrame& frame)
{
m_isOk = true;
wxXmlNode *nodeFrame = new wxXmlNode(wxXML_ELEMENT_NODE, _T("frame"));
m_nodeStack->AddChild(nodeFrame);
NumProperty(nodeFrame, _T("level"), frame.GetLevel());
wxString func = frame.GetName();
if ( !func.empty() )
{
nodeFrame->AddProperty(_T("function"), func);
HexProperty(nodeFrame, _T("offset"), frame.GetOffset());
}
if ( frame.HasSourceLocation() )
{
nodeFrame->AddProperty(_T("file"), frame.GetFileName());
NumProperty(nodeFrame, _T("line"), frame.GetLine());
}
const size_t nParams = frame.GetParamCount();
if ( nParams )
{
wxXmlNode *nodeParams = new wxXmlNode(wxXML_ELEMENT_NODE, _T("parameters"));
nodeFrame->AddChild(nodeParams);
for ( size_t n = 0; n < nParams; n++ )
{
wxXmlNode *
nodeParam = new wxXmlNode(wxXML_ELEMENT_NODE, _T("parameter"));
nodeParams->AddChild(nodeParam);
NumProperty(nodeParam, _T("number"), n);
wxString type, name, value;
if ( !frame.GetParam(n, &type, &name, &value) )
continue;
if ( !type.empty() )
TextElement(nodeParam, _T("type"), type);
if ( !name.empty() )
TextElement(nodeParam, _T("name"), name);
if ( !value.empty() )
TextElement(nodeParam, _T("value"), value);
}
}
}
#endif // wxUSE_STACKWALKER
// ============================================================================
// wxDebugReport implementation
// ============================================================================
// ----------------------------------------------------------------------------
// initialization and cleanup
// ----------------------------------------------------------------------------
wxDebugReport::wxDebugReport()
{
// get a temporary directory name
wxString appname(wxTheApp ? wxTheApp->GetAppName() : _T("wx"));
// we can't use CreateTempFileName() because it creates a file, not a
// directory, so do our best to create a unique name ourselves
//
// of course, this doesn't protect us against malicious users...
wxFileName fn;
fn.AssignTempFileName(appname);
m_dir.Printf(_T("%s%c%s_dbgrpt-%lu-%s"),
fn.GetPath(), wxFILE_SEP_PATH, appname.c_str(),
wxGetProcessId(),
wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str());
// as we are going to save the process state there use restrictive
// permissions
if ( !wxMkdir(m_dir, 0700) )
{
wxLogSysError(_("Failed to create directory \"%s\""), m_dir.c_str());
wxLogError(_("Debug report couldn't be created."));
Reset();
}
}
wxDebugReport::~wxDebugReport()
{
if ( !m_dir.empty() )
{
// remove all files in this directory
wxDir dir(m_dir);
wxString file;
for ( bool cont = dir.GetFirst(&file); cont; cont = dir.GetNext(&file) )
{
if ( wxRemove(wxFileName(m_dir, file).GetFullPath()) != 0 )
{
wxLogSysError(_("Failed to remove debug report file \"%s\""),
file.c_str());
m_dir.clear();
break;
}
}
}
if ( !m_dir.empty() )
{
if ( wxRmDir(m_dir) != 0 )
{
wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
m_dir.c_str());
}
}
}
// ----------------------------------------------------------------------------
// various helpers
// ----------------------------------------------------------------------------
wxString wxDebugReport::GetReportName() const
{
return wxString(wxTheApp ? wxTheApp->GetAppName() : _T("wx"));
}
void wxDebugReport::AddFile(const wxString& name, const wxString& description)
{
m_files.Add(name);
m_descriptions.Add(description);
}
void wxDebugReport::RemoveFile(const wxString& name)
{
const int n = m_files.Index(name);
wxCHECK_RET( n != wxNOT_FOUND, _T("No such file in wxDebugReport") );
m_files.RemoveAt(n);
m_descriptions.RemoveAt(n);
wxRemove(wxFileName(GetDirectory(), name).GetFullPath());
}
bool wxDebugReport::GetFile(size_t n, wxString *name, wxString *desc) const
{
if ( n >= m_files.GetCount() )
return false;
if ( name )
*name = m_files[n];
if ( desc )
*desc = m_descriptions[n];
return true;
}
void wxDebugReport::AddAll(Context context)
{
#if wxUSE_STACKWALKER
AddContext(context);
#endif // wxUSE_STACKWALKER
#if wxUSE_CRASHREPORT
AddDump(context);
#endif // wxUSE_CRASHREPORT
}
// ----------------------------------------------------------------------------
// adding basic text information about current context
// ----------------------------------------------------------------------------
#if wxUSE_STACKWALKER
bool wxDebugReport::DoAddSystemInfo(wxXmlNode *nodeSystemInfo)
{
nodeSystemInfo->AddProperty(_T("description"), wxGetOsDescription());
return true;
}
bool wxDebugReport::DoAddLoadedModules(wxXmlNode *nodeModules)
{
wxDynamicLibraryDetailsArray modules(wxDynamicLibrary::ListLoaded());
const size_t count = modules.GetCount();
if ( !count )
return false;
for ( size_t n = 0; n < count; n++ )
{
const wxDynamicLibraryDetails& info = modules[n];
wxXmlNode *nodeModule = new wxXmlNode(wxXML_ELEMENT_NODE, _T("module"));
nodeModules->AddChild(nodeModule);
wxString path = info.GetPath();
if ( path.empty() )
path = info.GetName();
if ( !path.empty() )
nodeModule->AddProperty(_T("path"), path);
void *addr;
size_t len;
if ( info.GetAddress(&addr, &len) )
{
HexProperty(nodeModule, _T("address"), (unsigned long)addr);
HexProperty(nodeModule, _T("size"), len);
}
wxString ver = info.GetVersion();
if ( !ver.empty() )
{
nodeModule->AddProperty(_T("version"), ver);
}
}
return true;
}
bool wxDebugReport::DoAddExceptionInfo(wxXmlNode *nodeContext)
{
#if wxUSE_CRASHREPORT
wxCrashContext c;
if ( !c.code )
return false;
wxXmlNode *nodeExc = new wxXmlNode(wxXML_ELEMENT_NODE, _T("exception"));
nodeContext->AddChild(nodeExc);
HexProperty(nodeExc, _T("code"), c.code);
nodeExc->AddProperty(_T("name"), c.GetExceptionString());
HexProperty(nodeExc, _T("address"), (unsigned long)c.addr);
#ifdef __INTEL__
wxXmlNode *nodeRegs = new wxXmlNode(wxXML_ELEMENT_NODE, _T("registers"));
nodeContext->AddChild(nodeRegs);
HexElement(nodeRegs, _T("eax"), c.regs.eax);
HexElement(nodeRegs, _T("ebx"), c.regs.ebx);
HexElement(nodeRegs, _T("ecx"), c.regs.edx);
HexElement(nodeRegs, _T("edx"), c.regs.edx);
HexElement(nodeRegs, _T("esi"), c.regs.esi);
HexElement(nodeRegs, _T("edi"), c.regs.edi);
HexElement(nodeRegs, _T("ebp"), c.regs.ebp);
HexElement(nodeRegs, _T("esp"), c.regs.esp);
HexElement(nodeRegs, _T("eip"), c.regs.eip);
HexElement(nodeRegs, _T("cs"), c.regs.cs);
HexElement(nodeRegs, _T("ds"), c.regs.ds);
HexElement(nodeRegs, _T("es"), c.regs.es);
HexElement(nodeRegs, _T("fs"), c.regs.fs);
HexElement(nodeRegs, _T("gs"), c.regs.gs);
HexElement(nodeRegs, _T("ss"), c.regs.ss);
HexElement(nodeRegs, _T("flags"), c.regs.flags);
#endif // __INTEL__
return true;
#else // !wxUSE_CRASHREPORT
wxUnusedVar(nodeContext);
return false;
#endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
}
bool wxDebugReport::AddContext(wxDebugReport::Context ctx)
{
wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
// create XML dump of current context
wxXmlDocument xmldoc;
wxXmlNode *nodeRoot = new wxXmlNode(wxXML_ELEMENT_NODE, _T("report"));
xmldoc.SetRoot(nodeRoot);
nodeRoot->AddProperty(_T("version"), _T("1.0"));
nodeRoot->AddProperty(_T("kind"), ctx == Context_Curent ? _T("user")
: _T("exception"));
// add system information
wxXmlNode *nodeSystemInfo = new wxXmlNode(wxXML_ELEMENT_NODE, _T("system"));
if ( DoAddSystemInfo(nodeSystemInfo) )
nodeRoot->AddChild(nodeSystemInfo);
else
delete nodeSystemInfo;
// add information about the loaded modules
wxXmlNode *nodeModules = new wxXmlNode(wxXML_ELEMENT_NODE, _T("modules"));
if ( DoAddLoadedModules(nodeModules) )
nodeRoot->AddChild(nodeModules);
else
delete nodeModules;
// add CPU context information: this only makes sense for exceptions as our
// current context is not very interesting otherwise
if ( ctx == Context_Exception )
{
wxXmlNode *nodeContext = new wxXmlNode(wxXML_ELEMENT_NODE, _T("context"));
if ( DoAddExceptionInfo(nodeContext) )
nodeRoot->AddChild(nodeContext);
else
delete nodeContext;
}
// add stack traceback
#if wxUSE_STACKWALKER
wxXmlNode *nodeStack = new wxXmlNode(wxXML_ELEMENT_NODE, _T("stack"));
XmlStackWalker sw(nodeStack);
if ( ctx == Context_Exception )
{
sw.WalkFromException();
}
else // Context_Curent
{
sw.Walk();
}
if ( sw.IsOk() )
nodeRoot->AddChild(nodeStack);
else
delete nodeStack;
#endif // wxUSE_STACKWALKER
// finally let the user add any extra information he needs
DoAddCustomContext(nodeRoot);
// save the entire context dump in a file
wxFileName fn(m_dir, GetReportName(), _T("xml"));
if ( !xmldoc.Save(fn.GetFullPath()) )
return false;
AddFile(fn.GetFullName(), _("process context description"));
return true;
}
#endif // wxUSE_STACKWALKER
// ----------------------------------------------------------------------------
// adding core dump
// ----------------------------------------------------------------------------
#if wxUSE_CRASHREPORT
bool wxDebugReport::AddDump(Context ctx)
{
wxCHECK_MSG( IsOk(), false, _T("use IsOk() first") );
wxFileName fn(m_dir, GetReportName(), _T("dmp"));
wxCrashReport::SetFileName(fn.GetFullPath());
if ( !(ctx == Context_Exception ? wxCrashReport::Generate()
: wxCrashReport::GenerateNow()) )
return false;
AddFile(fn.GetFullName(), _("dump of the process state (binary)"));
return true;
}
#endif // wxUSE_CRASHREPORT
// ----------------------------------------------------------------------------
// report processing
// ----------------------------------------------------------------------------
bool wxDebugReport::Process()
{
if ( !GetFilesCount() )
{
wxLogError(_("Debug report generation has failed."));
return false;
}
if ( !DoProcess() )
{
wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
GetDirectory().c_str());
Reset();
return false;
}
return true;
}
bool wxDebugReport::DoProcess()
{
wxString msg = _("*** A debug report has been generated\n");
msg += wxString::Format(_("*** It can be found in \"%s\"\n"),
GetDirectory().c_str());
msg += _("*** And includes the following files:\n");
wxString name, desc;
const size_t count = GetFilesCount();
for ( size_t n = 0; n < count; n++ )
{
GetFile(n, &name, &desc);
msg += wxString::Format(_("\t%s: %s\n"), name.c_str(), desc.c_str());
}
msg += _("\nPlease send this report to the program maintainer, thank you!\n");
wxLogMessage(_T("%s"), msg.c_str());
// we have to do this or the report would be deleted, and we don't even
// have any way to ask the user if he wants to keep it from here
Reset();
return true;
}
// ============================================================================
// wxDebugReport-derived classes
// ============================================================================
#if wxUSE_ZIPSTREAM
// ----------------------------------------------------------------------------
// wxDebugReportCompress
// ----------------------------------------------------------------------------
bool wxDebugReportCompress::DoProcess()
{
const size_t count = GetFilesCount();
if ( !count )
return false;
// create the streams
wxFileName fn(GetDirectory(), GetReportName(), _T("zip"));
wxFFileOutputStream os(fn.GetFullPath(), _T("wb"));
wxZipOutputStream zos(os, 9);
// add all files to the ZIP one
wxString name, desc;
for ( size_t n = 0; n < count; n++ )
{
GetFile(n, &name, &desc);
wxZipEntry *ze = new wxZipEntry(name);
ze->SetComment(desc);
if ( !zos.PutNextEntry(ze) )
return false;
wxFFileInputStream is(wxFileName(fn.GetPath(), name).GetFullPath());
if ( !is.IsOk() || !zos.Write(is).IsOk() )
return false;
}
if ( !zos.Close() )
return false;
m_zipfile = fn.GetFullPath();
return true;
}
// ----------------------------------------------------------------------------
// wxDebugReportUpload
// ----------------------------------------------------------------------------
wxDebugReportUpload::wxDebugReportUpload(const wxString& url,
const wxString& input,
const wxString& action,
const wxString& curl)
: m_uploadURL(url),
m_inputField(input),
m_curlCmd(curl)
{
if ( m_uploadURL.Last() != _T('/') )
m_uploadURL += _T('/');
m_uploadURL += action;
}
bool wxDebugReportUpload::DoProcess()
{
if ( !wxDebugReportCompress::DoProcess() )
return false;
wxArrayString output, errors;
int rc = wxExecute(wxString::Format
(
_T("%s -F %s=@%s %s"),
m_curlCmd.c_str(),
m_inputField.c_str(),
GetCompressedFileName().c_str(),
m_uploadURL.c_str()
),
output,
errors);
if ( rc == -1 )
{
wxLogError(_("Failed to execute curl, please install it in PATH."));
}
else if ( rc != 0 )
{
const size_t count = errors.GetCount();
if ( count )
{
for ( size_t n = 0; n < count; n++ )
{
wxLogWarning(_T("%s"), errors[n].c_str());
}
}
wxLogError(_("Failed to upload the debug report (error code %d)."), rc);
}
else // rc == 0
{
if ( OnServerReply(output) )
return true;
}
return false;
}
#endif // wxUSE_ZIPSTREAM
#endif // wxUSE_DEBUGREPORT

488
src/generic/dbgrptg.cpp Normal file
View File

@@ -0,0 +1,488 @@
///////////////////////////////////////////////////////////////////////////////
// Name: src/generic/dbgrptg.cpp
// Purpose: implementation of wxDebugReportPreviewStd
// Author: Vadim Zeitlin, Andrej Putrin
// Modified by:
// Created: 2005-01-21
// RCS-ID: $Id$
// Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// License: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/sizer.h"
#include "wx/checklst.h"
#include "wx/textctrl.h"
#endif // WX_PRECOMP
#if wxUSE_DEBUGREPORT
#include "wx/debugrpt.h"
#include "wx/intl.h"
#include "wx/filename.h"
#include "wx/ffile.h"
#include "wx/mimetype.h"
#include "wx/statline.h"
// ----------------------------------------------------------------------------
// wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
// ----------------------------------------------------------------------------
class wxDumpPreviewDlg : public wxDialog
{
public:
wxDumpPreviewDlg(wxWindow *parent,
const wxString& title,
const wxString& text);
private:
// the text we show
wxTextCtrl *m_text;
DECLARE_NO_COPY_CLASS(wxDumpPreviewDlg)
};
wxDumpPreviewDlg::wxDumpPreviewDlg(wxWindow *parent,
const wxString& title,
const wxString& text)
: wxDialog(parent, wxID_ANY, title,
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
// create controls
// ---------------
// use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
// faster than with wxTE_RICH
m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
wxPoint(0, 0), wxDefaultSize,
wxTE_MULTILINE |
wxTE_READONLY |
wxTE_NOHIDESEL |
wxTE_RICH2);
m_text->SetValue(text);
// use fixed-width font
m_text->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE,
wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));
wxButton *btnClose = new wxButton(this, wxID_CANCEL, _("Close"));
// layout them
// -----------
wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL),
*sizerBtns = new wxBoxSizer(wxHORIZONTAL);
sizerBtns->Add(btnClose, 0, 0, 1);
sizerTop->Add(m_text, 1, wxEXPAND);
sizerTop->Add(sizerBtns, 0, wxALIGN_RIGHT | wxTOP | wxBOTTOM | wxRIGHT, 1);
// set the sizer &c
// ----------------
// make the text window bigger to show more contents of the file
sizerTop->SetItemMinSize(m_text, 600, 300);
SetSizer(sizerTop);
Layout();
Fit();
m_text->SetFocus();
}
// ----------------------------------------------------------------------------
// wxDumpOpenExternalDlg: choose a command for opening the given file
// ----------------------------------------------------------------------------
class wxDumpOpenExternalDlg : public wxDialog
{
public:
wxDumpOpenExternalDlg(wxWindow *parent, const wxFileName& filename);
// return the command chosed by user to open this file
const wxString& GetCommand() const { return m_command; }
wxString m_command;
private:
void OnBrowse(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
DECLARE_NO_COPY_CLASS(wxDumpOpenExternalDlg)
};
BEGIN_EVENT_TABLE(wxDumpOpenExternalDlg, wxDialog)
EVT_BUTTON(wxID_MORE, wxDumpOpenExternalDlg::OnBrowse)
END_EVENT_TABLE()
wxDumpOpenExternalDlg::wxDumpOpenExternalDlg(wxWindow *parent,
const wxFileName& filename)
: wxDialog(parent,
wxID_ANY,
wxString::Format
(
_("Open file \"%s\""),
filename.GetFullPath().c_str()
))
{
// create controls
// ---------------
wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
sizerTop->Add(new wxStaticText(this, wxID_ANY,
wxString::Format
(
_("Enter command to open file \"%s\":"),
filename.GetFullName().c_str()
)),
wxSizerFlags().Border());
wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);
wxTextCtrl *command = new wxTextCtrl
(
this,
wxID_ANY,
wxEmptyString,
wxDefaultPosition,
wxSize(250, -1),
0,
wxTextValidator(wxFILTER_NONE, &m_command)
);
sizerH->Add(command,
wxSizerFlags(1).Align(wxALIGN_CENTER_VERTICAL));
wxButton *browse = new wxButton(this, wxID_MORE, wxT(">>"),
wxDefaultPosition, wxDefaultSize,
wxBU_EXACTFIT);
sizerH->Add(browse,
wxSizerFlags(0).Align(wxALIGN_CENTER_VERTICAL). Border(wxLEFT));
sizerTop->Add(sizerH, wxSizerFlags(0).Expand().Border());
sizerTop->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border());
sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL),
wxSizerFlags().Align(wxALIGN_RIGHT).Border());
// set the sizer &c
// ----------------
SetSizer(sizerTop);
Layout();
Fit();
command->SetFocus();
}
void wxDumpOpenExternalDlg::OnBrowse(wxCommandEvent& )
{
wxFileName fname(m_command);
wxFileDialog dlg(this,
wxFileSelectorPromptStr,
fname.GetPathWithSep(),
fname.GetFullName()
#ifdef __WXMSW__
, _("Executable files (*.exe)|*.exe|All files (*.*)|*.*||")
#endif // __WXMSW__
);
if ( dlg.ShowModal() == wxID_OK )
{
m_command = dlg.GetPath();
TransferDataToWindow();
}
}
// ----------------------------------------------------------------------------
// wxDebugReportDialog: class showing debug report to the user
// ----------------------------------------------------------------------------
class wxDebugReportDialog : public wxDialog
{
public:
wxDebugReportDialog(wxDebugReport& dbgrpt);
virtual bool TransferDataToWindow();
virtual bool TransferDataFromWindow();
private:
void OnView(wxCommandEvent& );
void OnViewUpdate(wxUpdateUIEvent& );
void OnOpen(wxCommandEvent& );
// small helper: add wxEXPAND and wxALL flags
static wxSizerFlags SizerFlags(int proportion)
{
return wxSizerFlags(proportion).Expand().Border();
}
wxDebugReport& m_dbgrpt;
wxCheckListBox *m_checklst;
wxTextCtrl *m_notes;
wxArrayString m_files;
DECLARE_EVENT_TABLE()
DECLARE_NO_COPY_CLASS(wxDebugReportDialog)
};
// ============================================================================
// wxDebugReportDialog implementation
// ============================================================================
BEGIN_EVENT_TABLE(wxDebugReportDialog, wxDialog)
EVT_BUTTON(wxID_VIEW_DETAILS, wxDebugReportDialog::OnView)
EVT_UPDATE_UI(wxID_VIEW_DETAILS, wxDebugReportDialog::OnViewUpdate)
EVT_BUTTON(wxID_OPEN, wxDebugReportDialog::OnOpen)
EVT_UPDATE_UI(wxID_OPEN, wxDebugReportDialog::OnViewUpdate)
END_EVENT_TABLE()
// ----------------------------------------------------------------------------
// construction
// ----------------------------------------------------------------------------
wxDebugReportDialog::wxDebugReportDialog(wxDebugReport& dbgrpt)
: wxDialog(NULL, wxID_ANY,
wxString::Format(_("Debug report \"%s\""),
dbgrpt.GetReportName().c_str()),
wxDefaultPosition,
wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxTHICK_FRAME),
m_dbgrpt(dbgrpt)
{
// upper part of the dialog: explanatory message
wxString msg;
msg << _("A debug report has been generated in the directory\n")
<< _T('\n')
<< _T(" \"") << dbgrpt.GetDirectory() << _T("\"\n")
<< _T('\n')
<< _("The report contains the files listed below. If any of these ")
<< _("files contain private information,\n")
<< _("please uncheck them and they will be removed from the report.\n")
<< _T('\n')
<< _("If you wish to suppress this debug report completely, please ")
<< _("choose the \"Cancel\" button,\n")
<< _("but be warned that it may hinder improving the program, so if\n")
<< _("at all possible please do continue with the report generation.\n")
<< _T('\n')
<< _(" Thank you and we're sorry for the inconvenience!\n")
<< _T("\n\n"); // just some white space to separate from other stuff
const wxSizerFlags flagsFixed(SizerFlags(0));
const wxSizerFlags flagsExpand(SizerFlags(1));
const wxSizerFlags flagsExpand2(SizerFlags(2));
wxSizer *sizerPreview =
new wxStaticBoxSizer(wxVERTICAL, this, _("&Debug report preview:"));
sizerPreview->Add(CreateTextSizer(msg), SizerFlags(0).Centre());
// ... and the list of files in this debug report with buttons to view them
wxSizer *sizerFileBtns = new wxBoxSizer(wxVERTICAL);
sizerFileBtns->AddStretchSpacer(1);
sizerFileBtns->Add(new wxButton(this, wxID_VIEW_DETAILS, _T("&View...")),
wxSizerFlags().Border(wxBOTTOM));
sizerFileBtns->Add(new wxButton(this, wxID_OPEN, _T("&Open...")),
wxSizerFlags().Border(wxTOP));
sizerFileBtns->AddStretchSpacer(1);
m_checklst = new wxCheckListBox(this, wxID_ANY);
wxSizer *sizerFiles = new wxBoxSizer(wxHORIZONTAL);
sizerFiles->Add(m_checklst, flagsExpand);
sizerFiles->Add(sizerFileBtns, flagsFixed);
sizerPreview->Add(sizerFiles, flagsExpand2);
// lower part of the dialog: notes field
wxSizer *sizerNotes = new wxStaticBoxSizer(wxVERTICAL, this, _("&Notes:"));
msg = _("If you have any additional information pertaining to this bug\n");
msg << _("report, please enter it here and it will be joined to it:");
m_notes = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE);
sizerNotes->Add(CreateTextSizer(msg), flagsFixed);
sizerNotes->Add(m_notes, flagsExpand);
wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
sizerTop->Add(sizerPreview, flagsExpand2);
sizerTop->AddSpacer(5);
sizerTop->Add(sizerNotes, flagsExpand);
sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), flagsFixed);
SetSizerAndFit(sizerTop);
Layout();
CentreOnScreen();
}
// ----------------------------------------------------------------------------
// data exchange
// ----------------------------------------------------------------------------
bool wxDebugReportDialog::TransferDataToWindow()
{
// all files are included in the report by default
const size_t count = m_dbgrpt.GetFilesCount();
for ( size_t n = 0; n < count; n++ )
{
wxString name,
desc;
if ( m_dbgrpt.GetFile(n, &name, &desc) )
{
m_checklst->Append(name + _T(" (") + desc + _T(')'));
m_checklst->Check(n);
m_files.Add(name);
}
}
return true;
}
bool wxDebugReportDialog::TransferDataFromWindow()
{
// any unchecked files should be removed from the report
const size_t count = m_checklst->GetCount();
for ( size_t n = 0; n < count; n++ )
{
if ( !m_checklst->IsChecked(n) )
{
m_dbgrpt.RemoveFile(m_files[n]);
}
}
// if the user entered any notes, add them to the report
const wxString notes = m_notes->GetValue();
if ( !notes.empty() )
{
// for now it's fixed, could make it configurable in the future...
const wxChar *NOTES_FILE_NAME = _T("notes.txt");
wxFileName fn(m_dbgrpt.GetDirectory(), NOTES_FILE_NAME);
wxFFile file(fn.GetFullPath(), _T("w"));
if ( file.IsOpened() && file.Write(notes) )
{
m_dbgrpt.AddFile(NOTES_FILE_NAME, _T("user notes"));
}
}
return true;
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void wxDebugReportDialog::OnView(wxCommandEvent& )
{
const int sel = m_checklst->GetSelection();
wxCHECK_RET( sel != -1, _T("invalid selection in OnView()") );
wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);
wxString str;
wxFFile file(fn.GetFullPath());
if ( file.IsOpened() && file.ReadAll(&str) )
{
wxDumpPreviewDlg dlg(this, m_files[sel], str);
dlg.ShowModal();
}
}
void wxDebugReportDialog::OnOpen(wxCommandEvent& )
{
const int sel = m_checklst->GetSelection();
wxCHECK_RET( sel != -1, _T("invalid selection in OnOpen()") );
wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);
// try to get the command to open this kind of files ourselves
wxString command;
wxFileType *
ft = wxTheMimeTypesManager->GetFileTypeFromExtension(fn.GetExt());
if ( ft )
{
command = ft->GetOpenCommand(fn.GetFullPath());
}
// if we couldn't, ask the user
if ( command.empty() )
{
wxDumpOpenExternalDlg dlg(this, fn);
if ( dlg.ShowModal() == wxID_OK )
{
// get the command chosen by the user and append file name to it
// if we don't have place marker for file name in the command...
wxString cmd = dlg.GetCommand();
if ( cmd.find(_T('%')) == wxString::npos )
{
// ...add it
cmd += _T(" \"%s\"");
}
command = wxFileType::ExpandCommand(cmd, fn.GetFullPath());
}
}
if ( !command.empty() )
::wxExecute(command);
}
void wxDebugReportDialog::OnViewUpdate(wxUpdateUIEvent& event)
{
int sel = m_checklst->GetSelection();
if (sel >= 0)
{
wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);
event.Enable(fn.FileExists());
}
else
event.Enable(false);
}
// ============================================================================
// wxDebugReportPreviewStd implementation
// ============================================================================
bool wxDebugReportPreviewStd::Show(wxDebugReport& dbgrpt) const
{
if ( !dbgrpt.GetFilesCount() )
return false;
wxDebugReportDialog dlg(dbgrpt);
return dlg.ShowModal() == wxID_OK && dbgrpt.GetFilesCount() != 0;
}
#endif // wxUSE_DEBUGREPORT

View File

@@ -401,6 +401,10 @@ SOURCE=.\common\dcbase.cpp
# End Source File
# Begin Source File
SOURCE=.\common\debugrpt.cpp
# End Source File
# Begin Source File
SOURCE=.\common\dircmn.cpp
# End Source File
# Begin Source File
@@ -918,6 +922,10 @@ SOURCE=.\generic\datectlg.cpp
# End Source File
# Begin Source File
SOURCE=.\generic\dbgrptg.cpp
# End Source File
# Begin Source File
SOURCE=.\generic\dcpsg.cpp
# End Source File
# Begin Source File
@@ -2139,6 +2147,10 @@ SOURCE=..\include\wx\debug.h
# End Source File
# Begin Source File
SOURCE=..\include\wx\debugrpt.h
# End Source File
# Begin Source File
SOURCE=..\include\wx\defs.h
# End Source File
# Begin Source File