]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/log/logtest.cpp
Merged GSOC Ribbon work from SOC2009_RIBBON branch into trunk.
[wxWidgets.git] / tests / log / logtest.cpp
index f1dbb8cffab1ad197bd86319f9b4bceeb7963491..8c24835b43058c07d99190a820ced7ffa67b4dfb 100644 (file)
@@ -19,6 +19,7 @@
 
 #ifndef WX_PRECOMP
     #include "wx/log.h"
 
 #ifndef WX_PRECOMP
     #include "wx/log.h"
+    #include "wx/filefn.h"
 #endif // WX_PRECOMP
 
 #include "wx/scopeguard.h"
 #endif // WX_PRECOMP
 
 #include "wx/scopeguard.h"
@@ -31,6 +32,9 @@
     #endif // VC++ 7+
 #endif // WXWIN_COMPATIBILITY_2_8
 
     #endif // VC++ 7+
 #endif // WXWIN_COMPATIBILITY_2_8
 
+// all calls to wxLogXXX() functions from this file will use this log component
+#define wxLOG_COMPONENT "test"
+
 // ----------------------------------------------------------------------------
 // test loggers
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // test loggers
 // ----------------------------------------------------------------------------
@@ -42,19 +46,28 @@ class TestLogBase : public wxLog
 public:
     TestLogBase() { }
 
 public:
     TestLogBase() { }
 
-    wxString GetLog(wxLogLevel level) const
+    const wxString& GetLog(wxLogLevel level) const
     {
         return m_logs[level];
     }
 
     {
         return m_logs[level];
     }
 
+    const wxLogRecordInfo& GetInfo(wxLogLevel level) const
+    {
+        return m_logsInfo[level];
+    }
+
     void Clear()
     {
         for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
     void Clear()
     {
         for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
+        {
             m_logs[n].clear();
             m_logs[n].clear();
+            m_logsInfo[n] = wxLogRecordInfo();
+        }
     }
 
 protected:
     wxString m_logs[wxLOG_Trace + 1];
     }
 
 protected:
     wxString m_logs[wxLOG_Trace + 1];
+    wxLogRecordInfo m_logsInfo[wxLOG_Trace + 1];
 
     wxDECLARE_NO_COPY_CLASS(TestLogBase);
 };
 
     wxDECLARE_NO_COPY_CLASS(TestLogBase);
 };
@@ -68,9 +81,10 @@ public:
 protected:
     virtual void DoLogRecord(wxLogLevel level,
                              const wxString& msg,
 protected:
     virtual void DoLogRecord(wxLogLevel level,
                              const wxString& msg,
-                             const wxLogRecordInfo& WXUNUSED(info))
+                             const wxLogRecordInfo& info)
     {
         m_logs[level] = msg;
     {
         m_logs[level] = msg;
+        m_logsInfo[level] = info;
     }
 
 private:
     }
 
 private:
@@ -147,6 +161,7 @@ private:
     CPPUNIT_TEST_SUITE( LogTestCase );
         CPPUNIT_TEST( Functions );
         CPPUNIT_TEST( Null );
     CPPUNIT_TEST_SUITE( LogTestCase );
         CPPUNIT_TEST( Functions );
         CPPUNIT_TEST( Null );
+        CPPUNIT_TEST( Component );
 #if wxDEBUG_LEVEL
         CPPUNIT_TEST( Trace );
 #endif // wxDEBUG_LEVEL
 #if wxDEBUG_LEVEL
         CPPUNIT_TEST( Trace );
 #endif // wxDEBUG_LEVEL
@@ -154,10 +169,12 @@ private:
         CPPUNIT_TEST( CompatLogger );
         CPPUNIT_TEST( CompatLogger2 );
 #endif // WXWIN_COMPATIBILITY_2_8
         CPPUNIT_TEST( CompatLogger );
         CPPUNIT_TEST( CompatLogger2 );
 #endif // WXWIN_COMPATIBILITY_2_8
+        CPPUNIT_TEST( SysError );
     CPPUNIT_TEST_SUITE_END();
 
     void Functions();
     void Null();
     CPPUNIT_TEST_SUITE_END();
 
     void Functions();
     void Null();
+    void Component();
 #if wxDEBUG_LEVEL
     void Trace();
 #endif // wxDEBUG_LEVEL
 #if wxDEBUG_LEVEL
     void Trace();
 #endif // wxDEBUG_LEVEL
