]> git.saurik.com Git - wxWidgets.git/blame - src/common/debugrpt.cpp
as we always have CoreText available under 10.5+, we can properly determine fixed...
[wxWidgets.git] / src / common / debugrpt.cpp
CommitLineData
ce4fd7b5
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/debugrpt.cpp
3// Purpose: wxDebugReport and related classes implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 2005-01-17
7// RCS-ID: $Id$
8// Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
526954c5 9// Licence: wxWindows licence
ce4fd7b5
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/log.h"
29 #include "wx/intl.h"
2b1d737b 30 #include "wx/utils.h"
ce4fd7b5
VZ
31#endif // WX_PRECOMP
32
2cbe2aea 33#if wxUSE_DEBUGREPORT && wxUSE_XML
ce4fd7b5
VZ
34
35#include "wx/debugrpt.h"
36
a44f14cf 37#include "wx/ffile.h"
ce4fd7b5
VZ
38#include "wx/filename.h"
39#include "wx/dir.h"
40#include "wx/dynlib.h"
41
42#include "wx/xml/xml.h"
43
44#if wxUSE_STACKWALKER
45 #include "wx/stackwalk.h"
46#endif
47
48#if wxUSE_CRASHREPORT
49 #include "wx/msw/crashrpt.h"
50#endif
51
52#if wxUSE_ZIPSTREAM
53 #include "wx/wfstream.h"
54 #include "wx/zipstrm.h"
55#endif // wxUSE_ZIPSTREAM
56
61639efb 57WX_CHECK_BUILD_OPTIONS("wxQA")
ce4fd7b5
VZ
58
59// ----------------------------------------------------------------------------
60// XmlStackWalker: stack walker specialization which dumps stack in XML
61// ----------------------------------------------------------------------------
62
61639efb
VZ
63#if wxUSE_STACKWALKER
64
ce4fd7b5
VZ
65class XmlStackWalker : public wxStackWalker
66{
67public:
68 XmlStackWalker(wxXmlNode *nodeStack)
69 {
70 m_isOk = false;
71 m_nodeStack = nodeStack;
72 }
73
74 bool IsOk() const { return m_isOk; }
75
76protected:
77 virtual void OnStackFrame(const wxStackFrame& frame);
78
79 wxXmlNode *m_nodeStack;
80 bool m_isOk;
81};
82
ce4fd7b5
VZ
83// ----------------------------------------------------------------------------
84// local functions
85// ----------------------------------------------------------------------------
86
87static inline void
88HexProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
89{
9a83f860 90 node->AddAttribute(name, wxString::Format(wxT("%08lx"), value));
ce4fd7b5
VZ
91}
92
93static inline void
94NumProperty(wxXmlNode *node, const wxChar *name, unsigned long value)
95{
9a83f860 96 node->AddAttribute(name, wxString::Format(wxT("%lu"), value));
ce4fd7b5
VZ
97}
98
99static inline void
100TextElement(wxXmlNode *node, const wxChar *name, const wxString& value)
101{
102 wxXmlNode *nodeChild = new wxXmlNode(wxXML_ELEMENT_NODE, name);
103 node->AddChild(nodeChild);
7a893a31 104 nodeChild->AddChild(new wxXmlNode(wxXML_TEXT_NODE, wxEmptyString, value));
ce4fd7b5
VZ
105}
106
17a1ebd1
VZ
107#if wxUSE_CRASHREPORT && defined(__INTEL__)
108
ce4fd7b5
VZ
109static inline void
110HexElement(wxXmlNode *node, const wxChar *name, unsigned long value)
111{
9a83f860 112 TextElement(node, name, wxString::Format(wxT("%08lx"), value));
ce4fd7b5
VZ
113}
114
17a1ebd1
VZ
115#endif // wxUSE_CRASHREPORT
116
ce4fd7b5
VZ
117// ============================================================================
118// XmlStackWalker implementation
119// ============================================================================
120
121void XmlStackWalker::OnStackFrame(const wxStackFrame& frame)
122{
123 m_isOk = true;
124
9a83f860 125 wxXmlNode *nodeFrame = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("frame"));
ce4fd7b5
VZ
126 m_nodeStack->AddChild(nodeFrame);
127
9a83f860 128 NumProperty(nodeFrame, wxT("level"), frame.GetLevel());
ce4fd7b5
VZ
129 wxString func = frame.GetName();
130 if ( !func.empty() )
131 {
9a83f860
VZ
132 nodeFrame->AddAttribute(wxT("function"), func);
133 HexProperty(nodeFrame, wxT("offset"), frame.GetOffset());
ce4fd7b5
VZ
134 }
135
136 if ( frame.HasSourceLocation() )
137 {
9a83f860
VZ
138 nodeFrame->AddAttribute(wxT("file"), frame.GetFileName());
139 NumProperty(nodeFrame, wxT("line"), frame.GetLine());
ce4fd7b5
VZ
140 }
141
142 const size_t nParams = frame.GetParamCount();
143 if ( nParams )
144 {
9a83f860 145 wxXmlNode *nodeParams = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("parameters"));
ce4fd7b5
VZ
146 nodeFrame->AddChild(nodeParams);
147
148 for ( size_t n = 0; n < nParams; n++ )
149 {
150 wxXmlNode *
9a83f860 151 nodeParam = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("parameter"));
ce4fd7b5
VZ
152 nodeParams->AddChild(nodeParam);
153
9a83f860 154 NumProperty(nodeParam, wxT("number"), n);
ce4fd7b5
VZ
155
156 wxString type, name, value;
157 if ( !frame.GetParam(n, &type, &name, &value) )
158 continue;
159
160 if ( !type.empty() )
9a83f860 161 TextElement(nodeParam, wxT("type"), type);
ce4fd7b5
VZ
162
163 if ( !name.empty() )
9a83f860 164 TextElement(nodeParam, wxT("name"), name);
ce4fd7b5
VZ
165
166 if ( !value.empty() )
9a83f860 167 TextElement(nodeParam, wxT("value"), value);
ce4fd7b5
VZ
168 }
169 }
170}
171
172#endif // wxUSE_STACKWALKER
173
174// ============================================================================
175// wxDebugReport implementation
176// ============================================================================
177
178// ----------------------------------------------------------------------------
179// initialization and cleanup
180// ----------------------------------------------------------------------------
181
182wxDebugReport::wxDebugReport()
183{
184 // get a temporary directory name
2cdd63c6 185 wxString appname = GetReportName();
ce4fd7b5
VZ
186
187 // we can't use CreateTempFileName() because it creates a file, not a
188 // directory, so do our best to create a unique name ourselves
189 //
190 // of course, this doesn't protect us against malicious users...
191 wxFileName fn;
192 fn.AssignTempFileName(appname);
34af0de4 193#if wxUSE_DATETIME
9a83f860 194 m_dir.Printf(wxT("%s%c%s_dbgrpt-%lu-%s"),
61ad154c 195 fn.GetPath().c_str(), wxFILE_SEP_PATH, appname.c_str(),
ce4fd7b5 196 wxGetProcessId(),
9a83f860 197 wxDateTime::Now().Format(wxT("%Y%m%dT%H%M%S")).c_str());
34af0de4 198#else
9a83f860 199 m_dir.Printf(wxT("%s%c%s_dbgrpt-%lu"),
34af0de4
VZ
200 fn.GetPath().c_str(), wxFILE_SEP_PATH, appname.c_str(),
201 wxGetProcessId());
202#endif
ce4fd7b5
VZ
203
204 // as we are going to save the process state there use restrictive
205 // permissions
206 if ( !wxMkdir(m_dir, 0700) )
207 {
208 wxLogSysError(_("Failed to create directory \"%s\""), m_dir.c_str());
209 wxLogError(_("Debug report couldn't be created."));
210
211 Reset();
212 }
213}
214
215wxDebugReport::~wxDebugReport()
216{
217 if ( !m_dir.empty() )
218 {
219 // remove all files in this directory
220 wxDir dir(m_dir);
221 wxString file;
222 for ( bool cont = dir.GetFirst(&file); cont; cont = dir.GetNext(&file) )
223 {
224 if ( wxRemove(wxFileName(m_dir, file).GetFullPath()) != 0 )
225 {
226 wxLogSysError(_("Failed to remove debug report file \"%s\""),
227 file.c_str());
228 m_dir.clear();
229 break;
230 }
231 }
232 }
233
234 if ( !m_dir.empty() )
235 {
9034c590
JS
236 // Temp fix: what should this be? eVC++ doesn't like wxRmDir
237#ifdef __WXWINCE__
238 if ( wxRmdir(m_dir.fn_str()) != 0 )
239#else
2cdd63c6 240 if ( wxRmDir(m_dir.fn_str()) != 0 )
9034c590 241#endif
ce4fd7b5
VZ
242 {
243 wxLogSysError(_("Failed to clean up debug report directory \"%s\""),
244 m_dir.c_str());
245 }
246 }
247}
248
249// ----------------------------------------------------------------------------
250// various helpers
251// ----------------------------------------------------------------------------
252
253wxString wxDebugReport::GetReportName() const
254{
7735b2ea
VZ
255 if ( wxTheApp )
256 return wxTheApp->GetAppName();
2cdd63c6 257
9a83f860 258 return wxT("wx");
ce4fd7b5
VZ
259}
260
fdc1aa52
VZ
261void
262wxDebugReport::AddFile(const wxString& filename, const wxString& description)
ce4fd7b5 263{
fdc1aa52
VZ
264 wxString name;
265 wxFileName fn(filename);
266 if ( fn.IsAbsolute() )
267 {
268 // we need to copy the file to the debug report directory: give it the
269 // same name there
270 name = fn.GetFullName();
c02f03d5
FM
271
272 if (!wxCopyFile(fn.GetFullPath(),
273 wxFileName(GetDirectory(), name).GetFullPath()))
274 return;
fdc1aa52
VZ
275 }
276 else // file relative to the report directory
277 {
278 name = filename;
279
280 wxASSERT_MSG( wxFileName(GetDirectory(), name).FileExists(),
9a83f860 281 wxT("file should exist in debug report directory") );
fdc1aa52
VZ
282 }
283
ce4fd7b5
VZ
284 m_files.Add(name);
285 m_descriptions.Add(description);
286}
287
e18c3e02 288bool
fdc1aa52 289wxDebugReport::AddText(const wxString& filename,
e18c3e02
VZ
290 const wxString& text,
291 const wxString& description)
292{
fdc1aa52 293 wxASSERT_MSG( !wxFileName(filename).IsAbsolute(),
9a83f860 294 wxT("filename should be relative to debug report directory") );
fdc1aa52
VZ
295
296 wxFileName fn(GetDirectory(), filename);
9a83f860 297 wxFFile file(fn.GetFullPath(), wxT("w"));
e18c3e02
VZ
298 if ( !file.IsOpened() || !file.Write(text) )
299 return false;
300
fdc1aa52 301 AddFile(filename, description);
e18c3e02
VZ
302
303 return true;
304}
305
ce4fd7b5
VZ
306void wxDebugReport::RemoveFile(const wxString& name)
307{
308 const int n = m_files.Index(name);
9a83f860 309 wxCHECK_RET( n != wxNOT_FOUND, wxT("No such file in wxDebugReport") );
ce4fd7b5
VZ
310
311 m_files.RemoveAt(n);
312 m_descriptions.RemoveAt(n);
313
314 wxRemove(wxFileName(GetDirectory(), name).GetFullPath());
315}
316
317bool wxDebugReport::GetFile(size_t n, wxString *name, wxString *desc) const
318{
319 if ( n >= m_files.GetCount() )
320 return false;
321
322 if ( name )
323 *name = m_files[n];
324 if ( desc )
325 *desc = m_descriptions[n];
326
327 return true;
328}
329
330void wxDebugReport::AddAll(Context context)
331{
332#if wxUSE_STACKWALKER
333 AddContext(context);
334#endif // wxUSE_STACKWALKER
335
336#if wxUSE_CRASHREPORT
337 AddDump(context);
338#endif // wxUSE_CRASHREPORT
2cdd63c6
WS
339
340#if !wxUSE_STACKWALKER && !wxUSE_CRASHREPORT
341 wxUnusedVar(context);
342#endif
ce4fd7b5
VZ
343}
344
345// ----------------------------------------------------------------------------
346// adding basic text information about current context
347// ----------------------------------------------------------------------------
348
349#if wxUSE_STACKWALKER
350
351bool wxDebugReport::DoAddSystemInfo(wxXmlNode *nodeSystemInfo)
352{
9a83f860 353 nodeSystemInfo->AddAttribute(wxT("description"), wxGetOsDescription());
ce4fd7b5
VZ
354
355 return true;
356}
357
358bool wxDebugReport::DoAddLoadedModules(wxXmlNode *nodeModules)
359{
360 wxDynamicLibraryDetailsArray modules(wxDynamicLibrary::ListLoaded());
361 const size_t count = modules.GetCount();
362 if ( !count )
363 return false;
364
365 for ( size_t n = 0; n < count; n++ )
366 {
367 const wxDynamicLibraryDetails& info = modules[n];
368
9a83f860 369 wxXmlNode *nodeModule = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("module"));
ce4fd7b5
VZ
370 nodeModules->AddChild(nodeModule);
371
372 wxString path = info.GetPath();
373 if ( path.empty() )
374 path = info.GetName();
375 if ( !path.empty() )
9a83f860 376 nodeModule->AddAttribute(wxT("path"), path);
ce4fd7b5 377
d64dc197
WS
378 void *addr = NULL;
379 size_t len = 0;
ce4fd7b5
VZ
380 if ( info.GetAddress(&addr, &len) )
381 {
9a83f860
VZ
382 HexProperty(nodeModule, wxT("address"), wxPtrToUInt(addr));
383 HexProperty(nodeModule, wxT("size"), len);
ce4fd7b5
VZ
384 }
385
386 wxString ver = info.GetVersion();
387 if ( !ver.empty() )
388 {
9a83f860 389 nodeModule->AddAttribute(wxT("version"), ver);
ce4fd7b5
VZ
390 }
391 }
392
393 return true;
394}
395
396bool wxDebugReport::DoAddExceptionInfo(wxXmlNode *nodeContext)
397{
398#if wxUSE_CRASHREPORT
399 wxCrashContext c;
400 if ( !c.code )
401 return false;
402
9a83f860 403 wxXmlNode *nodeExc = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("exception"));
ce4fd7b5
VZ
404 nodeContext->AddChild(nodeExc);
405
9a83f860
VZ
406 HexProperty(nodeExc, wxT("code"), c.code);
407 nodeExc->AddAttribute(wxT("name"), c.GetExceptionString());
408 HexProperty(nodeExc, wxT("address"), wxPtrToUInt(c.addr));
ce4fd7b5
VZ
409
410#ifdef __INTEL__
9a83f860 411 wxXmlNode *nodeRegs = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("registers"));
ce4fd7b5 412 nodeContext->AddChild(nodeRegs);
9a83f860
VZ
413 HexElement(nodeRegs, wxT("eax"), c.regs.eax);
414 HexElement(nodeRegs, wxT("ebx"), c.regs.ebx);
415 HexElement(nodeRegs, wxT("ecx"), c.regs.edx);
416 HexElement(nodeRegs, wxT("edx"), c.regs.edx);
417 HexElement(nodeRegs, wxT("esi"), c.regs.esi);
418 HexElement(nodeRegs, wxT("edi"), c.regs.edi);
419
420 HexElement(nodeRegs, wxT("ebp"), c.regs.ebp);
421 HexElement(nodeRegs, wxT("esp"), c.regs.esp);
422 HexElement(nodeRegs, wxT("eip"), c.regs.eip);
423
424 HexElement(nodeRegs, wxT("cs"), c.regs.cs);
425 HexElement(nodeRegs, wxT("ds"), c.regs.ds);
426 HexElement(nodeRegs, wxT("es"), c.regs.es);
427 HexElement(nodeRegs, wxT("fs"), c.regs.fs);
428 HexElement(nodeRegs, wxT("gs"), c.regs.gs);
429 HexElement(nodeRegs, wxT("ss"), c.regs.ss);
430
431 HexElement(nodeRegs, wxT("flags"), c.regs.flags);
ce4fd7b5
VZ
432#endif // __INTEL__
433
434 return true;
435#else // !wxUSE_CRASHREPORT
436 wxUnusedVar(nodeContext);
437
438 return false;
439#endif // wxUSE_CRASHREPORT/!wxUSE_CRASHREPORT
440}
441
442bool wxDebugReport::AddContext(wxDebugReport::Context ctx)
443{
9a83f860 444 wxCHECK_MSG( IsOk(), false, wxT("use IsOk() first") );
ce4fd7b5
VZ
445
446 // create XML dump of current context
447 wxXmlDocument xmldoc;
9a83f860 448 wxXmlNode *nodeRoot = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("report"));
ce4fd7b5 449 xmldoc.SetRoot(nodeRoot);
9a83f860
VZ
450 nodeRoot->AddAttribute(wxT("version"), wxT("1.0"));
451 nodeRoot->AddAttribute(wxT("kind"), ctx == Context_Current ? wxT("user")
452 : wxT("exception"));
ce4fd7b5
VZ
453
454 // add system information
9a83f860 455 wxXmlNode *nodeSystemInfo = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("system"));
ce4fd7b5
VZ
456 if ( DoAddSystemInfo(nodeSystemInfo) )
457 nodeRoot->AddChild(nodeSystemInfo);
458 else
459 delete nodeSystemInfo;
460
461 // add information about the loaded modules
9a83f860 462 wxXmlNode *nodeModules = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("modules"));
ce4fd7b5
VZ
463 if ( DoAddLoadedModules(nodeModules) )
464 nodeRoot->AddChild(nodeModules);
465 else
466 delete nodeModules;
467
468 // add CPU context information: this only makes sense for exceptions as our
469 // current context is not very interesting otherwise
470 if ( ctx == Context_Exception )
471 {
9a83f860 472 wxXmlNode *nodeContext = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("context"));
ce4fd7b5
VZ
473 if ( DoAddExceptionInfo(nodeContext) )
474 nodeRoot->AddChild(nodeContext);
475 else
476 delete nodeContext;
477 }
478
479 // add stack traceback
480#if wxUSE_STACKWALKER
9a83f860 481 wxXmlNode *nodeStack = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("stack"));
ce4fd7b5 482 XmlStackWalker sw(nodeStack);
4db307e1 483#if wxUSE_ON_FATAL_EXCEPTION
ce4fd7b5
VZ
484 if ( ctx == Context_Exception )
485 {
486 sw.WalkFromException();
487 }
478cde32 488 else // Context_Current
4db307e1 489#endif // wxUSE_ON_FATAL_EXCEPTION
ce4fd7b5
VZ
490 {
491 sw.Walk();
492 }
493
494 if ( sw.IsOk() )
495 nodeRoot->AddChild(nodeStack);
496 else
497 delete nodeStack;
498#endif // wxUSE_STACKWALKER
499
500 // finally let the user add any extra information he needs
501 DoAddCustomContext(nodeRoot);
502
503
504 // save the entire context dump in a file
9a83f860 505 wxFileName fn(m_dir, GetReportName(), wxT("xml"));
ce4fd7b5
VZ
506
507 if ( !xmldoc.Save(fn.GetFullPath()) )
508 return false;
509
510 AddFile(fn.GetFullName(), _("process context description"));
511
512 return true;
513}
514
515#endif // wxUSE_STACKWALKER
516
517// ----------------------------------------------------------------------------
518// adding core dump
519// ----------------------------------------------------------------------------
520
521#if wxUSE_CRASHREPORT
522
523bool wxDebugReport::AddDump(Context ctx)
524{
9a83f860 525 wxCHECK_MSG( IsOk(), false, wxT("use IsOk() first") );
ce4fd7b5 526
9a83f860 527 wxFileName fn(m_dir, GetReportName(), wxT("dmp"));
ce4fd7b5
VZ
528 wxCrashReport::SetFileName(fn.GetFullPath());
529
530 if ( !(ctx == Context_Exception ? wxCrashReport::Generate()
531 : wxCrashReport::GenerateNow()) )
532 return false;
533
534 AddFile(fn.GetFullName(), _("dump of the process state (binary)"));
535
536 return true;
537}
538
539#endif // wxUSE_CRASHREPORT
540
541// ----------------------------------------------------------------------------
542// report processing
543// ----------------------------------------------------------------------------
544
545bool wxDebugReport::Process()
546{
547 if ( !GetFilesCount() )
548 {
549 wxLogError(_("Debug report generation has failed."));
550
551 return false;
552 }
553
554 if ( !DoProcess() )
555 {
556 wxLogError(_("Processing debug report has failed, leaving the files in \"%s\" directory."),
557 GetDirectory().c_str());
558
559 Reset();
560
561 return false;
562 }
563
564 return true;
565}
566
567bool wxDebugReport::DoProcess()
568{
96be8b4d 569 wxString msg(_("A debug report has been generated. It can be found in"));
9a83f860
VZ
570 msg << wxT("\n")
571 wxT("\t") << GetDirectory() << wxT("\n\n")
96be8b4d 572 << _("And includes the following files:\n");
ce4fd7b5
VZ
573
574 wxString name, desc;
575 const size_t count = GetFilesCount();
576 for ( size_t n = 0; n < count; n++ )
577 {
578 GetFile(n, &name, &desc);
78a11854 579 msg += wxString::Format("\t%s: %s\n", name, desc);
ce4fd7b5
VZ
580 }
581
582 msg += _("\nPlease send this report to the program maintainer, thank you!\n");
583
9a83f860 584 wxLogMessage(wxT("%s"), msg.c_str());
ce4fd7b5
VZ
585
586 // we have to do this or the report would be deleted, and we don't even
587 // have any way to ask the user if he wants to keep it from here
588 Reset();
589
590 return true;
591}
592
593// ============================================================================
594// wxDebugReport-derived classes
595// ============================================================================
596
597#if wxUSE_ZIPSTREAM
598
599// ----------------------------------------------------------------------------
600// wxDebugReportCompress
601// ----------------------------------------------------------------------------
602
fdf20a26
VZ
603void wxDebugReportCompress::SetCompressedFileDirectory(const wxString& dir)
604{
605 wxASSERT_MSG( m_zipfile.empty(), "Too late: call this before Process()" );
606
607 m_zipDir = dir;
608}
609
610void wxDebugReportCompress::SetCompressedFileBaseName(const wxString& name)
611{
612 wxASSERT_MSG( m_zipfile.empty(), "Too late: call this before Process()" );
613
614 m_zipName = name;
615}
616
ce4fd7b5
VZ
617bool wxDebugReportCompress::DoProcess()
618{
619 const size_t count = GetFilesCount();
620 if ( !count )
621 return false;
622
b78df856
VZ
623 // create the compressed report file outside of the directory with the
624 // report files as it will be deleted by wxDebugReport dtor but we want to
625 // keep this one: for this we simply treat the directory name as the name
626 // of the file so that its last component becomes our base name
627 wxFileName fn(GetDirectory());
fdf20a26
VZ
628 if ( !m_zipDir.empty() )
629 fn.SetPath(m_zipDir);
630 if ( !m_zipName.empty() )
631 fn.SetName(m_zipName);
b78df856
VZ
632 fn.SetExt("zip");
633
ce4fd7b5 634 // create the streams
9a83f860 635 wxFFileOutputStream os(fn.GetFullPath(), wxT("wb"));
3b676ed7 636 wxZipOutputStream zos(os, 9);
ce4fd7b5
VZ
637
638 // add all files to the ZIP one
639 wxString name, desc;
640 for ( size_t n = 0; n < count; n++ )
641 {
642 GetFile(n, &name, &desc);
643
644 wxZipEntry *ze = new wxZipEntry(name);
645 ze->SetComment(desc);
646
3b676ed7 647 if ( !zos.PutNextEntry(ze) )
ce4fd7b5
VZ
648 return false;
649
b78df856 650 const wxFileName filename(GetDirectory(), name);
2cdd63c6 651 wxFFileInputStream is(filename.GetFullPath());
3b676ed7 652 if ( !is.IsOk() || !zos.Write(is).IsOk() )
ce4fd7b5
VZ
653 return false;
654 }
655
3b676ed7 656 if ( !zos.Close() )
ce4fd7b5
VZ
657 return false;
658
659 m_zipfile = fn.GetFullPath();
660
661 return true;
662}
663
664// ----------------------------------------------------------------------------
665// wxDebugReportUpload
666// ----------------------------------------------------------------------------
667
668wxDebugReportUpload::wxDebugReportUpload(const wxString& url,
669 const wxString& input,
670 const wxString& action,
671 const wxString& curl)
672 : m_uploadURL(url),
673 m_inputField(input),
674 m_curlCmd(curl)
675{
9a83f860
VZ
676 if ( m_uploadURL.Last() != wxT('/') )
677 m_uploadURL += wxT('/');
ce4fd7b5
VZ
678 m_uploadURL += action;
679}
680
681bool wxDebugReportUpload::DoProcess()
682{
683 if ( !wxDebugReportCompress::DoProcess() )
684 return false;
685
686
687 wxArrayString output, errors;
688 int rc = wxExecute(wxString::Format
689 (
9a83f860 690 wxT("%s -F %s=@\"%s\" %s"),
ce4fd7b5
VZ
691 m_curlCmd.c_str(),
692 m_inputField.c_str(),
693 GetCompressedFileName().c_str(),
694 m_uploadURL.c_str()
695 ),
696 output,
697 errors);
698 if ( rc == -1 )
699 {
700 wxLogError(_("Failed to execute curl, please install it in PATH."));
701 }
702 else if ( rc != 0 )
703 {
704 const size_t count = errors.GetCount();
705 if ( count )
706 {
707 for ( size_t n = 0; n < count; n++ )
708 {
9a83f860 709 wxLogWarning(wxT("%s"), errors[n].c_str());
ce4fd7b5
VZ
710 }
711 }
712
713 wxLogError(_("Failed to upload the debug report (error code %d)."), rc);
714 }
715 else // rc == 0
716 {
717 if ( OnServerReply(output) )
718 return true;
719 }
720
721 return false;
722}
723
724#endif // wxUSE_ZIPSTREAM
725
726#endif // wxUSE_DEBUGREPORT