Improved logging code. Each log line now has a timestamp.

This commit is contained in:
LoRd_MuldeR 2016-12-14 15:08:34 +01:00
parent 36f35075ed
commit b160867111
7 changed files with 51 additions and 22 deletions

View File

@ -295,7 +295,7 @@ QModelIndex JobListModel::insertJob(EncodeThread *thread)
connect(thread, SIGNAL(statusChanged(QUuid, JobStatus)), this, SLOT(updateStatus(QUuid, JobStatus)), Qt::QueuedConnection);
connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
connect(thread, SIGNAL(messageLogged(QUuid, QString)), logFile, SLOT(addLogMessage(QUuid, QString)), Qt::QueuedConnection);
connect(thread, SIGNAL(messageLogged(QUuid, qint64, QString)), logFile, SLOT(addLogMessage(QUuid, qint64, QString)), Qt::QueuedConnection);
connect(thread, SIGNAL(detailsChanged(QUuid, QString)), this, SLOT(updateDetails(QUuid, QString)), Qt::QueuedConnection);
return createIndex(m_jobs.count() - 1, 0, NULL);

View File

@ -27,13 +27,18 @@
#include <QClipboard>
#include <QDir>
#include <QTextStream>
#include <QDateTime>
static const QLatin1String FMT_TIMESTAMP("[yyyy-MM-dd][HH:mm:ss] ");
LogFileModel::LogFileModel(const QString &sourceName, const QString &outputName, const QString &configName)
{
m_lines << "Job not started yet." << QString();
m_lines << QString("Scheduled source: %1").arg(QDir::toNativeSeparators(sourceName));
m_lines << QString("Scheduled output: %1").arg(QDir::toNativeSeparators(outputName));
m_lines << QString("Scheduled config: %1").arg(configName);
const qint64 timeStamp = QDateTime::currentMSecsSinceEpoch();
m_lines << qMakePair(timeStamp, QString("Job not started yet."));
m_lines << qMakePair(timeStamp, QString());
m_lines << qMakePair(timeStamp, QString("Scheduled source: %1").arg(QDir::toNativeSeparators(sourceName)));
m_lines << qMakePair(timeStamp, QString("Scheduled output: %1").arg(QDir::toNativeSeparators(outputName)));
m_lines << qMakePair(timeStamp, QString("Scheduled config: %1").arg(configName));
m_firstLine = true;
}
@ -76,7 +81,16 @@ QVariant LogFileModel::data(const QModelIndex &index, int role) const
{
if(index.row() >= 0 && index.row() < m_lines.count() && index.column() == 0)
{
return m_lines.at(index.row());
if (role == Qt::ToolTipRole)
{
const LogEntry &entry = m_lines.at(index.row());
const QString timeStamp = QDateTime::fromMSecsSinceEpoch(entry.first).toString(FMT_TIMESTAMP);
return timeStamp + entry.second;
}
else
{
return m_lines.at(index.row()).second;
}
}
}
@ -89,8 +103,14 @@ QVariant LogFileModel::data(const QModelIndex &index, int role) const
void LogFileModel::copyToClipboard(void)
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(m_lines.join("\r\n"));
QClipboard *const clipboard = QApplication::clipboard();
QStringList buffer;
for (QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
{
const QString timeStamp = QDateTime::fromMSecsSinceEpoch(iter->first).toString(FMT_TIMESTAMP);
buffer << (timeStamp + iter->second);
}
clipboard->setText(buffer.join("\r\n"));
}
bool LogFileModel::saveToLocalFile(const QString &fileName)
@ -105,9 +125,10 @@ bool LogFileModel::saveToLocalFile(const QString &fileName)
stream.setCodec("UTF-8");
stream.setGenerateByteOrderMark(true);
for(QStringList::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
for(QList<LogEntry>::ConstIterator iter = m_lines.constBegin(); iter != m_lines.constEnd(); iter++)
{
stream << (*iter) << QLatin1String("\r\n");
const QString timeStamp = QDateTime::fromMSecsSinceEpoch(iter->first).toString(FMT_TIMESTAMP);
stream << timeStamp << iter->second << QLatin1String("\r\n");
if(stream.status() != QTextStream::Status::Ok)
{
file.close();
@ -130,7 +151,7 @@ bool LogFileModel::saveToLocalFile(const QString &fileName)
// Slots
///////////////////////////////////////////////////////////////////////////////
void LogFileModel::addLogMessage(const QUuid &jobId, const QString &text)
void LogFileModel::addLogMessage(const QUuid &jobId, const qint64 &timeStamp, const QString &text)
{
beginInsertRows(QModelIndex(), m_lines.count(), m_lines.count());
@ -140,10 +161,10 @@ void LogFileModel::addLogMessage(const QUuid &jobId, const QString &text)
m_lines.clear();
}
QStringList lines = text.split("\n");
for(int i = 0; i < lines.count(); i++)
const QStringList lines = text.split("\n");
for(QStringList::ConstIterator iter = lines.constBegin(); iter != lines.constEnd(); iter++)
{
m_lines.append(lines.at(i));
m_lines << qMakePair(timeStamp, (*iter));
}
endInsertRows();

View File

@ -25,7 +25,7 @@
#include "QAbstractItemModel"
#include <QUuid>
#include <QStringList>
#include <QList>
#include <QMap>
class LogFileModel : public QAbstractItemModel
@ -47,9 +47,10 @@ public:
bool saveToLocalFile(const QString &fileName);
protected:
QStringList m_lines;
bool m_firstLine;
typedef QPair<qint64, QString> LogEntry;
QList<LogEntry> m_lines;
public slots:
void addLogMessage(const QUuid &jobId, const QString &text);
void addLogMessage(const QUuid &jobId, const qint64 &timeStamp, const QString &text);
};

View File

@ -363,7 +363,7 @@ void EncodeThread::encode(void)
void EncodeThread::log(const QString &text)
{
emit messageLogged(m_jobId, text);
emit messageLogged(m_jobId, QDateTime::currentMSecsSinceEpoch(), text);
}
void EncodeThread::setStatus(const JobStatus &newStatus)

View File

@ -111,7 +111,7 @@ protected:
signals:
void statusChanged(const QUuid &jobId, const JobStatus &newStatus);
void progressChanged(const QUuid &jobId, const unsigned int &newProgress);
void messageLogged(const QUuid &jobId, const QString &text);
void messageLogged(const QUuid &jobId, qint64, const QString &text);
void detailsChanged(const QUuid &jobId, const QString &details);
private slots:

View File

@ -26,7 +26,7 @@
#define VER_X264_MAJOR 2
#define VER_X264_MINOR 7
#define VER_X264_PATCH 7
#define VER_X264_BUILD 1066
#define VER_X264_BUILD 1069
#define VER_X264_PORTABLE_EDITION (0)

View File

@ -735,8 +735,15 @@ void MainWindow::saveLogFile(const QModelIndex &index)
{
if(LogFileModel *log = m_jobList->getLogFile(index))
{
QDir(QString("%1/logs").arg(x264_data_path())).mkpath(".");
QString logFilePath = QString("%1/logs/LOG.%2.%3.txt").arg(x264_data_path(), QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString(Qt::ISODate).replace(':', "-"));
const QString outputDir = QString("%1/logs").arg(x264_data_path());
const QString timeStamp = QDateTime::currentDateTime().toString("yyyy-MM-dd.HH-mm-ss");
QDir(outputDir).mkpath(".");
const QString logFilePath = MUtils::make_unique_file(outputDir, QString("LOG.%1").arg(timeStamp), QLatin1String("txt"));
if(logFilePath.isEmpty())
{
qWarning("Failed to generate log file name. Giving up!");
return;
}
if(!log->saveToLocalFile(logFilePath))
{
qWarning("Failed to open log file for writing:\n%s", logFilePath.toUtf8().constData());