Skip to content

Commit

Permalink
Added LifetimeWatch to debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
medo64 committed Dec 11, 2021
1 parent 45bcca8 commit f6d0f03
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 50 deletions.
5 changes: 5 additions & 0 deletions src/QText.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
29 changes: 27 additions & 2 deletions src/medo/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QSaveFile>
#include <QStandardPaths>
#include <QTextStream>
Expand All @@ -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();
Expand All @@ -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;
}
}


Expand Down
60 changes: 60 additions & 0 deletions src/medo/lifetimewatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Josip Medved <[email protected]> * www.medo64.com * MIT License */

#include <QDebug>
#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();
}
30 changes: 30 additions & 0 deletions src/medo/lifetimewatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Josip Medved <[email protected]> * www.medo64.com * MIT License */

// 2021-12-09: Initial version

#pragma once

#include <QElapsedTimer>
#include <QMutex>
#include <QString>
#include <QUuid>

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;

};
3 changes: 3 additions & 0 deletions src/storage/fileitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QFileInfo>
#include <QMenu>
#include <QTextDocumentFragment>
#include "medo/lifetimewatch.h"
#include "ui/inserttimedialog.h"
#include "clipboard.h"
#include "fileitem.h"
Expand Down Expand Up @@ -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()) {
Expand All @@ -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(); }
Expand Down
2 changes: 2 additions & 0 deletions src/storage/folderitem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <QDir>
#include <QString>
#include "medo/config.h"
#include "medo/lifetimewatch.h"
#include "storage/fileitem.h"
#include "storage/folderitem.h"
#include "storage/storage.h"
Expand Down Expand Up @@ -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()) {
Expand Down
94 changes: 49 additions & 45 deletions src/storage/storagemonitorthread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QDebug>
#include <QDir>
#include "medo/lifetimewatch.h"
#include "storage.h"
#include "storagemonitorthread.h"

Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 9 additions & 3 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -890,6 +891,7 @@ void MainWindow::onTabMenuRequested(const QPoint& point) {
}

void MainWindow::onTabChanged() {
LifetimeWatch watch("onTabChanged()", true);
auto file = dynamic_cast<FileItem*>(ui->tabWidget->currentWidget());
bool exists = (file != nullptr);
bool hasAny = (ui->tabWidget->count() == 0);
Expand Down Expand Up @@ -917,6 +919,7 @@ void MainWindow::onTabMoved(int from, int to) {


void MainWindow::onTextStateChanged() {
LifetimeWatch watch("onTextStateChanged()");
auto file = dynamic_cast<FileItem*>(ui->tabWidget->currentWidget());

bool hasFile = (file != nullptr) ? true : false;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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<FileItem*>(ui->tabWidget->widget(i));
if (file->name().compare(fileName, Qt::CaseInsensitive) == 0) {
Expand Down

0 comments on commit f6d0f03

Please sign in to comment.