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