]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/log.cpp
document On{Open,Save}Document()
[wxWidgets.git] / src / common / log.cpp
index 13c01367530592d59451333d68e48d6429412e53..9cdf13a271914e3ec106bd6cfad99d9cb66a4b50 100644 (file)
 
 #include <stdlib.h>
 
+#ifndef __WXPALMOS5__
 #ifndef __WXWINCE__
 #include <time.h>
 #else
 #include "wx/msw/wince/time.h"
 #endif
+#endif /* ! __WXPALMOS5__ */
 
 #if defined(__WINDOWS__)
     #include "wx/msw/private.h" // includes windows.h
 #endif
 
+#if wxUSE_THREADS
+
+// define static functions providing access to the critical sections we use
+// instead of just using static critical section variables as log functions may
+// be used during static initialization and while this is certainly not
+// advisable it's still better to not crash (as we'd do if we used a yet
+// uninitialized critical section) if it happens
+
+static inline wxCriticalSection& GetTraceMaskCS()
+{
+    static wxCriticalSection s_csTrace;
+
+    return s_csTrace;
+}
+
+static inline wxCriticalSection& GetPreviousLogCS()
+{
+    static wxCriticalSection s_csPrev;
+
+    return s_csPrev;
+}
+
+#endif // wxUSE_THREADS
+
 // ----------------------------------------------------------------------------
 // non member functions
 // ----------------------------------------------------------------------------
@@ -457,10 +483,15 @@ void WXDLLIMPEXP_BASE wxVLogSysError(unsigned long err, const wxString& format,
 // wxLog class implementation
 // ----------------------------------------------------------------------------
 
-unsigned wxLog::LogLastRepetitionCountIfNeeded()
+unsigned wxLog::LogLastRepeatIfNeeded()
 {
-    wxCRIT_SECT_LOCKER(lock, ms_prevCS);
+    wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
+
+    return LogLastRepeatIfNeededUnlocked();
+}
 
+unsigned wxLog::LogLastRepeatIfNeededUnlocked()
+{
     const unsigned count = ms_prevCounter;
 
     if ( ms_prevCounter )
@@ -485,7 +516,17 @@ unsigned wxLog::LogLastRepetitionCountIfNeeded()
 
 wxLog::~wxLog()
 {
-    LogLastRepetitionCountIfNeeded();
+    // Flush() must be called before destroying the object as otherwise some
+    // messages could be lost
+    if ( ms_prevCounter )
+    {
+        wxMessageOutputDebug().Printf
+        (
+            wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
+            ms_prevString,
+            ms_prevCounter
+        );
+    }
 }
 
 /* static */
@@ -498,7 +539,7 @@ void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
         {
             if ( GetRepetitionCounting() )
             {
-                wxCRIT_SECT_LOCKER(lock, ms_prevCS);
+                wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
 
                 if ( szString == ms_prevString )
                 {
@@ -509,7 +550,7 @@ void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
                     return;
                 }
 
-                pLogger->LogLastRepetitionCountIfNeeded();
+                pLogger->LogLastRepeatIfNeededUnlocked();
 
                 // reset repetition counter for a new message
                 ms_prevString = szString;
@@ -601,8 +642,17 @@ void wxLog::DoCreateOnDemand()
     ms_bAutoCreate = true;
 }
 
+void wxLog::AddTraceMask(const wxString& str)
+{
+    wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
+
+    ms_aTraceMasks.push_back(str);
+}
+
 void wxLog::RemoveTraceMask(const wxString& str)
 {
+    wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
+
     int index = ms_aTraceMasks.Index(str);
     if ( index != wxNOT_FOUND )
         ms_aTraceMasks.RemoveAt((size_t)index);
@@ -610,6 +660,8 @@ void wxLog::RemoveTraceMask(const wxString& str)
 
 void wxLog::ClearTraceMasks()
 {
+    wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
+
     ms_aTraceMasks.Clear();
 }
 
@@ -700,16 +752,21 @@ void wxLog::DoLogString(const wxString& szString, time_t t)
 
 void wxLog::Flush()
 {
-    // nothing to do here
+    LogLastRepeatIfNeeded();
 }
 
 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
 {
+    wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
+
     for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
                                   en = ms_aTraceMasks.end();
          it != en; ++it )
+    {
         if ( *it == mask)
             return true;
+    }
+
     return false;
 }
 
@@ -891,7 +948,7 @@ wxLogInterposer::wxLogInterposer()
 wxLogInterposerTemp::wxLogInterposerTemp()
                 : wxLogChain(this)
 {
-       DetachOldLog();
+    DetachOldLog();
 }
 
 #ifdef __VISUALC__
@@ -906,9 +963,6 @@ wxLogInterposerTemp::wxLogInterposerTemp()
 // static variables
 // ----------------------------------------------------------------------------
 
-#if wxUSE_THREADS
-wxCriticalSection wxLog::ms_prevCS;
-#endif // wxUSE_THREADS
 bool            wxLog::ms_bRepetCounting = false;
 wxString        wxLog::ms_prevString;
 unsigned int    wxLog::ms_prevCounter = 0;