#include "PrtCnvTelegramLogThread.hpp" #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include static int gettimeofday(struct timeval *__p, void *__t) { union { unsigned long long ns100; FILETIME ft; } now; constexpr long long SECONDS_SINCE_1601 = 11644473600LL; constexpr long long USEC_IN_SEC = 1000000LL; GetSystemTimeAsFileTime(&now.ft); __p->tv_usec = (long)((now.ns100 / 10LL) % USEC_IN_SEC); __p->tv_sec = (long)(((now.ns100 / 10LL) / USEC_IN_SEC) - SECONDS_SINCE_1601); return (0); } #else #include #endif namespace PrtCnv { PrtCnvTelegramLogThread::PrtCnvTelegramLogThread(PrtCnvThreadStarter* a_parent) : PrtCnvThread(a_parent, "PrtCnvTelegramLogThread") , m_DoLogCentralTelegram(true) , m_DoLogWlsTelegram(true) , m_DoLogRadioTelegram(true) { m_Name = QStringLiteral("TelegramLog"); } int32_t PrtCnvTelegramLogThread::create(NGL::Settings *settings) { PrtCnvThread::create(settings); connect(this, &PrtCnvTelegramLogThread::telegramLogReceived, this, &PrtCnvTelegramLogThread::writeTelegramLog, Qt::QueuedConnection); return 0; } int32_t PrtCnvTelegramLogThread::init() { PrtCnvThread::init(); m_DoLogCentralTelegram = m_Config->getBoolean(QStringLiteral("Common"), QStringLiteral("LogCentralTelegrams"), true); m_DoLogWlsTelegram = m_Config->getBoolean(QStringLiteral("Common"), QStringLiteral("LogWlsTelegrams"), true); m_DoLogRadioTelegram = m_Config->getBoolean(QStringLiteral("Common"), QStringLiteral("LogRadioTelegrams"), true); QString path = m_Config->getString(QStringLiteral("Common"), QStringLiteral("LogTelegramPath"), QStringLiteral(".")); if (!QDir::isAbsolutePath(path)) path = QCoreApplication::applicationDirPath() + QStringLiteral("/../") + path; m_LogTelegramPathWithPrefix = path + QLatin1Char('/') + m_Config->getString(QStringLiteral("Common"), QStringLiteral("LogTelegramPrefix"), QStringLiteral("PrtCnv_")); if (!m_LogTelegramPathWithPrefix.endsWith(QLatin1Char('_'))) m_LogTelegramPathWithPrefix += QLatin1Char('_'); QFileInfo fileInfo(m_LogTelegramPathWithPrefix); QDir dir; dir.mkpath(fileInfo.absolutePath()); INFO_LOG(m_logger); return 0; } static inline int setSend(int protocol, bool bIsSend) { return (protocol | (bIsSend ? N_CoPrtCnv::CoPrtCnvLogHeader::e_DestSend : N_CoPrtCnv::CoPrtCnvLogHeader::e_DestReceive)); } void PrtCnvTelegramLogThread::writeCentralTelegramLog(N_CoPrtCnv::CoPrtCnvCentralTelegram *a_centralTelegram, bool a_isSend, int a_priority) { if (!m_DoLogCentralTelegram) return; emit telegramLogReceived(a_centralTelegram->data(), setSend(N_CoPrtCnv::CoPrtCnvLogHeader::e_DestProtocolCentral, a_isSend), a_priority); } void PrtCnvTelegramLogThread::writeWlsTelegramLog(const TrainingAreaBase::TetraIcd::TrainingAreaTelegram *tele, bool a_isSend, int a_priority) { if (!m_DoLogWlsTelegram) return; N_CoPrtCnv::CoPrtCnvCentralTelegram central(*tele); emit telegramLogReceived(central.data(), setSend(N_CoPrtCnv::CoPrtCnvLogHeader::e_DestProtocolWls, a_isSend), a_priority); } void PrtCnvTelegramLogThread::writeRadioTelegramLog(const QByteArray &tele, bool a_isSend, int a_priority) { if (!m_DoLogRadioTelegram) return; emit telegramLogReceived(tele, setSend(N_CoPrtCnv::CoPrtCnvLogHeader::e_DestProtocolTetra, a_isSend), a_priority); } void PrtCnvTelegramLogThread::writeTelegramLog(const QByteArray &a_data, int a_protocolType, int a_priority) { timeval tv; gettimeofday(&tv, nullptr); uint64_t exactTime = tv.tv_sec; exactTime = exactTime << 32; exactTime += tv.tv_usec; QString fileName = QStringLiteral("%1%2.krlog") .arg(m_LogTelegramPathWithPrefix, QDateTime::fromSecsSinceEpoch(tv.tv_sec).toString(QStringLiteral("yyMMdd_hh"))); QFile telegramFile(fileName); telegramFile.open(QIODevice::WriteOnly | QIODevice::Append); N_CoPrtCnv::CoPrtCnvLogHeader lh; lh.m_Length = a_data.size() + sizeof(N_CoPrtCnv::CoPrtCnvLogHeader); lh.m_Version = 3; lh.m_Priority = a_priority; lh.m_Protocol = a_protocolType; lh.m_Time = exactTime; lh.m_Reserved = 0; telegramFile.write(reinterpret_cast(&lh), sizeof(N_CoPrtCnv::CoPrtCnvLogHeader)); telegramFile.write(a_data); telegramFile.close(); } }