refs #863 Move interpolator logging into a separate class.

A single logger instance can be shared between multiple interpolator instances.
This commit is contained in:
Mathew Sutcliffe
2017-02-06 19:24:39 +00:00
parent e67b5fccc4
commit 04ddd3bfa5
16 changed files with 419 additions and 318 deletions

View File

@@ -363,9 +363,8 @@ namespace BlackCore
}
if (p == "clear" || p == "clr")
{
//! \todo refactoring broken by rebase
//this->m_interpolator->clearLog();
//CStatusMessage(this).info("Cleared interpolation logging");
this->m_interpolationLogger.clearLog();
CStatusMessage(this).info("Cleared interpolation logging");
return true;
}
if (p == "write" || p == "save")
@@ -374,9 +373,8 @@ namespace BlackCore
this->m_interpolationRenderingSetup.clearInterpolatorLogCallsigns();
// write
//! \todo refactoring broken by rebase
//this->m_interpolator->writeLogInBackground();
//CLogMessage(this).info("Started writing interpolation log");
this->m_interpolationLogger.writeLogInBackground();
CLogMessage(this).info("Started writing interpolation log");
return true;
}

View File

@@ -31,6 +31,7 @@
#include "blackmisc/simulation/simulatorsettings.h"
#include "blackmisc/simulation/interpolationrenderingsetup.h"
#include "blackmisc/simulation/interpolationhints.h"
#include "blackmisc/simulation/interpolationlogger.h"
#include "blackmisc/weather/weathergridprovider.h"
#include "blackmisc/pq/length.h"
#include "blackmisc/pq/time.h"
@@ -184,6 +185,7 @@ namespace BlackCore
BlackMisc::Simulation::CSimulatorInternals m_simulatorInternals; //!< setup object
BlackMisc::Simulation::CInterpolationAndRenderingSetup m_interpolationRenderingSetup; //!< logging, rendering etc.
mutable QReadWriteLock m_interpolationRenderingSetupMutex; //!< mutex protecting setup object
BlackMisc::Simulation::CInterpolationLogger m_interpolationLogger; //!< log interpolation
// some optional functionality which can be used by the sims as needed
BlackMisc::Simulation::CSimulatedAircraftList m_aircraftToAddAgainWhenRemoved; //!< add this model again when removed, normally used to change model

View File

