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