]> git.saurik.com Git - wxWidgets.git/commitdiff
optionally count repeating wxLog messages instead of logging all (patch 1520815)
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 25 Jul 2006 00:04:35 +0000 (00:04 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 25 Jul 2006 00:04:35 +0000 (00:04 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40294 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/log.tex
include/wx/log.h
src/common/log.cpp
src/generic/logg.cpp

index 92420fb3b34e1b8615adafed3c00f87293907607..8dfff8deb16cc815dfdb232bb65108799d480c18 100644 (file)
@@ -87,6 +87,7 @@ All:
 - Fixed wxDb::Open(wxDbConnectInf) when using connection string (Hellwolf Misty)
 - Fixed crash in wxDb::Open() in Unicode build (Massimiliano Marretta)
 - Fixed wxTimeSpan::Format() for negative time spans
 - Fixed wxDb::Open(wxDbConnectInf) when using connection string (Hellwolf Misty)
 - Fixed crash in wxDb::Open() in Unicode build (Massimiliano Marretta)
 - Fixed wxTimeSpan::Format() for negative time spans
+- Optionally count repeating wxLog messages instead of logging all (Lauri Nurmi)
 
 All (GUI):
 
 
 All (GUI):
 
index 2fd6de0d1af3fb5c556acf32e7c1bfc537cf6e4b..573022808a5e17ea8073673f39cc1b47727c2de7 100644 (file)
@@ -155,7 +155,9 @@ window on the corresponding error message. If you wish to enable it, please use
 \helpref{SetTimestamp}{wxlogsettimestamp}\\
 \helpref{GetTimestamp}{wxloggettimestamp}\\
 \helpref{SetTraceMask}{wxlogsettracemask}\\
 \helpref{SetTimestamp}{wxlogsettimestamp}\\
 \helpref{GetTimestamp}{wxloggettimestamp}\\
 \helpref{SetTraceMask}{wxlogsettracemask}\\
-\helpref{GetTraceMask}{wxloggettracemask}
+\helpref{GetTraceMask}{wxloggettracemask}\\
+\helpref{SetRepetitionCounting}{wxlogsetrepetitioncounting}\\
+\helpref{GetRepetitionCounting}{wxloggetrepetitioncounting}
 
 %%%%% MEMBERS HERE %%%%%
 \helponly{\insertatlevel{2}{
 
 %%%%% MEMBERS HERE %%%%%
 \helponly{\insertatlevel{2}{
@@ -322,6 +324,21 @@ and not sent to the active log target.
 
 Returns the current log level limit.
 
 
 Returns the current log level limit.
 
+\membersection{wxLog::SetRepetitionCounting}\label{wxlogsetrepetitioncounting}
+
+\func{static void}{SetRepetitionCounting}{\param{bool }{ repetCounting = true}}
+
+Enables logging mode in which a log message is logged once, and in case exactly
+the same message successively repeats one or more times, only the number of 
+repetitions is logged.
+
+\membersection{wxLog::GetRepetitionCounting}\label{wxloggetrepetitioncounting}
+
+\func{static bool}{GetRepetitionCounting}{\void}
+
+Returns whether the repetition counting mode is enabled.
+
+
 \membersection{wxLog::SetTimestamp}\label{wxlogsettimestamp}
 
 \func{void}{SetTimestamp}{\param{const char * }{ format}}
 \membersection{wxLog::SetTimestamp}\label{wxlogsettimestamp}
 
 \func{void}{SetTimestamp}{\param{const char * }{ format}}
index 29c3464b30de6c2f2e5fd10bc93f775d7b4d736f..c3ee768cbfdd5678e6372b4b037971893f00ed4d 100644 (file)
@@ -139,15 +139,7 @@ public:
 
     // static sink function - see DoLog() for function to overload in the
     // derived classes
 
     // static sink function - see DoLog() for function to overload in the
     // derived classes
-    static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
-    {
-        if ( IsEnabled() && ms_logLevel >= level )
-        {
-            wxLog *pLogger = GetActiveTarget();
-            if ( pLogger )
-                pLogger->DoLog(level, szString, t);
-        }
-    }
+    static void OnLog(wxLogLevel level, const wxChar *szString, time_t t);
 
     // message buffering
 
 
     // message buffering
 
@@ -195,6 +187,14 @@ public:
     // current is NULL?
     static void DontCreateOnDemand();
 
     // current is NULL?
     static void DontCreateOnDemand();
 
+    // log the count of repeating messages instead of logging the messages
+    // multiple times
+    static void SetRepetitionCounting(bool bRepetCounting = true)
+    { ms_bRepetCounting = bRepetCounting; }
+
+    // gets duplicate counting status
+    static bool GetRepetitionCounting() { return ms_bRepetCounting; }
+
     // trace mask (see wxTraceXXX constants for details)
     static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
 
     // trace mask (see wxTraceXXX constants for details)
     static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
 
@@ -242,7 +242,7 @@ public:
     static void TimeStamp(wxString *str);
 
     // make dtor virtual for all derived classes
     static void TimeStamp(wxString *str);
 
     // make dtor virtual for all derived classes
-    virtual ~wxLog() { }
+    virtual ~wxLog();
 
 
     // this method exists for backwards compatibility only, don't use
 
 
     // this method exists for backwards compatibility only, don't use
@@ -259,10 +259,23 @@ protected:
     // you override DoLog() you might not need it at all
     virtual void DoLogString(const wxChar *szString, time_t t);
 
     // you override DoLog() you might not need it at all
     virtual void DoLogString(const wxChar *szString, time_t t);
 
+    // log a line containing the number of times the previous message was
+    // repeated
+    // returns: the number
+    static unsigned DoLogNumberOfRepeats();
+
 private:
     // static variables
     // ----------------
 
 private:
     // static variables
     // ----------------
 
+    // traditional behaviour or counting repetitions
+    static bool        ms_bRepetCounting;
+    static wxString    ms_prevString;   // previous message that was logged
+    // how many times the previous message was logged
+    static unsigned    ms_prevCounter;
+    static time_t      ms_prevTimeStamp;// timestamp of the previous message
+    static wxLogLevel  ms_prevLevel;    // level of the previous message
+
     static wxLog      *ms_pLogger;      // currently active log sink
     static bool        ms_doLog;        // false => all logging disabled
     static bool        ms_bAutoCreate;  // create new log targets on demand?
     static wxLog      *ms_pLogger;      // currently active log sink
     static bool        ms_doLog;        // false => all logging disabled
     static bool        ms_bAutoCreate;  // create new log targets on demand?
index e2113458647e9d432e419eecf2627bb0ffbf6949..b58b349dacc8541c15ff4e4b09b8dd6469fd7e33 100644 (file)
@@ -357,6 +357,62 @@ void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
 // wxLog class implementation
 // ----------------------------------------------------------------------------
 
 // wxLog class implementation
 // ----------------------------------------------------------------------------
 
+/* static */
+unsigned wxLog::DoLogNumberOfRepeats()
+{
+    long retval = ms_prevCounter;
+    wxLog *pLogger = GetActiveTarget();
+    if ( pLogger && ms_prevCounter > 0 )
+    {
+        wxString msg;
+        msg.Printf(wxPLURAL("The previous message repeated once.",
+                            "The previous message repeated %lu times.",
+                            ms_prevCounter),
+                   ms_prevCounter);
+        ms_prevCounter = 0;
+        ms_prevString.clear();
+        pLogger->DoLog(ms_prevLevel, msg.c_str(), ms_prevTimeStamp);
+    }
+    return retval;
+}
+
+wxLog::~wxLog()
+{
+    if ( ms_prevCounter > 0 )
+    {
+        // looks like the repeat count has not been logged yet,
+        // so let's do it now
+        wxLog::DoLogNumberOfRepeats();
+    }
+}
+
+/* static */
+void wxLog::OnLog(wxLogLevel level, const wxChar *szString, time_t t)
+{
+    if ( IsEnabled() && ms_logLevel >= level )
+    {
+        wxLog *pLogger = GetActiveTarget();
+        if ( pLogger )
+        {
+            if ( GetRepetitionCounting() && ms_prevString == szString )
+            {
+                ms_prevCounter++;
+            }
+            else
+            {
+                if ( GetRepetitionCounting() )
+                {
+                    pLogger->DoLogNumberOfRepeats();
+                }
+                ms_prevString = szString;
+                ms_prevLevel = level;
+                ms_prevTimeStamp = t;
+                pLogger->DoLog(level, szString, t);
+            }
+        }
+    }
+}
+
 wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size)
 {
     wxChar *oldbuf = s_szBuf;
 wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size)
 {
     wxChar *oldbuf = s_szBuf;
@@ -696,6 +752,12 @@ wxLogPassThrough::wxLogPassThrough()
 // static variables
 // ----------------------------------------------------------------------------
 
 // static variables
 // ----------------------------------------------------------------------------
 
+bool            wxLog::ms_bRepetCounting = false;
+wxString        wxLog::ms_prevString;
+size_t          wxLog::ms_prevCounter = 0;
+time_t          wxLog::ms_prevTimeStamp= 0;
+wxLogLevel      wxLog::ms_prevLevel;
+
 wxLog          *wxLog::ms_pLogger      = (wxLog *)NULL;
 bool            wxLog::ms_doLog        = true;
 bool            wxLog::ms_bAutoCreate  = true;
 wxLog          *wxLog::ms_pLogger      = (wxLog *)NULL;
 bool            wxLog::ms_doLog        = true;
 bool            wxLog::ms_bAutoCreate  = true;
index 02e9c0275c9d857fbb2c29f605231c85bd4f485e..50e1457c19b354350e8104ba7d823f22c414a8f3 100644 (file)
@@ -244,6 +244,12 @@ void wxLogGui::Flush()
     // do it right now to block any new calls to Flush() while we're here
     m_bHasMessages = false;
 
     // do it right now to block any new calls to Flush() while we're here
     m_bHasMessages = false;
 
+    unsigned repeatCount = 0;
+    if ( wxLog::GetRepetitionCounting() )
+    {
+        repeatCount = wxLog::DoLogNumberOfRepeats();
+    }
+
     wxString appName = wxTheApp->GetAppName();
     if ( !appName.empty() )
         appName[0u] = (wxChar)wxToupper(appName[0u]);
     wxString appName = wxTheApp->GetAppName();
     if ( !appName.empty() )
         appName[0u] = (wxChar)wxToupper(appName[0u]);
@@ -281,6 +287,8 @@ void wxLogGui::Flush()
     {
 #if wxUSE_LOG_DIALOG
 
     {
 #if wxUSE_LOG_DIALOG
 
+        if ( repeatCount > 0 )
+            m_aMessages[nMsgCount-1] += wxString::Format(wxT(" (%s)"), m_aMessages[nMsgCount-2].c_str());
         wxLogDialog dlg(NULL,
                         m_aMessages, m_aSeverity, m_aTimes,
                         title, style);
         wxLogDialog dlg(NULL,
                         m_aMessages, m_aSeverity, m_aTimes,
                         title, style);