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