Add new error reporting function / callback

This commit is contained in:
James Turner 2020-12-29 16:17:43 +00:00
parent 368cbcb377
commit 7b4dc51f93
2 changed files with 119 additions and 0 deletions

View File

@ -17,11 +17,15 @@
#include "ErrorReportingCallback.hxx" #include "ErrorReportingCallback.hxx"
#include <simgear/misc/sg_path.hxx>
using std::string; using std::string;
namespace simgear { namespace simgear {
static ErrorReportCallback static_callback; static ErrorReportCallback static_callback;
static ContextCallback static_contextCallback;
void setErrorReportCallback(ErrorReportCallback cb) void setErrorReportCallback(ErrorReportCallback cb)
{ {
@ -44,4 +48,48 @@ void reportFatalError(const std::string& msg, const std::string& more)
static_callback(msg, more, true); static_callback(msg, more, true);
} }
static FailureCallback static_failureCallback;
void reportFailure(LoadFailure type, ErrorCode code, const std::string& details, sg_location loc)
{
if (!static_failureCallback) {
return;
}
static_failureCallback(type, code, details, loc);
}
void reportFailure(LoadFailure type, ErrorCode code, const std::string& details, const SGPath& path)
{
if (!static_failureCallback) {
return;
}
static_failureCallback(type, code, details, sg_location{path});
}
void setFailureCallback(FailureCallback cb)
{
static_failureCallback = cb;
}
void setErrorContextCallback(ContextCallback cb)
{
static_contextCallback = cb;
}
ErrorReportContext::ErrorReportContext(const std::string& key, const std::string& value) : _key(key)
{
if (static_contextCallback) {
static_contextCallback(key, value);
}
}
ErrorReportContext::~ErrorReportContext()
{
if (static_contextCallback) {
static_contextCallback(_key, "POP");
}
}
} // namespace simgear } // namespace simgear

View File

@ -18,6 +18,11 @@
#include <functional> #include <functional>
#include <string> #include <string>
#include <simgear/structure/exception.hxx>
//forward decls
class SGPath;
namespace simgear { namespace simgear {
void reportError(const std::string& msg, const std::string& more = {}); void reportError(const std::string& msg, const std::string& more = {});
@ -28,4 +33,70 @@ using ErrorReportCallback = std::function<void(const std::string& msg, const std
void setErrorReportCallback(ErrorReportCallback cb); void setErrorReportCallback(ErrorReportCallback cb);
enum class LoadFailure {
Unknown,
NotFound,
OutOfMemory,
BadHeader,
BadData,
Misconfigured
};
/**
@brief enum of the operations which can fail. This should be extended as necessary: it maps to
translated error messages for the user.
*/
enum class ErrorCode {
MissingShader,
LoadingTexture,
XMLModelLoad,
ThreeDModelLoad, // AC3D, OBJ, etc
BTGLoad,
ScenarioLoad,
GUIDialog,
AudioFX,
XMLLoadCommand,
AircraftSystems,
InputDeviceConfig
};
/**
@brief Define an error-reporting context value, for the duration of this
object's lifetime. The context value will be active for any errors occuring on the same thread, while
this object exists.
*/
class ErrorReportContext
{
public:
ErrorReportContext(const std::string& key, const std::string& value);
~ErrorReportContext();
private:
const std::string _key;
};
/**
* @brief Report failure to load a resource, so they can be collated for reporting
* to the user.
*
* @param type - the reason for the failure, if it can be determined
* @param msg - an informational message about what caused the failure
* @param path - path on disk to the resource. In some cases this may be a relative path;
* especially in the case of a resource not found, we cannot report a file path.
*/
void reportFailure(LoadFailure type, ErrorCode code, const std::string& detailedMessage = {}, sg_location loc = {});
/**
overload taking a path as the location
*/
void reportFailure(LoadFailure type, ErrorCode code, const std::string& detailedMessage, const SGPath& p);
using FailureCallback = std::function<void(LoadFailure type, ErrorCode code, const std::string& details, const sg_location& location)>;
void setFailureCallback(FailureCallback cb);
using ContextCallback = std::function<void(const std::string& key, const std::string& value)>;
void setErrorContextCallback(ContextCallback cb);
} // namespace simgear } // namespace simgear