Changed wxLog DoLogXXX() callbacks and introduced wxLogRecordInfo.
[wxWidgets.git] / tests / log / logtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/log/logtest.cpp
3 // Purpose: wxLog unit test
4 // Author: Vadim Zeitlin
5 // Created: 2009-07-07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/log.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/scopeguard.h"
25
26 // ----------------------------------------------------------------------------
27 // test loggers
28 // ----------------------------------------------------------------------------
29
30 // base class for all test loggers which simply store all logged messages for
31 // future examination in the test code
32 class TestLogBase : public wxLog
33 {
34 public:
35 TestLogBase() { }
36
37 wxString GetLog(wxLogLevel level) const
38 {
39 return m_logs[level];
40 }
41
42 void Clear()
43 {
44 for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
45 m_logs[n].clear();
46 }
47
48 protected:
49 wxString m_logs[wxLOG_Trace + 1];
50
51 wxDECLARE_NO_COPY_CLASS(TestLogBase);
52 };
53
54 // simple log sink which just stores the messages logged for each level
55 class TestLog : public TestLogBase
56 {
57 public:
58 TestLog() { }
59
60 protected:
61 virtual void DoLogRecord(wxLogLevel level,
62 const wxString& msg,
63 const wxLogRecordInfo& WXUNUSED(info))
64 {
65 m_logs[level] = msg;
66 }
67
68 private:
69 wxDECLARE_NO_COPY_CLASS(TestLog);
70 };
71
72 #if WXWIN_COMPATIBILITY_2_8
73
74 // log sink overriding the old DoLogXXX() functions should still work too
75
76 // this one overrides DoLog(char*)
77 class CompatTestLog : public TestLogBase
78 {
79 public:
80 CompatTestLog() { }
81
82 protected:
83 virtual void DoLog(wxLogLevel level, const char *str, time_t WXUNUSED(t))
84 {
85 m_logs[level] = str;
86 }
87
88 // get rid of the warning about hiding the other overload
89 virtual void DoLog(wxLogLevel WXUNUSED(level),
90 const wchar_t *WXUNUSED(str),
91 time_t WXUNUSED(t))
92 {
93 }
94
95 private:
96 wxDECLARE_NO_COPY_CLASS(CompatTestLog);
97 };
98
99 // and this one overload DoLogString(wchar_t*)
100 class CompatTestLog2 : public wxLog
101 {
102 public:
103 CompatTestLog2() { }
104
105 const wxString& Get() const { return m_msg; }
106
107 protected:
108 virtual void DoLogString(const wchar_t *msg, time_t WXUNUSED(t))
109 {
110 m_msg = msg;
111 }
112
113 // get rid of the warning
114 virtual void DoLogString(const char *WXUNUSED(msg), time_t WXUNUSED(t))
115 {
116 }
117
118 private:
119 wxString m_msg;
120
121 wxDECLARE_NO_COPY_CLASS(CompatTestLog2);
122 };
123
124 #endif // WXWIN_COMPATIBILITY_2_8
125
126 // ----------------------------------------------------------------------------
127 // test class
128 // ----------------------------------------------------------------------------
129
130 class LogTestCase : public CppUnit::TestCase
131 {
132 public:
133 LogTestCase() { }
134
135 virtual void setUp();
136 virtual void tearDown();
137
138 private:
139 CPPUNIT_TEST_SUITE( LogTestCase );
140 CPPUNIT_TEST( Functions );
141 CPPUNIT_TEST( Null );
142 #if wxDEBUG_LEVEL
143 CPPUNIT_TEST( Trace );
144 #endif // wxDEBUG_LEVEL
145 #if WXWIN_COMPATIBILITY_2_8
146 CPPUNIT_TEST( CompatLogger );
147 CPPUNIT_TEST( CompatLogger2 );
148 #endif // WXWIN_COMPATIBILITY_2_8
149 CPPUNIT_TEST_SUITE_END();
150
151 void Functions();
152 void Null();
153 #if wxDEBUG_LEVEL
154 void Trace();
155 #endif // wxDEBUG_LEVEL
156 #if WXWIN_COMPATIBILITY_2_8
157 void CompatLogger();
158 void CompatLogger2();
159 #endif // WXWIN_COMPATIBILITY_2_8
160
161 TestLog *m_log;
162 wxLog *m_logOld;
163 bool m_logWasEnabled;
164
165 wxDECLARE_NO_COPY_CLASS(LogTestCase);
166 };
167
168 // register in the unnamed registry so that these tests are run by default
169 CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase );
170
171 // also include in it's own registry so that these tests can be run alone
172 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
173
174 void LogTestCase::setUp()
175 {
176 m_logOld = wxLog::SetActiveTarget(m_log = new TestLog);
177 m_logWasEnabled = wxLog::EnableLogging();
178 }
179
180 void LogTestCase::tearDown()
181 {
182 delete wxLog::SetActiveTarget(m_logOld);
183 wxLog::EnableLogging(m_logWasEnabled);
184 }
185
186 void LogTestCase::Functions()
187 {
188 wxLogMessage("Message");
189 CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) );
190
191 wxLogError("Error %d", 17);
192 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) );
193
194 wxLogDebug("Debug");
195 #if wxDEBUG_LEVEL
196 CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) );
197 #else
198 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) );
199 #endif
200 }
201
202 void LogTestCase::Null()
203 {
204 {
205 wxLogNull noLog;
206 wxLogWarning("%s warning", "Not important");
207
208 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) );
209 }
210
211 wxLogWarning("%s warning", "Important");
212 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
213 }
214
215 #if wxDEBUG_LEVEL
216
217 void LogTestCase::Trace()
218 {
219 static const char *TEST_MASK = "test";
220
221 wxLogTrace(TEST_MASK, "Not shown");
222 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
223
224 wxLog::AddTraceMask(TEST_MASK);
225 wxLogTrace(TEST_MASK, "Shown");
226 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
227 m_log->GetLog(wxLOG_Trace) );
228
229 wxLog::RemoveTraceMask(TEST_MASK);
230 m_log->Clear();
231
232 wxLogTrace(TEST_MASK, "Not shown again");
233 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
234 }
235
236 #endif // wxDEBUG_LEVEL
237
238 #if WXWIN_COMPATIBILITY_2_8
239
240 void LogTestCase::CompatLogger()
241 {
242 CompatTestLog log;
243 wxLog * const logOld = wxLog::SetActiveTarget(&log);
244 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
245
246 wxLogError("Old error");
247 CPPUNIT_ASSERT_EQUAL( "Old error", log.GetLog(wxLOG_Error) );
248 }
249
250 void LogTestCase::CompatLogger2()
251 {
252 CompatTestLog2 log;
253 wxLog * const logOld = wxLog::SetActiveTarget(&log);
254 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
255
256 wxLogWarning("Old warning");
257 CPPUNIT_ASSERT_EQUAL( "Old warning", log.Get() );
258 }
259
260 #endif // WXWIN_COMPATIBILITY_2_8