]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
Add information about the log message generation location to wxLog.
[wxWidgets.git] / src / common / log.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/common/log.cpp
c801d85f
KB
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>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
dd85fc6b 19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
e2478fde 24 #pragma hdrstop
c801d85f
KB
25#endif
26
e2478fde
VZ
27#if wxUSE_LOG
28
77ffb593 29// wxWidgets
c801d85f 30#ifndef WX_PRECOMP
e4db172a 31 #include "wx/log.h"
e90c1d2a 32 #include "wx/app.h"
df5168c4 33 #include "wx/arrstr.h"
e2478fde
VZ
34 #include "wx/intl.h"
35 #include "wx/string.h"
de6185e2 36 #include "wx/utils.h"
9ef3052c 37#endif //WX_PRECOMP
c801d85f 38
e2478fde 39#include "wx/apptrait.h"
7b2d1c74 40#include "wx/datetime.h"
e2478fde 41#include "wx/file.h"
e2478fde
VZ
42#include "wx/msgout.h"
43#include "wx/textfile.h"
44#include "wx/thread.h"
3a3dde0d 45#include "wx/crt.h"
f94dfb38 46
c801d85f 47// other standard headers
1c193821 48#ifndef __WXWINCE__
e2478fde 49#include <errno.h>
1c193821
JS
50#endif
51
e2478fde 52#include <stdlib.h>
1c193821 53
9b4da627 54#ifndef __WXPALMOS5__
1c193821 55#ifndef __WXWINCE__
e2478fde 56#include <time.h>
1c193821
JS
57#else
58#include "wx/msw/wince/time.h"
59#endif
9b4da627 60#endif /* ! __WXPALMOS5__ */
31907d03 61
9cce3be7
VS
62#if defined(__WINDOWS__)
63 #include "wx/msw/private.h" // includes windows.h
64#endif
65
766aecab
VZ
66#if wxUSE_THREADS
67
68// define static functions providing access to the critical sections we use
69// instead of just using static critical section variables as log functions may
70// be used during static initialization and while this is certainly not
71// advisable it's still better to not crash (as we'd do if we used a yet
72// uninitialized critical section) if it happens
73
74static inline wxCriticalSection& GetTraceMaskCS()
75{
76 static wxCriticalSection s_csTrace;
77
78 return s_csTrace;
79}
80
81static inline wxCriticalSection& GetPreviousLogCS()
82{
83 static wxCriticalSection s_csPrev;
84
85 return s_csPrev;
86}
87
88#endif // wxUSE_THREADS
89
c801d85f
KB
90// ----------------------------------------------------------------------------
91// non member functions
92// ----------------------------------------------------------------------------
93
94// define this to enable wrapping of log messages
95//#define LOG_PRETTY_WRAP
96
9ef3052c 97#ifdef LOG_PRETTY_WRAP
c801d85f
KB
98 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
99#endif
100
bc73d5ae
VZ
101// ----------------------------------------------------------------------------
102// module globals
103// ----------------------------------------------------------------------------
104
105namespace
106{
107
108// this struct is used to store information about the previous log message used
109// by OnLog() to (optionally) avoid logging multiple copies of the same message
110struct PreviousLogInfo
111{
112 PreviousLogInfo()
113 {
114 numRepeated = 0;
115 }
116
117
118 // previous message itself
119 wxString msg;
120
121 // its level
122 wxLogLevel level;
123
124 // other information about it
125 wxLogRecordInfo info;
126
127 // the number of times it was already repeated
128 unsigned numRepeated;
129};
130
131PreviousLogInfo gs_prevLog;
132
133} // anonymous namespace
134
c801d85f
KB
135// ============================================================================
136// implementation
137// ============================================================================
138
b568d04f 139// ----------------------------------------------------------------------------
af588446 140// helper global functions
b568d04f
VZ
141// ----------------------------------------------------------------------------
142
c11d62a6
VZ
143void wxSafeShowMessage(const wxString& title, const wxString& text)
144{
145#ifdef __WINDOWS__
dc874fb4 146 ::MessageBox(NULL, text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP);
c11d62a6 147#else
a0516656 148 wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str());
65f06384 149 fflush(stderr);
c11d62a6
VZ
150#endif
151}
152
c801d85f
KB
153// ----------------------------------------------------------------------------
154// wxLog class implementation
155// ----------------------------------------------------------------------------
156
dbaa16de 157unsigned wxLog::LogLastRepeatIfNeeded()
f9837791 158{
766aecab 159 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
a2d38265 160
dbaa16de
VZ
161 return LogLastRepeatIfNeededUnlocked();
162}
163
164unsigned wxLog::LogLastRepeatIfNeededUnlocked()
165{
bc73d5ae 166 const unsigned count = gs_prevLog.numRepeated;
0250efd6 167
bc73d5ae 168 if ( gs_prevLog.numRepeated )
f9837791
VZ
169 {
170 wxString msg;
459b97df 171#if wxUSE_INTL
f9837791
VZ
172 msg.Printf(wxPLURAL("The previous message repeated once.",
173 "The previous message repeated %lu times.",
bc73d5ae
VZ
174 gs_prevLog.numRepeated),
175 gs_prevLog.numRepeated);
459b97df 176#else
a0516656 177 msg.Printf(wxS("The previous message was repeated %lu times."),
bc73d5ae 178 gs_prevLog.numRepeated);
459b97df 179#endif
bc73d5ae
VZ
180 gs_prevLog.numRepeated = 0;
181 gs_prevLog.msg.clear();
182 DoLogRecord(gs_prevLog.level, msg, gs_prevLog.info);
f9837791 183 }
0250efd6
VZ
184
185 return count;
f9837791
VZ
186}
187
188wxLog::~wxLog()
189{
6f6b48f1
VZ
190 // Flush() must be called before destroying the object as otherwise some
191 // messages could be lost
bc73d5ae 192 if ( gs_prevLog.numRepeated )
6f6b48f1
VZ
193 {
194 wxMessageOutputDebug().Printf
195 (
196 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
bc73d5ae
VZ
197 gs_prevLog.msg,
198 gs_prevLog.numRepeated
6f6b48f1
VZ
199 );
200 }
f9837791
VZ
201}
202
bc73d5ae
VZ
203// ----------------------------------------------------------------------------
204// wxLog logging functions
205// ----------------------------------------------------------------------------
206
207/* static */
208void
209wxLog::OnLog(wxLogLevel level, const wxString& msg, time_t t)
210{
211 wxLogRecordInfo info;
212 info.timestamp = t;
213#if wxUSE_THREADS
214 info.threadId = wxThread::GetCurrentId();
215#endif // wxUSE_THREADS
216
217 OnLog(level, msg, info);
218}
219
f9837791 220/* static */
bc73d5ae
VZ
221void
222wxLog::OnLog(wxLogLevel level,
223 const wxString& msg,
224 const wxLogRecordInfo& info)
f9837791 225{
af588446
VZ
226 // fatal errors can't be suppressed nor handled by the custom log target
227 // and always terminate the program
228 if ( level == wxLOG_FatalError )
f9837791 229 {
af588446 230 wxSafeShowMessage(wxS("Fatal Error"), msg);
a2d38265 231
af588446
VZ
232#ifdef __WXWINCE__
233 ExitThread(3);
234#else
235 abort();
236#endif
237 }
2064113c 238
af588446
VZ
239 wxLog *pLogger = GetActiveTarget();
240 if ( !pLogger )
241 return;
2064113c 242
af588446
VZ
243 if ( GetRepetitionCounting() )
244 {
245 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
2064113c 246
af588446
VZ
247 if ( msg == gs_prevLog.msg )
248 {
249 gs_prevLog.numRepeated++;
2064113c 250
af588446
VZ
251 // nothing else to do, in particular, don't log the
252 // repeated message
253 return;
f9837791 254 }
af588446
VZ
255
256 pLogger->LogLastRepeatIfNeededUnlocked();
257
258 // reset repetition counter for a new message
259 gs_prevLog.msg = msg;
260 gs_prevLog.level = level;
261 gs_prevLog.info = info;
f9837791 262 }
af588446
VZ
263
264 // handle extra data which may be passed to us by wxLogXXX()
265 wxString prefix, suffix;
266 wxUIntPtr num;
267 if ( info.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE, &num) )
268 {
269 long err = static_cast<long>(num);
270 if ( !err )
271 err = wxSysErrorCode();
272
273 suffix.Printf(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
274 }
275
276#if wxUSE_LOG_TRACE
277 wxString str;
278 if ( level == wxLOG_Trace && info.GetStrValue(wxLOG_KEY_TRACE_MASK, &str) )
279 {
280 prefix = "(" + str + ") ";
281 }
282#endif // wxUSE_LOG_TRACE
283
284 pLogger->DoLogRecord(level, prefix + msg + suffix, info);
f9837791
VZ
285}
286
bc73d5ae
VZ
287void wxLog::DoLogRecord(wxLogLevel level,
288 const wxString& msg,
289 const wxLogRecordInfo& info)
04662def 290{
bc73d5ae
VZ
291#if WXWIN_COMPATIBILITY_2_8
292 // call the old DoLog() to ensure that existing custom log classes still
293 // work
294 //
295 // as the user code could have defined it as either taking "const char *"
296 // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice
297 // but to call both of them
298 DoLog(level, (const char*)msg.mb_str(), info.timestamp);
299 DoLog(level, (const wchar_t*)msg.wc_str(), info.timestamp);
300#endif // WXWIN_COMPATIBILITY_2_8
301
302
303 // TODO: it would be better to extract message formatting in a separate
304 // wxLogFormatter class but for now we hard code formatting here
305
306 wxString prefix;
307
308 // don't time stamp debug messages under MSW as debug viewers usually
309 // already have an option to do it
310#ifdef __WXMSW__
311 if ( level != wxLOG_Debug && level != wxLOG_Trace )
312#endif // __WXMSW__
313 TimeStamp(&prefix);
314
315 // TODO: use the other wxLogRecordInfo fields
316
317 switch ( level )
318 {
319 case wxLOG_Error:
320 prefix += _("Error: ");
321 break;
322
323 case wxLOG_Warning:
324 prefix += _("Warning: ");
325 break;
326
327 // don't prepend "debug/trace" prefix under MSW as it goes to the debug
328 // window anyhow and so can't be confused with something else
329#ifndef __WXMSW__
330 case wxLOG_Debug:
331 // this prefix (as well as the one below) is intentionally not
332 // translated as nobody translates debug messages anyhow
333 prefix += "Debug: ";
334 break;
335
336 case wxLOG_Trace:
337 prefix += "Trace: ";
338 break;
339#endif // !__WXMSW__
340 }
341
342 DoLogTextAtLevel(level, prefix + msg);
04662def
RL
343}
344
bc73d5ae
VZ
345void wxLog::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
346{
347 // we know about debug messages (because using wxMessageOutputDebug is the
348 // right thing to do in 99% of all cases and also for compatibility) but
349 // anything else needs to be handled in the derived class
350 if ( level == wxLOG_Debug || level == wxLOG_Trace )
351 {
352 wxMessageOutputDebug().Output(msg + wxS('\n'));
353 }
354 else
355 {
356 DoLogText(msg);
357 }
358}
dcc40ba5 359
bc73d5ae
VZ
360void wxLog::DoLogText(const wxString& WXUNUSED(msg))
361{
362 // in 2.8-compatible build the derived class might override DoLog() or
363 // DoLogString() instead so we can't have this assert there
364#if !WXWIN_COMPATIBILITY_2_8
365 wxFAIL_MSG( "must be overridden if it is called" );
366#endif // WXWIN_COMPATIBILITY_2_8
367}
5d88a6b5 368
fbbc4e8f
VZ
369#if WXWIN_COMPATIBILITY_2_8
370
bc73d5ae 371void wxLog::DoLog(wxLogLevel WXUNUSED(level), const char *szString, time_t t)
5d88a6b5 372{
bc73d5ae 373 DoLogString(szString, t);
5d88a6b5
VZ
374}
375
bc73d5ae 376void wxLog::DoLog(wxLogLevel WXUNUSED(level), const wchar_t *wzString, time_t t)
5d88a6b5 377{
bc73d5ae 378 DoLogString(wzString, t);
5d88a6b5
VZ
379}
380
fbbc4e8f
VZ
381#endif // WXWIN_COMPATIBILITY_2_8
382
bc73d5ae
VZ
383// ----------------------------------------------------------------------------
384// wxLog active target management
385// ----------------------------------------------------------------------------
5d88a6b5 386
9ec05cc9
VZ
387wxLog *wxLog::GetActiveTarget()
388{
0fb67cd1
VZ
389 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
390 // prevent infinite recursion if someone calls wxLogXXX() from
391 // wxApp::CreateLogTarget()
f644b28c 392 static bool s_bInGetActiveTarget = false;
0fb67cd1 393 if ( !s_bInGetActiveTarget ) {
f644b28c 394 s_bInGetActiveTarget = true;
0fb67cd1 395
0fb67cd1
VZ
396 // ask the application to create a log target for us
397 if ( wxTheApp != NULL )
dc6d5e38 398 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
0fb67cd1
VZ
399 else
400 ms_pLogger = new wxLogStderr;
0fb67cd1 401
f644b28c 402 s_bInGetActiveTarget = false;
0fb67cd1
VZ
403
404 // do nothing if it fails - what can we do?
405 }
275bf4c1 406 }
c801d85f 407
0fb67cd1 408 return ms_pLogger;
c801d85f
KB
409}
410
c085e333 411wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 412{
0fb67cd1
VZ
413 if ( ms_pLogger != NULL ) {
414 // flush the old messages before changing because otherwise they might
415 // get lost later if this target is not restored
416 ms_pLogger->Flush();
417 }
c801d85f 418
0fb67cd1
VZ
419 wxLog *pOldLogger = ms_pLogger;
420 ms_pLogger = pLogger;
c085e333 421
0fb67cd1 422 return pOldLogger;
c801d85f
KB
423}
424
36bd6902
VZ
425void wxLog::DontCreateOnDemand()
426{
f644b28c 427 ms_bAutoCreate = false;
36bd6902
VZ
428
429 // this is usually called at the end of the program and we assume that it
430 // is *always* called at the end - so we free memory here to avoid false
431 // memory leak reports from wxWin memory tracking code
432 ClearTraceMasks();
433}
434
e94cd97d
DE
435void wxLog::DoCreateOnDemand()
436{
437 ms_bAutoCreate = true;
438}
439
f96233d5
VZ
440void wxLog::AddTraceMask(const wxString& str)
441{
766aecab 442 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
f96233d5
VZ
443
444 ms_aTraceMasks.push_back(str);
445}
446
0fb67cd1 447void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 448{
766aecab 449 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
f96233d5 450
0fb67cd1
VZ
451 int index = ms_aTraceMasks.Index(str);
452 if ( index != wxNOT_FOUND )
dc6d5e38 453 ms_aTraceMasks.RemoveAt((size_t)index);
0fb67cd1 454}
c801d85f 455
36bd6902
VZ
456void wxLog::ClearTraceMasks()
457{
766aecab 458 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
f96233d5 459
36bd6902
VZ
460 ms_aTraceMasks.Clear();
461}
462
d2e1ef19
VZ
463void wxLog::TimeStamp(wxString *str)
464{
7b2d1c74 465#if wxUSE_DATETIME
cb296f30 466 if ( !ms_timestamp.empty() )
d2e1ef19
VZ
467 {
468 wxChar buf[256];
469 time_t timeNow;
470 (void)time(&timeNow);
83e8b44c
VZ
471
472 struct tm tm;
473 wxStrftime(buf, WXSIZEOF(buf),
474 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
d2e1ef19
VZ
475
476 str->Empty();
a0516656 477 *str << buf << wxS(": ");
d2e1ef19 478 }
7b2d1c74 479#endif // wxUSE_DATETIME
d2e1ef19
VZ
480}
481
c801d85f
KB
482void wxLog::Flush()
483{
6f6b48f1 484 LogLastRepeatIfNeeded();
c801d85f
KB
485}
486
5a20d2ce 487/*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
df5168c4 488{
766aecab 489 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
f96233d5 490
df5168c4
MB
491 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
492 en = ms_aTraceMasks.end();
493 it != en; ++it )
f96233d5 494 {
df5168c4
MB
495 if ( *it == mask)
496 return true;
f96233d5
VZ
497 }
498
df5168c4
MB
499 return false;
500}
501
d3fc1755
VZ
502// ----------------------------------------------------------------------------
503// wxLogBuffer implementation
504// ----------------------------------------------------------------------------
505
506void wxLogBuffer::Flush()
507{
a23bbe93
VZ
508 if ( !m_str.empty() )
509 {
510 wxMessageOutputBest out;
a0516656 511 out.Printf(wxS("%s"), m_str.c_str());
a23bbe93
VZ
512 m_str.clear();
513 }
d3fc1755
VZ
514}
515
bc73d5ae 516void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
83250f1a 517{
711f12ef
VZ
518 // don't put debug messages in the buffer, we don't want to show
519 // them to the user in a msg box, log them immediately
bc73d5ae 520 switch ( level )
711f12ef 521 {
bc73d5ae
VZ
522 case wxLOG_Debug:
523 case wxLOG_Trace:
524 wxLog::DoLogTextAtLevel(level, msg);
525 break;
83250f1a 526
bc73d5ae
VZ
527 default:
528 m_str << msg << wxS("\n");
83250f1a
VZ
529 }
530}
531
c801d85f
KB
532// ----------------------------------------------------------------------------
533// wxLogStderr class implementation
534// ----------------------------------------------------------------------------
535
536wxLogStderr::wxLogStderr(FILE *fp)
537{
0fb67cd1
VZ
538 if ( fp == NULL )
539 m_fp = stderr;
540 else
541 m_fp = fp;
c801d85f
KB
542}
543
bc73d5ae 544void wxLogStderr::DoLogText(const wxString& msg)
c801d85f 545{
bc73d5ae 546 wxFputs(msg + '\n', m_fp);
0fb67cd1 547 fflush(m_fp);
1e8a4bc2 548
e2478fde
VZ
549 // under GUI systems such as Windows or Mac, programs usually don't have
550 // stderr at all, so show the messages also somewhere else, typically in
551 // the debugger window so that they go at least somewhere instead of being
552 // simply lost
1ec5cbf3
VZ
553 if ( m_fp == stderr )
554 {
e2478fde
VZ
555 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
556 if ( traits && !traits->HasStderr() )
557 {
bc73d5ae 558 wxMessageOutputDebug().Output(msg + wxS('\n'));
e2478fde 559 }
03147cd0 560 }
c801d85f
KB
561}
562
563// ----------------------------------------------------------------------------
564// wxLogStream implementation
565// ----------------------------------------------------------------------------
566
4bf78aae 567#if wxUSE_STD_IOSTREAM
65f19af1 568#include "wx/ioswrap.h"
dd107c50 569wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 570{
0fb67cd1 571 if ( ostr == NULL )
dd107c50 572 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
573 else
574 m_ostr = ostr;
c801d85f
KB
575}
576
bc73d5ae 577void wxLogStream::DoLogText(const wxString& msg)
c801d85f 578{
bc73d5ae 579 (*m_ostr) << msg << wxSTD endl;
c801d85f 580}
0fb67cd1 581#endif // wxUSE_STD_IOSTREAM
c801d85f 582
03147cd0
VZ
583// ----------------------------------------------------------------------------
584// wxLogChain
585// ----------------------------------------------------------------------------
586
587wxLogChain::wxLogChain(wxLog *logger)
588{
f644b28c 589 m_bPassMessages = true;
71debe95 590
03147cd0
VZ
591 m_logNew = logger;
592 m_logOld = wxLog::SetActiveTarget(this);
593}
594
199e91fb
VZ
595wxLogChain::~wxLogChain()
596{
e95f8fde
VZ
597 delete m_logOld;
598
599 if ( m_logNew != this )
600 delete m_logNew;
199e91fb
VZ
601}
602
03147cd0
VZ
603void wxLogChain::SetLog(wxLog *logger)
604{
605 if ( m_logNew != this )
606 delete m_logNew;
607
03147cd0
VZ
608 m_logNew = logger;
609}
610
611void wxLogChain::Flush()
612{
613 if ( m_logOld )
614 m_logOld->Flush();
615
1ec5cbf3 616 // be careful to avoid infinite recursion
03147cd0
VZ
617 if ( m_logNew && m_logNew != this )
618 m_logNew->Flush();
619}
620
bc73d5ae
VZ
621void wxLogChain::DoLogRecord(wxLogLevel level,
622 const wxString& msg,
623 const wxLogRecordInfo& info)
03147cd0
VZ
624{
625 // let the previous logger show it
626 if ( m_logOld && IsPassingMessages() )
bc73d5ae 627 m_logOld->LogRecord(level, msg, info);
03147cd0 628
efce878a 629 // and also send it to the new one
03147cd0 630 if ( m_logNew && m_logNew != this )
bc73d5ae 631 m_logNew->LogRecord(level, msg, info);
03147cd0
VZ
632}
633
93d4c1d0
VZ
634#ifdef __VISUALC__
635 // "'this' : used in base member initializer list" - so what?
636 #pragma warning(disable:4355)
637#endif // VC++
638
47fe7ff3
JS
639// ----------------------------------------------------------------------------
640// wxLogInterposer
641// ----------------------------------------------------------------------------
642
643wxLogInterposer::wxLogInterposer()
644 : wxLogChain(this)
645{
646}
647
648// ----------------------------------------------------------------------------
649// wxLogInterposerTemp
650// ----------------------------------------------------------------------------
651
652wxLogInterposerTemp::wxLogInterposerTemp()
93d4c1d0
VZ
653 : wxLogChain(this)
654{
766aecab 655 DetachOldLog();
93d4c1d0
VZ
656}
657
658#ifdef __VISUALC__
659 #pragma warning(default:4355)
660#endif // VC++
661
c801d85f
KB
662// ============================================================================
663// Global functions/variables
664// ============================================================================
665
666// ----------------------------------------------------------------------------
667// static variables
668// ----------------------------------------------------------------------------
0fb67cd1 669
f9837791 670bool wxLog::ms_bRepetCounting = false;
f9837791 671
d3b9f782 672wxLog *wxLog::ms_pLogger = NULL;
f644b28c
WS
673bool wxLog::ms_doLog = true;
674bool wxLog::ms_bAutoCreate = true;
675bool wxLog::ms_bVerbose = false;
d2e1ef19 676
edc73852
RD
677wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
678
2ed3265e
VZ
679size_t wxLog::ms_suspendCount = 0;
680
a0516656 681wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
d2e1ef19 682
34085a0d 683#if WXWIN_COMPATIBILITY_2_8
0fb67cd1 684wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
34085a0d
VZ
685#endif // wxDEBUG_LEVEL
686
0fb67cd1 687wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
688
689// ----------------------------------------------------------------------------
690// stdout error logging helper
691// ----------------------------------------------------------------------------
692
693// helper function: wraps the message and justifies it under given position
694// (looks more pretty on the terminal). Also adds newline at the end.
695//
0fb67cd1
VZ
696// TODO this is now disabled until I find a portable way of determining the
697// terminal window size (ok, I found it but does anybody really cares?)
698#ifdef LOG_PRETTY_WRAP
c801d85f
KB
699static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
700{
0fb67cd1
VZ
701 size_t nMax = 80; // FIXME
702 size_t nStart = strlen(pszPrefix);
703 fputs(pszPrefix, f);
704
705 size_t n;
706 while ( *psz != '\0' ) {
707 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
708 putc(*psz++, f);
709
710 // wrapped?
711 if ( *psz != '\0' ) {
712 /*putc('\n', f);*/
713 for ( n = 0; n < nStart; n++ )
714 putc(' ', f);
715
716 // as we wrapped, squeeze all white space
717 while ( isspace(*psz) )
718 psz++;
719 }
c801d85f 720 }
c801d85f 721
0fb67cd1 722 putc('\n', f);
c801d85f
KB
723}
724#endif //LOG_PRETTY_WRAP
725
726// ----------------------------------------------------------------------------
727// error code/error message retrieval functions
728// ----------------------------------------------------------------------------
729
730// get error code from syste
731unsigned long wxSysErrorCode()
732{
8cb172b4 733#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 734 return ::GetLastError();
0fb67cd1 735#else //Unix
c801d85f 736 return errno;
0fb67cd1 737#endif //Win/Unix
c801d85f
KB
738}
739
740// get error message from system
50920146 741const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 742{
0fb67cd1
VZ
743 if ( nErrCode == 0 )
744 nErrCode = wxSysErrorCode();
745
8cb172b4 746#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
2e7f3845 747 static wxChar s_szBuf[1024];
0fb67cd1
VZ
748
749 // get error message from system
750 LPVOID lpMsgBuf;
d0822e56
VZ
751 if ( ::FormatMessage
752 (
753 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
754 NULL,
755 nErrCode,
0fb67cd1
VZ
756 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
757 (LPTSTR)&lpMsgBuf,
d0822e56
VZ
758 0,
759 NULL
760 ) == 0 )
761 {
762 // if this happens, something is seriously wrong, so don't use _() here
763 // for safety
a0516656 764 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
c4b401b6 765 return s_szBuf;
d0822e56
VZ
766 }
767
0fb67cd1
VZ
768
769 // copy it to our buffer and free memory
d0822e56 770 // Crashes on SmartPhone (FIXME)
0c44ec97 771#if !defined(__SMARTPHONE__) /* of WinCE */
7448de8d
WS
772 if( lpMsgBuf != 0 )
773 {
e408bf52 774 wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
251244a0
VZ
775
776 LocalFree(lpMsgBuf);
777
778 // returned string is capitalized and ended with '\r\n' - bad
779 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
780 size_t len = wxStrlen(s_szBuf);
781 if ( len > 0 ) {
782 // truncate string
a0516656
VZ
783 if ( s_szBuf[len - 2] == wxS('\r') )
784 s_szBuf[len - 2] = wxS('\0');
251244a0
VZ
785 }
786 }
a9928e9d 787 else
2e7f3845 788#endif // !__SMARTPHONE__
a9928e9d 789 {
a0516656 790 s_szBuf[0] = wxS('\0');
0fb67cd1
VZ
791 }
792
793 return s_szBuf;
2e7f3845
VZ
794#else // !__WXMSW__
795 #if wxUSE_UNICODE
796 static wchar_t s_wzBuf[1024];
797 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
798 WXSIZEOF(s_wzBuf) - 1);
799 return s_wzBuf;
800 #else
801 return strerror((int)nErrCode);
802 #endif
803#endif // __WXMSW__/!__WXMSW__
c801d85f
KB
804}
805
e2478fde 806#endif // wxUSE_LOG