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