2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-06-23 20:36:52 -07:00
|
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/ErrorBase.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-06-05 07:33:37 -07:00
|
|
|
#include <cerrno>
|
2016-09-21 23:48:54 -07:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2018-06-18 00:13:28 -07:00
|
|
|
#include <set>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/Format.h>
|
|
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2017-10-16 20:00:32 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/WPIErrors.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2019-06-23 20:36:52 -07:00
|
|
|
namespace {
|
|
|
|
|
struct GlobalErrors {
|
|
|
|
|
wpi::mutex mutex;
|
|
|
|
|
std::set<Error> errors;
|
|
|
|
|
const Error* lastError{nullptr};
|
|
|
|
|
|
|
|
|
|
static GlobalErrors& GetInstance();
|
|
|
|
|
static void Insert(const Error& error);
|
|
|
|
|
static void Insert(Error&& error);
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
GlobalErrors& GlobalErrors::GetInstance() {
|
|
|
|
|
static GlobalErrors inst;
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalErrors::Insert(const Error& error) {
|
|
|
|
|
GlobalErrors& inst = GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(inst.mutex);
|
2019-06-23 20:36:52 -07:00
|
|
|
inst.lastError = &(*inst.errors.insert(error).first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalErrors::Insert(Error&& error) {
|
|
|
|
|
GlobalErrors& inst = GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(inst.mutex);
|
2019-06-23 20:36:52 -07:00
|
|
|
inst.lastError = &(*inst.errors.insert(std::move(error)).first);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-10-16 20:00:32 -07:00
|
|
|
ErrorBase::ErrorBase() { HAL_Initialize(500, 0); }
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Error& ErrorBase::GetError() { return m_error; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
const Error& ErrorBase::GetError() const { return m_error; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::ClearError() const { m_error.Clear(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void ErrorBase::SetErrnoError(const wpi::Twine& contextMessage,
|
2018-05-13 17:09:56 -07:00
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
|
|
|
|
int lineNumber) const {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
wpi::raw_svector_ostream err(buf);
|
2015-06-25 15:07:55 -04:00
|
|
|
int errNo = errno;
|
|
|
|
|
if (errNo == 0) {
|
2017-12-01 21:31:35 -08:00
|
|
|
err << "OK: ";
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
2018-04-29 23:33:19 -07:00
|
|
|
err << std::strerror(errNo) << " (" << wpi::format_hex(errNo, 10, true)
|
2017-12-01 21:31:35 -08:00
|
|
|
<< "): ";
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
// Set the current error information for this object.
|
2017-12-01 21:31:35 -08:00
|
|
|
m_error.Set(-1, err.str() + contextMessage, filename, function, lineNumber,
|
|
|
|
|
this);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(m_error);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void ErrorBase::SetImaqError(int success, const wpi::Twine& contextMessage,
|
|
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
2016-09-06 00:01:45 -07:00
|
|
|
int lineNumber) const {
|
2016-05-20 17:30:37 -07:00
|
|
|
// If there was an error
|
2015-06-25 15:07:55 -04:00
|
|
|
if (success <= 0) {
|
2016-05-20 17:30:37 -07:00
|
|
|
// Set the current error information for this object.
|
2018-04-29 23:33:19 -07:00
|
|
|
m_error.Set(success, wpi::Twine(success) + ": " + contextMessage, filename,
|
2017-12-01 21:31:35 -08:00
|
|
|
function, lineNumber, this);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(m_error);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void ErrorBase::SetError(Error::Code code, const wpi::Twine& contextMessage,
|
|
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
2016-09-06 00:01:45 -07:00
|
|
|
int lineNumber) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
// If there was an error
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
m_error.Set(code, contextMessage, filename, function, lineNumber, this);
|
|
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(m_error);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-13 20:29:28 -07:00
|
|
|
void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange,
|
|
|
|
|
int32_t maxRange, int32_t requestedValue,
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& contextMessage,
|
2018-05-13 17:09:56 -07:00
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
|
|
|
|
int lineNumber) const {
|
2016-07-13 20:29:28 -07:00
|
|
|
// If there was an error
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
// Set the current error information for this object.
|
2017-12-01 21:31:35 -08:00
|
|
|
m_error.Set(code,
|
2018-04-29 23:33:19 -07:00
|
|
|
contextMessage + ", Minimum Value: " + wpi::Twine(minRange) +
|
|
|
|
|
", MaximumValue: " + wpi::Twine(maxRange) +
|
|
|
|
|
", Requested Value: " + wpi::Twine(requestedValue),
|
2017-12-01 21:31:35 -08:00
|
|
|
filename, function, lineNumber, this);
|
2016-07-13 20:29:28 -07:00
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(m_error);
|
2016-07-13 20:29:28 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void ErrorBase::SetWPIError(const wpi::Twine& errorMessage, Error::Code code,
|
|
|
|
|
const wpi::Twine& contextMessage,
|
|
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
2016-09-06 00:01:45 -07:00
|
|
|
int lineNumber) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
// Set the current error information for this object.
|
2017-12-01 21:31:35 -08:00
|
|
|
m_error.Set(code, errorMessage + ": " + contextMessage, filename, function,
|
|
|
|
|
lineNumber, this);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(m_error);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
void ErrorBase::CloneError(const ErrorBase& rhs) const {
|
2018-06-18 00:13:28 -07:00
|
|
|
m_error = rhs.GetError();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; }
|
|
|
|
|
|
2017-12-01 21:31:35 -08:00
|
|
|
void ErrorBase::SetGlobalError(Error::Code code,
|
2018-04-29 23:33:19 -07:00
|
|
|
const wpi::Twine& contextMessage,
|
2018-05-13 17:09:56 -07:00
|
|
|
wpi::StringRef filename, wpi::StringRef function,
|
|
|
|
|
int lineNumber) {
|
2016-05-20 17:30:37 -07:00
|
|
|
// If there was an error
|
2015-06-25 15:07:55 -04:00
|
|
|
if (code != 0) {
|
2016-05-20 17:30:37 -07:00
|
|
|
// Set the current error information for this object.
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(
|
|
|
|
|
Error(code, contextMessage, filename, function, lineNumber, nullptr));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void ErrorBase::SetGlobalWPIError(const wpi::Twine& errorMessage,
|
|
|
|
|
const wpi::Twine& contextMessage,
|
|
|
|
|
wpi::StringRef filename,
|
|
|
|
|
wpi::StringRef function, int lineNumber) {
|
2019-06-23 20:36:52 -07:00
|
|
|
GlobalErrors::Insert(Error(-1, errorMessage + ": " + contextMessage, filename,
|
|
|
|
|
function, lineNumber, nullptr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Error ErrorBase::GetGlobalError() {
|
|
|
|
|
auto& inst = GlobalErrors::GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock mutex(inst.mutex);
|
2019-06-23 20:36:52 -07:00
|
|
|
if (!inst.lastError) return Error{};
|
|
|
|
|
return *inst.lastError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Error> ErrorBase::GetGlobalErrors() {
|
|
|
|
|
auto& inst = GlobalErrors::GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock mutex(inst.mutex);
|
2019-06-23 20:36:52 -07:00
|
|
|
std::vector<Error> rv;
|
|
|
|
|
for (auto&& error : inst.errors) rv.push_back(error);
|
|
|
|
|
return rv;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2019-06-23 20:36:52 -07:00
|
|
|
void ErrorBase::ClearGlobalErrors() {
|
|
|
|
|
auto& inst = GlobalErrors::GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock mutex(inst.mutex);
|
2019-06-23 20:36:52 -07:00
|
|
|
inst.errors.clear();
|
|
|
|
|
inst.lastError = nullptr;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|