]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: log.cpp | |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "log.h" | |
21 | #endif | |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | // wxWindows | |
31 | #ifndef WX_PRECOMP | |
0c589ad0 | 32 | #include "wx/window.h" |
dbf3cd7a | 33 | #ifdef __WXMSW__ |
0c589ad0 | 34 | #include "wx/msw/private.h" |
dbf3cd7a RR |
35 | #endif |
36 | #include "wx/event.h" | |
37 | #include "wx/app.h" | |
38 | #include "wx/string.h" | |
39 | #include "wx/intl.h" | |
40 | #include "wx/menu.h" | |
41 | #include "wx/frame.h" | |
42 | #include "wx/msgdlg.h" | |
43 | #include "wx/filedlg.h" | |
44 | #include "wx/textctrl.h" | |
9ef3052c | 45 | #endif //WX_PRECOMP |
c801d85f | 46 | |
dbf3cd7a RR |
47 | #include "wx/file.h" |
48 | #include "wx/textfile.h" | |
49 | #include "wx/utils.h" | |
50 | #include "wx/log.h" | |
c801d85f KB |
51 | |
52 | // other standard headers | |
53 | #include <errno.h> | |
54 | #include <stdlib.h> | |
55 | #include <time.h> | |
56 | ||
2049ba38 | 57 | #ifdef __WXMSW__ |
9ef3052c | 58 | #include <windows.h> |
a0a302dc JS |
59 | // Redefines OutputDebugString if necessary |
60 | #include "wx/msw/private.h" | |
3078c3a6 VZ |
61 | #else //Unix |
62 | #include <signal.h> | |
63 | #endif //Win/Unix | |
c801d85f KB |
64 | |
65 | // ---------------------------------------------------------------------------- | |
66 | // non member functions | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // define this to enable wrapping of log messages | |
70 | //#define LOG_PRETTY_WRAP | |
71 | ||
9ef3052c | 72 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
73 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
74 | #endif | |
75 | ||
470b7da3 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // global variables | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | // we use a global variable to store the frame pointer for wxLogStatus - bad, | |
81 | // but it's he easiest way | |
0fb67cd1 | 82 | static wxFrame *gs_pFrame; // FIXME MT-unsafe |
470b7da3 | 83 | |
c801d85f KB |
84 | // ============================================================================ |
85 | // implementation | |
86 | // ============================================================================ | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // implementation of Log functions | |
90 | // | |
91 | // NB: unfortunately we need all these distinct functions, we can't make them | |
92 | // macros and not all compilers inline vararg functions. | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | // log functions can't allocate memory (LogError("out of memory...") should | |
96 | // work!), so we use a static buffer for all log messages | |
97 | #define LOG_BUFFER_SIZE (4096) | |
98 | ||
0fb67cd1 | 99 | // static buffer for error messages (FIXME MT-unsafe) |
50920146 | 100 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; |
c801d85f KB |
101 | |
102 | // generic log function | |
50920146 | 103 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) |
c801d85f | 104 | { |
9ef3052c VZ |
105 | if ( wxLog::GetActiveTarget() != NULL ) { |
106 | va_list argptr; | |
7502ba29 | 107 | va_start(argptr, szFormat); |
50920146 | 108 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
109 | va_end(argptr); |
110 | ||
0fb67cd1 | 111 | wxLog::OnLog(level, s_szBuf, time(NULL)); |
9ef3052c | 112 | } |
c801d85f KB |
113 | } |
114 | ||
115 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
50920146 | 116 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
117 | { \ |
118 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
119 | va_list argptr; \ | |
7502ba29 | 120 | va_start(argptr, szFormat); \ |
50920146 | 121 | wxVsprintf(s_szBuf, szFormat, argptr); \ |
c801d85f KB |
122 | va_end(argptr); \ |
123 | \ | |
0fb67cd1 | 124 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
125 | } \ |
126 | } | |
127 | ||
128 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
129 | IMPLEMENT_LOG_FUNCTION(Error) | |
130 | IMPLEMENT_LOG_FUNCTION(Warning) | |
131 | IMPLEMENT_LOG_FUNCTION(Message) | |
132 | IMPLEMENT_LOG_FUNCTION(Info) | |
133 | IMPLEMENT_LOG_FUNCTION(Status) | |
134 | ||
470b7da3 VZ |
135 | // accepts an additional argument which tells to which frame the output should |
136 | // be directed | |
50920146 | 137 | void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...) |
470b7da3 VZ |
138 | { |
139 | wxLog *pLog = wxLog::GetActiveTarget(); | |
140 | if ( pLog != NULL ) { | |
141 | va_list argptr; | |
142 | va_start(argptr, szFormat); | |
50920146 | 143 | wxVsprintf(s_szBuf, szFormat, argptr); |
470b7da3 VZ |
144 | va_end(argptr); |
145 | ||
146 | wxASSERT( gs_pFrame == NULL ); // should be reset! | |
147 | gs_pFrame = pFrame; | |
0fb67cd1 | 148 | wxLog::OnLog(wxLOG_Status, s_szBuf, time(NULL)); |
c67daf87 | 149 | gs_pFrame = (wxFrame *) NULL; |
470b7da3 VZ |
150 | } |
151 | } | |
152 | ||
9ef3052c | 153 | // same as info, but only if 'verbose' mode is on |
50920146 | 154 | void wxLogVerbose(const wxChar *szFormat, ...) |
9ef3052c VZ |
155 | { |
156 | wxLog *pLog = wxLog::GetActiveTarget(); | |
157 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
158 | va_list argptr; | |
7502ba29 | 159 | va_start(argptr, szFormat); |
50920146 | 160 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
161 | va_end(argptr); |
162 | ||
0fb67cd1 | 163 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); |
9ef3052c VZ |
164 | } |
165 | } | |
166 | ||
167 | // debug functions | |
b2aef89b | 168 | #ifdef __WXDEBUG__ |
9ef3052c | 169 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
50920146 | 170 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
171 | { \ |
172 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
173 | va_list argptr; \ | |
174 | va_start(argptr, szFormat); \ | |
50920146 | 175 | wxVsprintf(s_szBuf, szFormat, argptr); \ |
c801d85f KB |
176 | va_end(argptr); \ |
177 | \ | |
0fb67cd1 | 178 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
179 | } \ |
180 | } | |
181 | ||
50920146 | 182 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) |
0fb67cd1 VZ |
183 | { |
184 | wxLog *pLog = wxLog::GetActiveTarget(); | |
185 | ||
186 | if ( pLog != NULL && wxLog::IsAllowedTraceMask(mask) ) { | |
187 | va_list argptr; | |
188 | va_start(argptr, szFormat); | |
50920146 | 189 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 VZ |
190 | va_end(argptr); |
191 | ||
192 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
193 | } | |
194 | } | |
195 | ||
50920146 | 196 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) |
9ef3052c VZ |
197 | { |
198 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 199 | |
9ef3052c VZ |
200 | // we check that all of mask bits are set in the current mask, so |
201 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
202 | // if both bits are set. | |
d93f63db | 203 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { |
9ef3052c VZ |
204 | va_list argptr; |
205 | va_start(argptr, szFormat); | |
50920146 | 206 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
207 | va_end(argptr); |
208 | ||
0fb67cd1 | 209 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
9ef3052c VZ |
210 | } |
211 | } | |
212 | ||
213 | #else // release | |
214 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
215 | #endif | |
216 | ||
217 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
218 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
219 | ||
220 | // wxLogSysError: one uses the last error code, for other you must give it | |
221 | // explicitly | |
222 | ||
223 | // common part of both wxLogSysError | |
224 | void wxLogSysErrorHelper(long lErrCode) | |
c801d85f | 225 | { |
50920146 OK |
226 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; |
227 | wxSprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
228 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); | |
c801d85f | 229 | |
0fb67cd1 | 230 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); |
9ef3052c | 231 | } |
c801d85f | 232 | |
50920146 | 233 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) |
9ef3052c | 234 | { |
0fb67cd1 VZ |
235 | va_list argptr; |
236 | va_start(argptr, szFormat); | |
50920146 | 237 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 | 238 | va_end(argptr); |
9ef3052c | 239 | |
0fb67cd1 | 240 | wxLogSysErrorHelper(wxSysErrorCode()); |
c801d85f KB |
241 | } |
242 | ||
50920146 | 243 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) |
c801d85f | 244 | { |
0fb67cd1 VZ |
245 | va_list argptr; |
246 | va_start(argptr, szFormat); | |
50920146 | 247 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 | 248 | va_end(argptr); |
c801d85f | 249 | |
0fb67cd1 | 250 | wxLogSysErrorHelper(lErrCode); |
c801d85f KB |
251 | } |
252 | ||
253 | // ---------------------------------------------------------------------------- | |
254 | // wxLog class implementation | |
255 | // ---------------------------------------------------------------------------- | |
256 | ||
257 | wxLog::wxLog() | |
258 | { | |
0fb67cd1 | 259 | m_bHasMessages = FALSE; |
a2ace6ff | 260 | |
0fb67cd1 | 261 | // enable verbose messages by default in the debug builds |
a2ace6ff | 262 | #ifdef __WXDEBUG__ |
0fb67cd1 | 263 | m_bVerbose = TRUE; |
a2ace6ff | 264 | #else // release |
0fb67cd1 | 265 | m_bVerbose = FALSE; |
a2ace6ff | 266 | #endif // debug/release |
c801d85f KB |
267 | } |
268 | ||
9ec05cc9 VZ |
269 | wxLog *wxLog::GetActiveTarget() |
270 | { | |
0fb67cd1 VZ |
271 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
272 | // prevent infinite recursion if someone calls wxLogXXX() from | |
273 | // wxApp::CreateLogTarget() | |
274 | static bool s_bInGetActiveTarget = FALSE; | |
275 | if ( !s_bInGetActiveTarget ) { | |
276 | s_bInGetActiveTarget = TRUE; | |
277 | ||
278 | #ifdef wxUSE_NOGUI | |
279 | ms_pLogger = new wxLogStderr; | |
280 | #else | |
281 | // ask the application to create a log target for us | |
282 | if ( wxTheApp != NULL ) | |
283 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
284 | else | |
285 | ms_pLogger = new wxLogStderr; | |
286 | #endif | |
287 | ||
288 | s_bInGetActiveTarget = FALSE; | |
289 | ||
290 | // do nothing if it fails - what can we do? | |
291 | } | |
275bf4c1 | 292 | } |
c801d85f | 293 | |
0fb67cd1 | 294 | return ms_pLogger; |
c801d85f KB |
295 | } |
296 | ||
c085e333 | 297 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 298 | { |
0fb67cd1 VZ |
299 | if ( ms_pLogger != NULL ) { |
300 | // flush the old messages before changing because otherwise they might | |
301 | // get lost later if this target is not restored | |
302 | ms_pLogger->Flush(); | |
303 | } | |
c801d85f | 304 | |
0fb67cd1 VZ |
305 | wxLog *pOldLogger = ms_pLogger; |
306 | ms_pLogger = pLogger; | |
c085e333 | 307 | |
0fb67cd1 | 308 | return pOldLogger; |
c801d85f KB |
309 | } |
310 | ||
0fb67cd1 | 311 | void wxLog::RemoveTraceMask(const wxString& str) |
c801d85f | 312 | { |
0fb67cd1 VZ |
313 | int index = ms_aTraceMasks.Index(str); |
314 | if ( index != wxNOT_FOUND ) | |
315 | ms_aTraceMasks.Remove((size_t)index); | |
316 | } | |
c801d85f | 317 | |
d2e1ef19 VZ |
318 | void wxLog::TimeStamp(wxString *str) |
319 | { | |
320 | if ( ms_timestamp ) | |
321 | { | |
322 | wxChar buf[256]; | |
323 | time_t timeNow; | |
324 | (void)time(&timeNow); | |
325 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); | |
326 | ||
327 | str->Empty(); | |
328 | *str << buf << _T(": "); | |
329 | } | |
330 | } | |
331 | ||
50920146 | 332 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
0fb67cd1 | 333 | { |
0fb67cd1 VZ |
334 | switch ( level ) { |
335 | case wxLOG_FatalError: | |
786855a1 | 336 | DoLogString(wxString(_("Fatal error: ")) + szString, t); |
0fb67cd1 VZ |
337 | DoLogString(_("Program aborted."), t); |
338 | Flush(); | |
339 | abort(); | |
340 | break; | |
341 | ||
342 | case wxLOG_Error: | |
786855a1 | 343 | DoLogString(wxString(_("Error: ")) + szString, t); |
0fb67cd1 VZ |
344 | break; |
345 | ||
346 | case wxLOG_Warning: | |
786855a1 | 347 | DoLogString(wxString(_("Warning: ")) + szString, t); |
0fb67cd1 VZ |
348 | break; |
349 | ||
350 | case wxLOG_Info: | |
0fb67cd1 | 351 | if ( GetVerbose() ) |
37278984 | 352 | case wxLOG_Message: |
786855a1 VZ |
353 | default: // log unknown log levels too |
354 | DoLogString(szString, t); | |
0fb67cd1 VZ |
355 | // fall through |
356 | ||
357 | case wxLOG_Status: | |
358 | // nothing to do | |
359 | break; | |
360 | ||
361 | case wxLOG_Trace: | |
362 | case wxLOG_Debug: | |
363 | #ifdef __WXDEBUG__ | |
364 | DoLogString(szString, t); | |
365 | #endif | |
0fb67cd1 | 366 | break; |
0fb67cd1 | 367 | } |
c801d85f KB |
368 | } |
369 | ||
74e3313b | 370 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) |
c801d85f | 371 | { |
50920146 | 372 | wxFAIL_MSG(_T("DoLogString must be overriden if it's called.")); |
c801d85f KB |
373 | } |
374 | ||
375 | void wxLog::Flush() | |
376 | { | |
0fb67cd1 | 377 | // do nothing |
c801d85f KB |
378 | } |
379 | ||
380 | // ---------------------------------------------------------------------------- | |
381 | // wxLogStderr class implementation | |
382 | // ---------------------------------------------------------------------------- | |
383 | ||
384 | wxLogStderr::wxLogStderr(FILE *fp) | |
385 | { | |
0fb67cd1 VZ |
386 | if ( fp == NULL ) |
387 | m_fp = stderr; | |
388 | else | |
389 | m_fp = fp; | |
c801d85f KB |
390 | } |
391 | ||
74e3313b | 392 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 393 | { |
d2e1ef19 VZ |
394 | wxString str; |
395 | TimeStamp(&str); | |
396 | str << szString << _T('\n'); | |
1e8a4bc2 | 397 | |
50920146 | 398 | fputs(str.mb_str(), m_fp); |
0fb67cd1 | 399 | fflush(m_fp); |
1e8a4bc2 | 400 | |
0fb67cd1 VZ |
401 | // under Windows, programs usually don't have stderr at all, so make show the |
402 | // messages also under debugger | |
1e8a4bc2 | 403 | #ifdef __WXMSW__ |
50920146 | 404 | OutputDebugString(str + _T('\r')); |
1e8a4bc2 | 405 | #endif // MSW |
c801d85f KB |
406 | } |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // wxLogStream implementation | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
4bf78aae | 412 | #if wxUSE_STD_IOSTREAM |
c801d85f KB |
413 | wxLogStream::wxLogStream(ostream *ostr) |
414 | { | |
0fb67cd1 VZ |
415 | if ( ostr == NULL ) |
416 | m_ostr = &cerr; | |
417 | else | |
418 | m_ostr = ostr; | |
c801d85f KB |
419 | } |
420 | ||
74e3313b | 421 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 422 | { |
dcf924a3 | 423 | (*m_ostr) << wxConvCurrent->cWX2MB(szString) << endl << flush; |
c801d85f | 424 | } |
0fb67cd1 | 425 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 426 | |
0fb67cd1 | 427 | #ifndef wxUSE_NOGUI |
f5abe911 | 428 | |
c801d85f KB |
429 | // ---------------------------------------------------------------------------- |
430 | // wxLogTextCtrl implementation | |
431 | // ---------------------------------------------------------------------------- | |
5de76427 | 432 | |
f5abe911 | 433 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) |
c801d85f | 434 | { |
d2e1ef19 | 435 | m_pTextCtrl = pTextCtrl; |
c801d85f KB |
436 | } |
437 | ||
d2e1ef19 | 438 | void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t t) |
c801d85f | 439 | { |
d2e1ef19 VZ |
440 | wxString msg; |
441 | TimeStamp(&msg); | |
442 | msg << szString << _T('\n'); | |
443 | ||
444 | m_pTextCtrl->AppendText(msg); | |
c801d85f | 445 | } |
c801d85f KB |
446 | |
447 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 448 | // wxLogGui implementation (FIXME MT-unsafe) |
c801d85f KB |
449 | // ---------------------------------------------------------------------------- |
450 | ||
c801d85f KB |
451 | wxLogGui::wxLogGui() |
452 | { | |
0fb67cd1 VZ |
453 | Clear(); |
454 | } | |
455 | ||
456 | void wxLogGui::Clear() | |
457 | { | |
458 | m_bErrors = m_bWarnings = FALSE; | |
459 | m_aMessages.Empty(); | |
460 | m_aTimes.Empty(); | |
c801d85f KB |
461 | } |
462 | ||
463 | void wxLogGui::Flush() | |
464 | { | |
0fb67cd1 VZ |
465 | if ( !m_bHasMessages ) |
466 | return; | |
c801d85f | 467 | |
0fb67cd1 VZ |
468 | // do it right now to block any new calls to Flush() while we're here |
469 | m_bHasMessages = FALSE; | |
c9dac664 | 470 | |
0fb67cd1 VZ |
471 | // concatenate all strings (but not too many to not overfill the msg box) |
472 | wxString str; | |
473 | size_t nLines = 0, | |
474 | nMsgCount = m_aMessages.Count(); | |
9ec05cc9 | 475 | |
0fb67cd1 VZ |
476 | // start from the most recent message |
477 | for ( size_t n = nMsgCount; n > 0; n-- ) { | |
478 | // for Windows strings longer than this value are wrapped (NT 4.0) | |
479 | const size_t nMsgLineWidth = 156; | |
c801d85f | 480 | |
0fb67cd1 | 481 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; |
c801d85f | 482 | |
0fb67cd1 VZ |
483 | if ( nLines > 25 ) // don't put too many lines in message box |
484 | break; | |
c801d85f | 485 | |
50920146 | 486 | str << m_aMessages[n - 1] << _T("\n"); |
0fb67cd1 | 487 | } |
c801d85f | 488 | |
50920146 | 489 | const wxChar *title; |
0fb67cd1 | 490 | long style; |
c801d85f | 491 | |
0fb67cd1 VZ |
492 | if ( m_bErrors ) { |
493 | title = _("Error"); | |
494 | style = wxICON_STOP; | |
495 | } | |
496 | else if ( m_bWarnings ) { | |
497 | title = _("Warning"); | |
498 | style = wxICON_EXCLAMATION; | |
499 | } | |
500 | else { | |
501 | title = _("Information"); | |
502 | style = wxICON_INFORMATION; | |
503 | } | |
c801d85f | 504 | |
0fb67cd1 VZ |
505 | wxMessageBox(str, title, wxOK | style); |
506 | ||
507 | // no undisplayed messages whatsoever | |
508 | Clear(); | |
c801d85f KB |
509 | } |
510 | ||
511 | // the default behaviour is to discard all informational messages if there | |
512 | // are any errors/warnings. | |
50920146 | 513 | void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
c801d85f | 514 | { |
0fb67cd1 VZ |
515 | switch ( level ) { |
516 | case wxLOG_Info: | |
517 | if ( GetVerbose() ) | |
518 | case wxLOG_Message: | |
519 | if ( !m_bErrors ) { | |
520 | m_aMessages.Add(szString); | |
521 | m_aTimes.Add((long)t); | |
522 | m_bHasMessages = TRUE; | |
523 | } | |
524 | break; | |
525 | ||
526 | case wxLOG_Status: | |
88ac883a | 527 | #if wxUSE_STATUSBAR |
0fb67cd1 VZ |
528 | { |
529 | // find the top window and set it's status text if it has any | |
530 | wxFrame *pFrame = gs_pFrame; | |
531 | if ( pFrame == NULL ) { | |
532 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
533 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
534 | pFrame = (wxFrame *)pWin; | |
535 | } | |
536 | } | |
537 | ||
538 | if ( pFrame != NULL ) | |
539 | pFrame->SetStatusText(szString); | |
540 | } | |
88ac883a | 541 | #endif // wxUSE_STATUSBAR |
0fb67cd1 VZ |
542 | break; |
543 | ||
544 | case wxLOG_Trace: | |
545 | case wxLOG_Debug: | |
546 | #ifdef __WXDEBUG__ | |
547 | { | |
548 | #ifdef __WXMSW__ | |
549 | // don't prepend debug/trace here: it goes to the | |
550 | // debug window anyhow, but do put a timestamp | |
d2e1ef19 VZ |
551 | wxString str; |
552 | TimeStamp(&str); | |
553 | str << szString << _T("\n\r"); | |
554 | OutputDebugString(str); | |
0fb67cd1 VZ |
555 | #else |
556 | // send them to stderr | |
84fff0b3 | 557 | wxFprintf(stderr, _T("%s: %s\n"), |
d2e1ef19 VZ |
558 | level == wxLOG_Trace ? _T("Trace") |
559 | : _T("Debug"), | |
560 | szString); | |
0fb67cd1 VZ |
561 | fflush(stderr); |
562 | #endif | |
563 | } | |
564 | #endif // __WXDEBUG__ | |
565 | ||
566 | break; | |
567 | ||
568 | case wxLOG_FatalError: | |
569 | // show this one immediately | |
570 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); | |
571 | break; | |
572 | ||
573 | case wxLOG_Error: | |
574 | // discard earlier informational messages if this is the 1st | |
575 | // error because they might not make sense any more | |
576 | if ( !m_bErrors ) { | |
577 | m_aMessages.Empty(); | |
578 | m_aTimes.Empty(); | |
579 | m_bHasMessages = TRUE; | |
580 | m_bErrors = TRUE; | |
581 | } | |
582 | // fall through | |
583 | ||
584 | case wxLOG_Warning: | |
585 | if ( !m_bErrors ) { | |
586 | // for the warning we don't discard the info messages | |
587 | m_bWarnings = TRUE; | |
588 | } | |
589 | ||
590 | m_aMessages.Add(szString); | |
591 | m_aTimes.Add((long)t); | |
592 | break; | |
0fb67cd1 | 593 | } |
c801d85f KB |
594 | } |
595 | ||
9ef3052c | 596 | // ---------------------------------------------------------------------------- |
fe7b1156 | 597 | // wxLogWindow and wxLogFrame implementation |
9ef3052c VZ |
598 | // ---------------------------------------------------------------------------- |
599 | ||
600 | // log frame class | |
fe7b1156 | 601 | // --------------- |
9ef3052c VZ |
602 | class wxLogFrame : public wxFrame |
603 | { | |
604 | public: | |
0fb67cd1 | 605 | // ctor & dtor |
50920146 | 606 | wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle); |
0fb67cd1 | 607 | virtual ~wxLogFrame(); |
9ef3052c | 608 | |
0fb67cd1 VZ |
609 | // menu callbacks |
610 | void OnClose(wxCommandEvent& event); | |
611 | void OnCloseWindow(wxCloseEvent& event); | |
88ac883a | 612 | #if wxUSE_FILE |
0fb67cd1 | 613 | void OnSave (wxCommandEvent& event); |
88ac883a | 614 | #endif // wxUSE_FILE |
0fb67cd1 | 615 | void OnClear(wxCommandEvent& event); |
9ef3052c | 616 | |
0fb67cd1 | 617 | void OnIdle(wxIdleEvent&); |
fe7b1156 | 618 | |
0fb67cd1 VZ |
619 | // accessors |
620 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
9ef3052c VZ |
621 | |
622 | private: | |
0fb67cd1 VZ |
623 | enum |
624 | { | |
625 | Menu_Close = 100, | |
626 | Menu_Save, | |
627 | Menu_Clear | |
628 | }; | |
9ef3052c | 629 | |
0fb67cd1 VZ |
630 | // instead of closing just hide the window to be able to Show() it later |
631 | void DoClose() { Show(FALSE); } | |
fe7b1156 | 632 | |
0fb67cd1 VZ |
633 | wxTextCtrl *m_pTextCtrl; |
634 | wxLogWindow *m_log; | |
9ef3052c | 635 | |
0fb67cd1 | 636 | DECLARE_EVENT_TABLE() |
9ef3052c VZ |
637 | }; |
638 | ||
639 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
0fb67cd1 VZ |
640 | // wxLogWindow menu events |
641 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
88ac883a | 642 | #if wxUSE_FILE |
0fb67cd1 | 643 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) |
88ac883a | 644 | #endif // wxUSE_FILE |
0fb67cd1 | 645 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) |
9ef3052c | 646 | |
0fb67cd1 | 647 | EVT_CLOSE(wxLogFrame::OnCloseWindow) |
9ec05cc9 | 648 | END_EVENT_TABLE() |
9ef3052c | 649 | |
50920146 | 650 | wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle) |
470b7da3 | 651 | : wxFrame(pParent, -1, szTitle) |
9ef3052c | 652 | { |
0fb67cd1 VZ |
653 | m_log = log; |
654 | ||
655 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, | |
656 | wxDefaultSize, | |
657 | wxTE_MULTILINE | | |
658 | wxHSCROLL | | |
659 | wxTE_READONLY); | |
660 | ||
661 | // create menu | |
662 | wxMenuBar *pMenuBar = new wxMenuBar; | |
663 | wxMenu *pMenu = new wxMenu; | |
88ac883a | 664 | #if wxUSE_FILE |
0fb67cd1 | 665 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); |
88ac883a | 666 | #endif // wxUSE_FILE |
0fb67cd1 VZ |
667 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); |
668 | pMenu->AppendSeparator(); | |
669 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); | |
670 | pMenuBar->Append(pMenu, _("&Log")); | |
671 | SetMenuBar(pMenuBar); | |
672 | ||
88ac883a | 673 | #if wxUSE_STATUSBAR |
0fb67cd1 VZ |
674 | // status bar for menu prompts |
675 | CreateStatusBar(); | |
88ac883a | 676 | #endif // wxUSE_STATUSBAR |
0fb67cd1 VZ |
677 | |
678 | m_log->OnFrameCreate(this); | |
9ef3052c VZ |
679 | } |
680 | ||
46dc76ba | 681 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
9ef3052c | 682 | { |
0fb67cd1 | 683 | DoClose(); |
9ef3052c VZ |
684 | } |
685 | ||
46dc76ba | 686 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
5260b1c5 | 687 | { |
0fb67cd1 | 688 | DoClose(); |
fe7b1156 VZ |
689 | } |
690 | ||
88ac883a | 691 | #if wxUSE_FILE |
46dc76ba | 692 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) |
9ef3052c | 693 | { |
0fb67cd1 VZ |
694 | // get the file name |
695 | // ----------------- | |
50920146 | 696 | const wxChar *szFileName = wxSaveFileSelector(_T("log"), _T("txt"), _T("log.txt")); |
0fb67cd1 VZ |
697 | if ( szFileName == NULL ) { |
698 | // cancelled | |
699 | return; | |
700 | } | |
9ef3052c | 701 | |
0fb67cd1 VZ |
702 | // open file |
703 | // --------- | |
704 | wxFile file; | |
705 | bool bOk = FALSE; | |
706 | if ( wxFile::Exists(szFileName) ) { | |
707 | bool bAppend = FALSE; | |
708 | wxString strMsg; | |
709 | strMsg.Printf(_("Append log to file '%s' " | |
9ef3052c | 710 | "(choosing [No] will overwrite it)?"), szFileName); |
0fb67cd1 VZ |
711 | switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) { |
712 | case wxYES: | |
713 | bAppend = TRUE; | |
714 | break; | |
9ef3052c | 715 | |
0fb67cd1 VZ |
716 | case wxNO: |
717 | bAppend = FALSE; | |
718 | break; | |
9ef3052c | 719 | |
0fb67cd1 VZ |
720 | case wxCANCEL: |
721 | return; | |
9ef3052c | 722 | |
0fb67cd1 VZ |
723 | default: |
724 | wxFAIL_MSG(_("invalid message box return value")); | |
725 | } | |
9ef3052c | 726 | |
0fb67cd1 VZ |
727 | if ( bAppend ) { |
728 | bOk = file.Open(szFileName, wxFile::write_append); | |
729 | } | |
730 | else { | |
731 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
732 | } | |
9ef3052c VZ |
733 | } |
734 | else { | |
0fb67cd1 | 735 | bOk = file.Create(szFileName); |
9ef3052c | 736 | } |
9ef3052c | 737 | |
0fb67cd1 VZ |
738 | // retrieve text and save it |
739 | // ------------------------- | |
740 | int nLines = m_pTextCtrl->GetNumberOfLines(); | |
741 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
742 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + | |
743 | // we're not going to pull in the whole wxTextFile if all we need is this... | |
1ccbb61a | 744 | #if wxUSE_TEXTFILE |
0fb67cd1 | 745 | wxTextFile::GetEOL() |
1ccbb61a | 746 | #else // !wxUSE_TEXTFILE |
0fb67cd1 | 747 | '\n' |
1ccbb61a | 748 | #endif // wxUSE_TEXTFILE |
0fb67cd1 VZ |
749 | ); |
750 | } | |
9ec05cc9 | 751 | |
0fb67cd1 VZ |
752 | if ( bOk ) |
753 | bOk = file.Close(); | |
9ef3052c | 754 | |
0fb67cd1 VZ |
755 | if ( !bOk ) { |
756 | wxLogError(_("Can't save log contents to file.")); | |
757 | } | |
758 | else { | |
759 | wxLogStatus(this, _("Log saved to the file '%s'."), szFileName); | |
760 | } | |
9ef3052c | 761 | } |
88ac883a | 762 | #endif // wxUSE_FILE |
9ef3052c | 763 | |
46dc76ba | 764 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
9ef3052c | 765 | { |
0fb67cd1 | 766 | m_pTextCtrl->Clear(); |
9ef3052c VZ |
767 | } |
768 | ||
fe7b1156 VZ |
769 | wxLogFrame::~wxLogFrame() |
770 | { | |
0fb67cd1 | 771 | m_log->OnFrameDelete(this); |
fe7b1156 VZ |
772 | } |
773 | ||
774 | // wxLogWindow | |
775 | // ----------- | |
470b7da3 | 776 | wxLogWindow::wxLogWindow(wxFrame *pParent, |
88ac883a VZ |
777 | const wxChar *szTitle, |
778 | bool bShow, | |
779 | bool bDoPass) | |
9ef3052c | 780 | { |
0fb67cd1 | 781 | m_bPassMessages = bDoPass; |
a3622daa | 782 | |
0fb67cd1 VZ |
783 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); |
784 | m_pOldLog = wxLog::SetActiveTarget(this); | |
9ec05cc9 | 785 | |
0fb67cd1 VZ |
786 | if ( bShow ) |
787 | m_pLogFrame->Show(TRUE); | |
9ef3052c VZ |
788 | } |
789 | ||
e51c4943 | 790 | void wxLogWindow::Show(bool bShow) |
9ef3052c | 791 | { |
0fb67cd1 | 792 | m_pLogFrame->Show(bShow); |
9ef3052c VZ |
793 | } |
794 | ||
fe7b1156 | 795 | void wxLogWindow::Flush() |
06db8ebd | 796 | { |
0fb67cd1 VZ |
797 | if ( m_pOldLog != NULL ) |
798 | m_pOldLog->Flush(); | |
fe7b1156 | 799 | |
0fb67cd1 | 800 | m_bHasMessages = FALSE; |
06db8ebd VZ |
801 | } |
802 | ||
50920146 | 803 | void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
9ef3052c | 804 | { |
0fb67cd1 VZ |
805 | // first let the previous logger show it |
806 | if ( m_pOldLog != NULL && m_bPassMessages ) { | |
807 | // FIXME why can't we access protected wxLog method from here (we derive | |
808 | // from wxLog)? gcc gives "DoLog is protected in this context", what | |
809 | // does this mean? Anyhow, the cast is harmless and let's us do what | |
810 | // we want. | |
811 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString, t); | |
812 | } | |
9ec05cc9 | 813 | |
0fb67cd1 VZ |
814 | if ( m_pLogFrame ) { |
815 | switch ( level ) { | |
816 | case wxLOG_Status: | |
817 | // by default, these messages are ignored by wxLog, so process | |
818 | // them ourselves | |
37278984 | 819 | if ( !wxIsEmpty(szString) ) |
0fb67cd1 VZ |
820 | { |
821 | wxString str; | |
822 | str << _("Status: ") << szString; | |
823 | DoLogString(str, t); | |
824 | } | |
825 | break; | |
826 | ||
827 | // don't put trace messages in the text window for 2 reasons: | |
828 | // 1) there are too many of them | |
829 | // 2) they may provoke other trace messages thus sending a program | |
830 | // into an infinite loop | |
831 | case wxLOG_Trace: | |
832 | break; | |
833 | ||
834 | default: | |
835 | // and this will format it nicely and call our DoLogString() | |
836 | wxLog::DoLog(level, szString, t); | |
1c4a764c | 837 | } |
1c4a764c | 838 | } |
fe7b1156 | 839 | |
0fb67cd1 | 840 | m_bHasMessages = TRUE; |
9ef3052c VZ |
841 | } |
842 | ||
74e3313b | 843 | void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
9ef3052c | 844 | { |
0fb67cd1 VZ |
845 | // put the text into our window |
846 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
9ef3052c | 847 | |
0fb67cd1 VZ |
848 | // remove selection (WriteText is in fact ReplaceSelection) |
849 | #ifdef __WXMSW__ | |
f42d2625 VZ |
850 | long nLen = pText->GetLastPosition(); |
851 | pText->SetSelection(nLen, nLen); | |
0fb67cd1 | 852 | #endif // Windows |
9ef3052c | 853 | |
d2e1ef19 VZ |
854 | wxString msg; |
855 | TimeStamp(&msg); | |
856 | msg << szString << _T('\n'); | |
857 | ||
858 | pText->AppendText(msg); | |
9ef3052c | 859 | |
0fb67cd1 | 860 | // TODO ensure that the line can be seen |
9ef3052c VZ |
861 | } |
862 | ||
fe7b1156 VZ |
863 | wxFrame *wxLogWindow::GetFrame() const |
864 | { | |
0fb67cd1 | 865 | return m_pLogFrame; |
fe7b1156 VZ |
866 | } |
867 | ||
37cda9cb | 868 | void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame)) |
fe7b1156 VZ |
869 | { |
870 | } | |
871 | ||
37cda9cb | 872 | void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame)) |
fe7b1156 | 873 | { |
0fb67cd1 | 874 | m_pLogFrame = (wxLogFrame *)NULL; |
fe7b1156 VZ |
875 | } |
876 | ||
9ef3052c VZ |
877 | wxLogWindow::~wxLogWindow() |
878 | { | |
0fb67cd1 | 879 | delete m_pOldLog; |
c085e333 | 880 | |
0fb67cd1 VZ |
881 | // may be NULL if log frame already auto destroyed itself |
882 | delete m_pLogFrame; | |
9ef3052c VZ |
883 | } |
884 | ||
27fc802d | 885 | #endif //wxUSE_NOGUI |
c801d85f KB |
886 | |
887 | // ============================================================================ | |
888 | // Global functions/variables | |
889 | // ============================================================================ | |
890 | ||
891 | // ---------------------------------------------------------------------------- | |
892 | // static variables | |
893 | // ---------------------------------------------------------------------------- | |
0fb67cd1 VZ |
894 | |
895 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
896 | bool wxLog::ms_doLog = TRUE; | |
897 | bool wxLog::ms_bAutoCreate = TRUE; | |
d2e1ef19 VZ |
898 | |
899 | const wxChar *wxLog::ms_timestamp = "%X"; // time only, no date | |
900 | ||
0fb67cd1 VZ |
901 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
902 | wxArrayString wxLog::ms_aTraceMasks; | |
c801d85f KB |
903 | |
904 | // ---------------------------------------------------------------------------- | |
905 | // stdout error logging helper | |
906 | // ---------------------------------------------------------------------------- | |
907 | ||
908 | // helper function: wraps the message and justifies it under given position | |
909 | // (looks more pretty on the terminal). Also adds newline at the end. | |
910 | // | |
0fb67cd1 VZ |
911 | // TODO this is now disabled until I find a portable way of determining the |
912 | // terminal window size (ok, I found it but does anybody really cares?) | |
913 | #ifdef LOG_PRETTY_WRAP | |
c801d85f KB |
914 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
915 | { | |
0fb67cd1 VZ |
916 | size_t nMax = 80; // FIXME |
917 | size_t nStart = strlen(pszPrefix); | |
918 | fputs(pszPrefix, f); | |
919 | ||
920 | size_t n; | |
921 | while ( *psz != '\0' ) { | |
922 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
923 | putc(*psz++, f); | |
924 | ||
925 | // wrapped? | |
926 | if ( *psz != '\0' ) { | |
927 | /*putc('\n', f);*/ | |
928 | for ( n = 0; n < nStart; n++ ) | |
929 | putc(' ', f); | |
930 | ||
931 | // as we wrapped, squeeze all white space | |
932 | while ( isspace(*psz) ) | |
933 | psz++; | |
934 | } | |
c801d85f | 935 | } |
c801d85f | 936 | |
0fb67cd1 | 937 | putc('\n', f); |
c801d85f KB |
938 | } |
939 | #endif //LOG_PRETTY_WRAP | |
940 | ||
941 | // ---------------------------------------------------------------------------- | |
942 | // error code/error message retrieval functions | |
943 | // ---------------------------------------------------------------------------- | |
944 | ||
945 | // get error code from syste | |
946 | unsigned long wxSysErrorCode() | |
947 | { | |
0fb67cd1 VZ |
948 | #ifdef __WXMSW__ |
949 | #ifdef __WIN32__ | |
950 | return ::GetLastError(); | |
951 | #else //WIN16 | |
952 | // TODO what to do on Windows 3.1? | |
953 | return 0; | |
954 | #endif //WIN16/32 | |
955 | #else //Unix | |
c801d85f | 956 | return errno; |
0fb67cd1 | 957 | #endif //Win/Unix |
c801d85f KB |
958 | } |
959 | ||
960 | // get error message from system | |
50920146 | 961 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
c801d85f | 962 | { |
0fb67cd1 VZ |
963 | if ( nErrCode == 0 ) |
964 | nErrCode = wxSysErrorCode(); | |
965 | ||
966 | #ifdef __WXMSW__ | |
967 | #ifdef __WIN32__ | |
50920146 | 968 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
0fb67cd1 VZ |
969 | |
970 | // get error message from system | |
971 | LPVOID lpMsgBuf; | |
972 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
973 | NULL, nErrCode, | |
974 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
975 | (LPTSTR)&lpMsgBuf, | |
976 | 0, NULL); | |
977 | ||
978 | // copy it to our buffer and free memory | |
50920146 OK |
979 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); |
980 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = _T('\0'); | |
0fb67cd1 VZ |
981 | LocalFree(lpMsgBuf); |
982 | ||
983 | // returned string is capitalized and ended with '\r\n' - bad | |
50920146 OK |
984 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); |
985 | size_t len = wxStrlen(s_szBuf); | |
0fb67cd1 | 986 | if ( len > 0 ) { |
9ef3052c | 987 | // truncate string |
50920146 OK |
988 | if ( s_szBuf[len - 2] == _T('\r') ) |
989 | s_szBuf[len - 2] = _T('\0'); | |
0fb67cd1 VZ |
990 | } |
991 | ||
992 | return s_szBuf; | |
993 | #else //Win16 | |
994 | // TODO | |
995 | return NULL; | |
996 | #endif // Win16/32 | |
997 | #else // Unix | |
50920146 OK |
998 | #if wxUSE_UNICODE |
999 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
dcf924a3 | 1000 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); |
50920146 OK |
1001 | return s_szBuf; |
1002 | #else | |
9ef3052c | 1003 | return strerror(nErrCode); |
50920146 | 1004 | #endif |
0fb67cd1 | 1005 | #endif // Win/Unix |
c801d85f KB |
1006 | } |
1007 | ||
1008 | // ---------------------------------------------------------------------------- | |
1009 | // debug helper | |
1010 | // ---------------------------------------------------------------------------- | |
1011 | ||
b2aef89b | 1012 | #ifdef __WXDEBUG__ |
c801d85f | 1013 | |
0fb67cd1 | 1014 | // break into the debugger |
7502ba29 VZ |
1015 | void Trap() |
1016 | { | |
0fb67cd1 | 1017 | #ifdef __WXMSW__ |
7502ba29 | 1018 | DebugBreak(); |
0fb67cd1 VZ |
1019 | #elif defined(__WXMAC__) |
1020 | #if __powerc | |
17dff81c | 1021 | Debugger(); |
0fb67cd1 | 1022 | #else |
17dff81c | 1023 | SysBreak(); |
0fb67cd1 VZ |
1024 | #endif |
1025 | #elif defined(__UNIX__) | |
7502ba29 | 1026 | raise(SIGTRAP); |
0fb67cd1 VZ |
1027 | #else |
1028 | // TODO | |
1029 | #endif // Win/Unix | |
7502ba29 VZ |
1030 | } |
1031 | ||
c801d85f | 1032 | // this function is called when an assert fails |
6f9eb452 | 1033 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) |
c801d85f | 1034 | { |
0fb67cd1 VZ |
1035 | // this variable can be set to true to suppress "assert failure" messages |
1036 | static bool s_bNoAsserts = FALSE; | |
1037 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
7502ba29 | 1038 | |
0fb67cd1 VZ |
1039 | if ( s_bInAssert ) { |
1040 | // He-e-e-e-elp!! we're trapped in endless loop | |
1041 | Trap(); | |
c085e333 | 1042 | |
0fb67cd1 | 1043 | s_bInAssert = FALSE; |
1e8a4bc2 | 1044 | |
0fb67cd1 VZ |
1045 | return; |
1046 | } | |
7502ba29 | 1047 | |
0fb67cd1 | 1048 | s_bInAssert = TRUE; |
c801d85f | 1049 | |
50920146 | 1050 | wxChar szBuf[LOG_BUFFER_SIZE]; |
c263077e | 1051 | |
0fb67cd1 VZ |
1052 | // make life easier for people using VC++ IDE: clicking on the message |
1053 | // will take us immediately to the place of the failed assert | |
3f4a0c5b | 1054 | #ifdef __VISUALC__ |
84fff0b3 | 1055 | wxSprintf(szBuf, _T("%s(%d): assert failed"), szFile, nLine); |
c263077e | 1056 | #else // !VC++ |
0fb67cd1 | 1057 | // make the error message more clear for all the others |
50920146 | 1058 | wxSprintf(szBuf, _T("Assert failed in file %s at line %d"), szFile, nLine); |
c263077e VZ |
1059 | #endif // VC/!VC |
1060 | ||
0fb67cd1 | 1061 | if ( szMsg != NULL ) { |
50920146 OK |
1062 | wxStrcat(szBuf, _T(": ")); |
1063 | wxStrcat(szBuf, szMsg); | |
0fb67cd1 VZ |
1064 | } |
1065 | else { | |
50920146 | 1066 | wxStrcat(szBuf, _T(".")); |
0fb67cd1 | 1067 | } |
c801d85f | 1068 | |
0fb67cd1 VZ |
1069 | if ( !s_bNoAsserts ) { |
1070 | // send it to the normal log destination | |
1071 | wxLogDebug(szBuf); | |
7502ba29 | 1072 | |
0fb67cd1 VZ |
1073 | #if wxUSE_NOGUI |
1074 | Trap(); | |
1075 | #else | |
1076 | // this message is intentionally not translated - it is for | |
1077 | // developpers only | |
50920146 | 1078 | wxStrcat(szBuf, _T("\nDo you want to stop the program?" |
0fb67cd1 | 1079 | "\nYou can also choose [Cancel] to suppress " |
50920146 | 1080 | "further warnings.")); |
0fb67cd1 VZ |
1081 | |
1082 | switch ( wxMessageBox(szBuf, _("Debug"), | |
1083 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
1084 | case wxYES: | |
1085 | Trap(); | |
1086 | break; | |
1087 | ||
1088 | case wxCANCEL: | |
1089 | s_bNoAsserts = TRUE; | |
1090 | break; | |
1091 | ||
1092 | //case wxNO: nothing to do | |
1093 | } | |
1094 | #endif // USE_NOGUI | |
1095 | } | |
1096 | ||
1097 | s_bInAssert = FALSE; | |
c801d85f KB |
1098 | } |
1099 | ||
b2aef89b | 1100 | #endif //WXDEBUG |
c801d85f | 1101 |