mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Replace globalError in ErrorBase with a global set of all errors (#615)
This commit is contained in:
committed by
Peter Johnson
parent
2faba39b58
commit
212f378d08
@@ -15,14 +15,30 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
void Error::Clone(const Error& error) {
|
||||
m_code = error.m_code;
|
||||
m_message = error.m_message;
|
||||
m_filename = error.m_filename;
|
||||
m_function = error.m_function;
|
||||
m_lineNumber = error.m_lineNumber;
|
||||
m_originatingObject = error.m_originatingObject;
|
||||
m_timestamp = error.m_timestamp;
|
||||
Error::Error(Code code, const wpi::Twine& contextMessage,
|
||||
wpi::StringRef filename, wpi::StringRef function, int lineNumber,
|
||||
const ErrorBase* originatingObject) {
|
||||
Set(code, contextMessage, filename, function, lineNumber, originatingObject);
|
||||
}
|
||||
|
||||
bool Error::operator<(const Error& rhs) const {
|
||||
if (m_code < rhs.m_code) {
|
||||
return true;
|
||||
} else if (m_message < rhs.m_message) {
|
||||
return true;
|
||||
} else if (m_filename < rhs.m_filename) {
|
||||
return true;
|
||||
} else if (m_function < rhs.m_function) {
|
||||
return true;
|
||||
} else if (m_lineNumber < rhs.m_lineNumber) {
|
||||
return true;
|
||||
} else if (m_originatingObject < rhs.m_originatingObject) {
|
||||
return true;
|
||||
} else if (m_timestamp < rhs.m_timestamp) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Error::Code Error::GetCode() const { return m_code; }
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <set>
|
||||
|
||||
#include <HAL/HAL.h>
|
||||
#include <wpi/Format.h>
|
||||
@@ -21,8 +22,8 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
wpi::mutex ErrorBase::_globalErrorMutex;
|
||||
Error ErrorBase::_globalError;
|
||||
static wpi::mutex globalErrorsMutex;
|
||||
static std::set<Error> globalErrors;
|
||||
|
||||
ErrorBase::ErrorBase() { HAL_Initialize(500, 0); }
|
||||
|
||||
@@ -50,10 +51,8 @@ void ErrorBase::SetErrnoError(const wpi::Twine& contextMessage,
|
||||
this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() == 0) {
|
||||
_globalError.Clone(m_error);
|
||||
}
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.insert(m_error);
|
||||
}
|
||||
|
||||
void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage,
|
||||
@@ -66,10 +65,8 @@ void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage,
|
||||
function, lineNumber, this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() == 0) {
|
||||
_globalError.Clone(m_error);
|
||||
}
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.insert(m_error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,10 +79,8 @@ void ErrorBase::SetError(Error::Code code, const wpi::Twine& contextMessage,
|
||||
m_error.Set(code, contextMessage, filename, function, lineNumber, this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() == 0) {
|
||||
_globalError.Clone(m_error);
|
||||
}
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.insert(m_error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,10 +99,8 @@ void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange,
|
||||
filename, function, lineNumber, this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() == 0) {
|
||||
_globalError.Clone(m_error);
|
||||
}
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.insert(m_error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,14 +113,12 @@ void ErrorBase::SetWPIError(const wpi::Twine& errorMessage, Error::Code code,
|
||||
lineNumber, this);
|
||||
|
||||
// Update the global error if there is not one already set.
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() == 0) {
|
||||
_globalError.Clone(m_error);
|
||||
}
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.insert(m_error);
|
||||
}
|
||||
|
||||
void ErrorBase::CloneError(const ErrorBase& rhs) const {
|
||||
m_error.Clone(rhs.GetError());
|
||||
m_error = rhs.GetError();
|
||||
}
|
||||
|
||||
bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; }
|
||||
@@ -138,11 +129,11 @@ void ErrorBase::SetGlobalError(Error::Code code,
|
||||
int lineNumber) {
|
||||
// If there was an error
|
||||
if (code != 0) {
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
|
||||
// Set the current error information for this object.
|
||||
_globalError.Set(code, contextMessage, filename, function, lineNumber,
|
||||
nullptr);
|
||||
globalErrors.emplace(code, contextMessage, filename, function, lineNumber,
|
||||
nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,15 +141,12 @@ void ErrorBase::SetGlobalWPIError(const wpi::Twine& errorMessage,
|
||||
const wpi::Twine& contextMessage,
|
||||
wpi::StringRef filename,
|
||||
wpi::StringRef function, int lineNumber) {
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
if (_globalError.GetCode() != 0) {
|
||||
_globalError.Clear();
|
||||
}
|
||||
_globalError.Set(-1, errorMessage + ": " + contextMessage, filename, function,
|
||||
lineNumber, nullptr);
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
globalErrors.emplace(-1, errorMessage + ": " + contextMessage, filename,
|
||||
function, lineNumber, nullptr);
|
||||
}
|
||||
|
||||
Error& ErrorBase::GetGlobalError() {
|
||||
std::lock_guard<wpi::mutex> mutex(_globalErrorMutex);
|
||||
return _globalError;
|
||||
const Error& ErrorBase::GetGlobalError() {
|
||||
std::lock_guard<wpi::mutex> mutex(globalErrorsMutex);
|
||||
return *globalErrors.begin();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user