]>
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 | // ---------------------------------------------------------------------------- | |
dd85fc6b | 19 | |
c801d85f KB |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "log.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | // wxWindows | |
32 | #ifndef WX_PRECOMP | |
e90c1d2a VZ |
33 | #include "wx/string.h" |
34 | #include "wx/intl.h" | |
35 | #include "wx/app.h" | |
36 | ||
37 | #if wxUSE_GUI | |
38 | #include "wx/window.h" | |
39 | #ifdef __WXMSW__ | |
40 | #include "wx/msw/private.h" | |
41 | #endif | |
42 | #include "wx/msgdlg.h" | |
43 | #endif | |
9ef3052c | 44 | #endif //WX_PRECOMP |
c801d85f | 45 | |
dbf3cd7a RR |
46 | #include "wx/file.h" |
47 | #include "wx/textfile.h" | |
48 | #include "wx/utils.h" | |
a324a7bc | 49 | #include "wx/wxchar.h" |
dbf3cd7a | 50 | #include "wx/log.h" |
b568d04f | 51 | #include "wx/thread.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__ |
b568d04f | 59 | #include "wx/msw/private.h" // includes windows.h for OutputDebugString |
3078c3a6 VZ |
60 | #else //Unix |
61 | #include <signal.h> | |
62 | #endif //Win/Unix | |
c801d85f KB |
63 | |
64 | // ---------------------------------------------------------------------------- | |
65 | // non member functions | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | // define this to enable wrapping of log messages | |
69 | //#define LOG_PRETTY_WRAP | |
70 | ||
9ef3052c | 71 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
72 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
73 | #endif | |
74 | ||
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
b568d04f | 80 | // globals |
c801d85f KB |
81 | // ---------------------------------------------------------------------------- |
82 | ||
83 | // log functions can't allocate memory (LogError("out of memory...") should | |
84 | // work!), so we use a static buffer for all log messages | |
85 | #define LOG_BUFFER_SIZE (4096) | |
86 | ||
b568d04f | 87 | // static buffer for error messages |
50920146 | 88 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; |
c801d85f | 89 | |
b568d04f VZ |
90 | #if wxUSE_THREADS |
91 | ||
92 | // the critical section protecting the static buffer | |
93 | static wxCriticalSection gs_csLogBuf; | |
94 | ||
95 | #endif // wxUSE_THREADS | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // implementation of Log functions | |
99 | // | |
100 | // NB: unfortunately we need all these distinct functions, we can't make them | |
101 | // macros and not all compilers inline vararg functions. | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
c801d85f | 104 | // generic log function |
50920146 | 105 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) |
c801d85f | 106 | { |
9ef3052c | 107 | if ( wxLog::GetActiveTarget() != NULL ) { |
b568d04f VZ |
108 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
109 | ||
9ef3052c | 110 | va_list argptr; |
7502ba29 | 111 | va_start(argptr, szFormat); |
378b05f7 | 112 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
113 | va_end(argptr); |
114 | ||
0fb67cd1 | 115 | wxLog::OnLog(level, s_szBuf, time(NULL)); |
9ef3052c | 116 | } |
c801d85f KB |
117 | } |
118 | ||
119 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
50920146 | 120 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
121 | { \ |
122 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
b568d04f VZ |
123 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
124 | \ | |
c801d85f | 125 | va_list argptr; \ |
7502ba29 | 126 | va_start(argptr, szFormat); \ |
378b05f7 | 127 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ |
c801d85f KB |
128 | va_end(argptr); \ |
129 | \ | |
0fb67cd1 | 130 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
131 | } \ |
132 | } | |
133 | ||
134 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
135 | IMPLEMENT_LOG_FUNCTION(Error) | |
136 | IMPLEMENT_LOG_FUNCTION(Warning) | |
137 | IMPLEMENT_LOG_FUNCTION(Message) | |
138 | IMPLEMENT_LOG_FUNCTION(Info) | |
139 | IMPLEMENT_LOG_FUNCTION(Status) | |
140 | ||
9ef3052c | 141 | // same as info, but only if 'verbose' mode is on |
50920146 | 142 | void wxLogVerbose(const wxChar *szFormat, ...) |
9ef3052c VZ |
143 | { |
144 | wxLog *pLog = wxLog::GetActiveTarget(); | |
145 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
b568d04f VZ |
146 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
147 | ||
9ef3052c | 148 | va_list argptr; |
7502ba29 | 149 | va_start(argptr, szFormat); |
378b05f7 | 150 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
151 | va_end(argptr); |
152 | ||
0fb67cd1 | 153 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); |
9ef3052c VZ |
154 | } |
155 | } | |
156 | ||
157 | // debug functions | |
b2aef89b | 158 | #ifdef __WXDEBUG__ |
9ef3052c | 159 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
50920146 | 160 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
161 | { \ |
162 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
b568d04f VZ |
163 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
164 | \ | |
c801d85f KB |
165 | va_list argptr; \ |
166 | va_start(argptr, szFormat); \ | |
378b05f7 | 167 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ |
c801d85f KB |
168 | va_end(argptr); \ |
169 | \ | |
0fb67cd1 | 170 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
171 | } \ |
172 | } | |
173 | ||
50920146 | 174 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) |
0fb67cd1 VZ |
175 | { |
176 | wxLog *pLog = wxLog::GetActiveTarget(); | |
177 | ||
178 | if ( pLog != NULL && wxLog::IsAllowedTraceMask(mask) ) { | |
b568d04f VZ |
179 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
180 | ||
0fb67cd1 VZ |
181 | va_list argptr; |
182 | va_start(argptr, szFormat); | |
378b05f7 | 183 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
0fb67cd1 VZ |
184 | va_end(argptr); |
185 | ||
186 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
187 | } | |
188 | } | |
189 | ||
50920146 | 190 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) |
9ef3052c VZ |
191 | { |
192 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 193 | |
9ef3052c VZ |
194 | // we check that all of mask bits are set in the current mask, so |
195 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
196 | // if both bits are set. | |
d93f63db | 197 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { |
b568d04f VZ |
198 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
199 | ||
9ef3052c VZ |
200 | va_list argptr; |
201 | va_start(argptr, szFormat); | |
378b05f7 | 202 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
9ef3052c VZ |
203 | va_end(argptr); |
204 | ||
0fb67cd1 | 205 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
9ef3052c VZ |
206 | } |
207 | } | |
208 | ||
209 | #else // release | |
210 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
211 | #endif | |
212 | ||
213 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
214 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
215 | ||
216 | // wxLogSysError: one uses the last error code, for other you must give it | |
217 | // explicitly | |
218 | ||
219 | // common part of both wxLogSysError | |
220 | void wxLogSysErrorHelper(long lErrCode) | |
c801d85f | 221 | { |
50920146 | 222 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; |
378b05f7 VZ |
223 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), |
224 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
50920146 | 225 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); |
c801d85f | 226 | |
0fb67cd1 | 227 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); |
9ef3052c | 228 | } |
c801d85f | 229 | |
50920146 | 230 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) |
9ef3052c | 231 | { |
b568d04f VZ |
232 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
233 | ||
0fb67cd1 VZ |
234 | va_list argptr; |
235 | va_start(argptr, szFormat); | |
378b05f7 | 236 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); |
0fb67cd1 | 237 | va_end(argptr); |
9ef3052c | 238 | |
0fb67cd1 | 239 | wxLogSysErrorHelper(wxSysErrorCode()); |
c801d85f KB |
240 | } |
241 | ||
50920146 | 242 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) |
c801d85f | 243 | { |
b568d04f VZ |
244 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
245 | ||
0fb67cd1 VZ |
246 | va_list argptr; |
247 | va_start(argptr, szFormat); | |
378b05f7 | 248 | wxVsnprintf(s_szBuf, WXSIZEOF(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 | ||
0fb67cd1 VZ |
279 | // ask the application to create a log target for us |
280 | if ( wxTheApp != NULL ) | |
281 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
282 | else | |
283 | ms_pLogger = new wxLogStderr; | |
0fb67cd1 VZ |
284 | |
285 | s_bInGetActiveTarget = FALSE; | |
286 | ||
287 | // do nothing if it fails - what can we do? | |
288 | } | |
275bf4c1 | 289 | } |
c801d85f | 290 | |
0fb67cd1 | 291 | return ms_pLogger; |
c801d85f KB |
292 | } |
293 | ||
c085e333 | 294 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 295 | { |
0fb67cd1 VZ |
296 | if ( ms_pLogger != NULL ) { |
297 | // flush the old messages before changing because otherwise they might | |
298 | // get lost later if this target is not restored | |
299 | ms_pLogger->Flush(); | |
300 | } | |
c801d85f | 301 | |
0fb67cd1 VZ |
302 | wxLog *pOldLogger = ms_pLogger; |
303 | ms_pLogger = pLogger; | |
c085e333 | 304 | |
0fb67cd1 | 305 | return pOldLogger; |
c801d85f KB |
306 | } |
307 | ||
0fb67cd1 | 308 | void wxLog::RemoveTraceMask(const wxString& str) |
c801d85f | 309 | { |
0fb67cd1 VZ |
310 | int index = ms_aTraceMasks.Index(str); |
311 | if ( index != wxNOT_FOUND ) | |
312 | ms_aTraceMasks.Remove((size_t)index); | |
313 | } | |
c801d85f | 314 | |
d2e1ef19 VZ |
315 | void wxLog::TimeStamp(wxString *str) |
316 | { | |
317 | if ( ms_timestamp ) | |
318 | { | |
319 | wxChar buf[256]; | |
320 | time_t timeNow; | |
321 | (void)time(&timeNow); | |
c49245f8 | 322 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); |
d2e1ef19 VZ |
323 | |
324 | str->Empty(); | |
223d09f6 | 325 | *str << buf << wxT(": "); |
d2e1ef19 VZ |
326 | } |
327 | } | |
328 | ||
50920146 | 329 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
0fb67cd1 | 330 | { |
0fb67cd1 VZ |
331 | switch ( level ) { |
332 | case wxLOG_FatalError: | |
786855a1 | 333 | DoLogString(wxString(_("Fatal error: ")) + szString, t); |
0fb67cd1 VZ |
334 | DoLogString(_("Program aborted."), t); |
335 | Flush(); | |
336 | abort(); | |
337 | break; | |
338 | ||
339 | case wxLOG_Error: | |
786855a1 | 340 | DoLogString(wxString(_("Error: ")) + szString, t); |
0fb67cd1 VZ |
341 | break; |
342 | ||
343 | case wxLOG_Warning: | |
786855a1 | 344 | DoLogString(wxString(_("Warning: ")) + szString, t); |
0fb67cd1 VZ |
345 | break; |
346 | ||
347 | case wxLOG_Info: | |
0fb67cd1 | 348 | if ( GetVerbose() ) |
37278984 | 349 | case wxLOG_Message: |
87a1e308 | 350 | case wxLOG_Status: |
786855a1 VZ |
351 | default: // log unknown log levels too |
352 | DoLogString(szString, t); | |
0fb67cd1 VZ |
353 | break; |
354 | ||
355 | case wxLOG_Trace: | |
356 | case wxLOG_Debug: | |
357 | #ifdef __WXDEBUG__ | |
358 | DoLogString(szString, t); | |
359 | #endif | |
0fb67cd1 | 360 | break; |
0fb67cd1 | 361 | } |
c801d85f KB |
362 | } |
363 | ||
74e3313b | 364 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) |
c801d85f | 365 | { |
223d09f6 | 366 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); |
c801d85f KB |
367 | } |
368 | ||
369 | void wxLog::Flush() | |
370 | { | |
0fb67cd1 | 371 | // do nothing |
c801d85f KB |
372 | } |
373 | ||
374 | // ---------------------------------------------------------------------------- | |
375 | // wxLogStderr class implementation | |
376 | // ---------------------------------------------------------------------------- | |
377 | ||
378 | wxLogStderr::wxLogStderr(FILE *fp) | |
379 | { | |
0fb67cd1 VZ |
380 | if ( fp == NULL ) |
381 | m_fp = stderr; | |
382 | else | |
383 | m_fp = fp; | |
c801d85f KB |
384 | } |
385 | ||
74e3313b | 386 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 387 | { |
d2e1ef19 VZ |
388 | wxString str; |
389 | TimeStamp(&str); | |
b568d04f | 390 | str << szString; |
1e8a4bc2 | 391 | |
50920146 | 392 | fputs(str.mb_str(), m_fp); |
b568d04f | 393 | fputc(_T('\n'), m_fp); |
0fb67cd1 | 394 | fflush(m_fp); |
1e8a4bc2 | 395 | |
378b05f7 | 396 | // under Windows, programs usually don't have stderr at all, so show the |
b568d04f VZ |
397 | // messages also under debugger - unless it's a console program |
398 | #if defined(__WXMSW__) && wxUSE_GUI | |
399 | OutputDebugString(str + wxT("\r\n")); | |
1e8a4bc2 | 400 | #endif // MSW |
37e2cb08 SC |
401 | #if defined(__WXMAC__) && wxUSE_GUI |
402 | debugstr(str + wxT("\r\n")); | |
403 | #endif // MSW | |
c801d85f KB |
404 | } |
405 | ||
406 | // ---------------------------------------------------------------------------- | |
407 | // wxLogStream implementation | |
408 | // ---------------------------------------------------------------------------- | |
409 | ||
4bf78aae | 410 | #if wxUSE_STD_IOSTREAM |
c801d85f KB |
411 | wxLogStream::wxLogStream(ostream *ostr) |
412 | { | |
0fb67cd1 VZ |
413 | if ( ostr == NULL ) |
414 | m_ostr = &cerr; | |
415 | else | |
416 | m_ostr = ostr; | |
c801d85f KB |
417 | } |
418 | ||
74e3313b | 419 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 420 | { |
e90c1d2a | 421 | (*m_ostr) << wxConvertWX2MB(szString) << endl; |
c801d85f | 422 | } |
0fb67cd1 | 423 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 424 | |
c801d85f KB |
425 | // ============================================================================ |
426 | // Global functions/variables | |
427 | // ============================================================================ | |
428 | ||
429 | // ---------------------------------------------------------------------------- | |
430 | // static variables | |
431 | // ---------------------------------------------------------------------------- | |
0fb67cd1 VZ |
432 | |
433 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
434 | bool wxLog::ms_doLog = TRUE; | |
435 | bool wxLog::ms_bAutoCreate = TRUE; | |
d2e1ef19 | 436 | |
9f83044f VZ |
437 | #if wxUSE_GUI |
438 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
439 | #else | |
440 | const wxChar *wxLog::ms_timestamp = NULL; // save space | |
441 | #endif | |
d2e1ef19 | 442 | |
0fb67cd1 VZ |
443 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
444 | wxArrayString wxLog::ms_aTraceMasks; | |
c801d85f KB |
445 | |
446 | // ---------------------------------------------------------------------------- | |
447 | // stdout error logging helper | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | // helper function: wraps the message and justifies it under given position | |
451 | // (looks more pretty on the terminal). Also adds newline at the end. | |
452 | // | |
0fb67cd1 VZ |
453 | // TODO this is now disabled until I find a portable way of determining the |
454 | // terminal window size (ok, I found it but does anybody really cares?) | |
455 | #ifdef LOG_PRETTY_WRAP | |
c801d85f KB |
456 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
457 | { | |
0fb67cd1 VZ |
458 | size_t nMax = 80; // FIXME |
459 | size_t nStart = strlen(pszPrefix); | |
460 | fputs(pszPrefix, f); | |
461 | ||
462 | size_t n; | |
463 | while ( *psz != '\0' ) { | |
464 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
465 | putc(*psz++, f); | |
466 | ||
467 | // wrapped? | |
468 | if ( *psz != '\0' ) { | |
469 | /*putc('\n', f);*/ | |
470 | for ( n = 0; n < nStart; n++ ) | |
471 | putc(' ', f); | |
472 | ||
473 | // as we wrapped, squeeze all white space | |
474 | while ( isspace(*psz) ) | |
475 | psz++; | |
476 | } | |
c801d85f | 477 | } |
c801d85f | 478 | |
0fb67cd1 | 479 | putc('\n', f); |
c801d85f KB |
480 | } |
481 | #endif //LOG_PRETTY_WRAP | |
482 | ||
483 | // ---------------------------------------------------------------------------- | |
484 | // error code/error message retrieval functions | |
485 | // ---------------------------------------------------------------------------- | |
486 | ||
487 | // get error code from syste | |
488 | unsigned long wxSysErrorCode() | |
489 | { | |
0fb67cd1 VZ |
490 | #ifdef __WXMSW__ |
491 | #ifdef __WIN32__ | |
492 | return ::GetLastError(); | |
493 | #else //WIN16 | |
494 | // TODO what to do on Windows 3.1? | |
495 | return 0; | |
496 | #endif //WIN16/32 | |
497 | #else //Unix | |
c801d85f | 498 | return errno; |
0fb67cd1 | 499 | #endif //Win/Unix |
c801d85f KB |
500 | } |
501 | ||
502 | // get error message from system | |
50920146 | 503 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
c801d85f | 504 | { |
0fb67cd1 VZ |
505 | if ( nErrCode == 0 ) |
506 | nErrCode = wxSysErrorCode(); | |
507 | ||
508 | #ifdef __WXMSW__ | |
509 | #ifdef __WIN32__ | |
50920146 | 510 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
0fb67cd1 VZ |
511 | |
512 | // get error message from system | |
513 | LPVOID lpMsgBuf; | |
514 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
515 | NULL, nErrCode, | |
516 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
517 | (LPTSTR)&lpMsgBuf, | |
518 | 0, NULL); | |
519 | ||
520 | // copy it to our buffer and free memory | |
50920146 | 521 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); |
223d09f6 | 522 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); |
0fb67cd1 VZ |
523 | LocalFree(lpMsgBuf); |
524 | ||
525 | // returned string is capitalized and ended with '\r\n' - bad | |
50920146 OK |
526 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); |
527 | size_t len = wxStrlen(s_szBuf); | |
0fb67cd1 | 528 | if ( len > 0 ) { |
9ef3052c | 529 | // truncate string |
223d09f6 KB |
530 | if ( s_szBuf[len - 2] == wxT('\r') ) |
531 | s_szBuf[len - 2] = wxT('\0'); | |
0fb67cd1 VZ |
532 | } |
533 | ||
534 | return s_szBuf; | |
535 | #else //Win16 | |
536 | // TODO | |
537 | return NULL; | |
538 | #endif // Win16/32 | |
539 | #else // Unix | |
50920146 OK |
540 | #if wxUSE_UNICODE |
541 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
dcf924a3 | 542 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); |
50920146 OK |
543 | return s_szBuf; |
544 | #else | |
13111b2a | 545 | return strerror((int)nErrCode); |
50920146 | 546 | #endif |
0fb67cd1 | 547 | #endif // Win/Unix |
c801d85f KB |
548 | } |
549 | ||
550 | // ---------------------------------------------------------------------------- | |
551 | // debug helper | |
552 | // ---------------------------------------------------------------------------- | |
553 | ||
b2aef89b | 554 | #ifdef __WXDEBUG__ |
c801d85f | 555 | |
0fb67cd1 | 556 | // break into the debugger |
7502ba29 VZ |
557 | void Trap() |
558 | { | |
0fb67cd1 | 559 | #ifdef __WXMSW__ |
7502ba29 | 560 | DebugBreak(); |
0fb67cd1 VZ |
561 | #elif defined(__WXMAC__) |
562 | #if __powerc | |
17dff81c | 563 | Debugger(); |
0fb67cd1 | 564 | #else |
17dff81c | 565 | SysBreak(); |
0fb67cd1 VZ |
566 | #endif |
567 | #elif defined(__UNIX__) | |
7502ba29 | 568 | raise(SIGTRAP); |
0fb67cd1 VZ |
569 | #else |
570 | // TODO | |
571 | #endif // Win/Unix | |
7502ba29 VZ |
572 | } |
573 | ||
c801d85f | 574 | // this function is called when an assert fails |
6f9eb452 | 575 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) |
c801d85f | 576 | { |
0fb67cd1 VZ |
577 | // this variable can be set to true to suppress "assert failure" messages |
578 | static bool s_bNoAsserts = FALSE; | |
579 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
7502ba29 | 580 | |
0fb67cd1 VZ |
581 | if ( s_bInAssert ) { |
582 | // He-e-e-e-elp!! we're trapped in endless loop | |
583 | Trap(); | |
c085e333 | 584 | |
0fb67cd1 | 585 | s_bInAssert = FALSE; |
1e8a4bc2 | 586 | |
0fb67cd1 VZ |
587 | return; |
588 | } | |
7502ba29 | 589 | |
0fb67cd1 | 590 | s_bInAssert = TRUE; |
c801d85f | 591 | |
50920146 | 592 | wxChar szBuf[LOG_BUFFER_SIZE]; |
c263077e | 593 | |
0fb67cd1 VZ |
594 | // make life easier for people using VC++ IDE: clicking on the message |
595 | // will take us immediately to the place of the failed assert | |
378b05f7 | 596 | wxSnprintf(szBuf, WXSIZEOF(szBuf), |
3f4a0c5b | 597 | #ifdef __VISUALC__ |
378b05f7 | 598 | wxT("%s(%d): assert failed"), |
c263077e | 599 | #else // !VC++ |
0fb67cd1 | 600 | // make the error message more clear for all the others |
378b05f7 | 601 | wxT("Assert failed in file %s at line %d"), |
c263077e | 602 | #endif // VC/!VC |
378b05f7 | 603 | szFile, nLine); |
c263077e | 604 | |
0fb67cd1 | 605 | if ( szMsg != NULL ) { |
223d09f6 | 606 | wxStrcat(szBuf, wxT(": ")); |
50920146 | 607 | wxStrcat(szBuf, szMsg); |
0fb67cd1 VZ |
608 | } |
609 | else { | |
223d09f6 | 610 | wxStrcat(szBuf, wxT(".")); |
0fb67cd1 | 611 | } |
c801d85f | 612 | |
0fb67cd1 VZ |
613 | if ( !s_bNoAsserts ) { |
614 | // send it to the normal log destination | |
615 | wxLogDebug(szBuf); | |
7502ba29 | 616 | |
b76b015e | 617 | #if wxUSE_GUI || defined(__WXMSW__) |
0fb67cd1 VZ |
618 | // this message is intentionally not translated - it is for |
619 | // developpers only | |
223d09f6 | 620 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?" |
b76b015e VZ |
621 | "\nYou can also choose [Cancel] to suppress " |
622 | "further warnings.")); | |
0fb67cd1 | 623 | |
b76b015e VZ |
624 | #if wxUSE_GUI |
625 | switch ( wxMessageBox(szBuf, "Debug", | |
626 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
0fb67cd1 VZ |
627 | case wxYES: |
628 | Trap(); | |
629 | break; | |
630 | ||
631 | case wxCANCEL: | |
632 | s_bNoAsserts = TRUE; | |
633 | break; | |
634 | ||
b76b015e VZ |
635 | //case wxNO: nothing to do |
636 | } | |
637 | #else // !GUI, but MSW | |
638 | switch ( ::MessageBox(NULL, szBuf, "Debug", | |
639 | MB_YESNOCANCEL | MB_ICONSTOP ) ) { | |
640 | case IDYES: | |
641 | Trap(); | |
642 | break; | |
643 | ||
644 | case IDCANCEL: | |
645 | s_bNoAsserts = TRUE; | |
646 | break; | |
647 | ||
648 | //case IDNO: nothing to do | |
0fb67cd1 | 649 | } |
b76b015e VZ |
650 | #endif // GUI or MSW |
651 | ||
e90c1d2a VZ |
652 | #else // !GUI |
653 | Trap(); | |
654 | #endif // GUI/!GUI | |
0fb67cd1 VZ |
655 | } |
656 | ||
657 | s_bInAssert = FALSE; | |
c801d85f KB |
658 | } |
659 | ||
b2aef89b | 660 | #endif //WXDEBUG |
c801d85f | 661 |