@@ -165,6 +182,7 @@ private:
     void CompatLogger();
     void CompatLogger2();
 #endif // WXWIN_COMPATIBILITY_2_8
     void CompatLogger();
     void CompatLogger2();
 #endif // WXWIN_COMPATIBILITY_2_8
+    void SysError();
 
     TestLog *m_log;
     wxLog *m_logOld;
 
     TestLog *m_log;
     wxLog *m_logOld;
@@ -220,24 +238,78 @@ void LogTestCase::Null()
     CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
 }
 
     CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
 }
 
+void LogTestCase::Component()
+{
+    wxLogMessage("Message");
+    CPPUNIT_ASSERT_EQUAL( wxLOG_COMPONENT,
+                          m_log->GetInfo(wxLOG_Message).component );
+
+    // completely disable logging for this component
+    wxLog::SetComponentLevel("test/ignore", wxLOG_FatalError);
+
+    // but enable it for one of its subcomponents
+    wxLog::SetComponentLevel("test/ignore/not", wxLOG_Max);
+
+    #undef wxLOG_COMPONENT
+    #define wxLOG_COMPONENT "test/ignore"
+
+    // this shouldn't be output as this component is ignored
+    wxLogError("Error");
+    CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
+
+    // and so are its subcomponents
+    #undef wxLOG_COMPONENT
+    #define wxLOG_COMPONENT "test/ignore/sub/subsub"
+    wxLogError("Error");
+    CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
+
+    // but one subcomponent is not
+    #undef wxLOG_COMPONENT
+    #define wxLOG_COMPONENT "test/ignore/not"
+    wxLogError("Error");
+    CPPUNIT_ASSERT_EQUAL( "Error", m_log->GetLog(wxLOG_Error) );
+
+    // restore the original value
+    #undef wxLOG_COMPONENT
+    #define wxLOG_COMPONENT "test"
+}
+
 #if wxDEBUG_LEVEL
 
 #if wxDEBUG_LEVEL
 
+namespace
+{
+
+const char *TEST_MASK = "test";
+
+// this is a test vararg function (a real one, not a variadic-template-like as
+// wxVLogTrace(), so care should be taken with its arguments)
+void TraceTest(const char *format, ...)
+{
+    va_list argptr;
+    va_start(argptr, format);
+    wxVLogTrace(TEST_MASK, format, argptr);
+    va_end(argptr);
+}
+
+} // anonymous namespace
+
 void LogTestCase::Trace()
 {
 void LogTestCase::Trace()
 {
-    static const char *TEST_MASK = "test";
+    // we use wxLogTrace() or wxVLogTrace() from inside TraceTest()
+    // interchangeably here, it shouldn't make any difference
 
     wxLogTrace(TEST_MASK, "Not shown");
     CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
 
     wxLog::AddTraceMask(TEST_MASK);
 
     wxLogTrace(TEST_MASK, "Not shown");
     CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
 
     wxLog::AddTraceMask(TEST_MASK);
-    wxLogTrace(TEST_MASK, "Shown");
+    TraceTest("Shown");
     CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
                           m_log->GetLog(wxLOG_Trace) );
 
     wxLog::RemoveTraceMask(TEST_MASK);
     m_log->Clear();
 
     CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
                           m_log->GetLog(wxLOG_Trace) );
 
     wxLog::RemoveTraceMask(TEST_MASK);
     m_log->Clear();
 
-    wxLogTrace(TEST_MASK, "Not shown again");
+    TraceTest("Not shown again");
     CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
 }
 
     CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
 }
 
@@ -266,3 +338,21 @@ void LogTestCase::CompatLogger2()
 }
 
 #endif // WXWIN_COMPATIBILITY_2_8
 }
 
 #endif // WXWIN_COMPATIBILITY_2_8
+
+void LogTestCase::SysError()
+{
+    wxString s;
+    wxLogSysError("Success");
+    CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Success (", &s) );
+    CPPUNIT_ASSERT( s.StartsWith("error 0") );
+
+    wxLogSysError(17, "Error");
+    CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Error (", &s) );
+    CPPUNIT_ASSERT( s.StartsWith("error 17") );
+
+    wxOpen("no-such-file", 0, 0);
+    wxLogSysError("Not found");
+    CPPUNIT_ASSERT( m_log->GetLog(wxLOG_Error).StartsWith("Not found (", &s) );
+    WX_ASSERT_MESSAGE( ("Error message is \"(%s\"", s), s.StartsWith("error 2") );
+}
+