Really support milliseconds in wxLog

Although this was supposed to work, specifying "%l" in wxLog time stamp
format actually didn't because wxLog timestamps were stored as seconds.

Fix this by storing them as milliseconds and add a simple test (not
executed by default) showing that "%l" works correctly now.

Still keep the old wxLogRecordInfo::timestamp field for compatibility,
as it was documented.

See #13059.
This commit is contained in:
Vadim Zeitlin
2021-01-13 23:50:44 +01:00
parent 89bc7b522f
commit e6cedf6649
6 changed files with 99 additions and 11 deletions

View File

@@ -62,6 +62,7 @@ class WXDLLIMPEXP_FWD_BASE wxObject;
#include "wx/dynarray.h"
#include "wx/hashmap.h"
#include "wx/msgout.h"
#include "wx/time.h"
#if wxUSE_THREADS
#include "wx/thread.h"
@@ -160,9 +161,11 @@ public:
// 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
// just before logging it, which won't change it by much
timestampMS = 0;
#if WXWIN_COMPATIBILITY_3_0
timestamp = 0;
#endif // WXWIN_COMPATIBILITY_3_0
#if wxUSE_THREADS
threadId = wxThread::GetCurrentId();
@@ -208,8 +211,13 @@ public:
// not set (i.e. wxLOG_COMPONENT not defined). It must be in ASCII.
const char *component;
// time of record generation
// time of record generation in milliseconds since Epoch
wxLongLong_t timestampMS;
#if WXWIN_COMPATIBILITY_3_0
// preserved for compatibility only, use timestampMS instead now
time_t timestamp;
#endif // WXWIN_COMPATIBILITY_3_0
#if wxUSE_THREADS
// id of the thread which logged this record
@@ -334,7 +342,12 @@ public:
protected:
// Override this method to change just the time stamp formatting. It is
// called by default Format() implementation.
virtual wxString FormatTimeMS(wxLongLong_t msec) const;
#if WXWIN_COMPATIBILITY_3_0
// Old function which only worked at second resolution.
virtual wxString FormatTime(time_t t) const;
#endif // WXWIN_COMPATIBILITY_3_0
};
@@ -1166,7 +1179,11 @@ private:
// 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);
m_info.timestampMS = wxGetUTCTimeMillis().GetValue();
#if WXWIN_COMPATIBILITY_3_0
m_info.timestamp = m_info.timestampMS / 1000;
#endif // WXWIN_COMPATIBILITY_3_0
wxLog::OnLog(level, wxString::FormatV(format, argptr), m_info);
}