diff --git a/src/app/core/vapplication.cpp b/src/app/core/vapplication.cpp index c6c212e01..cc4f78997 100644 --- a/src/app/core/vapplication.cpp +++ b/src/app/core/vapplication.cpp @@ -48,6 +48,7 @@ #include #include #include +#include Q_LOGGING_CATEGORY(vApp, "v.application") @@ -85,29 +86,33 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con if (isGuiThread) { - QString debugdate = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss"); + QString debugdate = "[" + QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss"); QMessageBox messageBox; switch (type) { case QtDebugMsg: - debugdate += QString(" [Debug] %1: \"%2\" %3").arg(context.category).arg(msg).arg(context.function); + debugdate += QString(":DEBUG:%1(%2)] %3: %4: %5").arg(context.file).arg(context.line) + .arg(context.function).arg(context.category).arg(msg); break; case QtWarningMsg: - debugdate += QString(" [Warning] %1: \"%2\" %3").arg(context.category).arg(msg).arg(context.function); + debugdate += QString(":WARNING:%1(%2)] %3: %4: %5").arg(context.file).arg(context.line) + .arg(context.function).arg(context.category).arg(msg); messageBox.setIcon(QMessageBox::Warning); messageBox.setInformativeText(msg); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); break; case QtCriticalMsg: - debugdate += QString(" [Critical] %1: \"%2\" %3").arg(context.category).arg(msg).arg(context.function); + debugdate += QString(":CRITICAL:%1(%2)] %3: %4: %5").arg(context.file).arg(context.line) + .arg(context.function).arg(context.category).arg(msg); messageBox.setIcon(QMessageBox::Critical); messageBox.setInformativeText(msg); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); break; case QtFatalMsg: - debugdate += QString(" [Fatal] %1: \"%2\" %3").arg(context.category).arg(msg).arg(context.function); + debugdate += QString(":FATAL:%1(%2)] %3: %4: %5").arg(context.file).arg(context.line) + .arg(context.function).arg(context.category).arg(msg); messageBox.setIcon(QMessageBox::Critical); messageBox.setInformativeText(msg); messageBox.setStandardButtons(QMessageBox::Ok); @@ -156,7 +161,7 @@ VApplication::VApplication(int &argc, char **argv) variables(QMap()), functions(QMap()), postfixOperators(QMap()), stDescriptions(QMap()), undoStack(nullptr), sceneView(nullptr), currentScene(nullptr), autoSaveTimer(nullptr), mainWindow(nullptr), - openingPattern(false), settings(nullptr), doc(nullptr), log(nullptr), out(nullptr) + openingPattern(false), settings(nullptr), doc(nullptr), log(nullptr), out(nullptr), logLock(nullptr) { undoStack = new QUndoStack(this); @@ -179,6 +184,7 @@ VApplication::~VApplication() { log->close(); delete log; + delete logLock; } } @@ -434,7 +440,86 @@ QString VApplication::LogDirPath() const //--------------------------------------------------------------------------------------------------------------------- QString VApplication::LogPath() const { - return LogDirPath() + "/valentina.log"; + return QString("%1/valentina-pid%2.log").arg(LogDirPath()).arg(qApp->applicationPid()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::CreateLogDir() const +{ + QDir logDir(LogDirPath()); + if (logDir.exists() == false) + { + logDir.mkpath("."); // Create directory for log if need + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::BeginLogging() +{ + log = new QFile(LogPath()); + if (log->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) + { + out = new QTextStream(log); + qInstallMessageHandler(noisyFailureMsgHandler); + logLock = new QLockFile(LogPath()+".lock"); + logLock->setStaleLockTime(0); + if (logLock->tryLock()) + { + qCDebug(vApp) << "Log file"<error(); + } + } + else + { + delete log; + qCDebug(vApp) << "Error opening log file '" << LogPath() << "'. All debug output redirected to console."; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::ClearOldLogs() const +{ + QStringList filters{"*.log"}; + QDir logsDir(LogDirPath()); + logsDir.setNameFilters(filters); + logsDir.setCurrent(LogDirPath()); + + const QStringList allFiles = logsDir.entryList(QDir::NoDotAndDotDot | QDir::Files); + if (allFiles.isEmpty() == false) + { + qCDebug(vApp) << "Clearing old logs"; + for (int i = 0; i < allFiles.size(); ++i) + { + QFileInfo info(allFiles.at(i)); + QLockFile *lock = new QLockFile(info.absoluteFilePath() + ".lock"); + if (lock->tryLock()) + { + qCDebug(vApp) << "Locked file"<open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) - { - out = new QTextStream(log); - qInstallMessageHandler(noisyFailureMsgHandler); - } - else - { - delete log; - qDebug() << "Error opening log file '" << LogPath() << "'. All debug output redirected to console."; - } + CreateLogDir(); + BeginLogging(); + ClearOldLogs(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/core/vapplication.h b/src/app/core/vapplication.h index 9d50fe6bc..c9f8d12e1 100644 --- a/src/app/core/vapplication.h +++ b/src/app/core/vapplication.h @@ -41,6 +41,7 @@ class VMainGraphicsScene; class VPattern; class QFile; class VSettings; +class QLockFile; #if defined(qApp) #undef qApp @@ -154,6 +155,7 @@ private: VPattern *doc; QFile *log; QTextStream *out; + QLockFile *logLock; void InitLineWidth(); void InitMeasurements(); void InitVariables(); @@ -182,6 +184,9 @@ private: QString LogDirPath()const; QString LogPath()const; + void CreateLogDir()const; + void BeginLogging(); + void ClearOldLogs()const; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/main.cpp b/src/app/main.cpp index e8d06c2b4..8e72651aa 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -73,6 +73,7 @@ int main(int argc, char *argv[]) qDebug()<<"Based on Qt "<getSettings()->GetLocale(); qDebug()<<"Checked locale:"<