Don't call time() from wxLogRecordInfo ctor

This is a tiny optimization (or maybe not so tiny on platforms other
than Linux where time() might not as fast as just reading a memory
location), but mostly is done to work around faketime bug[*] which
prevented it from being used for testing programs using wxWidgets, such
as our own unit tests because time() was called from wxLogTrace() in
wxCSConv::DoCreate() called when creating global conversion objects
during the library initialization.

Arguably, it might be better to avoid calling wxLogTrace() during the
initialization, but this can't be done as simply and this change might
have a small performance benefit too.

[*] https://github.com/wolfcw/libfaketime/issues/132
This commit is contained in:
Vadim Zeitlin
2017-11-29 02:22:16 +01:00
parent 5b2f7aa96d
commit 39dc254bf4

View File

@@ -158,7 +158,11 @@ public:
line = line_;
component = component_;
timestamp = time(NULL);
// don't initialize the timestamp yet, we might not need it at all if
// the message doesn't end up being logged and otherwise we'll fill it
// just before logging it, which won't change it by much and definitely
// less than a second resolution of the timestamp
timestamp = 0;
#if wxUSE_THREADS
threadId = wxThread::GetCurrentId();
@@ -1162,6 +1166,11 @@ private:
void DoCallOnLog(wxLogLevel level, const wxString& format, va_list argptr)
{
// As explained in wxLogRecordInfo ctor, we don't initialize its
// timestamp to avoid calling time() unnecessary, but now that we are
// about to log the message, we do need to do it.
m_info.timestamp = time(NULL);
wxLog::OnLog(level, wxString::FormatV(format, argptr), m_info);
}