@@ -0,0 +1,256 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "interpolationlogger.h"
#include "blackconfig/buildconfig.h"
#include "blackmisc/aviation/callsign.h"
#include "blackmisc/aviation/heading.h"
#include "blackmisc/pq/angle.h"
#include "blackmisc/pq/speed.h"
#include "blackmisc/pq/units.h"
#include "blackmisc/pq/length.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/worker.h"
#include "blackmisc/directoryutils.h"
#include <QDateTime>
#include <QStringBuilder>
using namespace BlackConfig;
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
using namespace BlackMisc::Geo;
using namespace BlackMisc::Math;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Simulation;
namespace BlackMisc
{
namespace Simulation
{
CInterpolationLogger::CInterpolationLogger(QObject *parent) :
QObject(parent)
{
this->setObjectName("CInterpolationLogger");
}
CWorker *CInterpolationLogger::writeLogInBackground()
{
QList<SituationLog> interpolation;
QList<PartsLog> parts;
{
QReadLocker l(&m_lockLogs);
interpolation = m_situationLogs;
parts = m_partsLogs;
}
CWorker *worker = CWorker::fromTask(this, "WriteInterpolationLog", [interpolation, parts]()
{
const CStatusMessageList msg = CInterpolationLogger::writeLogFile(interpolation, parts);
CLogMessage::preformatted(msg);
});
return worker;
}
QStringList CInterpolationLogger::getLatestLogFiles()
{
QStringList files({ "", ""});
const QString logDir = CDirectoryUtils::getLogDirectory();
QDir logs(logDir);
if (!logs.exists()) { return files; }
logs.setNameFilters(QStringList() << "*interpolation.html" << "*parts.html");
const QStringList interpolations = logs.entryList(QStringList({"*interpolation.html"}), QDir::NoFilter, QDir::Time);
if (!interpolations.isEmpty())
{
files[0] = CFileUtils::appendFilePaths(logDir, interpolations.first());
}
const QStringList parts = logs.entryList(QStringList({"*parts.html"}), QDir::NoFilter, QDir::Time);
if (!parts.isEmpty())
{
files[1] = CFileUtils::appendFilePaths(logDir, parts.first());
}
return files;
}
CStatusMessageList CInterpolationLogger::writeLogFile(const QList<SituationLog> &interpolation, const QList<PartsLog> &parts)
{
if (parts.isEmpty() && interpolation.isEmpty()) { return CStatusMessage(static_cast<CInterpolationLogger *>(nullptr)).warning("No data for log"); }
static const QString html = QStringLiteral("Entries: %1\n\n%2");
const QString htmlTemplate = CFileUtils::readFileToString(CBuildConfig::getHtmlTemplateFileName());
CStatusMessageList msgs;
const QString ts = QDateTime::currentDateTimeUtc().toString("yyyyMMddhhmmss");
const QString htmlInterpolation = CInterpolationLogger::getHtmlInterpolationLog(interpolation);
if (!htmlInterpolation.isEmpty())
{
const QString fn = CFileUtils::appendFilePaths(CDirectoryUtils::getLogDirectory(), QString("%1 interpolation.html").arg(ts));
const bool s = CFileUtils::writeStringToFile(htmlTemplate.arg(html.arg(interpolation.size()).arg(htmlInterpolation)), fn);
msgs.push_back(CInterpolationLogger::logStatusFileWriting(s, fn));
}
const QString htmlParts = CInterpolationLogger::getHtmlPartsLog(parts);
if (!htmlParts.isEmpty())
{
const QString fn = CFileUtils::appendFilePaths(CDirectoryUtils::getLogDirectory(), QString("%1 parts.html").arg(ts));
const bool s = CFileUtils::writeStringToFile(htmlTemplate.arg(html.arg(parts.size()).arg(htmlParts)), fn);
msgs.push_back(CInterpolationLogger::logStatusFileWriting(s, fn));
}
return msgs;
}
CStatusMessage CInterpolationLogger::logStatusFileWriting(bool success, const QString &fileName)
{
if (success)
{
return CStatusMessage(static_cast<CInterpolationLogger *>(nullptr)).info("Written log file '%1'") << fileName;
}
else
{
return CStatusMessage(static_cast<CInterpolationLogger *>(nullptr)).error("Failed to write log file '%1'") << fileName;
}
}
void CInterpolationLogger::logInterpolation(const CInterpolationLogger::SituationLog &log)
{
QWriteLocker l(&m_lockLogs);
m_situationLogs.append(log);
}
void CInterpolationLogger::logParts(const CInterpolationLogger::PartsLog &parts)
{
QWriteLocker l(&m_lockLogs);
m_partsLogs.append(parts);
}
QString CInterpolationLogger::getHtmlInterpolationLog(const QList<SituationLog> &logs)
{
if (logs.isEmpty()) { return {}; }
const QString tableHeader =
QLatin1String("<thead><tr>") %
QLatin1String("<th>c.</th><th>CS</th><th>VTOL</th><th>timestamp</th><th>since</th>") %
QLatin1String("<th>ts old</th><th>ts new</th><th>ts cur</th>") %
QLatin1String("<th>&Delta;t</th><th>&Delta;t fr.</th><th>fraction</th>") %
QLatin1String("<th>lat.old</th><th>lat.new</th><th>lat.cur</th>") %
QLatin1String("<th>lng.old</th><th>lng.new</th><th>lng.cur</th>") %
QLatin1String("<th>alt.old</th><th>alt.new</th><th>alt.cur</th>") %
QLatin1String("<th>elv.old</th><th>elv.new</th><th>elv.cur</th>") %
QLatin1String("<th>gnd.factor</th>") %
QLatin1String("<th>onGnd.old</th><th>onGnd.new</th><th>onGnd.cur</th>") %
QLatin1String("<th>parts</th><th>c.</th><th>parts details</th>") %
QLatin1String("</tr></thead>\n");
static const CLengthUnit ft = CLengthUnit::ft();
const SituationLog firstLog = logs.first();
qint64 newPosTs = firstLog.newSituation.getMSecsSinceEpoch();
CAircraftParts lastParts; // default, so shown if parts are different from default
QString tableRows("<tbody>\n");
for (const SituationLog &log : logs)
{
const bool changedNewPosition = newPosTs != log.newSituation.getMSecsSinceEpoch();
const bool changedParts = lastParts != log.parts;
newPosTs = log.newSituation.getMSecsSinceEpoch();
lastParts = log.parts;
// concatenating in multiple steps, otherwise C4503 warnings
tableRows +=
QLatin1String("<tr>") %
(changedNewPosition ? QLatin1String("<td class=\"changed\">*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % log.callsign.asString() % QLatin1String("</td>") %
QLatin1String("<td>") % boolToYesNo(log.vtolAircraft) % QLatin1String("</td>") %
QLatin1String("<td>") % msSinceEpochToTime(log.timestamp) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.timestamp - firstLog.timestamp) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % msSinceEpochToTime(log.oldSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.oldSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % msSinceEpochToTime(log.newSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.newSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % msSinceEpochToTime(log.currentSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.currentSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.deltaTimeMs) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.deltaTimeFractionMs) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.simulationTimeFraction) % QLatin1String("</td>");
tableRows +=
QLatin1String("<td class=\"old\">") % log.oldSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.longitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.longitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.longitudeAsString() % QLatin1String("</td>");
tableRows +=
QLatin1String("<td class=\"old\">") % log.oldSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.groundFactor) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.getOnGroundInfo() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getOnGroundInfo() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getOnGroundInfo() % QLatin1String("</td>");
tableRows +=
QLatin1String("<td>") % boolToYesNo(log.useParts) % QLatin1String("</td>") %
(changedParts ? QLatin1String("<td>*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % (log.useParts ? log.parts.toQString(true) : QLatin1String("")) % QLatin1String("</td>") %
QLatin1String("</tr>\n");
}
tableRows += QLatin1String("</tbody>\n");
return QLatin1String("<table class=\"small\">\n") % tableHeader % tableRows % QLatin1String("</table>\n");
}
QString CInterpolationLogger::getHtmlPartsLog(const QList<PartsLog> &logs)
{
if (logs.isEmpty()) { return {}; }
const QString tableHeader =
QLatin1String("<thead><tr>") %
QLatin1String("<th>CS</th><th>timestamp</th>") %
QLatin1String("<th>c.</th>") %
QLatin1String("<th>parts</th>") %
QLatin1String("</tr></thead>\n");
CAircraftParts lastParts; // default, so shown if parts are different from default
QString tableRows("<tbody>\n");
for (const PartsLog &log : logs)
{
const bool changedParts = lastParts != log.parts;
lastParts = log.parts;
tableRows +=
QLatin1String("<tr>") %
QLatin1String("<td>") % log.callsign.asString() % QLatin1String("</td>") %
QLatin1String("<td>") % msSinceEpochToTime(log.timestamp) % QLatin1String("</td>") %
(changedParts ? QLatin1String("<td class=\"changed\">*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % log.parts.toQString() % QLatin1String("</td>");
}
tableRows += QLatin1String("</tbody>\n");
return QLatin1String("<table class=\"small\">\n") % tableHeader % tableRows % QLatin1String("</table>\n");
}
void CInterpolationLogger::clearLog()
{
QWriteLocker l(&m_lockLogs);
this->m_partsLogs.clear();
this->m_situationLogs.clear();
}
QString CInterpolationLogger::msSinceEpochToTime(qint64 ms)
{
static const QString dateFormat("hh:mm:ss.zzz");
return QDateTime::fromMSecsSinceEpoch(ms).toString(dateFormat);
}
QString CInterpolationLogger::msSinceEpochToTime(qint64 t1, qint64 t2, qint64 t3)
{
if (t3 < 0) return QString("%1 %2").arg(msSinceEpochToTime(t1), msSinceEpochToTime(t2));
return QString("%1 %2 %3").arg(msSinceEpochToTime(t1), msSinceEpochToTime(t2), msSinceEpochToTime(t3));
}
} // namespace
} // namespace

View File

@@ -0,0 +1,118 @@
/* Copyright (C) 2014
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_SIMULATION_INTERPOLATIONLOGGER_H
#define BLACKMISC_SIMULATION_INTERPOLATIONLOGGER_H
#include "interpolationrenderingsetup.h"
#include "blackmisc/aviation/aircraftpartslist.h"
#include "blackmisc/aviation/aircraftsituation.h"
#include "blackmisc/aviation/aircraftpartslist.h"
#include "blackmisc/simulation/remoteaircraftprovider.h"
#include <QObject>
#include <QString>
#include <QtGlobal>
namespace BlackMisc
{
class CWorker;
namespace Aviation { class CCallsign; }
namespace Simulation
{
class CInterpolationHints;
class CInterpolatorLinear;
class CInterpolatorSpline;
struct CInterpolationStatus;
struct CPartsStatus;
//! Record internal state of interpolator for debugging
class BLACKMISC_EXPORT CInterpolationLogger : public QObject
{
Q_OBJECT
public:
//! Constructor
CInterpolationLogger(QObject *parent = nullptr);
//! Log category
static QString getLogCategory() { return "swift.interpolationlogger"; }
//! Write a log in background
BlackMisc::CWorker *writeLogInBackground();
//! Clear log file
void clearLog();
//! Latest log files: 0: Interpolation / 1: Parts
static QStringList getLatestLogFiles();
//! Log entry for situation interpolation
struct SituationLog
{
qint64 timestamp = -1; //!< current timestamp
double groundFactor = -1; //!< current ground factor
double vtolAircraft = false; //!< VTOL aircraft
double deltaTimeMs = 0; //!< delta time to last situation
double simulationTimeFraction = -1; //!< time fraction, normally 0..1
double deltaTimeFractionMs = -1; //!< delta time fraction
bool useParts = false; //!< supporting aircraft parts
BlackMisc::Aviation::CCallsign callsign; //!< current callsign
BlackMisc::Aviation::CAircraftParts parts; //!< corresponding parts used in interpolator
BlackMisc::Aviation::CAircraftSituation oldSituation; //!< old situation
BlackMisc::Aviation::CAircraftSituation newSituation; //!< new situation
BlackMisc::Aviation::CAircraftSituation currentSituation; //!< interpolated situation
};
//! Log entry for parts interpolation
struct PartsLog
{
qint64 timestamp = -1; //!< current timestamp
BlackMisc::Aviation::CCallsign callsign; //!< current callsign
BlackMisc::Aviation::CAircraftParts parts; //!< parts to be logged
};
//! Log current interpolation cycle, only stores in memory, for performance reasons
//! \remark const to allow const interpolator functions
//! \threadsafe
void logInterpolation(const SituationLog &log);
//! Log current parts cycle, only stores in memory, for performance reasons
//! \remark const to allow const interpolator functions
//! \threadsafe
void logParts(const PartsLog &parts);
private:
//! Get log as HTML table
static QString getHtmlInterpolationLog(const QList<SituationLog> &logs);
//! Get log as HTML table
static QString getHtmlPartsLog(const QList<PartsLog> &logs);
//! Write log to file
static CStatusMessageList writeLogFile(const QList<SituationLog> &interpolation, const QList<PartsLog> &parts);
//! Status of file operation
static CStatusMessage logStatusFileWriting(bool success, const QString &fileName);
//! Create readable time
static QString msSinceEpochToTime(qint64 ms);
//! Create readable time
static QString msSinceEpochToTime(qint64 t1, qint64 t2, qint64 t3 = -1);
mutable QReadWriteLock m_lockLogs; //!< lock logging
QList<PartsLog> m_partsLogs; //!< logs of parts
QList<SituationLog> m_situationLogs; //!< logs of interpolation
};
} // namespace
} // namespace
#endif // guard

View File

@@ -10,6 +10,7 @@
#include "interpolator.h"
#include "blackconfig/buildconfig.h"
#include "blackmisc/simulation/interpolationhints.h"
#include "blackmisc/simulation/interpolationlogger.h"
#include "blackmisc/simulation/interpolatorlinear.h"
#include "blackmisc/simulation/interpolatorspline.h"
#include "blackmisc/aviation/callsign.h"
@@ -19,8 +20,6 @@
#include "blackmisc/pq/units.h"
#include "blackmisc/pq/length.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/worker.h"
#include "blackmisc/directoryutils.h"
#include <QDateTime>
#include <QStringBuilder>
@@ -49,7 +48,7 @@ namespace BlackMisc
const CInterpolationAndRenderingSetup &setup, const CInterpolationHints &hints, CInterpolationStatus &status)
{
status.reset();
InterpolationLog log;
CInterpolationLogger::SituationLog log;
// any data at all?
if (m_aircraftSituations.isEmpty()) { return {}; }
@@ -111,7 +110,7 @@ namespace BlackMisc
status.setChangedPosition(true);
}
status.setInterpolationSucceeded(true);
if (hints.isLoggingInterpolation())
if (m_logger && hints.isLoggingInterpolation())
{
log.timestamp = currentTimeMsSinceEpoc;
log.callsign = m_callsign;
@@ -119,7 +118,7 @@ namespace BlackMisc
log.currentSituation = currentSituation;
log.useParts = hints.hasAircraftParts();
log.parts = hints.getAircraftParts();
this->logInterpolation(log);
m_logger->logInterpolation(log);
}
return currentSituation;
@@ -225,13 +224,13 @@ namespace BlackMisc
}
while (false);
if (log)
if (m_logger && log)
{
PartsLog log;
CInterpolationLogger::PartsLog log;
log.callsign = m_callsign;
log.timestamp = currentTimeMsSinceEpoch;
log.parts = currentParts;
CInterpolator::logParts(log);
m_logger->logParts(log);
}
return currentParts;
@@ -250,216 +249,6 @@ namespace BlackMisc
IRemoteAircraftProvider::removeOutdatedParts(m_aircraftParts);
}
template <typename Derived>
CWorker *CInterpolator<Derived>::writeLogInBackground()
{
QList<InterpolationLog> interpolation;
QList<PartsLog> parts;
{
QReadLocker l(&m_lockLogs);
interpolation = m_interpolationLogs;
parts = m_partsLogs;
}
CWorker *worker = CWorker::fromTask(this, "WriteInterpolationLog", [interpolation, parts]()
{
const CStatusMessageList msg = CInterpolator::writeLogFile(interpolation, parts);
CLogMessage::preformatted(msg);
});
return worker;
}
QStringList IInterpolator::getLatestLogFiles()
{
QStringList files({ "", ""});
const QString logDir = CDirectoryUtils::getLogDirectory();
QDir logs(logDir);
if (!logs.exists()) { return files; }
logs.setNameFilters(QStringList() << "*interpolation.html" << "*parts.html");
const QStringList interpolations = logs.entryList(QStringList({"*interpolation.html"}), QDir::NoFilter, QDir::Time);
if (!interpolations.isEmpty())
{
files[0] = CFileUtils::appendFilePaths(logDir, interpolations.first());
}
const QStringList parts = logs.entryList(QStringList({"*parts.html"}), QDir::NoFilter, QDir::Time);
if (!parts.isEmpty())
{
files[1] = CFileUtils::appendFilePaths(logDir, parts.first());
}
return files;
}
template <typename Derived>
CStatusMessageList CInterpolator<Derived>::writeLogFile(const QList<InterpolationLog> &interpolation, const QList<PartsLog> &parts)
{
if (parts.isEmpty() && interpolation.isEmpty()) { return CStatusMessage(static_cast<CInterpolator *>(nullptr)).warning("No data for log"); }
static const QString html = QLatin1String("Entries: %1\n\n%2");
const QString htmlTemplate = CFileUtils::readFileToString(CBuildConfig::getHtmlTemplateFileName());
CStatusMessageList msgs;
const QString ts = QDateTime::currentDateTimeUtc().toString("yyyyMMddhhmmss");
const QString htmlInterpolation = CInterpolator::getHtmlInterpolationLog(interpolation);
if (!htmlInterpolation.isEmpty())
{
const QString fn = CFileUtils::appendFilePaths(CDirectoryUtils::getLogDirectory(), QString("%1 interpolation.html").arg(ts));
const bool s = CFileUtils::writeStringToFile(htmlTemplate.arg(html.arg(interpolation.size()).arg(htmlInterpolation)), fn);
msgs.push_back(CInterpolator::logStatusFileWriting(s, fn));
}
const QString htmlParts = CInterpolator::getHtmlPartsLog(parts);
if (!htmlParts.isEmpty())
{
const QString fn = CFileUtils::appendFilePaths(CDirectoryUtils::getLogDirectory(), QString("%1 parts.html").arg(ts));
const bool s = CFileUtils::writeStringToFile(htmlTemplate.arg(html.arg(parts.size()).arg(htmlParts)), fn);
msgs.push_back(CInterpolator::logStatusFileWriting(s, fn));
}
return msgs;
}
template <typename Derived>
CStatusMessage CInterpolator<Derived>::logStatusFileWriting(bool success, const QString &fileName)
{
if (success)
{
return CStatusMessage(static_cast<CInterpolator *>(nullptr)).info("Written log file '%1'") << fileName;
}
else
{
return CStatusMessage(static_cast<CInterpolator *>(nullptr)).error("Failed to write log file '%1'") << fileName;
}
}
template <typename Derived>
void CInterpolator<Derived>::logInterpolation(const typename CInterpolator<Derived>::InterpolationLog &log) const
{
QWriteLocker l(&m_lockLogs);
m_interpolationLogs.append(log);
}
template <typename Derived>
void CInterpolator<Derived>::logParts(const typename CInterpolator<Derived>::PartsLog &parts) const
{
QWriteLocker l(&m_lockLogs);
m_partsLogs.append(parts);
}
template <typename Derived>
QString CInterpolator<Derived>::getHtmlInterpolationLog(const QList<InterpolationLog> &logs)
{
if (logs.isEmpty()) { return {}; }
const QString tableHeader =
QLatin1String("<thead><tr>") %
QLatin1String("<th>c.</th><th>CS</th><th>VTOL</th><th>timestamp</th><th>since</th>") %
QLatin1String("<th>ts old</th><th>ts new</th><th>ts cur</th>") %
QLatin1String("<th>&Delta;t</th><th>&Delta;t fr.</th><th>fraction</th>") %
QLatin1String("<th>lat.old</th><th>lat.new</th><th>lat.cur</th>") %
QLatin1String("<th>lng.old</th><th>lng.new</th><th>lng.cur</th>") %
QLatin1String("<th>alt.old</th><th>alt.new</th><th>alt.cur</th>") %
QLatin1String("<th>elv.old</th><th>elv.new</th><th>elv.cur</th>") %
QLatin1String("<th>gnd.factor</th>") %
QLatin1String("<th>onGnd.old</th><th>onGnd.new</th><th>onGnd.cur</th>") %
QLatin1String("<th>parts</th><th>c.</th><th>parts details</th>") %
QLatin1String("</tr></thead>\n");
static const CLengthUnit ft = CLengthUnit::ft();
const InterpolationLog firstLog = logs.first();
qint64 newPosTs = firstLog.newSituation.getMSecsSinceEpoch();
CAircraftParts lastParts; // default, so shown if parts are different from default
QString tableRows("<tbody>\n");
for (const InterpolationLog &log : logs)
{
const bool changedNewPosition = newPosTs != log.newSituation.getMSecsSinceEpoch();
const bool changedParts = lastParts != log.parts;
newPosTs = log.newSituation.getMSecsSinceEpoch();
lastParts = log.parts;
// concatenating in multiple steps, otherwise C4503 warnings
tableRows +=
QLatin1String("<tr>") %
(changedNewPosition ? QLatin1String("<td class=\"changed\">*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % log.callsign.asString() % QLatin1String("</td>") %
QLatin1String("<td>") % boolToYesNo(log.vtolAircraft) % QLatin1String("</td>") %
QLatin1String("<td>") % msSinceEpochToTime(log.timestamp) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.timestamp - firstLog.timestamp) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % msSinceEpochToTime(log.oldSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.oldSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % msSinceEpochToTime(log.newSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.newSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % msSinceEpochToTime(log.currentSituation.getAdjustedMSecsSinceEpoch()) % QLatin1Char('-') % QString::number(log.currentSituation.getTimeOffsetMs()) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.deltaTimeMs) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.deltaTimeFractionMs) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.simulationTimeFraction) % QLatin1String("</td>");
tableRows +=
QLatin1String("<td class=\"old\">") % log.oldSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.latitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.longitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.longitudeAsString() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.longitudeAsString() % QLatin1String("</td>");
tableRows +=
QLatin1String("<td class=\"old\">") % log.oldSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getAltitude().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getGroundElevation().valueRoundedWithUnit(ft, 1) % QLatin1String("</td>") %
QLatin1String("<td>") % QString::number(log.groundFactor) % QLatin1String("</td>") %
QLatin1String("<td class=\"old\">") % log.oldSituation.getOnGroundInfo() % QLatin1String("</td>") %
QLatin1String("<td class=\"new\">") % log.newSituation.getOnGroundInfo() % QLatin1String("</td>") %
QLatin1String("<td class=\"cur\">") % log.currentSituation.getOnGroundInfo() % QLatin1String("</td>");
tableRows +=
QLatin1String("<td>") % boolToYesNo(log.useParts) % QLatin1String("</td>") %
(changedParts ? QLatin1String("<td>*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % (log.useParts ? log.parts.toQString(true) : QLatin1String("")) % QLatin1String("</td>") %
QLatin1String("</tr>\n");
}
tableRows += QLatin1String("</tbody>\n");
return QLatin1String("<table class=\"small\">\n") % tableHeader % tableRows % QLatin1String("</table>\n");
}
template <typename Derived>
QString CInterpolator<Derived>::getHtmlPartsLog(const QList<PartsLog> &logs)
{
if (logs.isEmpty()) { return {}; }
const QString tableHeader =
QLatin1String("<thead><tr>") %
QLatin1String("<th>CS</th><th>timestamp</th>") %
QLatin1String("<th>c.</th>") %
QLatin1String("<th>parts</th>") %
QLatin1String("</tr></thead>\n");
CAircraftParts lastParts; // default, so shown if parts are different from default
QString tableRows("<tbody>\n");
for (const PartsLog &log : logs)
{
const bool changedParts = lastParts != log.parts;
lastParts = log.parts;
tableRows +=
QLatin1String("<tr>") %
QLatin1String("<td>") % log.callsign.asString() % QLatin1String("</td>") %
QLatin1String("<td>") % msSinceEpochToTime(log.timestamp) % QLatin1String("</td>") %
(changedParts ? QLatin1String("<td class=\"changed\">*</td>") : QLatin1String("<td></td>")) %
QLatin1String("<td>") % log.parts.toQString() % QLatin1String("</td>");
}
tableRows += QLatin1String("</tbody>\n");
return QLatin1String("<table class=\"small\">\n") % tableHeader % tableRows % QLatin1String("</table>\n");
}
template <typename Derived>
void CInterpolator<Derived>::clearLog()
{
QWriteLocker l(&m_lockLogs);
this->m_partsLogs.clear();
this->m_interpolationLogs.clear();
}
template <typename Derived>
void CInterpolator<Derived>::setGroundElevationFromHint(const CInterpolationHints &hints, CAircraftSituation &situation, bool override)
{
@@ -520,20 +309,6 @@ namespace BlackMisc
situation.setOnGround(CAircraftSituation::OnGround, CAircraftSituation::OnGroundByGuessing);
}
template <typename Derived>
QString CInterpolator<Derived>::msSinceEpochToTime(qint64 ms)
{
static const QString dateFormat("hh:mm:ss.zzz");
return QDateTime::fromMSecsSinceEpoch(ms).toString(dateFormat);
}
template <typename Derived>
QString CInterpolator<Derived>::msSinceEpochToTime(qint64 t1, qint64 t2, qint64 t3)
{
if (t3 < 0) return QString("%1 %2").arg(msSinceEpochToTime(t1), msSinceEpochToTime(t2));
return QString("%1 %2 %3").arg(msSinceEpochToTime(t1), msSinceEpochToTime(t2), msSinceEpochToTime(t3));
}
bool CInterpolationStatus::allTrue() const
{
return m_interpolationSucceeded && m_changedPosition;

View File

@@ -29,6 +29,7 @@ namespace BlackMisc
namespace Simulation
{
class CInterpolationHints;
class CInterpolationLogger;
class CInterpolatorLinear;
class CInterpolatorSpline;
struct CInterpolationStatus;
@@ -56,13 +57,6 @@ namespace BlackMisc
//! Add a new aircraft parts
void addAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts);
//! Write a log in background
//! \threadsafe
BlackMisc::CWorker *writeLogInBackground();
//! Clear log file
void clearLog();
//! Takes input between 0 and 1 and returns output between 0 and 1 smoothed with an S-shaped curve.
//!
//! Useful for making interpolation seem smoother, efficiently as it just uses simple arithmetic.
@@ -73,60 +67,17 @@ namespace BlackMisc
return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
}
//! Latest log files: 0: Interpolation / 1: Parts
static QStringList getLatestLogFiles();
//! Attach an observer to read the interpolator's state for debugging
void attachLogger(CInterpolationLogger *logger) { m_logger = logger; }
protected:
BlackMisc::Aviation::CAircraftSituationList m_aircraftSituations; //!< recent situations
BlackMisc::Aviation::CAircraftPartsList m_aircraftParts; //!< recent parts
BlackMisc::Aviation::CCallsign m_callsign; //!< callsign
//! Log for interpolation
struct InterpolationLog
{
qint64 timestamp = -1; //!< current timestamp
double groundFactor = -1; //!< current ground factor
double vtolAircraft = false; //!< VTOL aircraft
double deltaTimeMs = 0; //!< delta time to last situation
double simulationTimeFraction = -1; //!< time fraction, normally 0..1
double deltaTimeFractionMs = -1; //!< delta time fraction
bool useParts = false; //!< supporting aircraft parts
BlackMisc::Aviation::CCallsign callsign; //!< current callsign
BlackMisc::Aviation::CAircraftParts parts; //!< corresponding parts used in interpolator
BlackMisc::Aviation::CAircraftSituation oldSituation; //!< old situation
BlackMisc::Aviation::CAircraftSituation newSituation; //!< new situation
BlackMisc::Aviation::CAircraftSituation currentSituation; //!< interpolated situation
};
//! Log for parts
struct PartsLog
{
qint64 timestamp = -1; //!< current timestamp
BlackMisc::Aviation::CCallsign callsign; //!< current callsign
BlackMisc::Aviation::CAircraftParts parts; //!< parts to be logged
};
//! Constructor
CInterpolator(const QString &objectName, const BlackMisc::Aviation::CCallsign &callsign, QObject *parent);
//! Log current interpolation cycle, only stores in memory, for performance reasons
//! \remark const to allow const interpolator functions
//! \threadsafe
void logInterpolation(const InterpolationLog &log) const;
//! Log current parts cycle, only stores in memory, for performance reasons
//! \remark const to allow const interpolator functions
//! \threadsafe
void logParts(const PartsLog &parts) const;
//! Get log as HTML table
//! \threadsafe
static QString getHtmlInterpolationLog(const QList<InterpolationLog> &logs);
//! Get log as HTML table
//! \threadsafe
static QString getHtmlPartsLog(const QList<PartsLog> &logs);
//! Set the ground elevation from hints
static void setGroundElevationFromHint(const CInterpolationHints &hints, BlackMisc::Aviation::CAircraftSituation &situation, bool override = true);
@@ -134,24 +85,10 @@ namespace BlackMisc
static void setGroundFlagFromInterpolator(const CInterpolationHints &hints, double groundFactor, BlackMisc::Aviation::CAircraftSituation &situation);
private:
//! Write log to file
static CStatusMessageList writeLogFile(const QList<InterpolationLog> &interpolation, const QList<PartsLog> &parts);
//! Status of file operation
static CStatusMessage logStatusFileWriting(bool success, const QString &fileName);
//! Create readable time
static QString msSinceEpochToTime(qint64 ms);
//! Create readable time
static QString msSinceEpochToTime(qint64 t1, qint64 t2, qint64 t3 = -1);
CInterpolationLogger *m_logger = nullptr;
Derived *derived() { return static_cast<Derived *>(this); }
const Derived *derived() const { return static_cast<const Derived *>(this); }
mutable QReadWriteLock m_lockLogs; //!< lock logging
mutable QList<PartsLog> m_partsLogs; //!< logs of parts
mutable QList<InterpolationLog> m_interpolationLogs; //!< logs of interpolation
};
//! Simple interpolator for pitch, bank, heading, groundspeed

View File

@@ -36,7 +36,7 @@ namespace BlackMisc
namespace Simulation
{
CInterpolatorLinear::Interpolant CInterpolatorLinear::getInterpolant(qint64 currentTimeMsSinceEpoc,
const CInterpolationAndRenderingSetup &setup, const CInterpolationHints &hints, CInterpolationStatus &status, InterpolationLog &log) const
const CInterpolationAndRenderingSetup &setup, const CInterpolationHints &hints, CInterpolationStatus &status, CInterpolationLogger::SituationLog &log) const
{
Q_UNUSED(setup);
Q_UNUSED(hints);

View File

@@ -15,6 +15,7 @@
#include "interpolator.h"
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/aviation/aircraftsituation.h"
#include "blackmisc/simulation/interpolationlogger.h"
#include <QString>
#include <QtGlobal>
@@ -66,7 +67,7 @@ namespace BlackMisc
//! Get the interpolant for the given time point
Interpolant getInterpolant(qint64 currentTimeMsSinceEpoc, const CInterpolationAndRenderingSetup &setup,
const CInterpolationHints &hints, CInterpolationStatus &status, InterpolationLog &log) const;
const CInterpolationHints &hints, CInterpolationStatus &status, CInterpolationLogger::SituationLog &log) const;
//! Log category
static QString getLogCategory() { return "swift.interpolatorlinear"; }

View File

@@ -90,7 +90,7 @@ namespace BlackMisc
}
CInterpolatorSpline::Interpolant CInterpolatorSpline::getInterpolant(qint64 currentTimeMsSinceEpoc,
const CInterpolationAndRenderingSetup &setup, const CInterpolationHints &hints, CInterpolationStatus &status, InterpolationLog &log)
const CInterpolationAndRenderingSetup &setup, const CInterpolationHints &hints, CInterpolationStatus &status, CInterpolationLogger::SituationLog &log)
{
Q_UNUSED(hints);
Q_UNUSED(setup);

View File

@@ -15,6 +15,7 @@
#include "blackmisc/simulation/interpolator.h"
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/aviation/aircraftsituation.h"
#include "blackmisc/simulation/interpolationlogger.h"
#include <QString>
#include <QtGlobal>
@@ -56,7 +57,7 @@ namespace BlackMisc
//! Strategy used by CInterpolator::getInterpolatedSituation
Interpolant getInterpolant(qint64 currentTimeMsSinceEpoc, const CInterpolationAndRenderingSetup &setup,
const CInterpolationHints &hints, CInterpolationStatus &status, InterpolationLog &log);
const CInterpolationHints &hints, CInterpolationStatus &status, CInterpolationLogger::SituationLog &log);
//! Log category
static QString getLogCategory() { return "swift.interpolatorspline"; }

View File

@@ -118,13 +118,14 @@ namespace BlackSimPlugin
return positionSlewMode;
}
CFs9Client::CFs9Client(const CCallsign &callsign, const QString &modelName,
const CTime &updateInterval, QObject *owner) :
CFs9Client::CFs9Client(const CCallsign &callsign, const QString &modelName, const CTime &updateInterval,
BlackMisc::Simulation::CInterpolationLogger *logger, QObject *owner) :
CDirectPlayPeer(owner, callsign),
m_updateInterval(updateInterval),
m_interpolator(callsign),
m_modelName(modelName)
{
m_interpolator.attachLogger(logger);
}
CFs9Client::~CFs9Client()

View File

@@ -41,7 +41,8 @@ namespace BlackSimPlugin
//! Constructor
CFs9Client(const BlackMisc::Aviation::CCallsign &callsign, const QString &modelName,
const BlackMisc::PhysicalQuantities::CTime &updateInterval, QObject *owner);
const BlackMisc::PhysicalQuantities::CTime &updateInterval,
BlackMisc::Simulation::CInterpolationLogger *logger, QObject *owner);
//! Destructor
virtual ~CFs9Client();

View File

@@ -171,7 +171,7 @@ namespace BlackSimPlugin
bool rendered = true;
updateAircraftRendered(callsign, rendered);
CFs9Client *client = new CFs9Client(callsign, newRemoteAircraft.getModelString(), CTime(25, CTimeUnit::ms()), this);
CFs9Client *client = new CFs9Client(callsign, newRemoteAircraft.getModelString(), CTime(25, CTimeUnit::ms()), &m_interpolationLogger, this);
client->setHostAddress(m_fs9Host->getHostAddress());
client->setPlayerUserId(m_fs9Host->getPlayerUserId());
client->start();

View File

@@ -20,10 +20,13 @@ namespace BlackSimPlugin
CSimConnectObject::CSimConnectObject()
{ }
CSimConnectObject::CSimConnectObject(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, DWORD requestId) :
CSimConnectObject::CSimConnectObject(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, DWORD requestId,
BlackMisc::Simulation::CInterpolationLogger *logger) :
m_aircraft(aircraft), m_requestId(requestId), m_validRequestId(true),
m_interpolator(QSharedPointer<BlackMisc::Simulation::CInterpolatorLinear>::create(aircraft.getCallsign()))
{ }
{
m_interpolator->attachLogger(logger);
}
bool CSimConnectObject::isPendingAdded() const
{

View File

@@ -16,7 +16,14 @@
#include "simconnectdatadefinition.h"
#include <QSharedPointer>
namespace BlackMisc { namespace Simulation { class CInterpolatorLinear; } }
namespace BlackMisc
{
namespace Simulation
{
class CInterpolatorLinear;
class CInterpolationLogger;
}
}
namespace BlackSimPlugin
{
namespace Fsx
@@ -29,7 +36,8 @@ namespace BlackSimPlugin
CSimConnectObject();
//! Constructor
CSimConnectObject(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, DWORD requestId);
CSimConnectObject(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, DWORD requestId,
BlackMisc::Simulation::CInterpolationLogger *logger);
//! Destructor
~CSimConnectObject() {}

View File

@@ -174,7 +174,7 @@ namespace BlackSimPlugin
// we will request a new aircraft by request ID, later we will receive its object id
// so far this object id is -1
addedAircraft.setRendered(false);
const CSimConnectObject simObject(addedAircraft, requestId);
const CSimConnectObject simObject(addedAircraft, requestId, &m_interpolationLogger);
m_simConnectObjects.insert(callsign, simObject);
adding = true;
}