]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
Removed critical section protecting last repeat counter.
[wxWidgets.git] / src / common / log.cpp
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/datetime.h"
41 #include "wx/file.h"
42 #include "wx/msgout.h"
43 #include "wx/textfile.h"
44 #include "wx/thread.h"
45 #include "wx/crt.h"
46 #include "wx/vector.h"
47
48 // other standard headers
49 #ifndef __WXWINCE__
50 #include <errno.h>
51 #endif
52
53 #include <stdlib.h>
54
55 #ifndef __WXPALMOS5__
56 #ifndef __WXWINCE__
57 #include <time.h>
58 #else
59 #include "wx/msw/wince/time.h"
60 #endif
61 #endif /* ! __WXPALMOS5__ */
62
63 #if defined(__WINDOWS__)
64 #include "wx/msw/private.h" // includes windows.h
65 #endif
66
67 #undef wxLOG_COMPONENT
68 const char *wxLOG_COMPONENT = "";
69
70 #if wxUSE_THREADS
71
72 namespace
73 {
74
75 // contains messages logged by the other threads and waiting to be shown until
76 // Flush() is called in the main one
77 typedef wxVector<wxLogRecord> wxLogRecords;
78 wxLogRecords gs_bufferedLogRecords;
79
80
81 // define static functions providing access to the critical sections we use
82 // instead of just using static critical section variables as log functions may
83 // be used during static initialization and while this is certainly not
84 // advisable it's still better to not crash (as we'd do if we used a yet
85 // uninitialized critical section) if it happens
86
87 // this critical section is used for buffering the messages from threads other
88 // than main, i.e. it protects all accesses to gs_bufferedLogRecords above
89 inline wxCriticalSection& GetBackgroundLogCS()
90 {
91 static wxCriticalSection s_csBackground;
92
93 return s_csBackground;
94 }
95
96 inline wxCriticalSection& GetTraceMaskCS()
97 {
98 static wxCriticalSection s_csTrace;
99
100 return s_csTrace;
101 }
102
103 inline wxCriticalSection& GetPreviousLogCS()
104 {
105 static wxCriticalSection s_csPrev;
106
107 return s_csPrev;
108 }
109
110 inline wxCriticalSection& GetLevelsCS()
111 {
112 static wxCriticalSection s_csLevels;
113
114 return s_csLevels;
115 }
116
117 } // anonymous namespace
118
119 #endif // wxUSE_THREADS
120
121 // ----------------------------------------------------------------------------
122 // non member functions
123 // ----------------------------------------------------------------------------
124
125 // define this to enable wrapping of log messages
126 //#define LOG_PRETTY_WRAP
127
128 #ifdef LOG_PRETTY_WRAP
129 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // module globals
134 // ----------------------------------------------------------------------------
135
136 namespace
137 {
138
139 // this struct is used to store information about the previous log message used
140 // by OnLog() to (optionally) avoid logging multiple copies of the same message
141 struct PreviousLogInfo
142 {
143 PreviousLogInfo()
144 {
145 numRepeated = 0;
146 }
147
148
149 // previous message itself
150 wxString msg;
151
152 // its level
153 wxLogLevel level;
154
155 // other information about it
156 wxLogRecordInfo info;
157
158 // the number of times it was already repeated
159 unsigned numRepeated;
160 };
161
162 PreviousLogInfo gs_prevLog;
163
164
165 // map containing all components for which log level was explicitly set
166 //
167 // NB: all accesses to it must be protected by GetLevelsCS() critical section
168 wxStringToNumHashMap gs_componentLevels;
169
170 } // anonymous namespace
171
172 // ============================================================================
173 // implementation
174 // ============================================================================
175
176 // ----------------------------------------------------------------------------
177 // helper global functions
178 // ----------------------------------------------------------------------------
179
180 void wxSafeShowMessage(const wxString& title, const wxString& text)
181 {
182 #ifdef __WINDOWS__
183 ::MessageBox(NULL, text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP);
184 #else
185 wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str());
186 fflush(stderr);
187 #endif
188 }
189
190 // ----------------------------------------------------------------------------
191 // wxLog class implementation
192 // ----------------------------------------------------------------------------
193
194 unsigned wxLog::LogLastRepeatIfNeeded()
195 {
196 const unsigned count = gs_prevLog.numRepeated;
197
198 if ( gs_prevLog.numRepeated )
199 {
200 wxString msg;
201 #if wxUSE_INTL
202 msg.Printf(wxPLURAL("The previous message repeated once.",
203 "The previous message repeated %lu times.",
204 gs_prevLog.numRepeated),
205 gs_prevLog.numRepeated);
206 #else
207 msg.Printf(wxS("The previous message was repeated %lu times."),
208 gs_prevLog.numRepeated);
209 #endif
210 gs_prevLog.numRepeated = 0;
211 gs_prevLog.msg.clear();
212 DoLogRecord(gs_prevLog.level, msg, gs_prevLog.info);
213 }
214
215 return count;
216 }
217
218 wxLog::~wxLog()
219 {
220 // Flush() must be called before destroying the object as otherwise some
221 // messages could be lost
222 if ( gs_prevLog.numRepeated )
223 {
224 wxMessageOutputDebug().Printf
225 (
226 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
227 gs_prevLog.msg,
228 gs_prevLog.numRepeated
229 );
230 }
231 }
232
233 // ----------------------------------------------------------------------------
234 // wxLog logging functions
235 // ----------------------------------------------------------------------------
236
237 /* static */
238 void
239 wxLog::OnLog(wxLogLevel level, const wxString& msg, time_t t)
240 {
241 wxLogRecordInfo info;
242 info.timestamp = t;
243 #if wxUSE_THREADS
244 info.threadId = wxThread::GetCurrentId();
245 #endif // wxUSE_THREADS
246
247 OnLog(level, msg, info);
248 }
249
250 /* static */
251 void
252 wxLog::OnLog(wxLogLevel level,
253 const wxString& msg,
254 const wxLogRecordInfo& info)
255 {
256 // fatal errors can't be suppressed nor handled by the custom log target
257 // and always terminate the program
258 if ( level == wxLOG_FatalError )
259 {
260 wxSafeShowMessage(wxS("Fatal Error"), msg);
261
262 #ifdef __WXWINCE__
263 ExitThread(3);
264 #else
265 abort();
266 #endif
267 }
268
269 wxLog *pLogger = GetActiveTarget();
270 if ( !pLogger )
271 return;
272
273 #if wxUSE_THREADS
274 if ( !wxThread::IsMain() )
275 {
276 wxCriticalSectionLocker lock(GetBackgroundLogCS());
277
278 gs_bufferedLogRecords.push_back(wxLogRecord(level, msg, info));
279
280 // ensure that our Flush() will be called soon
281 wxWakeUpIdle();
282
283 return;
284 }
285 #endif // wxUSE_THREADS
286
287 pLogger->OnLogInMainThread(level, msg, info);
288 }
289
290 void
291 wxLog::OnLogInMainThread(wxLogLevel level,
292 const wxString& msg,
293 const wxLogRecordInfo& info)
294 {
295 if ( GetRepetitionCounting() )
296 {
297 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
298
299 if ( msg == gs_prevLog.msg )
300 {
301 gs_prevLog.numRepeated++;
302
303 // nothing else to do, in particular, don't log the
304 // repeated message
305 return;
306 }
307
308 LogLastRepeatIfNeeded();
309
310 // reset repetition counter for a new message
311 gs_prevLog.msg = msg;
312 gs_prevLog.level = level;
313 gs_prevLog.info = info;
314 }
315
316 // handle extra data which may be passed to us by wxLogXXX()
317 wxString prefix, suffix;
318 wxUIntPtr num = 0;
319 if ( info.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE, &num) )
320 {
321 long err = static_cast<long>(num);
322 if ( !err )
323 err = wxSysErrorCode();
324
325 suffix.Printf(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
326 }
327
328 #if wxUSE_LOG_TRACE
329 wxString str;
330 if ( level == wxLOG_Trace && info.GetStrValue(wxLOG_KEY_TRACE_MASK, &str) )
331 {
332 prefix = "(" + str + ") ";
333 }
334 #endif // wxUSE_LOG_TRACE
335
336 DoLogRecord(level, prefix + msg + suffix, info);
337 }
338
339 void wxLog::DoLogRecord(wxLogLevel level,
340 const wxString& msg,
341 const wxLogRecordInfo& info)
342 {
343 #if WXWIN_COMPATIBILITY_2_8
344 // call the old DoLog() to ensure that existing custom log classes still
345 // work
346 //
347 // as the user code could have defined it as either taking "const char *"
348 // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice
349 // but to call both of them
350 DoLog(level, (const char*)msg.mb_str(), info.timestamp);
351 DoLog(level, (const wchar_t*)msg.wc_str(), info.timestamp);
352 #endif // WXWIN_COMPATIBILITY_2_8
353
354
355 // TODO: it would be better to extract message formatting in a separate
356 // wxLogFormatter class but for now we hard code formatting here
357
358 wxString prefix;
359
360 // don't time stamp debug messages under MSW as debug viewers usually
361 // already have an option to do it
362 #ifdef __WXMSW__
363 if ( level != wxLOG_Debug && level != wxLOG_Trace )
364 #endif // __WXMSW__
365 TimeStamp(&prefix);
366
367 // TODO: use the other wxLogRecordInfo fields
368
369 switch ( level )
370 {
371 case wxLOG_Error:
372 prefix += _("Error: ");
373 break;
374
375 case wxLOG_Warning:
376 prefix += _("Warning: ");
377 break;
378
379 // don't prepend "debug/trace" prefix under MSW as it goes to the debug
380 // window anyhow and so can't be confused with something else
381 #ifndef __WXMSW__
382 case wxLOG_Debug:
383 // this prefix (as well as the one below) is intentionally not
384 // translated as nobody translates debug messages anyhow
385 prefix += "Debug: ";
386 break;
387
388 case wxLOG_Trace:
389 prefix += "Trace: ";
390 break;
391 #endif // !__WXMSW__
392 }
393
394 DoLogTextAtLevel(level, prefix + msg);
395 }
396
397 void wxLog::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
398 {
399 // we know about debug messages (because using wxMessageOutputDebug is the
400 // right thing to do in 99% of all cases and also for compatibility) but
401 // anything else needs to be handled in the derived class
402 if ( level == wxLOG_Debug || level == wxLOG_Trace )
403 {
404 wxMessageOutputDebug().Output(msg + wxS('\n'));
405 }
406 else
407 {
408 DoLogText(msg);
409 }
410 }
411
412 void wxLog::DoLogText(const wxString& WXUNUSED(msg))
413 {
414 // in 2.8-compatible build the derived class might override DoLog() or
415 // DoLogString() instead so we can't have this assert there
416 #if !WXWIN_COMPATIBILITY_2_8
417 wxFAIL_MSG( "must be overridden if it is called" );
418 #endif // WXWIN_COMPATIBILITY_2_8
419 }
420
421 #if WXWIN_COMPATIBILITY_2_8
422
423 void wxLog::DoLog(wxLogLevel WXUNUSED(level), const char *szString, time_t t)
424 {
425 DoLogString(szString, t);
426 }
427
428 void wxLog::DoLog(wxLogLevel WXUNUSED(level), const wchar_t *wzString, time_t t)
429 {
430 DoLogString(wzString, t);
431 }
432
433 #endif // WXWIN_COMPATIBILITY_2_8
434
435 // ----------------------------------------------------------------------------
436 // wxLog active target management
437 // ----------------------------------------------------------------------------
438
439 wxLog *wxLog::GetActiveTarget()
440 {
441 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
442 // prevent infinite recursion if someone calls wxLogXXX() from
443 // wxApp::CreateLogTarget()
444 static bool s_bInGetActiveTarget = false;
445 if ( !s_bInGetActiveTarget ) {
446 s_bInGetActiveTarget = true;
447
448 // ask the application to create a log target for us
449 if ( wxTheApp != NULL )
450 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
451 else
452 ms_pLogger = new wxLogStderr;
453
454 s_bInGetActiveTarget = false;
455
456 // do nothing if it fails - what can we do?
457 }
458 }
459
460 return ms_pLogger;
461 }
462
463 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
464 {
465 if ( ms_pLogger != NULL ) {
466 // flush the old messages before changing because otherwise they might
467 // get lost later if this target is not restored
468 ms_pLogger->Flush();
469 }
470
471 wxLog *pOldLogger = ms_pLogger;
472 ms_pLogger = pLogger;
473
474 return pOldLogger;
475 }
476
477 void wxLog::DontCreateOnDemand()
478 {
479 ms_bAutoCreate = false;
480
481 // this is usually called at the end of the program and we assume that it
482 // is *always* called at the end - so we free memory here to avoid false
483 // memory leak reports from wxWin memory tracking code
484 ClearTraceMasks();
485 }
486
487 void wxLog::DoCreateOnDemand()
488 {
489 ms_bAutoCreate = true;
490 }
491
492 // ----------------------------------------------------------------------------
493 // wxLog components levels
494 // ----------------------------------------------------------------------------
495
496 /* static */
497 void wxLog::SetComponentLevel(const wxString& component, wxLogLevel level)
498 {
499 if ( component.empty() )
500 {
501 SetLogLevel(level);
502 }
503 else
504 {
505 wxCRIT_SECT_LOCKER(lock, GetLevelsCS());
506
507 gs_componentLevels[component] = level;
508 }
509 }
510
511 /* static */
512 wxLogLevel wxLog::GetComponentLevel(wxString component)
513 {
514 wxCRIT_SECT_LOCKER(lock, GetLevelsCS());
515
516 while ( !component.empty() )
517 {
518 wxStringToNumHashMap::const_iterator
519 it = gs_componentLevels.find(component);
520 if ( it != gs_componentLevels.end() )
521 return static_cast<wxLogLevel>(it->second);
522
523 component = component.BeforeLast('/');
524 }
525
526 return GetLogLevel();
527 }
528
529 // ----------------------------------------------------------------------------
530 // wxLog trace masks
531 // ----------------------------------------------------------------------------
532
533 void wxLog::AddTraceMask(const wxString& str)
534 {
535 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
536
537 ms_aTraceMasks.push_back(str);
538 }
539
540 void wxLog::RemoveTraceMask(const wxString& str)
541 {
542 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
543
544 int index = ms_aTraceMasks.Index(str);
545 if ( index != wxNOT_FOUND )
546 ms_aTraceMasks.RemoveAt((size_t)index);
547 }
548
549 void wxLog::ClearTraceMasks()
550 {
551 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
552
553 ms_aTraceMasks.Clear();
554 }
555
556 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
557 {
558 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
559
560 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
561 en = ms_aTraceMasks.end();
562 it != en; ++it )
563 {
564 if ( *it == mask)
565 return true;
566 }
567
568 return false;
569 }
570
571 // ----------------------------------------------------------------------------
572 // wxLog miscellaneous other methods
573 // ----------------------------------------------------------------------------
574
575 void wxLog::TimeStamp(wxString *str)
576 {
577 #if wxUSE_DATETIME
578 if ( !ms_timestamp.empty() )
579 {
580 wxChar buf[256];
581 time_t timeNow;
582 (void)time(&timeNow);
583
584 struct tm tm;
585 wxStrftime(buf, WXSIZEOF(buf),
586 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
587
588 str->Empty();
589 *str << buf << wxS(": ");
590 }
591 #endif // wxUSE_DATETIME
592 }
593
594 void wxLog::Flush()
595 {
596 #if wxUSE_THREADS
597 wxASSERT_MSG( wxThread::IsMain(),
598 "should be called from the main thread only" );
599
600 // check if we have queued messages from other threads
601 wxLogRecords bufferedLogRecords;
602
603 {
604 wxCriticalSectionLocker lock(GetBackgroundLogCS());
605 bufferedLogRecords.swap(gs_bufferedLogRecords);
606
607 // release the lock now to not keep it while we are logging the
608 // messages below, allowing background threads to run
609 }
610
611 if ( !bufferedLogRecords.empty() )
612 {
613 for ( wxLogRecords::const_iterator it = bufferedLogRecords.begin();
614 it != bufferedLogRecords.end();
615 ++it )
616 {
617 OnLogInMainThread(it->level, it->msg, it->info);
618 }
619 }
620 #endif // wxUSE_THREADS
621
622 LogLastRepeatIfNeeded();
623 }
624
625 // ----------------------------------------------------------------------------
626 // wxLogBuffer implementation
627 // ----------------------------------------------------------------------------
628
629 void wxLogBuffer::Flush()
630 {
631 wxLog::Flush();
632
633 if ( !m_str.empty() )
634 {
635 wxMessageOutputBest out;
636 out.Printf(wxS("%s"), m_str.c_str());
637 m_str.clear();
638 }
639 }
640
641 void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level, const wxString& msg)
642 {
643 // don't put debug messages in the buffer, we don't want to show
644 // them to the user in a msg box, log them immediately
645 switch ( level )
646 {
647 case wxLOG_Debug:
648 case wxLOG_Trace:
649 wxLog::DoLogTextAtLevel(level, msg);
650 break;
651
652 default:
653 m_str << msg << wxS("\n");
654 }
655 }
656
657 // ----------------------------------------------------------------------------
658 // wxLogStderr class implementation
659 // ----------------------------------------------------------------------------
660
661 wxLogStderr::wxLogStderr(FILE *fp)
662 {
663 if ( fp == NULL )
664 m_fp = stderr;
665 else
666 m_fp = fp;
667 }
668
669 void wxLogStderr::DoLogText(const wxString& msg)
670 {
671 wxFputs(msg + '\n', m_fp);
672 fflush(m_fp);
673
674 // under GUI systems such as Windows or Mac, programs usually don't have
675 // stderr at all, so show the messages also somewhere else, typically in
676 // the debugger window so that they go at least somewhere instead of being
677 // simply lost
678 if ( m_fp == stderr )
679 {
680 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
681 if ( traits && !traits->HasStderr() )
682 {
683 wxMessageOutputDebug().Output(msg + wxS('\n'));
684 }
685 }
686 }
687
688 // ----------------------------------------------------------------------------
689 // wxLogStream implementation
690 // ----------------------------------------------------------------------------
691
692 #if wxUSE_STD_IOSTREAM
693 #include "wx/ioswrap.h"
694 wxLogStream::wxLogStream(wxSTD ostream *ostr)
695 {
696 if ( ostr == NULL )
697 m_ostr = &wxSTD cerr;
698 else
699 m_ostr = ostr;
700 }
701
702 void wxLogStream::DoLogText(const wxString& msg)
703 {
704 (*m_ostr) << msg << wxSTD endl;
705 }
706 #endif // wxUSE_STD_IOSTREAM
707
708 // ----------------------------------------------------------------------------
709 // wxLogChain
710 // ----------------------------------------------------------------------------
711
712 wxLogChain::wxLogChain(wxLog *logger)
713 {
714 m_bPassMessages = true;
715
716 m_logNew = logger;
717 m_logOld = wxLog::SetActiveTarget(this);
718 }
719
720 wxLogChain::~wxLogChain()
721 {
722 delete m_logOld;
723
724 if ( m_logNew != this )
725 delete m_logNew;
726 }
727
728 void wxLogChain::SetLog(wxLog *logger)
729 {
730 if ( m_logNew != this )
731 delete m_logNew;
732
733 m_logNew = logger;
734 }
735
736 void wxLogChain::Flush()
737 {
738 if ( m_logOld )
739 m_logOld->Flush();
740
741 // be careful to avoid infinite recursion
742 if ( m_logNew && m_logNew != this )
743 m_logNew->Flush();
744 }
745
746 void wxLogChain::DoLogRecord(wxLogLevel level,
747 const wxString& msg,
748 const wxLogRecordInfo& info)
749 {
750 // let the previous logger show it
751 if ( m_logOld && IsPassingMessages() )
752 m_logOld->LogRecord(level, msg, info);
753
754 // and also send it to the new one
755 if ( m_logNew && m_logNew != this )
756 m_logNew->LogRecord(level, msg, info);
757 }
758
759 #ifdef __VISUALC__
760 // "'this' : used in base member initializer list" - so what?
761 #pragma warning(disable:4355)
762 #endif // VC++
763
764 // ----------------------------------------------------------------------------
765 // wxLogInterposer
766 // ----------------------------------------------------------------------------
767
768 wxLogInterposer::wxLogInterposer()
769 : wxLogChain(this)
770 {
771 }
772
773 // ----------------------------------------------------------------------------
774 // wxLogInterposerTemp
775 // ----------------------------------------------------------------------------
776
777 wxLogInterposerTemp::wxLogInterposerTemp()
778 : wxLogChain(this)
779 {
780 DetachOldLog();
781 }
782
783 #ifdef __VISUALC__
784 #pragma warning(default:4355)
785 #endif // VC++
786
787 // ============================================================================
788 // Global functions/variables
789 // ============================================================================
790
791 // ----------------------------------------------------------------------------
792 // static variables
793 // ----------------------------------------------------------------------------
794
795 bool wxLog::ms_bRepetCounting = false;
796
797 wxLog *wxLog::ms_pLogger = NULL;
798 bool wxLog::ms_doLog = true;
799 bool wxLog::ms_bAutoCreate = true;
800 bool wxLog::ms_bVerbose = false;
801
802 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
803
804 size_t wxLog::ms_suspendCount = 0;
805
806 wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
807
808 #if WXWIN_COMPATIBILITY_2_8
809 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
810 #endif // wxDEBUG_LEVEL
811
812 wxArrayString wxLog::ms_aTraceMasks;
813
814 // ----------------------------------------------------------------------------
815 // stdout error logging helper
816 // ----------------------------------------------------------------------------
817
818 // helper function: wraps the message and justifies it under given position
819 // (looks more pretty on the terminal). Also adds newline at the end.
820 //
821 // TODO this is now disabled until I find a portable way of determining the
822 // terminal window size (ok, I found it but does anybody really cares?)
823 #ifdef LOG_PRETTY_WRAP
824 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
825 {
826 size_t nMax = 80; // FIXME
827 size_t nStart = strlen(pszPrefix);
828 fputs(pszPrefix, f);
829
830 size_t n;
831 while ( *psz != '\0' ) {
832 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
833 putc(*psz++, f);
834
835 // wrapped?
836 if ( *psz != '\0' ) {
837 /*putc('\n', f);*/
838 for ( n = 0; n < nStart; n++ )
839 putc(' ', f);
840
841 // as we wrapped, squeeze all white space
842 while ( isspace(*psz) )
843 psz++;
844 }
845 }
846
847 putc('\n', f);
848 }
849 #endif //LOG_PRETTY_WRAP
850
851 // ----------------------------------------------------------------------------
852 // error code/error message retrieval functions
853 // ----------------------------------------------------------------------------
854
855 // get error code from syste
856 unsigned long wxSysErrorCode()
857 {
858 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
859 return ::GetLastError();
860 #else //Unix
861 return errno;
862 #endif //Win/Unix
863 }
864
865 // get error message from system
866 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
867 {
868 if ( nErrCode == 0 )
869 nErrCode = wxSysErrorCode();
870
871 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
872 static wxChar s_szBuf[1024];
873
874 // get error message from system
875 LPVOID lpMsgBuf;
876 if ( ::FormatMessage
877 (
878 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
879 NULL,
880 nErrCode,
881 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
882 (LPTSTR)&lpMsgBuf,
883 0,
884 NULL
885 ) == 0 )
886 {
887 // if this happens, something is seriously wrong, so don't use _() here
888 // for safety
889 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
890 return s_szBuf;
891 }
892
893
894 // copy it to our buffer and free memory
895 // Crashes on SmartPhone (FIXME)
896 #if !defined(__SMARTPHONE__) /* of WinCE */
897 if( lpMsgBuf != 0 )
898 {
899 wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
900
901 LocalFree(lpMsgBuf);
902
903 // returned string is capitalized and ended with '\r\n' - bad
904 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
905 size_t len = wxStrlen(s_szBuf);
906 if ( len > 0 ) {
907 // truncate string
908 if ( s_szBuf[len - 2] == wxS('\r') )
909 s_szBuf[len - 2] = wxS('\0');
910 }
911 }
912 else
913 #endif // !__SMARTPHONE__
914 {
915 s_szBuf[0] = wxS('\0');
916 }
917
918 return s_szBuf;
919 #else // !__WXMSW__
920 #if wxUSE_UNICODE
921 static wchar_t s_wzBuf[1024];
922 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
923 WXSIZEOF(s_wzBuf) - 1);
924 return s_wzBuf;
925 #else
926 return strerror((int)nErrCode);
927 #endif
928 #endif // __WXMSW__/!__WXMSW__
929 }
930
931 #endif // wxUSE_LOG