From 39dc254bf4d5be10dcaba6561b569e21298896a4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 29 Nov 2017 02:22:16 +0100 Subject: [PATCH] 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 --- include/wx/log.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/wx/log.h b/include/wx/log.h index d6945eb0d2..3d7d273cbe 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -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); }