]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | |
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 | |
44 | #endif //WX_PRECOMP | |
45 | ||
46 | #include "wx/file.h" | |
47 | #include "wx/textfile.h" | |
48 | #include "wx/utils.h" | |
49 | #include "wx/wxchar.h" | |
50 | #include "wx/log.h" | |
51 | #include "wx/thread.h" | |
52 | ||
53 | #if wxUSE_LOG | |
54 | ||
55 | // other standard headers | |
56 | #include <errno.h> | |
57 | #include <stdlib.h> | |
58 | #include <time.h> | |
59 | ||
60 | #ifdef __WXMSW__ | |
61 | #include "wx/msw/private.h" // includes windows.h for OutputDebugString | |
62 | #else //Unix | |
63 | #include <signal.h> | |
64 | #endif //Win/Unix | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // non member functions | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | // define this to enable wrapping of log messages | |
71 | //#define LOG_PRETTY_WRAP | |
72 | ||
73 | #ifdef LOG_PRETTY_WRAP | |
74 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); | |
75 | #endif | |
76 | ||
77 | // ============================================================================ | |
78 | // implementation | |
79 | // ============================================================================ | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // globals | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | // log functions can't allocate memory (LogError("out of memory...") should | |
86 | // work!), so we use a static buffer for all log messages | |
87 | #define LOG_BUFFER_SIZE (4096) | |
88 | ||
89 | // static buffer for error messages | |
90 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; | |
91 | ||
92 | #if wxUSE_THREADS | |
93 | ||
94 | // the critical section protecting the static buffer | |
95 | static wxCriticalSection gs_csLogBuf; | |
96 | ||
97 | #endif // wxUSE_THREADS | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // implementation of Log functions | |
101 | // | |
102 | // NB: unfortunately we need all these distinct functions, we can't make them | |
103 | // macros and not all compilers inline vararg functions. | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | // generic log function | |
107 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) | |
108 | { | |
109 | if ( wxLog::GetActiveTarget() != NULL ) { | |
110 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
111 | ||
112 | va_list argptr; | |
113 | va_start(argptr, szFormat); | |
114 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
115 | va_end(argptr); | |
116 | ||
117 | wxLog::OnLog(level, s_szBuf, time(NULL)); | |
118 | } | |
119 | } | |
120 | ||
121 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
122 | void wxLog##level(const wxChar *szFormat, ...) \ | |
123 | { \ | |
124 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
125 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ | |
126 | \ | |
127 | va_list argptr; \ | |
128 | va_start(argptr, szFormat); \ | |
129 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ | |
130 | va_end(argptr); \ | |
131 | \ | |
132 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ | |
133 | } \ | |
134 | } | |
135 | ||
136 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
137 | IMPLEMENT_LOG_FUNCTION(Error) | |
138 | IMPLEMENT_LOG_FUNCTION(Warning) | |
139 | IMPLEMENT_LOG_FUNCTION(Message) | |
140 | IMPLEMENT_LOG_FUNCTION(Info) | |
141 | IMPLEMENT_LOG_FUNCTION(Status) | |
142 | ||
143 | // same as info, but only if 'verbose' mode is on | |
144 | void wxLogVerbose(const wxChar *szFormat, ...) | |
145 | { | |
146 | wxLog *pLog = wxLog::GetActiveTarget(); | |
147 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
148 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
149 | ||
150 | va_list argptr; | |
151 | va_start(argptr, szFormat); | |
152 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
153 | va_end(argptr); | |
154 | ||
155 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); | |
156 | } | |
157 | } | |
158 | ||
159 | // debug functions | |
160 | #ifdef __WXDEBUG__ | |
161 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ | |
162 | void wxLog##level(const wxChar *szFormat, ...) \ | |
163 | { \ | |
164 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
165 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ | |
166 | \ | |
167 | va_list argptr; \ | |
168 | va_start(argptr, szFormat); \ | |
169 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \ | |
170 | va_end(argptr); \ | |
171 | \ | |
172 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ | |
173 | } \ | |
174 | } | |
175 | ||
176 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) | |
177 | { | |
178 | wxLog *pLog = wxLog::GetActiveTarget(); | |
179 | ||
180 | if ( pLog != NULL && wxLog::IsAllowedTraceMask(mask) ) { | |
181 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
182 | ||
183 | wxChar *p = s_szBuf; | |
184 | size_t len = WXSIZEOF(s_szBuf); | |
185 | wxStrncpy(s_szBuf, _T("("), len); | |
186 | len -= 1; // strlen("(") | |
187 | p += 1; | |
188 | wxStrncat(p, mask, len); | |
189 | size_t lenMask = wxStrlen(mask); | |
190 | len -= lenMask; | |
191 | p += lenMask; | |
192 | ||
193 | wxStrncat(p, _T(") "), len); | |
194 | len -= 2; | |
195 | p += 2; | |
196 | ||
197 | va_list argptr; | |
198 | va_start(argptr, szFormat); | |
199 | wxVsnprintf(p, len, szFormat, argptr); | |
200 | va_end(argptr); | |
201 | ||
202 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
203 | } | |
204 | } | |
205 | ||
206 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) | |
207 | { | |
208 | wxLog *pLog = wxLog::GetActiveTarget(); | |
209 | ||
210 | // we check that all of mask bits are set in the current mask, so | |
211 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
212 | // if both bits are set. | |
213 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { | |
214 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
215 | ||
216 | va_list argptr; | |
217 | va_start(argptr, szFormat); | |
218 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
219 | va_end(argptr); | |
220 | ||
221 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
222 | } | |
223 | } | |
224 | ||
225 | #else // release | |
226 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
227 | #endif | |
228 | ||
229 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
230 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
231 | ||
232 | // wxLogSysError: one uses the last error code, for other you must give it | |
233 | // explicitly | |
234 | ||
235 | // common part of both wxLogSysError | |
236 | void wxLogSysErrorHelper(long lErrCode) | |
237 | { | |
238 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; | |
239 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), | |
240 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
241 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); | |
242 | ||
243 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); | |
244 | } | |
245 | ||
246 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) | |
247 | { | |
248 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
249 | ||
250 | va_list argptr; | |
251 | va_start(argptr, szFormat); | |
252 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
253 | va_end(argptr); | |
254 | ||
255 | wxLogSysErrorHelper(wxSysErrorCode()); | |
256 | } | |
257 | ||
258 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) | |
259 | { | |
260 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); | |
261 | ||
262 | va_list argptr; | |
263 | va_start(argptr, szFormat); | |
264 | wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); | |
265 | va_end(argptr); | |
266 | ||
267 | wxLogSysErrorHelper(lErrCode); | |
268 | } | |
269 | ||
270 | // ---------------------------------------------------------------------------- | |
271 | // wxLog class implementation | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | wxLog::wxLog() | |
275 | { | |
276 | m_bHasMessages = FALSE; | |
277 | m_bVerbose = FALSE; | |
278 | } | |
279 | ||
280 | wxLog *wxLog::GetActiveTarget() | |
281 | { | |
282 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { | |
283 | // prevent infinite recursion if someone calls wxLogXXX() from | |
284 | // wxApp::CreateLogTarget() | |
285 | static bool s_bInGetActiveTarget = FALSE; | |
286 | if ( !s_bInGetActiveTarget ) { | |
287 | s_bInGetActiveTarget = TRUE; | |
288 | ||
289 | // ask the application to create a log target for us | |
290 | if ( wxTheApp != NULL ) | |
291 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
292 | else | |
293 | ms_pLogger = new wxLogStderr; | |
294 | ||
295 | s_bInGetActiveTarget = FALSE; | |
296 | ||
297 | // do nothing if it fails - what can we do? | |
298 | } | |
299 | } | |
300 | ||
301 | return ms_pLogger; | |
302 | } | |
303 | ||
304 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) | |
305 | { | |
306 | if ( ms_pLogger != NULL ) { | |
307 | // flush the old messages before changing because otherwise they might | |
308 | // get lost later if this target is not restored | |
309 | ms_pLogger->Flush(); | |
310 | } | |
311 | ||
312 | wxLog *pOldLogger = ms_pLogger; | |
313 | ms_pLogger = pLogger; | |
314 | ||
315 | return pOldLogger; | |
316 | } | |
317 | ||
318 | void wxLog::RemoveTraceMask(const wxString& str) | |
319 | { | |
320 | int index = ms_aTraceMasks.Index(str); | |
321 | if ( index != wxNOT_FOUND ) | |
322 | ms_aTraceMasks.Remove((size_t)index); | |
323 | } | |
324 | ||
325 | void wxLog::TimeStamp(wxString *str) | |
326 | { | |
327 | if ( ms_timestamp ) | |
328 | { | |
329 | wxChar buf[256]; | |
330 | time_t timeNow; | |
331 | (void)time(&timeNow); | |
332 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); | |
333 | ||
334 | str->Empty(); | |
335 | *str << buf << wxT(": "); | |
336 | } | |
337 | } | |
338 | ||
339 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
340 | { | |
341 | switch ( level ) { | |
342 | case wxLOG_FatalError: | |
343 | DoLogString(wxString(_("Fatal error: ")) + szString, t); | |
344 | DoLogString(_("Program aborted."), t); | |
345 | Flush(); | |
346 | abort(); | |
347 | break; | |
348 | ||
349 | case wxLOG_Error: | |
350 | DoLogString(wxString(_("Error: ")) + szString, t); | |
351 | break; | |
352 | ||
353 | case wxLOG_Warning: | |
354 | DoLogString(wxString(_("Warning: ")) + szString, t); | |
355 | break; | |
356 | ||
357 | case wxLOG_Info: | |
358 | if ( GetVerbose() ) | |
359 | case wxLOG_Message: | |
360 | case wxLOG_Status: | |
361 | default: // log unknown log levels too | |
362 | DoLogString(szString, t); | |
363 | break; | |
364 | ||
365 | case wxLOG_Trace: | |
366 | case wxLOG_Debug: | |
367 | #ifdef __WXDEBUG__ | |
368 | { | |
369 | wxString msg = level == wxLOG_Trace ? wxT("Trace: ") | |
370 | : wxT("Debug: "); | |
371 | msg << szString; | |
372 | DoLogString(msg, t); | |
373 | } | |
374 | #endif // Debug | |
375 | break; | |
376 | } | |
377 | } | |
378 | ||
379 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) | |
380 | { | |
381 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); | |
382 | } | |
383 | ||
384 | void wxLog::Flush() | |
385 | { | |
386 | // do nothing | |
387 | } | |
388 | ||
389 | // ---------------------------------------------------------------------------- | |
390 | // wxLogStderr class implementation | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
393 | wxLogStderr::wxLogStderr(FILE *fp) | |
394 | { | |
395 | if ( fp == NULL ) | |
396 | m_fp = stderr; | |
397 | else | |
398 | m_fp = fp; | |
399 | } | |
400 | ||
401 | #if defined(__WXMAC__) | |
402 | #define kDebuggerSignature 'MWDB' | |
403 | ||
404 | static Boolean FindProcessBySignature(OSType signature, ProcessInfoRec* info) | |
405 | { | |
406 | OSErr err; | |
407 | ProcessSerialNumber psn; | |
408 | Boolean found = false; | |
409 | psn.highLongOfPSN = 0; | |
410 | psn.lowLongOfPSN = kNoProcess; | |
411 | ||
412 | if (!info) return false; | |
413 | ||
414 | info->processInfoLength = sizeof(ProcessInfoRec); | |
415 | info->processName = NULL; | |
416 | info->processAppSpec = NULL; | |
417 | ||
418 | err = noErr; | |
419 | while (!found && err == noErr) | |
420 | { | |
421 | err = GetNextProcess(&psn); | |
422 | if (err == noErr) | |
423 | { | |
424 | err = GetProcessInformation(&psn, info); | |
425 | found = err == noErr && info->processSignature == signature; | |
426 | } | |
427 | } | |
428 | return found; | |
429 | } | |
430 | ||
431 | pascal Boolean MWDebuggerIsRunning(void) | |
432 | { | |
433 | ProcessInfoRec info; | |
434 | return FindProcessBySignature(kDebuggerSignature, &info); | |
435 | } | |
436 | ||
437 | pascal OSErr AmIBeingMWDebugged(Boolean* result) | |
438 | { | |
439 | OSErr err; | |
440 | ProcessSerialNumber psn; | |
441 | OSType sig = kDebuggerSignature; | |
442 | AppleEvent theAE = {typeNull, NULL}; | |
443 | AppleEvent theReply = {typeNull, NULL}; | |
444 | AEAddressDesc addr = {typeNull, NULL}; | |
445 | DescType actualType; | |
446 | Size actualSize; | |
447 | ||
448 | if (!result) return paramErr; | |
449 | ||
450 | err = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &addr); | |
451 | if (err != noErr) goto exit; | |
452 | ||
453 | err = AECreateAppleEvent('MWDB', 'Dbg?', &addr, | |
454 | kAutoGenerateReturnID, kAnyTransactionID, &theAE); | |
455 | if (err != noErr) goto exit; | |
456 | ||
457 | GetCurrentProcess(&psn); | |
458 | err = AEPutParamPtr(&theAE, keyDirectObject, typeProcessSerialNumber, | |
459 | &psn, sizeof(psn)); | |
460 | if (err != noErr) goto exit; | |
461 | ||
462 | err = AESend(&theAE, &theReply, kAEWaitReply, kAENormalPriority, | |
463 | kAEDefaultTimeout, NULL, NULL); | |
464 | if (err != noErr) goto exit; | |
465 | ||
466 | err = AEGetParamPtr(&theReply, keyAEResult, typeBoolean, &actualType, result, | |
467 | sizeof(Boolean), &actualSize); | |
468 | ||
469 | exit: | |
470 | if (addr.dataHandle) | |
471 | AEDisposeDesc(&addr); | |
472 | if (theAE.dataHandle) | |
473 | AEDisposeDesc(&theAE); | |
474 | if (theReply.dataHandle) | |
475 | AEDisposeDesc(&theReply); | |
476 | ||
477 | return err; | |
478 | } | |
479 | #endif | |
480 | ||
481 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
482 | { | |
483 | wxString str; | |
484 | TimeStamp(&str); | |
485 | str << szString; | |
486 | ||
487 | fputs(str.mb_str(), m_fp); | |
488 | fputc(_T('\n'), m_fp); | |
489 | fflush(m_fp); | |
490 | ||
491 | // under Windows, programs usually don't have stderr at all, so show the | |
492 | // messages also under debugger - unless it's a console program | |
493 | #if defined(__WXMSW__) && wxUSE_GUI | |
494 | str += wxT("\r\n") ; | |
495 | OutputDebugString(str.c_str()); | |
496 | #endif // MSW | |
497 | #if defined(__WXMAC__) && wxUSE_GUI | |
498 | Str255 pstr ; | |
499 | strcpy( (char*) pstr , str.c_str() ) ; | |
500 | strcat( (char*) pstr , ";g" ) ; | |
501 | c2pstr( (char*) pstr ) ; | |
502 | #if __WXDEBUG__ | |
503 | Boolean running = false ; | |
504 | ||
505 | /* | |
506 | if ( MWDebuggerIsRunning() ) | |
507 | { | |
508 | AmIBeingMWDebugged( &running ) ; | |
509 | } | |
510 | */ | |
511 | if (running) | |
512 | { | |
513 | #ifdef __powerc | |
514 | DebugStr(pstr); | |
515 | #else | |
516 | SysBreakStr(pstr); | |
517 | #endif | |
518 | } | |
519 | else | |
520 | #endif | |
521 | { | |
522 | #ifdef __powerc | |
523 | DebugStr(pstr); | |
524 | #else | |
525 | DebugStr(pstr); | |
526 | #endif | |
527 | } | |
528 | #endif // MSW | |
529 | } | |
530 | ||
531 | // ---------------------------------------------------------------------------- | |
532 | // wxLogStream implementation | |
533 | // ---------------------------------------------------------------------------- | |
534 | ||
535 | #if wxUSE_STD_IOSTREAM | |
536 | wxLogStream::wxLogStream(ostream *ostr) | |
537 | { | |
538 | if ( ostr == NULL ) | |
539 | m_ostr = &cerr; | |
540 | else | |
541 | m_ostr = ostr; | |
542 | } | |
543 | ||
544 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
545 | { | |
546 | wxString str; | |
547 | TimeStamp(&str); | |
548 | (*m_ostr) << str << wxConvertWX2MB(szString) << endl; | |
549 | } | |
550 | #endif // wxUSE_STD_IOSTREAM | |
551 | ||
552 | // ============================================================================ | |
553 | // Global functions/variables | |
554 | // ============================================================================ | |
555 | ||
556 | // ---------------------------------------------------------------------------- | |
557 | // static variables | |
558 | // ---------------------------------------------------------------------------- | |
559 | ||
560 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
561 | bool wxLog::ms_doLog = TRUE; | |
562 | bool wxLog::ms_bAutoCreate = TRUE; | |
563 | ||
564 | size_t wxLog::ms_suspendCount = 0; | |
565 | ||
566 | #if wxUSE_GUI | |
567 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date | |
568 | #else | |
569 | const wxChar *wxLog::ms_timestamp = NULL; // save space | |
570 | #endif | |
571 | ||
572 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; | |
573 | wxArrayString wxLog::ms_aTraceMasks; | |
574 | ||
575 | // ---------------------------------------------------------------------------- | |
576 | // stdout error logging helper | |
577 | // ---------------------------------------------------------------------------- | |
578 | ||
579 | // helper function: wraps the message and justifies it under given position | |
580 | // (looks more pretty on the terminal). Also adds newline at the end. | |
581 | // | |
582 | // TODO this is now disabled until I find a portable way of determining the | |
583 | // terminal window size (ok, I found it but does anybody really cares?) | |
584 | #ifdef LOG_PRETTY_WRAP | |
585 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
586 | { | |
587 | size_t nMax = 80; // FIXME | |
588 | size_t nStart = strlen(pszPrefix); | |
589 | fputs(pszPrefix, f); | |
590 | ||
591 | size_t n; | |
592 | while ( *psz != '\0' ) { | |
593 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
594 | putc(*psz++, f); | |
595 | ||
596 | // wrapped? | |
597 | if ( *psz != '\0' ) { | |
598 | /*putc('\n', f);*/ | |
599 | for ( n = 0; n < nStart; n++ ) | |
600 | putc(' ', f); | |
601 | ||
602 | // as we wrapped, squeeze all white space | |
603 | while ( isspace(*psz) ) | |
604 | psz++; | |
605 | } | |
606 | } | |
607 | ||
608 | putc('\n', f); | |
609 | } | |
610 | #endif //LOG_PRETTY_WRAP | |
611 | ||
612 | // ---------------------------------------------------------------------------- | |
613 | // error code/error message retrieval functions | |
614 | // ---------------------------------------------------------------------------- | |
615 | ||
616 | // get error code from syste | |
617 | unsigned long wxSysErrorCode() | |
618 | { | |
619 | #ifdef __WXMSW__ | |
620 | #ifdef __WIN32__ | |
621 | return ::GetLastError(); | |
622 | #else //WIN16 | |
623 | // TODO what to do on Windows 3.1? | |
624 | return 0; | |
625 | #endif //WIN16/32 | |
626 | #else //Unix | |
627 | return errno; | |
628 | #endif //Win/Unix | |
629 | } | |
630 | ||
631 | // get error message from system | |
632 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) | |
633 | { | |
634 | if ( nErrCode == 0 ) | |
635 | nErrCode = wxSysErrorCode(); | |
636 | ||
637 | #ifdef __WXMSW__ | |
638 | #ifdef __WIN32__ | |
639 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
640 | ||
641 | // get error message from system | |
642 | LPVOID lpMsgBuf; | |
643 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
644 | NULL, nErrCode, | |
645 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
646 | (LPTSTR)&lpMsgBuf, | |
647 | 0, NULL); | |
648 | ||
649 | // copy it to our buffer and free memory | |
650 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
651 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); | |
652 | LocalFree(lpMsgBuf); | |
653 | ||
654 | // returned string is capitalized and ended with '\r\n' - bad | |
655 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); | |
656 | size_t len = wxStrlen(s_szBuf); | |
657 | if ( len > 0 ) { | |
658 | // truncate string | |
659 | if ( s_szBuf[len - 2] == wxT('\r') ) | |
660 | s_szBuf[len - 2] = wxT('\0'); | |
661 | } | |
662 | ||
663 | return s_szBuf; | |
664 | #else //Win16 | |
665 | // TODO | |
666 | return NULL; | |
667 | #endif // Win16/32 | |
668 | #else // Unix | |
669 | #if wxUSE_UNICODE | |
670 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
671 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); | |
672 | return s_szBuf; | |
673 | #else | |
674 | return strerror((int)nErrCode); | |
675 | #endif | |
676 | #endif // Win/Unix | |
677 | } | |
678 | ||
679 | // ---------------------------------------------------------------------------- | |
680 | // debug helper | |
681 | // ---------------------------------------------------------------------------- | |
682 | ||
683 | #ifdef __WXDEBUG__ | |
684 | ||
685 | // break into the debugger | |
686 | void Trap() | |
687 | { | |
688 | #ifdef __WXMSW__ | |
689 | DebugBreak(); | |
690 | #elif defined(__WXMAC__) | |
691 | #if __powerc | |
692 | Debugger(); | |
693 | #else | |
694 | SysBreak(); | |
695 | #endif | |
696 | #elif defined(__UNIX__) | |
697 | raise(SIGTRAP); | |
698 | #else | |
699 | // TODO | |
700 | #endif // Win/Unix | |
701 | } | |
702 | ||
703 | // this function is called when an assert fails | |
704 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) | |
705 | { | |
706 | // this variable can be set to true to suppress "assert failure" messages | |
707 | static bool s_bNoAsserts = FALSE; | |
708 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
709 | ||
710 | if ( s_bInAssert ) { | |
711 | // He-e-e-e-elp!! we're trapped in endless loop | |
712 | Trap(); | |
713 | ||
714 | s_bInAssert = FALSE; | |
715 | ||
716 | return; | |
717 | } | |
718 | ||
719 | s_bInAssert = TRUE; | |
720 | ||
721 | wxChar szBuf[LOG_BUFFER_SIZE]; | |
722 | ||
723 | // make life easier for people using VC++ IDE: clicking on the message | |
724 | // will take us immediately to the place of the failed assert | |
725 | wxSnprintf(szBuf, WXSIZEOF(szBuf), | |
726 | #ifdef __VISUALC__ | |
727 | wxT("%s(%d): assert failed"), | |
728 | #else // !VC++ | |
729 | // make the error message more clear for all the others | |
730 | wxT("Assert failed in file %s at line %d"), | |
731 | #endif // VC/!VC | |
732 | szFile, nLine); | |
733 | ||
734 | if ( szMsg != NULL ) { | |
735 | wxStrcat(szBuf, wxT(": ")); | |
736 | wxStrcat(szBuf, szMsg); | |
737 | } | |
738 | else { | |
739 | wxStrcat(szBuf, wxT(".")); | |
740 | } | |
741 | ||
742 | if ( !s_bNoAsserts ) { | |
743 | // send it to the normal log destination | |
744 | wxLogDebug(szBuf); | |
745 | ||
746 | #if wxUSE_GUI || defined(__WXMSW__) | |
747 | // this message is intentionally not translated - it is for | |
748 | // developpers only | |
749 | wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings.")); | |
750 | ||
751 | // use the native message box if available: this is more robust than | |
752 | // using our own | |
753 | #ifdef __WXMSW__ | |
754 | switch ( ::MessageBox(NULL, szBuf, _T("Debug"), | |
755 | MB_YESNOCANCEL | MB_ICONSTOP ) ) { | |
756 | case IDYES: | |
757 | Trap(); | |
758 | break; | |
759 | ||
760 | case IDCANCEL: | |
761 | s_bNoAsserts = TRUE; | |
762 | break; | |
763 | ||
764 | //case IDNO: nothing to do | |
765 | } | |
766 | #else // !MSW | |
767 | switch ( wxMessageBox(szBuf, wxT("Debug"), | |
768 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
769 | case wxYES: | |
770 | Trap(); | |
771 | break; | |
772 | ||
773 | case wxCANCEL: | |
774 | s_bNoAsserts = TRUE; | |
775 | break; | |
776 | ||
777 | //case wxNO: nothing to do | |
778 | } | |
779 | #endif // GUI or MSW | |
780 | ||
781 | #else // !GUI | |
782 | Trap(); | |
783 | #endif // GUI/!GUI | |
784 | } | |
785 | ||
786 | s_bInAssert = FALSE; | |
787 | } | |
788 | ||
789 | #endif //WXDEBUG | |
790 | ||
791 | #endif //wxUSE_LOG |