feat: add debug logging
This commit is contained in:
parent
71f27bb45f
commit
9a8c5ec7f9
1 changed files with 27 additions and 5 deletions
|
|
@ -21,6 +21,9 @@ ConfigObject::ConfigObject(QObject* parent)
|
||||||
void ConfigObject::loadFromJson(const QJsonObject& obj) {
|
void ConfigObject::loadFromJson(const QJsonObject& obj) {
|
||||||
const auto* meta = metaObject();
|
const auto* meta = metaObject();
|
||||||
|
|
||||||
|
qCDebug(lcConfig) << "Loading JSON into" << meta->className() << "with" << obj.keys().size()
|
||||||
|
<< "keys:" << obj.keys();
|
||||||
|
|
||||||
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
|
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
|
||||||
auto prop = meta->property(i);
|
auto prop = meta->property(i);
|
||||||
const auto key = QString::fromUtf8(prop.name());
|
const auto key = QString::fromUtf8(prop.name());
|
||||||
|
|
@ -35,6 +38,7 @@ void ConfigObject::loadFromJson(const QJsonObject& obj) {
|
||||||
auto* subObj = current.value<ConfigObject*>();
|
auto* subObj = current.value<ConfigObject*>();
|
||||||
|
|
||||||
if (subObj) {
|
if (subObj) {
|
||||||
|
qCDebug(lcConfig) << " Recursing into sub-object" << key;
|
||||||
subObj->loadFromJson(jsonVal.toObject());
|
subObj->loadFromJson(jsonVal.toObject());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -51,12 +55,14 @@ void ConfigObject::loadFromJson(const QJsonObject& obj) {
|
||||||
list.append(v.toString());
|
list.append(v.toString());
|
||||||
prop.write(this, QVariant::fromValue(list));
|
prop.write(this, QVariant::fromValue(list));
|
||||||
m_loadedKeys.insert(key);
|
m_loadedKeys.insert(key);
|
||||||
|
qCDebug(lcConfig) << " Loaded" << key << "=" << list;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For all other types, let Qt's variant conversion handle it
|
// For all other types, let Qt's variant conversion handle it
|
||||||
prop.write(this, jsonVal.toVariant());
|
prop.write(this, jsonVal.toVariant());
|
||||||
m_loadedKeys.insert(key);
|
m_loadedKeys.insert(key);
|
||||||
|
qCDebug(lcConfig) << " Loaded" << key << "=" << jsonVal.toVariant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,11 +183,13 @@ void ConfigObject::clearLoadedKeys() {
|
||||||
void ConfigObject::syncFromGlobal(ConfigObject* global) {
|
void ConfigObject::syncFromGlobal(ConfigObject* global) {
|
||||||
m_global = global;
|
m_global = global;
|
||||||
|
|
||||||
|
const auto* meta = metaObject();
|
||||||
|
qCDebug(lcConfig) << "Syncing" << meta->className() << "from global, loaded keys:" << m_loadedKeys;
|
||||||
|
|
||||||
// Connect batched change signal (single connection per ConfigObject pair)
|
// Connect batched change signal (single connection per ConfigObject pair)
|
||||||
connect(global, &ConfigObject::propertiesChanged, this, &ConfigObject::onGlobalPropertiesChanged);
|
connect(global, &ConfigObject::propertiesChanged, this, &ConfigObject::onGlobalPropertiesChanged);
|
||||||
|
|
||||||
// Initial sync: copy all non-loaded property values from global
|
// Initial sync: copy all non-loaded property values from global
|
||||||
const auto* meta = metaObject();
|
|
||||||
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
|
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
|
||||||
auto prop = meta->property(i);
|
auto prop = meta->property(i);
|
||||||
const auto key = QString::fromUtf8(prop.name());
|
const auto key = QString::fromUtf8(prop.name());
|
||||||
|
|
@ -200,8 +208,13 @@ void ConfigObject::syncFromGlobal(ConfigObject* global) {
|
||||||
if (!prop.isWritable())
|
if (!prop.isWritable())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!m_loadedKeys.contains(key))
|
if (!m_loadedKeys.contains(key)) {
|
||||||
prop.write(this, prop.read(global));
|
auto val = prop.read(global);
|
||||||
|
prop.write(this, val);
|
||||||
|
qCDebug(lcConfig) << " Synced" << key << "=" << val << "from global";
|
||||||
|
} else {
|
||||||
|
qCDebug(lcConfig) << " Keeping loaded" << key << "=" << prop.read(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,8 +249,11 @@ void ConfigObject::onGlobalPropertiesChanged(const QMap<QString, QVariant>& chan
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int idx = metaObject()->indexOfProperty(it.key().toUtf8().constData());
|
int idx = metaObject()->indexOfProperty(it.key().toUtf8().constData());
|
||||||
if (idx >= 0)
|
if (idx >= 0) {
|
||||||
metaObject()->property(idx).write(this, it.value());
|
metaObject()->property(idx).write(this, it.value());
|
||||||
|
qCDebug(lcConfig) << metaObject()->className() << "synced" << it.key() << "=" << it.value()
|
||||||
|
<< "from global change";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -293,6 +309,8 @@ void ConfigObject::setupFileBackend(const QString& path) {
|
||||||
|
|
||||||
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &ConfigObject::onFileChanged);
|
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &ConfigObject::onFileChanged);
|
||||||
|
|
||||||
|
qCDebug(lcConfig) << "Setting up file backend for" << metaObject()->className() << "at" << path;
|
||||||
|
|
||||||
reloadFromFile();
|
reloadFromFile();
|
||||||
|
|
||||||
if (QFile::exists(m_filePath))
|
if (QFile::exists(m_filePath))
|
||||||
|
|
@ -323,13 +341,17 @@ void ConfigObject::reloadFromFile() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qCDebug(lcConfig) << "Reloading" << metaObject()->className() << "from" << m_filePath;
|
||||||
|
|
||||||
clearLoadedKeys();
|
clearLoadedKeys();
|
||||||
loadFromJson(doc.object());
|
loadFromJson(doc.object());
|
||||||
|
|
||||||
// Re-sync non-loaded properties from global after reload
|
// Re-sync non-loaded properties from global after reload
|
||||||
if (m_global)
|
if (m_global) {
|
||||||
|
qCDebug(lcConfig) << "Re-syncing" << metaObject()->className() << "from global after reload";
|
||||||
resyncFromGlobal();
|
resyncFromGlobal();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ConfigObject::onFileChanged() {
|
void ConfigObject::onFileChanged() {
|
||||||
if (!m_watcher->files().contains(m_filePath))
|
if (!m_watcher->files().contains(m_filePath))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue