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