1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/log/logtest.cpp
3 // Purpose: wxLog unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/scopeguard.h"
26 #if WXWIN_COMPATIBILITY_2_8
27 // we override deprecated DoLog() and DoLogString() in this test, suppress
29 #if wxCHECK_VISUALC_VERSION(7)
30 #pragma warning(disable: 4996)
32 #endif // WXWIN_COMPATIBILITY_2_8
34 // all calls to wxLogXXX() functions from this file will use this log component
35 #define wxLOG_COMPONENT "test"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // base class for all test loggers which simply store all logged messages for
42 // future examination in the test code
43 class TestLogBase
: public wxLog
48 const wxString
& GetLog(wxLogLevel level
) const
53 const wxLogRecordInfo
& GetInfo(wxLogLevel level
) const
55 return m_logsInfo
[level
];
60 for ( unsigned n
= 0; n
< WXSIZEOF(m_logs
); n
++ )
63 m_logsInfo
[n
] = wxLogRecordInfo();
68 wxString m_logs
[wxLOG_Trace
+ 1];
69 wxLogRecordInfo m_logsInfo
[wxLOG_Trace
+ 1];
71 wxDECLARE_NO_COPY_CLASS(TestLogBase
);
74 // simple log sink which just stores the messages logged for each level
75 class TestLog
: public TestLogBase
81 virtual void DoLogRecord(wxLogLevel level
,
83 const wxLogRecordInfo
& info
)
86 m_logsInfo
[level
] = info
;
90 wxDECLARE_NO_COPY_CLASS(TestLog
);
93 #if WXWIN_COMPATIBILITY_2_8
95 // log sink overriding the old DoLogXXX() functions should still work too
97 // this one overrides DoLog(char*)
98 class CompatTestLog
: public TestLogBase
104 virtual void DoLog(wxLogLevel level
, const char *str
, time_t WXUNUSED(t
))
109 // get rid of the warning about hiding the other overload
110 virtual void DoLog(wxLogLevel
WXUNUSED(level
),
111 const wchar_t *WXUNUSED(str
),
117 wxDECLARE_NO_COPY_CLASS(CompatTestLog
);
120 // and this one overload DoLogString(wchar_t*)
121 class CompatTestLog2
: public wxLog
126 const wxString
& Get() const { return m_msg
; }
129 virtual void DoLogString(const wchar_t *msg
, time_t WXUNUSED(t
))
134 // get rid of the warning
135 virtual void DoLogString(const char *WXUNUSED(msg
), time_t WXUNUSED(t
))
142 wxDECLARE_NO_COPY_CLASS(CompatTestLog2
);
145 #endif // WXWIN_COMPATIBILITY_2_8
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 class LogTestCase
: public CppUnit::TestCase
156 virtual void setUp();
157 virtual void tearDown();
160 CPPUNIT_TEST_SUITE( LogTestCase
);
161 CPPUNIT_TEST( Functions
);
162 CPPUNIT_TEST( Null
);
163 CPPUNIT_TEST( Component
);
165 CPPUNIT_TEST( Trace
);
166 #endif // wxDEBUG_LEVEL
167 #if WXWIN_COMPATIBILITY_2_8
168 CPPUNIT_TEST( CompatLogger
);
169 CPPUNIT_TEST( CompatLogger2
);
170 #endif // WXWIN_COMPATIBILITY_2_8
171 CPPUNIT_TEST_SUITE_END();
178 #endif // wxDEBUG_LEVEL
179 #if WXWIN_COMPATIBILITY_2_8
181 void CompatLogger2();
182 #endif // WXWIN_COMPATIBILITY_2_8
186 bool m_logWasEnabled
;
188 wxDECLARE_NO_COPY_CLASS(LogTestCase
);
191 // register in the unnamed registry so that these tests are run by default
192 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase
);
194 // also include in it's own registry so that these tests can be run alone
195 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase
, "LogTestCase" );
197 void LogTestCase::setUp()
199 m_logOld
= wxLog::SetActiveTarget(m_log
= new TestLog
);
200 m_logWasEnabled
= wxLog::EnableLogging();
203 void LogTestCase::tearDown()
205 delete wxLog::SetActiveTarget(m_logOld
);
206 wxLog::EnableLogging(m_logWasEnabled
);
209 void LogTestCase::Functions()
211 wxLogMessage("Message");
212 CPPUNIT_ASSERT_EQUAL( "Message", m_log
->GetLog(wxLOG_Message
) );
214 wxLogError("Error %d", 17);
215 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log
->GetLog(wxLOG_Error
) );
219 CPPUNIT_ASSERT_EQUAL( "Debug", m_log
->GetLog(wxLOG_Debug
) );
221 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Debug
) );
225 void LogTestCase::Null()
229 wxLogWarning("%s warning", "Not important");
231 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Warning
) );
234 wxLogWarning("%s warning", "Important");
235 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log
->GetLog(wxLOG_Warning
) );
238 void LogTestCase::Component()
240 wxLogMessage("Message");
241 CPPUNIT_ASSERT_EQUAL( wxLOG_COMPONENT
,
242 m_log
->GetInfo(wxLOG_Message
).component
);
244 // completely disable logging for this component
245 wxLog::SetComponentLevel("test/ignore", wxLOG_FatalError
);
247 // but enable it for one of its subcomponents
248 wxLog::SetComponentLevel("test/ignore/not", wxLOG_Max
);
250 #undef wxLOG_COMPONENT
251 #define wxLOG_COMPONENT "test/ignore"
253 // this shouldn't be output as this component is ignored
255 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Error
) );
257 // and so are its subcomponents
258 #undef wxLOG_COMPONENT
259 #define wxLOG_COMPONENT "test/ignore/sub/subsub"
261 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Error
) );
263 // but one subcomponent is not
264 #undef wxLOG_COMPONENT
265 #define wxLOG_COMPONENT "test/ignore/not"
267 CPPUNIT_ASSERT_EQUAL( "Error", m_log
->GetLog(wxLOG_Error
) );
269 // restore the original value
270 #undef wxLOG_COMPONENT
271 #define wxLOG_COMPONENT "test"
276 void LogTestCase::Trace()
278 static const char *TEST_MASK
= "test";
280 wxLogTrace(TEST_MASK
, "Not shown");
281 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
283 wxLog::AddTraceMask(TEST_MASK
);
284 wxLogTrace(TEST_MASK
, "Shown");
285 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK
),
286 m_log
->GetLog(wxLOG_Trace
) );
288 wxLog::RemoveTraceMask(TEST_MASK
);
291 wxLogTrace(TEST_MASK
, "Not shown again");
292 CPPUNIT_ASSERT_EQUAL( "", m_log
->GetLog(wxLOG_Trace
) );
295 #endif // wxDEBUG_LEVEL
297 #if WXWIN_COMPATIBILITY_2_8
299 void LogTestCase::CompatLogger()
302 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
303 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
305 wxLogError("Old error");
306 CPPUNIT_ASSERT_EQUAL( "Old error", log
.GetLog(wxLOG_Error
) );
309 void LogTestCase::CompatLogger2()
312 wxLog
* const logOld
= wxLog::SetActiveTarget(&log
);
313 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget
, logOld
);
315 wxLogWarning("Old warning");
316 CPPUNIT_ASSERT_EQUAL( "Old warning", log
.Get() );
319 #endif // WXWIN_COMPATIBILITY_2_8