diff --git a/src/QText.pro b/src/QText.pro index 749346a..ab1e08f 100644 --- a/src/QText.pro +++ b/src/QText.pro @@ -46,6 +46,9 @@ QMAKE_CXXFLAGS_WARN_ON += -Wextra QMAKE_CXXFLAGS_WARN_ON += -Wshadow QMAKE_CXXFLAGS_WARN_ON += -Wdouble-promotion +QMAKE_CXXFLAGS_DEBUG *= -pg +QMAKE_LFLAGS_DEBUG *= -pg + SOURCES += \ medo/about.cpp \ @@ -65,6 +68,7 @@ SOURCES += \ medo/config.cpp \ medo/feedback.cpp \ medo/hotkey.cpp \ + medo/lifetimewatch.cpp \ medo/singleinstance.cpp \ medo/state.cpp \ medo/upgrade.cpp \ @@ -102,6 +106,7 @@ HEADERS += \ medo/config.h \ medo/feedback.h \ medo/hotkey.h \ + medo/lifetimewatch.h \ medo/singleinstance.h \ medo/state.h \ medo/upgrade.h \ diff --git a/src/medo/config.cpp b/src/medo/config.cpp index 5d52165..e6af53b 100644 --- a/src/medo/config.cpp +++ b/src/medo/config.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,9 @@ Config::ConfigFile* Config::_configFile(nullptr); Config::ConfigFile* Config::_stateFile(nullptr); void Config::reset() { + qDebug().noquote().nospace() << "[Config] reset()"; + QElapsedTimer stopwatch; stopwatch.start(); + QMutexLocker locker(&_publicAccessMutex); _configurationFilePath = QString(); @@ -25,19 +29,40 @@ void Config::reset() { _immediateSave = true; resetConfigFile(); resetStateFile(); + + qDebug().noquote().nospace() << "[Config] reset() done in " << stopwatch.elapsed() << "ms"; } bool Config::load() { + qDebug().noquote().nospace() << "[Config] load()"; + QElapsedTimer stopwatch; stopwatch.start(); + QMutexLocker locker(&_publicAccessMutex); resetConfigFile(); resetStateFile(); QFile file(configurationFilePath()); - return file.exists(); + + if (file.exists()) { + qDebug().noquote().nospace() << "[Config] load() done in " << stopwatch.elapsed() << "ms"; + return true; + } else { + qDebug().noquote().nospace() << "[Config] load() failed in " << stopwatch.elapsed() << "ms"; + return false; + } } bool Config::save() { + qDebug().noquote().nospace() << "[Config] save()"; + QElapsedTimer stopwatch; stopwatch.start(); + QMutexLocker locker(&_publicAccessMutex); - return getConfigFile()->save(); + if (getConfigFile()->save()) { + qDebug().noquote().nospace() << "[Config] save() done in " << stopwatch.elapsed() << "ms"; + return true; + } else { + qDebug().noquote().nospace() << "[Config] save() failed in " << stopwatch.elapsed() << "ms"; + return false; + } } diff --git a/src/medo/lifetimewatch.cpp b/src/medo/lifetimewatch.cpp new file mode 100644 index 0000000..e9f0ffb --- /dev/null +++ b/src/medo/lifetimewatch.cpp @@ -0,0 +1,60 @@ +/* Josip Medved * www.medo64.com * MIT License */ + +#include +#include "lifetimewatch.h" + + +LifetimeWatch::LifetimeWatch(const QString& text) + : LifetimeWatch(text, false) { +} + +LifetimeWatch::LifetimeWatch(const QString& text, bool addGuid) { +#ifdef QT_DEBUG + _text = text; + _stopwatch = new QElapsedTimer(); + _stopwatch->start(); + if (addGuid) { _uuid = QUuid::createUuid(); } + qDebug().noquote().nospace() << "[LifeTimeWatch] " << text + << ( _uuid.isNull() ? "" : " " + _uuid.toString() ); +#else + Q_UNUSED(text) + Q_UNUSED(addGuid) +#endif +} + +void LifetimeWatch::elapsed() { + this->elapsed(""); +} + +void LifetimeWatch::elapsed(const QString& extraText) { +#ifdef QT_DEBUG + _mutex.lock(); + if (_stopwatch != nullptr) { + qDebug().noquote().nospace() << "[LifeTimeWatch] " << _text + << " elapsed at " << _stopwatch->elapsed() << "ms" + << ( extraText.length() > 0 ? " (" + extraText + ")" : "" ) + << ( _uuid.isNull() ? "" : " " + _uuid.toString() ); + } + _mutex.unlock(); +#else + Q_UNUSED(extraText) +#endif +} + +void LifetimeWatch::done() { +#ifdef QT_DEBUG + _mutex.lock(); + if (_stopwatch != nullptr) { + qDebug().noquote().nospace() << "[LifeTimeWatch] " << _text + << " done in " << _stopwatch->elapsed() << "ms" + << ( _uuid.isNull() ? "" : " " + _uuid.toString() ); + delete(_stopwatch); + _stopwatch = nullptr; + } + _mutex.unlock(); +#endif +} + +LifetimeWatch::~LifetimeWatch() { + this->done(); +} diff --git a/src/medo/lifetimewatch.h b/src/medo/lifetimewatch.h new file mode 100644 index 0000000..25791d6 --- /dev/null +++ b/src/medo/lifetimewatch.h @@ -0,0 +1,30 @@ +/* Josip Medved * www.medo64.com * MIT License */ + +// 2021-12-09: Initial version + +#pragma once + +#include +#include +#include +#include + +namespace Medo { class LifetimeWatch; } + +class LifetimeWatch { + + public: + LifetimeWatch(const QString& text); + LifetimeWatch(const QString& text, bool addGuid); + ~LifetimeWatch(); + void done(); + void elapsed(); + void elapsed(const QString& extraText); + + private: + QMutex _mutex; + QUuid _uuid; + QElapsedTimer* _stopwatch = nullptr; + QString _text; + +}; diff --git a/src/storage/fileitem.cpp b/src/storage/fileitem.cpp index 6989e40..e639c72 100644 --- a/src/storage/fileitem.cpp +++ b/src/storage/fileitem.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "medo/lifetimewatch.h" #include "ui/inserttimedialog.h" #include "clipboard.h" #include "fileitem.h" @@ -529,6 +530,7 @@ bool FileItem::eventFilter(QObject* obj, QEvent* event) { } void FileItem::focusInEvent(QFocusEvent* e) { + LifetimeWatch watch("focusInEvent()"); qDebug().nospace() << "focusInEvent(" << QVariant::fromValue(e->reason()).toString() << ") " << path(); QFile file(path()); if (file.exists()) { @@ -551,6 +553,7 @@ void FileItem::focusInEvent(QFocusEvent* e) { } void FileItem::focusOutEvent(QFocusEvent* e) { + LifetimeWatch watch("focusOutEvent()"); qDebug().nospace() << "focusOutEvent(" << QVariant::fromValue(e->reason()).toString() << ") " << path(); QTextEdit::focusOutEvent(e); if (this->document()->isModified()) { save(); } diff --git a/src/storage/folderitem.cpp b/src/storage/folderitem.cpp index 6ecfc8f..fb61272 100644 --- a/src/storage/folderitem.cpp +++ b/src/storage/folderitem.cpp @@ -1,6 +1,7 @@ #include #include #include "medo/config.h" +#include "medo/lifetimewatch.h" #include "storage/fileitem.h" #include "storage/folderitem.h" #include "storage/storage.h" @@ -162,6 +163,7 @@ bool FolderItem::fileExists(QString title) const { bool FolderItem::saveAll() const { + LifetimeWatch watch("FolderItem.saveAll()"); bool allSaved = true; for (FileItem* file : _files) { if (file->isModified()) { diff --git a/src/storage/storagemonitorthread.cpp b/src/storage/storagemonitorthread.cpp index ad10d80..bda6710 100644 --- a/src/storage/storagemonitorthread.cpp +++ b/src/storage/storagemonitorthread.cpp @@ -1,5 +1,6 @@ #include #include +#include "medo/lifetimewatch.h" #include "storage.h" #include "storagemonitorthread.h" @@ -46,62 +47,65 @@ void StorageMonitorThread::run() { } } - _mutex.lock(); //lock raising events - if (_isMonitoring && (currPaths.count() > 0) && (_prevPaths.count() > 0)) { //analyze add/remove - QStringList addedDirs, addedFiles, removedDirs, removedFiles; + { + //LifetimeWatch watch("StorageMonitorThread.run() lock"); + _mutex.lock(); //lock raising events + if (_isMonitoring && (currPaths.count() > 0) && (_prevPaths.count() > 0)) { //analyze add/remove + QStringList addedDirs, addedFiles, removedDirs, removedFiles; + + //additions + for (QString path : currPaths) { + if (!_prevPaths.contains(path)) { + if (path.endsWith("/")) { + addedDirs.append(QDir::cleanPath(path)); + } else { + addedFiles.append(path); + } + } else { + _prevPaths.removeOne(path); + } + } - //additions - for (QString path : currPaths) { - if (!_prevPaths.contains(path)) { + //removals + for (QString path : _prevPaths) { if (path.endsWith("/")) { - addedDirs.append(QDir::cleanPath(path)); + removedDirs.append(QDir::cleanPath(path)); } else { - addedFiles.append(path); + removedFiles.append(path); } - } else { - _prevPaths.removeOne(path); } - } - //removals - for (QString path : _prevPaths) { - if (path.endsWith("/")) { - removedDirs.append(QDir::cleanPath(path)); - } else { - removedFiles.append(path); + //emit signals + for (QString path : removedDirs) { + qDebug().noquote().nospace() << "[StorageMonitorThread] directoryRemoved(\"" << path << "\") #" << QThread::currentThreadId(); + emit directoryRemoved(path); } - } - - //emit signals - for (QString path : removedDirs) { - qDebug().noquote().nospace() << "[StorageMonitorThread] directoryRemoved(\"" << path << "\") #" << QThread::currentThreadId(); - emit directoryRemoved(path); - } - for (QString path : removedFiles) { - QFileInfo file(path); - QString dirPath = QDir::cleanPath(file.dir().path()); - QString fileName = file.fileName(); - if (!removedDirs.contains(dirPath)) { //filter out files where folder was removed - qDebug().noquote().nospace() << "[StorageMonitorThread] fileRemoved(\"" << dirPath << "\", \"" << fileName << "\") #" << QThread::currentThreadId(); - emit fileRemoved(dirPath, fileName); + for (QString path : removedFiles) { + QFileInfo file(path); + QString dirPath = QDir::cleanPath(file.dir().path()); + QString fileName = file.fileName(); + if (!removedDirs.contains(dirPath)) { //filter out files where folder was removed + qDebug().noquote().nospace() << "[StorageMonitorThread] fileRemoved(\"" << dirPath << "\", \"" << fileName << "\") #" << QThread::currentThreadId(); + emit fileRemoved(dirPath, fileName); + } } - } - for (QString path : addedDirs) { - qDebug().noquote().nospace() << "[StorageMonitorThread] directoryAdded(\"" << path << "\") #" << QThread::currentThreadId(); - emit directoryAdded(path); - } - for (QString path : addedFiles) { - QFileInfo file(path); - QString dirPath = QDir::cleanPath(file.dir().path()); - QString fileName = file.fileName(); - if (!addedDirs.contains(dirPath)) { //filter out files where folder was added - qDebug().noquote().nospace() << "[StorageMonitorThread] fileAdded(\"" << dirPath << "\", \"" << fileName << "\") #" << QThread::currentThreadId(); - emit fileAdded(dirPath, fileName); + for (QString path : addedDirs) { + qDebug().noquote().nospace() << "[StorageMonitorThread] directoryAdded(\"" << path << "\") #" << QThread::currentThreadId(); + emit directoryAdded(path); + } + for (QString path : addedFiles) { + QFileInfo file(path); + QString dirPath = QDir::cleanPath(file.dir().path()); + QString fileName = file.fileName(); + if (!addedDirs.contains(dirPath)) { //filter out files where folder was added + qDebug().noquote().nospace() << "[StorageMonitorThread] fileAdded(\"" << dirPath << "\", \"" << fileName << "\") #" << QThread::currentThreadId(); + emit fileAdded(dirPath, fileName); + } } } + _prevPaths = currPaths; + _mutex.unlock(); //done changing paths } - _prevPaths = currPaths; - _mutex.unlock(); //done changing paths this->msleep(250); } diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 5ecd4f9..b63632a 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -21,6 +21,7 @@ #include "medo/about.h" #include "medo/config.h" #include "medo/feedback.h" +#include "medo/lifetimewatch.h" #include "medo/singleinstance.h" #include "medo/state.h" #include "medo/upgrade.h" @@ -890,6 +891,7 @@ void MainWindow::onTabMenuRequested(const QPoint& point) { } void MainWindow::onTabChanged() { + LifetimeWatch watch("onTabChanged()", true); auto file = dynamic_cast(ui->tabWidget->currentWidget()); bool exists = (file != nullptr); bool hasAny = (ui->tabWidget->count() == 0); @@ -917,6 +919,7 @@ void MainWindow::onTabMoved(int from, int to) { void MainWindow::onTextStateChanged() { + LifetimeWatch watch("onTextStateChanged()"); auto file = dynamic_cast(ui->tabWidget->currentWidget()); bool hasFile = (file != nullptr) ? true : false; @@ -969,9 +972,9 @@ void MainWindow::onTrayShow() { } bool MainWindow::selectFolder(QString folderName) { - qDebug().nospace() << "selectFolder(" << folderName << ") "; - FolderItem* selectedFolder = nullptr; + LifetimeWatch watch("selectFolder(" + folderName + ") "); + FolderItem* selectedFolder = nullptr; for (int i = 0; i < _storage->folderCount(); i++) { auto folder = _storage->folderAt(i); if (folder->name().compare(folderName, Qt::CaseSensitive) == 0) { @@ -994,6 +997,8 @@ bool MainWindow::selectFolder(QString folderName) { bool MainWindow::selectFolder(FolderItem* selectedFolder) { if (selectedFolder != nullptr) { + LifetimeWatch watch("selectFolder*(" + selectedFolder->name() + ")"); + if (_folder != nullptr) { _folder->saveAll(); } //save all files in previous folder / just in case _folder = selectedFolder; @@ -1030,7 +1035,8 @@ bool MainWindow::selectFolder(FolderItem* selectedFolder) { } void MainWindow::selectFile(QString fileName) { - qDebug().nospace() << "selectFile(" << fileName << ") " << _folder->name(); + LifetimeWatch watch("selectFile(" + fileName + ")"); + for (int i = 0; i < ui->tabWidget->count() ; i++) { auto file = dynamic_cast(ui->tabWidget->widget(i)); if (file->name().compare(fileName, Qt::CaseInsensitive) == 0) {