| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/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 licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_LOG |
| 28 | |
| 29 | // wxWidgets |
| 30 | #ifndef WX_PRECOMP |
| 31 | #include "wx/log.h" |
| 32 | #include "wx/app.h" |
| 33 | #include "wx/arrstr.h" |
| 34 | #include "wx/intl.h" |
| 35 | #include "wx/string.h" |
| 36 | #include "wx/utils.h" |
| 37 | #endif //WX_PRECOMP |
| 38 | |
| 39 | #include "wx/apptrait.h" |
| 40 | #include "wx/file.h" |
| 41 | #include "wx/msgout.h" |
| 42 | #include "wx/textfile.h" |
| 43 | #include "wx/thread.h" |
| 44 | #include "wx/wxchar.h" |
| 45 | |
| 46 | // other standard headers |
| 47 | #ifndef __WXWINCE__ |
| 48 | #include <errno.h> |
| 49 | #endif |
| 50 | |
| 51 | #include <stdlib.h> |
| 52 | |
| 53 | #ifndef __WXWINCE__ |
| 54 | #include <time.h> |
| 55 | #else |
| 56 | #include "wx/msw/wince/time.h" |
| 57 | #endif |
| 58 | |
| 59 | #if defined(__WINDOWS__) |
| 60 | #include "wx/msw/private.h" // includes windows.h |
| 61 | #endif |
| 62 | |
| 63 | // ---------------------------------------------------------------------------- |
| 64 | // non member functions |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | |
| 67 | // define this to enable wrapping of log messages |
| 68 | //#define LOG_PRETTY_WRAP |
| 69 | |
| 70 | #ifdef LOG_PRETTY_WRAP |
| 71 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
| 72 | #endif |
| 73 | |
| 74 | // ============================================================================ |
| 75 | // implementation |
| 76 | // ============================================================================ |
| 77 | |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | // globals |
| 80 | // ---------------------------------------------------------------------------- |
| 81 | |
| 82 | // log functions can't allocate memory (LogError("out of memory...") should |
| 83 | // work!), so we use a static buffer for all log messages |
| 84 | #define LOG_BUFFER_SIZE (4096) |
| 85 | |
| 86 | // static buffer for error messages |
| 87 | static wxChar s_szBufStatic[LOG_BUFFER_SIZE]; |
| 88 | |
| 89 | static wxChar *s_szBuf = s_szBufStatic; |
| 90 | static size_t s_szBufSize = WXSIZEOF( s_szBufStatic ); |
| 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 | // wrapper for wxVsnprintf(s_szBuf) which always NULL-terminates it |
| 107 | static inline void PrintfInLogBuf(const wxChar *szFormat, va_list argptr) |
| 108 | { |
| 109 | if ( wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr) < 0 ) |
| 110 | { |
| 111 | // must NUL-terminate it manually |
| 112 | s_szBuf[s_szBufSize - 1] = _T('\0'); |
| 113 | } |
| 114 | //else: NUL-terminated by vsnprintf() |
| 115 | } |
| 116 | |
| 117 | // generic log function |
| 118 | void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr) |
| 119 | { |
| 120 | if ( wxLog::IsEnabled() ) { |
| 121 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 122 | |
| 123 | PrintfInLogBuf(szFormat, argptr); |
| 124 | |
| 125 | wxLog::OnLog(level, s_szBuf, time(NULL)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) |
| 130 | { |
| 131 | va_list argptr; |
| 132 | va_start(argptr, szFormat); |
| 133 | wxVLogGeneric(level, szFormat, argptr); |
| 134 | va_end(argptr); |
| 135 | } |
| 136 | |
| 137 | #define IMPLEMENT_LOG_FUNCTION(level) \ |
| 138 | void wxVLog##level(const wxChar *szFormat, va_list argptr) \ |
| 139 | { \ |
| 140 | if ( wxLog::IsEnabled() ) { \ |
| 141 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
| 142 | \ |
| 143 | PrintfInLogBuf(szFormat, argptr); \ |
| 144 | \ |
| 145 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
| 146 | } \ |
| 147 | } \ |
| 148 | \ |
| 149 | void wxLog##level(const wxChar *szFormat, ...) \ |
| 150 | { \ |
| 151 | va_list argptr; \ |
| 152 | va_start(argptr, szFormat); \ |
| 153 | wxVLog##level(szFormat, argptr); \ |
| 154 | va_end(argptr); \ |
| 155 | } |
| 156 | |
| 157 | IMPLEMENT_LOG_FUNCTION(Error) |
| 158 | IMPLEMENT_LOG_FUNCTION(Warning) |
| 159 | IMPLEMENT_LOG_FUNCTION(Message) |
| 160 | IMPLEMENT_LOG_FUNCTION(Info) |
| 161 | IMPLEMENT_LOG_FUNCTION(Status) |
| 162 | |
| 163 | void wxSafeShowMessage(const wxString& title, const wxString& text) |
| 164 | { |
| 165 | #ifdef __WINDOWS__ |
| 166 | ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP); |
| 167 | #else |
| 168 | wxFprintf(stderr, _T("%s: %s\n"), title.c_str(), text.c_str()); |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | // fatal errors can't be suppressed nor handled by the custom log target and |
| 173 | // always terminate the program |
| 174 | void wxVLogFatalError(const wxChar *szFormat, va_list argptr) |
| 175 | { |
| 176 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); |
| 177 | |
| 178 | wxSafeShowMessage(_T("Fatal Error"), s_szBuf); |
| 179 | |
| 180 | #ifdef __WXWINCE__ |
| 181 | ExitThread(3); |
| 182 | #else |
| 183 | abort(); |
| 184 | #endif |
| 185 | } |
| 186 | |
| 187 | void wxLogFatalError(const wxChar *szFormat, ...) |
| 188 | { |
| 189 | va_list argptr; |
| 190 | va_start(argptr, szFormat); |
| 191 | wxVLogFatalError(szFormat, argptr); |
| 192 | |
| 193 | // some compilers warn about unreachable code and it shouldn't matter |
| 194 | // for the others anyhow... |
| 195 | //va_end(argptr); |
| 196 | } |
| 197 | |
| 198 | // same as info, but only if 'verbose' mode is on |
| 199 | void wxVLogVerbose(const wxChar *szFormat, va_list argptr) |
| 200 | { |
| 201 | if ( wxLog::IsEnabled() ) { |
| 202 | if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) { |
| 203 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 204 | |
| 205 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); |
| 206 | |
| 207 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void wxLogVerbose(const wxChar *szFormat, ...) |
| 213 | { |
| 214 | va_list argptr; |
| 215 | va_start(argptr, szFormat); |
| 216 | wxVLogVerbose(szFormat, argptr); |
| 217 | va_end(argptr); |
| 218 | } |
| 219 | |
| 220 | // debug functions |
| 221 | #ifdef __WXDEBUG__ |
| 222 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
| 223 | void wxVLog##level(const wxChar *szFormat, va_list argptr) \ |
| 224 | { \ |
| 225 | if ( wxLog::IsEnabled() ) { \ |
| 226 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \ |
| 227 | \ |
| 228 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); \ |
| 229 | \ |
| 230 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
| 231 | } \ |
| 232 | } \ |
| 233 | void wxLog##level(const wxChar *szFormat, ...) \ |
| 234 | { \ |
| 235 | va_list argptr; \ |
| 236 | va_start(argptr, szFormat); \ |
| 237 | wxVLog##level(szFormat, argptr); \ |
| 238 | va_end(argptr); \ |
| 239 | } |
| 240 | |
| 241 | void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr) |
| 242 | { |
| 243 | if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) { |
| 244 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 245 | |
| 246 | wxChar *p = s_szBuf; |
| 247 | size_t len = s_szBufSize; |
| 248 | wxStrncpy(s_szBuf, _T("("), len); |
| 249 | len -= 1; // strlen("(") |
| 250 | p += 1; |
| 251 | wxStrncat(p, mask, len); |
| 252 | size_t lenMask = wxStrlen(mask); |
| 253 | len -= lenMask; |
| 254 | p += lenMask; |
| 255 | |
| 256 | wxStrncat(p, _T(") "), len); |
| 257 | len -= 2; |
| 258 | p += 2; |
| 259 | |
| 260 | wxVsnprintf(p, len, szFormat, argptr); |
| 261 | |
| 262 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) |
| 267 | { |
| 268 | va_list argptr; |
| 269 | va_start(argptr, szFormat); |
| 270 | wxVLogTrace(mask, szFormat, argptr); |
| 271 | va_end(argptr); |
| 272 | } |
| 273 | |
| 274 | void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr) |
| 275 | { |
| 276 | // we check that all of mask bits are set in the current mask, so |
| 277 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something |
| 278 | // if both bits are set. |
| 279 | if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) { |
| 280 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 281 | |
| 282 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); |
| 283 | |
| 284 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) |
| 289 | { |
| 290 | va_list argptr; |
| 291 | va_start(argptr, szFormat); |
| 292 | wxVLogTrace(mask, szFormat, argptr); |
| 293 | va_end(argptr); |
| 294 | } |
| 295 | |
| 296 | #else // release |
| 297 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) |
| 298 | #endif |
| 299 | |
| 300 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) |
| 301 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) |
| 302 | |
| 303 | // wxLogSysError: one uses the last error code, for other you must give it |
| 304 | // explicitly |
| 305 | |
| 306 | // common part of both wxLogSysError |
| 307 | void wxLogSysErrorHelper(long lErrCode) |
| 308 | { |
| 309 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; |
| 310 | wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg), |
| 311 | _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); |
| 312 | wxStrncat(s_szBuf, szErrMsg, s_szBufSize - wxStrlen(s_szBuf)); |
| 313 | |
| 314 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); |
| 315 | } |
| 316 | |
| 317 | void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr) |
| 318 | { |
| 319 | if ( wxLog::IsEnabled() ) { |
| 320 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 321 | |
| 322 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); |
| 323 | |
| 324 | wxLogSysErrorHelper(wxSysErrorCode()); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) |
| 329 | { |
| 330 | va_list argptr; |
| 331 | va_start(argptr, szFormat); |
| 332 | wxVLogSysError(szFormat, argptr); |
| 333 | va_end(argptr); |
| 334 | } |
| 335 | |
| 336 | void WXDLLEXPORT wxVLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr) |
| 337 | { |
| 338 | if ( wxLog::IsEnabled() ) { |
| 339 | wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); |
| 340 | |
| 341 | wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); |
| 342 | |
| 343 | wxLogSysErrorHelper(lErrCode); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) |
| 348 | { |
| 349 | va_list argptr; |
| 350 | va_start(argptr, szFormat); |
| 351 | wxVLogSysError(lErrCode, szFormat, argptr); |
| 352 | va_end(argptr); |
| 353 | } |
| 354 | |
| 355 | // ---------------------------------------------------------------------------- |
| 356 | // wxLog class implementation |
| 357 | // ---------------------------------------------------------------------------- |
| 358 | |
| 359 | wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size) |
| 360 | { |
| 361 | wxChar *oldbuf = s_szBuf; |
| 362 | |
| 363 | if( buf == 0 ) |
| 364 | { |
| 365 | s_szBuf = s_szBufStatic; |
| 366 | s_szBufSize = WXSIZEOF( s_szBufStatic ); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | s_szBuf = buf; |
| 371 | s_szBufSize = size; |
| 372 | } |
| 373 | |
| 374 | return (oldbuf == s_szBufStatic ) ? 0 : oldbuf; |
| 375 | } |
| 376 | |
| 377 | wxLog *wxLog::GetActiveTarget() |
| 378 | { |
| 379 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
| 380 | // prevent infinite recursion if someone calls wxLogXXX() from |
| 381 | // wxApp::CreateLogTarget() |
| 382 | static bool s_bInGetActiveTarget = false; |
| 383 | if ( !s_bInGetActiveTarget ) { |
| 384 | s_bInGetActiveTarget = true; |
| 385 | |
| 386 | // ask the application to create a log target for us |
| 387 | if ( wxTheApp != NULL ) |
| 388 | ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget(); |
| 389 | else |
| 390 | ms_pLogger = new wxLogStderr; |
| 391 | |
| 392 | s_bInGetActiveTarget = false; |
| 393 | |
| 394 | // do nothing if it fails - what can we do? |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return ms_pLogger; |
| 399 | } |
| 400 | |
| 401 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
| 402 | { |
| 403 | if ( ms_pLogger != NULL ) { |
| 404 | // flush the old messages before changing because otherwise they might |
| 405 | // get lost later if this target is not restored |
| 406 | ms_pLogger->Flush(); |
| 407 | } |
| 408 | |
| 409 | wxLog *pOldLogger = ms_pLogger; |
| 410 | ms_pLogger = pLogger; |
| 411 | |
| 412 | return pOldLogger; |
| 413 | } |
| 414 | |
| 415 | void wxLog::DontCreateOnDemand() |
| 416 | { |
| 417 | ms_bAutoCreate = false; |
| 418 | |
| 419 | // this is usually called at the end of the program and we assume that it |
| 420 | // is *always* called at the end - so we free memory here to avoid false |
| 421 | // memory leak reports from wxWin memory tracking code |
| 422 | ClearTraceMasks(); |
| 423 | } |
| 424 | |
| 425 | void wxLog::RemoveTraceMask(const wxString& str) |
| 426 | { |
| 427 | int index = ms_aTraceMasks.Index(str); |
| 428 | if ( index != wxNOT_FOUND ) |
| 429 | ms_aTraceMasks.RemoveAt((size_t)index); |
| 430 | } |
| 431 | |
| 432 | void wxLog::ClearTraceMasks() |
| 433 | { |
| 434 | ms_aTraceMasks.Clear(); |
| 435 | } |
| 436 | |
| 437 | void wxLog::TimeStamp(wxString *str) |
| 438 | { |
| 439 | if ( ms_timestamp ) |
| 440 | { |
| 441 | wxChar buf[256]; |
| 442 | time_t timeNow; |
| 443 | (void)time(&timeNow); |
| 444 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); |
| 445 | |
| 446 | str->Empty(); |
| 447 | *str << buf << wxT(": "); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
| 452 | { |
| 453 | switch ( level ) { |
| 454 | case wxLOG_FatalError: |
| 455 | DoLogString(wxString(_("Fatal error: ")) + szString, t); |
| 456 | DoLogString(_("Program aborted."), t); |
| 457 | Flush(); |
| 458 | #ifdef __WXWINCE__ |
| 459 | ExitThread(3); |
| 460 | #else |
| 461 | abort(); |
| 462 | #endif |
| 463 | break; |
| 464 | |
| 465 | case wxLOG_Error: |
| 466 | DoLogString(wxString(_("Error: ")) + szString, t); |
| 467 | break; |
| 468 | |
| 469 | case wxLOG_Warning: |
| 470 | DoLogString(wxString(_("Warning: ")) + szString, t); |
| 471 | break; |
| 472 | |
| 473 | case wxLOG_Info: |
| 474 | if ( GetVerbose() ) |
| 475 | case wxLOG_Message: |
| 476 | case wxLOG_Status: |
| 477 | default: // log unknown log levels too |
| 478 | DoLogString(szString, t); |
| 479 | break; |
| 480 | |
| 481 | case wxLOG_Trace: |
| 482 | case wxLOG_Debug: |
| 483 | #ifdef __WXDEBUG__ |
| 484 | { |
| 485 | wxString msg = level == wxLOG_Trace ? wxT("Trace: ") |
| 486 | : wxT("Debug: "); |
| 487 | msg << szString; |
| 488 | DoLogString(msg, t); |
| 489 | } |
| 490 | #endif // Debug |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) |
| 496 | { |
| 497 | wxFAIL_MSG(wxT("DoLogString must be overriden if it's called.")); |
| 498 | } |
| 499 | |
| 500 | void wxLog::Flush() |
| 501 | { |
| 502 | // nothing to do here |
| 503 | } |
| 504 | |
| 505 | /*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask) |
| 506 | { |
| 507 | for ( wxArrayString::iterator it = ms_aTraceMasks.begin(), |
| 508 | en = ms_aTraceMasks.end(); |
| 509 | it != en; ++it ) |
| 510 | if ( *it == mask) |
| 511 | return true; |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | // ---------------------------------------------------------------------------- |
| 516 | // wxLogBuffer implementation |
| 517 | // ---------------------------------------------------------------------------- |
| 518 | |
| 519 | void wxLogBuffer::Flush() |
| 520 | { |
| 521 | if ( !m_str.empty() ) |
| 522 | { |
| 523 | wxMessageOutputBest out; |
| 524 | out.Printf(_T("%s"), m_str.c_str()); |
| 525 | m_str.clear(); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void wxLogBuffer::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
| 530 | { |
| 531 | switch ( level ) |
| 532 | { |
| 533 | case wxLOG_Trace: |
| 534 | case wxLOG_Debug: |
| 535 | #ifdef __WXDEBUG__ |
| 536 | // don't put debug messages in the buffer, we don't want to show |
| 537 | // them to the user in a msg box, log them immediately |
| 538 | { |
| 539 | wxString str; |
| 540 | TimeStamp(&str); |
| 541 | str += szString; |
| 542 | |
| 543 | wxMessageOutputDebug dbgout; |
| 544 | dbgout.Printf(_T("%s\n"), str.c_str()); |
| 545 | } |
| 546 | #endif // __WXDEBUG__ |
| 547 | break; |
| 548 | |
| 549 | default: |
| 550 | wxLog::DoLog(level, szString, t); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void wxLogBuffer::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
| 555 | { |
| 556 | m_str << szString << _T("\n"); |
| 557 | } |
| 558 | |
| 559 | // ---------------------------------------------------------------------------- |
| 560 | // wxLogStderr class implementation |
| 561 | // ---------------------------------------------------------------------------- |
| 562 | |
| 563 | wxLogStderr::wxLogStderr(FILE *fp) |
| 564 | { |
| 565 | if ( fp == NULL ) |
| 566 | m_fp = stderr; |
| 567 | else |
| 568 | m_fp = fp; |
| 569 | } |
| 570 | |
| 571 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
| 572 | { |
| 573 | wxString str; |
| 574 | TimeStamp(&str); |
| 575 | str << szString; |
| 576 | |
| 577 | fputs(str.mb_str(), m_fp); |
| 578 | fputc(_T('\n'), m_fp); |
| 579 | fflush(m_fp); |
| 580 | |
| 581 | // under GUI systems such as Windows or Mac, programs usually don't have |
| 582 | // stderr at all, so show the messages also somewhere else, typically in |
| 583 | // the debugger window so that they go at least somewhere instead of being |
| 584 | // simply lost |
| 585 | if ( m_fp == stderr ) |
| 586 | { |
| 587 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; |
| 588 | if ( traits && !traits->HasStderr() ) |
| 589 | { |
| 590 | wxMessageOutputDebug dbgout; |
| 591 | dbgout.Printf(_T("%s\n"), str.c_str()); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // ---------------------------------------------------------------------------- |
| 597 | // wxLogStream implementation |
| 598 | // ---------------------------------------------------------------------------- |
| 599 | |
| 600 | #if wxUSE_STD_IOSTREAM |
| 601 | #include "wx/ioswrap.h" |
| 602 | wxLogStream::wxLogStream(wxSTD ostream *ostr) |
| 603 | { |
| 604 | if ( ostr == NULL ) |
| 605 | m_ostr = &wxSTD cerr; |
| 606 | else |
| 607 | m_ostr = ostr; |
| 608 | } |
| 609 | |
| 610 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
| 611 | { |
| 612 | wxString str; |
| 613 | TimeStamp(&str); |
| 614 | (*m_ostr) << wxConvertWX2MB(str) << wxConvertWX2MB(szString) << wxSTD endl; |
| 615 | } |
| 616 | #endif // wxUSE_STD_IOSTREAM |
| 617 | |
| 618 | // ---------------------------------------------------------------------------- |
| 619 | // wxLogChain |
| 620 | // ---------------------------------------------------------------------------- |
| 621 | |
| 622 | wxLogChain::wxLogChain(wxLog *logger) |
| 623 | { |
| 624 | m_bPassMessages = true; |
| 625 | |
| 626 | m_logNew = logger; |
| 627 | m_logOld = wxLog::SetActiveTarget(this); |
| 628 | } |
| 629 | |
| 630 | wxLogChain::~wxLogChain() |
| 631 | { |
| 632 | delete m_logOld; |
| 633 | |
| 634 | if ( m_logNew != this ) |
| 635 | delete m_logNew; |
| 636 | } |
| 637 | |
| 638 | void wxLogChain::SetLog(wxLog *logger) |
| 639 | { |
| 640 | if ( m_logNew != this ) |
| 641 | delete m_logNew; |
| 642 | |
| 643 | m_logNew = logger; |
| 644 | } |
| 645 | |
| 646 | void wxLogChain::Flush() |
| 647 | { |
| 648 | if ( m_logOld ) |
| 649 | m_logOld->Flush(); |
| 650 | |
| 651 | // be careful to avoid infinite recursion |
| 652 | if ( m_logNew && m_logNew != this ) |
| 653 | m_logNew->Flush(); |
| 654 | } |
| 655 | |
| 656 | void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
| 657 | { |
| 658 | // let the previous logger show it |
| 659 | if ( m_logOld && IsPassingMessages() ) |
| 660 | { |
| 661 | // bogus cast just to access protected DoLog |
| 662 | ((wxLogChain *)m_logOld)->DoLog(level, szString, t); |
| 663 | } |
| 664 | |
| 665 | if ( m_logNew && m_logNew != this ) |
| 666 | { |
| 667 | // as above... |
| 668 | ((wxLogChain *)m_logNew)->DoLog(level, szString, t); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // ---------------------------------------------------------------------------- |
| 673 | // wxLogPassThrough |
| 674 | // ---------------------------------------------------------------------------- |
| 675 | |
| 676 | #ifdef __VISUALC__ |
| 677 | // "'this' : used in base member initializer list" - so what? |
| 678 | #pragma warning(disable:4355) |
| 679 | #endif // VC++ |
| 680 | |
| 681 | wxLogPassThrough::wxLogPassThrough() |
| 682 | : wxLogChain(this) |
| 683 | { |
| 684 | } |
| 685 | |
| 686 | #ifdef __VISUALC__ |
| 687 | #pragma warning(default:4355) |
| 688 | #endif // VC++ |
| 689 | |
| 690 | // ============================================================================ |
| 691 | // Global functions/variables |
| 692 | // ============================================================================ |
| 693 | |
| 694 | // ---------------------------------------------------------------------------- |
| 695 | // static variables |
| 696 | // ---------------------------------------------------------------------------- |
| 697 | |
| 698 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; |
| 699 | bool wxLog::ms_doLog = true; |
| 700 | bool wxLog::ms_bAutoCreate = true; |
| 701 | bool wxLog::ms_bVerbose = false; |
| 702 | |
| 703 | wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default |
| 704 | |
| 705 | size_t wxLog::ms_suspendCount = 0; |
| 706 | |
| 707 | const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date |
| 708 | |
| 709 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
| 710 | wxArrayString wxLog::ms_aTraceMasks; |
| 711 | |
| 712 | // ---------------------------------------------------------------------------- |
| 713 | // stdout error logging helper |
| 714 | // ---------------------------------------------------------------------------- |
| 715 | |
| 716 | // helper function: wraps the message and justifies it under given position |
| 717 | // (looks more pretty on the terminal). Also adds newline at the end. |
| 718 | // |
| 719 | // TODO this is now disabled until I find a portable way of determining the |
| 720 | // terminal window size (ok, I found it but does anybody really cares?) |
| 721 | #ifdef LOG_PRETTY_WRAP |
| 722 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
| 723 | { |
| 724 | size_t nMax = 80; // FIXME |
| 725 | size_t nStart = strlen(pszPrefix); |
| 726 | fputs(pszPrefix, f); |
| 727 | |
| 728 | size_t n; |
| 729 | while ( *psz != '\0' ) { |
| 730 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) |
| 731 | putc(*psz++, f); |
| 732 | |
| 733 | // wrapped? |
| 734 | if ( *psz != '\0' ) { |
| 735 | /*putc('\n', f);*/ |
| 736 | for ( n = 0; n < nStart; n++ ) |
| 737 | putc(' ', f); |
| 738 | |
| 739 | // as we wrapped, squeeze all white space |
| 740 | while ( isspace(*psz) ) |
| 741 | psz++; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | putc('\n', f); |
| 746 | } |
| 747 | #endif //LOG_PRETTY_WRAP |
| 748 | |
| 749 | // ---------------------------------------------------------------------------- |
| 750 | // error code/error message retrieval functions |
| 751 | // ---------------------------------------------------------------------------- |
| 752 | |
| 753 | // get error code from syste |
| 754 | unsigned long wxSysErrorCode() |
| 755 | { |
| 756 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
| 757 | return ::GetLastError(); |
| 758 | #else //Unix |
| 759 | return errno; |
| 760 | #endif //Win/Unix |
| 761 | } |
| 762 | |
| 763 | // get error message from system |
| 764 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
| 765 | { |
| 766 | if ( nErrCode == 0 ) |
| 767 | nErrCode = wxSysErrorCode(); |
| 768 | |
| 769 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
| 770 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
| 771 | |
| 772 | // get error message from system |
| 773 | LPVOID lpMsgBuf; |
| 774 | if ( ::FormatMessage |
| 775 | ( |
| 776 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 777 | NULL, |
| 778 | nErrCode, |
| 779 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 780 | (LPTSTR)&lpMsgBuf, |
| 781 | 0, |
| 782 | NULL |
| 783 | ) == 0 ) |
| 784 | { |
| 785 | // if this happens, something is seriously wrong, so don't use _() here |
| 786 | // for safety |
| 787 | wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode); |
| 788 | return s_szBuf; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | // copy it to our buffer and free memory |
| 793 | // Crashes on SmartPhone (FIXME) |
| 794 | #if !defined(__SMARTPHONE__) /* of WinCE */ |
| 795 | if( lpMsgBuf != 0 ) |
| 796 | { |
| 797 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); |
| 798 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0'); |
| 799 | |
| 800 | LocalFree(lpMsgBuf); |
| 801 | |
| 802 | // returned string is capitalized and ended with '\r\n' - bad |
| 803 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); |
| 804 | size_t len = wxStrlen(s_szBuf); |
| 805 | if ( len > 0 ) { |
| 806 | // truncate string |
| 807 | if ( s_szBuf[len - 2] == wxT('\r') ) |
| 808 | s_szBuf[len - 2] = wxT('\0'); |
| 809 | } |
| 810 | } |
| 811 | else |
| 812 | #endif |
| 813 | { |
| 814 | s_szBuf[0] = wxT('\0'); |
| 815 | } |
| 816 | |
| 817 | return s_szBuf; |
| 818 | #else // Unix-WXMICROWIN |
| 819 | #if wxUSE_UNICODE |
| 820 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
| 821 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); |
| 822 | return s_szBuf; |
| 823 | #else |
| 824 | return strerror((int)nErrCode); |
| 825 | #endif |
| 826 | #endif // Win/Unix-WXMICROWIN |
| 827 | } |
| 828 | |
| 829 | #endif // wxUSE_LOG |