Skip to content

Commit

Permalink
filesink: Use QElapsedTimer to avoid system time problems
Browse files Browse the repository at this point in the history
Do not trust user or OS, time can change while log is running

Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jul 15, 2019
1 parent fa945e9 commit 40a9223
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/link/filelink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Q_LOGGING_CATEGORY(PING_PROTOCOL_FILELINK, "ping.protocol.filelink")
FileLink::FileLink(QObject* parent)
: AbstractLink(parent)
, _openModeFlag(QIODevice::ReadWrite)
, _time(QTime::currentTime())
, _timer()
, _inout(&_file)
, _logThread(nullptr)
{
_timer.start();
setType(LinkType::File);

connect(this, &AbstractLink::sendData, this, &FileLink::_writeData);
}

Expand All @@ -34,7 +34,7 @@ void FileLink::_writeData(const QByteArray& data)

// This save the data as a structure to deal with the timestamp
if(_openModeFlag == QIODevice::WriteOnly && _file.isWritable()) {
QString time = _time.currentTime().toString(_timeFormat);
QString time = QTime::fromMSecsSinceStartOfDay(_timer.elapsed()).toString(_timeFormat);
Pack pack{time, data};
_inout << pack.time << pack.data;
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/link/filelink.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <QDataStream>
#include <QElapsedTimer>
#include <QFile>
#include <QTime>

Expand Down Expand Up @@ -133,7 +134,7 @@ class FileLink : public AbstractLink
};

QIODevice::OpenModeFlag _openModeFlag;
QTime _time;
QElapsedTimer _timer;

QFile _file;
QDataStream _inout;
Expand Down

0 comments on commit 40a9223

Please sign in to comment.