From cb15502b41f813f3af3c79d0d516bf79ec50b33e Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 24 Feb 2021 14:45:17 +0000 Subject: [PATCH] Error reporting: add SGModelData context support Allow us to pass the current error-context from the main thread into the osgDB loader thread. This is necessary so we can attribute AIModel (etc) load problems to the correct source. --- simgear/debug/ErrorReportingCallback.cxx | 18 ++++++++++++++++-- simgear/debug/ErrorReportingCallback.hxx | 12 +++++++++++- simgear/scene/model/modellib.cxx | 16 ++++++++++++++-- simgear/scene/model/modellib.hxx | 9 +++++---- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/simgear/debug/ErrorReportingCallback.cxx b/simgear/debug/ErrorReportingCallback.cxx index 13f1d1f4..2cea927f 100644 --- a/simgear/debug/ErrorReportingCallback.cxx +++ b/simgear/debug/ErrorReportingCallback.cxx @@ -78,17 +78,31 @@ void setErrorContextCallback(ContextCallback cb) static_contextCallback = cb; } -ErrorReportContext::ErrorReportContext(const std::string& key, const std::string& value) : _key(key) +ErrorReportContext::ErrorReportContext(const std::string& key, const std::string& value) { if (static_contextCallback) { + _keys.push_back(key); static_contextCallback(key, value); } } +ErrorReportContext::ErrorReportContext(const ContextMap& context) +{ + if (static_contextCallback) { + for (const auto& p : context) { + _keys.push_back(p.first); + static_contextCallback(p.first, p.second); + } + } +} + ErrorReportContext::~ErrorReportContext() { if (static_contextCallback) { - static_contextCallback(_key, "POP"); + // pop all our keys + for (const auto& k : _keys) { + static_contextCallback(k, "POP"); + } } } diff --git a/simgear/debug/ErrorReportingCallback.hxx b/simgear/debug/ErrorReportingCallback.hxx index 2a2a210a..6866029e 100644 --- a/simgear/debug/ErrorReportingCallback.hxx +++ b/simgear/debug/ErrorReportingCallback.hxx @@ -16,7 +16,9 @@ #pragma once #include +#include #include +#include #include @@ -70,10 +72,18 @@ class ErrorReportContext { public: ErrorReportContext(const std::string& key, const std::string& value); + + using ContextMap = std::map; + + /** + Allow establishing multiple context values in a single operation + */ + ErrorReportContext(const ContextMap& context); + ~ErrorReportContext(); private: - const std::string _key; + std::vector _keys; }; /** diff --git a/simgear/scene/model/modellib.cxx b/simgear/scene/model/modellib.cxx index ad990b6e..edd40bad 100644 --- a/simgear/scene/model/modellib.cxx +++ b/simgear/scene/model/modellib.cxx @@ -29,12 +29,13 @@ #include #include +#include +#include #include #include -#include #include +#include #include -#include #include "SGReaderWriterXML.hxx" @@ -138,6 +139,10 @@ SGModelLib::loadModel(const string &path, opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get()); opt->setModelData(data); + // establish the error report context so we can attribute any load + // errors correctly + simgear::ErrorReportContext ec(data->getErrorContext()); + if (load2DPanels) { opt->setLoadPanel(static_panelFunc); } @@ -160,6 +165,9 @@ SGModelLib::loadDeferredModel(const string &path, SGPropertyNode *prop_root, proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER); proxyNode->setFileName(0, path); + // establish the error report context so we can attribute any load + // errors correctly + simgear::ErrorReportContext ec(data->getErrorContext()); osg::ref_ptr opt; opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions()); @@ -192,6 +200,10 @@ SGModelLib::loadPagedModel(SGPropertyNode *prop_root, SGModelData *data, SGModel unsigned int simple_models = 0; osg::PagedLOD *plod = new osg::PagedLOD; + // establish the error report context so we can attribute any load + // errors correctly + simgear::ErrorReportContext ec(data->getErrorContext()); + osg::ref_ptr opt; opt = SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions()); opt->setPropertyNode(prop_root ? prop_root: static_propRoot.get()); diff --git a/simgear/scene/model/modellib.hxx b/simgear/scene/model/modellib.hxx index 65968c74..c9fd3d07 100644 --- a/simgear/scene/model/modellib.hxx +++ b/simgear/scene/model/modellib.hxx @@ -18,12 +18,9 @@ #ifndef _SG_MODEL_LIB_HXX #define _SG_MODEL_LIB_HXX 1 -#ifndef __cplusplus -# error This library requires C++ -#endif - #include // for SG_USING_STD +#include #include #include @@ -114,6 +111,10 @@ public: virtual void modelLoaded(const std::string& path, SGPropertyNode *prop, osg::Node* branch) = 0; virtual SGModelData* clone() const = 0; + + using ErrorContext = std::map; + + virtual ErrorContext getErrorContext() const = 0; }; /*