From 7b4dc51f934cab81d20868cde8727f9ed279e798 Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 29 Dec 2020 16:17:43 +0000 Subject: [PATCH] Add new error reporting function / callback --- simgear/debug/ErrorReportingCallback.cxx | 48 ++++++++++++++++ simgear/debug/ErrorReportingCallback.hxx | 71 ++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/simgear/debug/ErrorReportingCallback.cxx b/simgear/debug/ErrorReportingCallback.cxx index 85159025..13f1d1f4 100644 --- a/simgear/debug/ErrorReportingCallback.cxx +++ b/simgear/debug/ErrorReportingCallback.cxx @@ -17,11 +17,15 @@ #include "ErrorReportingCallback.hxx" +#include + using std::string; namespace simgear { static ErrorReportCallback static_callback; +static ContextCallback static_contextCallback; + void setErrorReportCallback(ErrorReportCallback cb) { @@ -44,4 +48,48 @@ void reportFatalError(const std::string& msg, const std::string& more) 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 diff --git a/simgear/debug/ErrorReportingCallback.hxx b/simgear/debug/ErrorReportingCallback.hxx index b707f3de..5f55e1d8 100644 --- a/simgear/debug/ErrorReportingCallback.hxx +++ b/simgear/debug/ErrorReportingCallback.hxx @@ -18,6 +18,11 @@ #include #include +#include + +//forward decls +class SGPath; + namespace simgear { void reportError(const std::string& msg, const std::string& more = {}); @@ -28,4 +33,70 @@ using ErrorReportCallback = std::function; + +void setFailureCallback(FailureCallback cb); + +using ContextCallback = std::function; + +void setErrorContextCallback(ContextCallback cb); + } // namespace simgear