/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/log.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/log.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22