Fix last error display in wxLogSysError().

After recent changes of wxLogXXX() functions into macros the last error was
overwritten by wxString::Format() called between the call to wxLogSysError()
and wxLog::CallDoLogNow() which called wxSysErrorCode() and so its original
value was lost and, unless the last error was specified explicitly, it always
came out as 0.

To fix this, call wxSysErrorCode() directly when calling wxLogSysError(). This
may be unnecessary (if the error is given explicitly) but there doesn't seem
to be any other way to fix it and the overhead of calling wxSysErrorCode()
shouldn't be that big.

Also add a unit test checking that wxLogSysError() behaves as expected.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61692 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-08-18 01:22:48 +00:00
parent 29a35dd5fe
commit b804f9924d
3 changed files with 37 additions and 8 deletions

View File

@@ -168,6 +168,7 @@ private:
CPPUNIT_TEST( CompatLogger );
CPPUNIT_TEST( CompatLogger2 );
#endif // WXWIN_COMPATIBILITY_2_8
CPPUNIT_TEST( SysError );
CPPUNIT_TEST_SUITE_END();
void Functions();
@@ -180,6 +181,7 @@ private:
void CompatLogger();
void CompatLogger2();
#endif // WXWIN_COMPATIBILITY_2_8
void SysError();
TestLog *m_log;
wxLog *m_logOld;
@@ -335,3 +337,21 @@ void LogTestCase::CompatLogger2()
}
#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") );
}