mirror of
https://gitlab.com/pholy/OSCAR-code.git
synced 2025-04-06 19:20:45 +00:00
Enable serial device recording in the main application.
This is transparent to the user and is recorded into a log directory within the OSCAR_Data directory. Also add log rotation so that these logs don't grow forever.
This commit is contained in:
parent
d8a7cf2133
commit
6a658b3be0
@ -92,7 +92,7 @@ void DeviceConnectionManager::replay(QFile* file)
|
|||||||
|
|
||||||
|
|
||||||
// Return singleton instance of DeviceConnectionManager, creating it if necessary.
|
// Return singleton instance of DeviceConnectionManager, creating it if necessary.
|
||||||
inline DeviceConnectionManager & DeviceConnectionManager::getInstance()
|
DeviceConnectionManager & DeviceConnectionManager::getInstance()
|
||||||
{
|
{
|
||||||
static DeviceConnectionManager instance;
|
static DeviceConnectionManager instance;
|
||||||
return instance;
|
return instance;
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
* for more details. */
|
* for more details. */
|
||||||
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "SleepLib/preferences.h"
|
||||||
|
#include "version.h"
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
#define ASSERTS_SUCK
|
#define ASSERTS_SUCK
|
||||||
|
|
||||||
@ -147,3 +150,86 @@ void LogThread::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString GetLogDir()
|
||||||
|
{
|
||||||
|
static const QString LOG_DIR_NAME = "logs";
|
||||||
|
|
||||||
|
QDir oscarData(GetAppData());
|
||||||
|
Q_ASSERT(oscarData.exists());
|
||||||
|
if (!oscarData.exists(LOG_DIR_NAME)) {
|
||||||
|
oscarData.mkdir(LOG_DIR_NAME);
|
||||||
|
}
|
||||||
|
QDir logDir(oscarData.canonicalPath() + "/" + LOG_DIR_NAME);
|
||||||
|
if (!logDir.exists()) {
|
||||||
|
qWarning() << "Unable to create" << logDir.absolutePath() << "reverting to" << oscarData.canonicalPath();
|
||||||
|
logDir = oscarData;
|
||||||
|
}
|
||||||
|
Q_ASSERT(logDir.exists());
|
||||||
|
|
||||||
|
return logDir.canonicalPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotateLogs(const QString & filePath, int maxPrevious)
|
||||||
|
{
|
||||||
|
if (maxPrevious < 0) {
|
||||||
|
if (getVersion().IsReleaseVersion()) {
|
||||||
|
maxPrevious = 1;
|
||||||
|
} else {
|
||||||
|
// keep more in testing builds
|
||||||
|
maxPrevious = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the list of rotated logs for this filePath.
|
||||||
|
QFileInfo info(filePath);
|
||||||
|
QString path = QDir(info.absolutePath()).canonicalPath();
|
||||||
|
QString base = info.baseName();
|
||||||
|
QString ext = info.completeSuffix();
|
||||||
|
if (!ext.isEmpty()) {
|
||||||
|
ext = "." + ext;
|
||||||
|
}
|
||||||
|
if (path.isEmpty()) {
|
||||||
|
qWarning() << "Skipping log rotation, directory does not exist:" << info.absoluteFilePath();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList logs;
|
||||||
|
logs.append(filePath);
|
||||||
|
for (int i = 0; i < maxPrevious; i++) {
|
||||||
|
logs.append(QString("%1/%2.%3%4").arg(path).arg(base).arg(i).arg(ext));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the expired log.
|
||||||
|
QFileInfo expired(logs[maxPrevious]);
|
||||||
|
if (expired.exists()) {
|
||||||
|
if (expired.isDir()) {
|
||||||
|
QDir dir(expired.canonicalFilePath());
|
||||||
|
//qDebug() << "Removing expired log directory" << dir.canonicalPath();
|
||||||
|
if (!dir.removeRecursively()) {
|
||||||
|
qWarning() << "Unable to delete expired log directory" << dir.canonicalPath();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
QFile file(expired.canonicalFilePath());
|
||||||
|
//qDebug() << "Removing expired log file" << file.fileName();
|
||||||
|
if (!file.remove()) {
|
||||||
|
qWarning() << "Unable to delete expired log file" << file.fileName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotate the remaining logs.
|
||||||
|
for (int i = maxPrevious; i > 0; i--) {
|
||||||
|
QFileInfo from(logs[i-1]);
|
||||||
|
QFileInfo to(logs[i]);
|
||||||
|
if (from.exists()) {
|
||||||
|
if (to.exists()) {
|
||||||
|
qWarning() << "Unable to rotate log:" << to.absoluteFilePath() << "exists";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//qDebug() << "Renaming" << from.absoluteFilePath() << "to" << to.absoluteFilePath();
|
||||||
|
if (!QFile::rename(from.absoluteFilePath(), to.absoluteFilePath())) {
|
||||||
|
qWarning() << "Unable to rename" << from.absoluteFilePath() << "to" << to.absoluteFilePath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,6 +10,10 @@
|
|||||||
void initializeLogger();
|
void initializeLogger();
|
||||||
void shutdownLogger();
|
void shutdownLogger();
|
||||||
|
|
||||||
|
QString GetLogDir();
|
||||||
|
void rotateLogs(const QString & filePath, int maxPrevious=-1);
|
||||||
|
|
||||||
|
|
||||||
void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt);
|
void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt);
|
||||||
|
|
||||||
class LogThread:public QObject, public QRunnable
|
class LogThread:public QObject, public QRunnable
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include "SleepLib/profiles.h"
|
#include "SleepLib/profiles.h"
|
||||||
#include "translation.h"
|
#include "translation.h"
|
||||||
#include "SleepLib/common.h"
|
#include "SleepLib/common.h"
|
||||||
|
#include "SleepLib/deviceconnection.h"
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -636,6 +637,21 @@ int main(int argc, char *argv[]) {
|
|||||||
MD300W1Loader::Register();
|
MD300W1Loader::Register();
|
||||||
ViatomLoader::Register();
|
ViatomLoader::Register();
|
||||||
|
|
||||||
|
// Begin logging device connection activity.
|
||||||
|
QString connectionsLogDir = GetLogDir() + "/connections";
|
||||||
|
rotateLogs(connectionsLogDir); // keep a limited set of previous logs
|
||||||
|
if (!QDir(connectionsLogDir).mkpath(".")) {
|
||||||
|
qWarning().noquote() << "Unable to create directory" << connectionsLogDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile deviceLog(connectionsLogDir + "/devices.xml");
|
||||||
|
if (deviceLog.open(QFile::ReadWrite)) {
|
||||||
|
qDebug().noquote() << "Logging device connections to" << deviceLog.fileName();
|
||||||
|
DeviceConnectionManager::getInstance().record(&deviceLog);
|
||||||
|
} else {
|
||||||
|
qWarning().noquote() << "Unable to start device connection logging to" << deviceLog.fileName();
|
||||||
|
}
|
||||||
|
|
||||||
schema::setOrders(); // could be called in init...
|
schema::setOrders(); // could be called in init...
|
||||||
|
|
||||||
// Scan for user profiles
|
// Scan for user profiles
|
||||||
@ -653,7 +669,11 @@ int main(int argc, char *argv[]) {
|
|||||||
mainwin->SetupGUI();
|
mainwin->SetupGUI();
|
||||||
mainwin->show();
|
mainwin->show();
|
||||||
|
|
||||||
return a.exec();
|
int result = a.exec();
|
||||||
|
|
||||||
|
DeviceConnectionManager::getInstance().record(nullptr);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !UNITTEST_MODE
|
#endif // !UNITTEST_MODE
|
||||||
|
Loading…
Reference in New Issue
Block a user