diff --git a/wpilibc/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index f463261b93..3c4db14f7d 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -56,9 +56,10 @@ DriverStation& DriverStation::GetInstance() { * * The error is also printed to the program console. */ -void DriverStation::ReportError(llvm::StringRef error) { +void DriverStation::ReportError(const llvm::Twine& error) { llvm::SmallString<128> temp; - HAL_SendError(1, 1, 0, error.c_str(temp), "", "", 1); + HAL_SendError(1, 1, 0, error.toNullTerminatedStringRef(temp).data(), "", "", + 1); } /** @@ -66,9 +67,10 @@ void DriverStation::ReportError(llvm::StringRef error) { * * The warning is also printed to the program console. */ -void DriverStation::ReportWarning(llvm::StringRef error) { +void DriverStation::ReportWarning(const llvm::Twine& error) { llvm::SmallString<128> temp; - HAL_SendError(0, 1, 0, error.c_str(temp), "", "", 1); + HAL_SendError(0, 1, 0, error.toNullTerminatedStringRef(temp).data(), "", "", + 1); } /** @@ -77,13 +79,16 @@ void DriverStation::ReportWarning(llvm::StringRef error) { * The error is also printed to the program console. */ void DriverStation::ReportError(bool isError, int32_t code, - llvm::StringRef error, llvm::StringRef location, - llvm::StringRef stack) { + const llvm::Twine& error, + const llvm::Twine& location, + const llvm::Twine& stack) { llvm::SmallString<128> errorTemp; llvm::SmallString<128> locationTemp; llvm::SmallString<128> stackTemp; - HAL_SendError(isError, code, 0, error.c_str(errorTemp), - location.c_str(locationTemp), stack.c_str(stackTemp), 1); + HAL_SendError(isError, code, 0, + error.toNullTerminatedStringRef(errorTemp).data(), + location.toNullTerminatedStringRef(locationTemp).data(), + stack.toNullTerminatedStringRef(stackTemp).data(), 1); } /** @@ -717,7 +722,7 @@ DriverStation::DriverStation() { * Reports errors related to unplugged joysticks * Throttles the errors so that they don't overwhelm the DS */ -void DriverStation::ReportJoystickUnpluggedError(llvm::StringRef message) { +void DriverStation::ReportJoystickUnpluggedError(const llvm::Twine& message) { double currentTime = Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { ReportError(message); @@ -730,7 +735,7 @@ void DriverStation::ReportJoystickUnpluggedError(llvm::StringRef message) { * * Throttles the errors so that they don't overwhelm the DS. */ -void DriverStation::ReportJoystickUnpluggedWarning(llvm::StringRef message) { +void DriverStation::ReportJoystickUnpluggedWarning(const llvm::Twine& message) { double currentTime = Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { ReportWarning(message); diff --git a/wpilibc/src/main/native/cpp/Error.cpp b/wpilibc/src/main/native/cpp/Error.cpp index 5e09edd4c5..8654c06ed4 100644 --- a/wpilibc/src/main/native/cpp/Error.cpp +++ b/wpilibc/src/main/native/cpp/Error.cpp @@ -43,7 +43,7 @@ const ErrorBase* Error::GetOriginatingObject() const { double Error::GetTimestamp() const { return m_timestamp; } -void Error::Set(Code code, llvm::StringRef contextMessage, +void Error::Set(Code code, const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber, const ErrorBase* originatingObject) { bool report = true; @@ -53,7 +53,7 @@ void Error::Set(Code code, llvm::StringRef contextMessage, } m_code = code; - m_message = contextMessage; + m_message = contextMessage.str(); m_filename = filename; m_function = function; m_lineNumber = lineNumber; @@ -66,13 +66,11 @@ void Error::Set(Code code, llvm::StringRef contextMessage, } void Error::Report() { - llvm::SmallString<128> buf; - llvm::raw_svector_ostream locStream(buf); - locStream << m_function << " [" << llvm::sys::path::filename(m_filename); - locStream << ":" << m_lineNumber << "]"; - - DriverStation::ReportError(true, m_code, m_message, locStream.str(), - GetStackTrace(4)); + DriverStation::ReportError( + true, m_code, m_message, + m_function + llvm::Twine(" [") + llvm::sys::path::filename(m_filename) + + llvm::Twine(':') + llvm::Twine(m_lineNumber) + llvm::Twine(']'), + GetStackTrace(4)); } void Error::Clear() { diff --git a/wpilibc/src/main/native/cpp/ErrorBase.cpp b/wpilibc/src/main/native/cpp/ErrorBase.cpp index 0642b7e8f9..0a0d57e788 100644 --- a/wpilibc/src/main/native/cpp/ErrorBase.cpp +++ b/wpilibc/src/main/native/cpp/ErrorBase.cpp @@ -49,21 +49,22 @@ void ErrorBase::ClearError() const { m_error.Clear(); } * @param function Function of the error source * @param lineNumber Line number of the error source */ -void ErrorBase::SetErrnoError(llvm::StringRef contextMessage, +void ErrorBase::SetErrnoError(const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const { llvm::SmallString<128> buf; llvm::raw_svector_ostream err(buf); int errNo = errno; if (errNo == 0) { - err << "OK: " << contextMessage; + err << "OK: "; } else { err << std::strerror(errNo) << " (" << llvm::format_hex(errNo, 10, true) - << "): " << contextMessage; + << "): "; } // Set the current error information for this object. - m_error.Set(-1, err.str(), filename, function, lineNumber, this); + m_error.Set(-1, err.str() + contextMessage, filename, function, lineNumber, + this); // Update the global error if there is not one already set. std::lock_guard mutex(_globalErrorMutex); @@ -82,17 +83,14 @@ void ErrorBase::SetErrnoError(llvm::StringRef contextMessage, * @param function Function of the error source * @param lineNumber Line number of the error source */ -void ErrorBase::SetImaqError(int success, llvm::StringRef contextMessage, +void ErrorBase::SetImaqError(int success, const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const { // If there was an error if (success <= 0) { - llvm::SmallString<128> buf; - llvm::raw_svector_ostream err(buf); - err << success << ": " << contextMessage; - // Set the current error information for this object. - m_error.Set(success, err.str(), filename, function, lineNumber, this); + m_error.Set(success, llvm::Twine(success) + ": " + contextMessage, filename, + function, lineNumber, this); // Update the global error if there is not one already set. std::lock_guard mutex(_globalErrorMutex); @@ -111,7 +109,7 @@ void ErrorBase::SetImaqError(int success, llvm::StringRef contextMessage, * @param function Function of the error source * @param lineNumber Line number of the error source */ -void ErrorBase::SetError(Error::Code code, llvm::StringRef contextMessage, +void ErrorBase::SetError(Error::Code code, const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const { // If there was an error @@ -142,20 +140,17 @@ void ErrorBase::SetError(Error::Code code, llvm::StringRef contextMessage, */ void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, - llvm::StringRef contextMessage, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const { // If there was an error if (code != 0) { - size_t size = contextMessage.size() + 100; - char* buf = new char[size]; - std::snprintf( - buf, size, - "%s, Minimum Value: %d, Maximum Value: %d, Requested Value: %d", - contextMessage.data(), minRange, maxRange, requestedValue); // Set the current error information for this object. - m_error.Set(code, buf, filename, function, lineNumber, this); - delete[] buf; + m_error.Set(code, + contextMessage + ", Minimum Value: " + llvm::Twine(minRange) + + ", MaximumValue: " + llvm::Twine(maxRange) + + ", Requested Value: " + llvm::Twine(requestedValue), + filename, function, lineNumber, this); // Update the global error if there is not one already set. std::lock_guard mutex(_globalErrorMutex); @@ -174,14 +169,13 @@ void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange, * @param function Function of the error source * @param lineNumber Line number of the error source */ -void ErrorBase::SetWPIError(llvm::StringRef errorMessage, Error::Code code, - llvm::StringRef contextMessage, +void ErrorBase::SetWPIError(const llvm::Twine& errorMessage, Error::Code code, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const { - std::string err = errorMessage.str() + ": " + contextMessage.str(); - // Set the current error information for this object. - m_error.Set(code, err, filename, function, lineNumber, this); + m_error.Set(code, errorMessage + ": " + contextMessage, filename, function, + lineNumber, this); // Update the global error if there is not one already set. std::lock_guard mutex(_globalErrorMutex); @@ -201,7 +195,8 @@ void ErrorBase::CloneError(const ErrorBase& rhs) const { */ bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; } -void ErrorBase::SetGlobalError(Error::Code code, llvm::StringRef contextMessage, +void ErrorBase::SetGlobalError(Error::Code code, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) { // If there was an error @@ -214,17 +209,16 @@ void ErrorBase::SetGlobalError(Error::Code code, llvm::StringRef contextMessage, } } -void ErrorBase::SetGlobalWPIError(llvm::StringRef errorMessage, - llvm::StringRef contextMessage, +void ErrorBase::SetGlobalWPIError(const llvm::Twine& errorMessage, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) { - std::string err = errorMessage.str() + ": " + contextMessage.str(); - std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() != 0) { _globalError.Clear(); } - _globalError.Set(-1, err, filename, function, lineNumber, nullptr); + _globalError.Set(-1, errorMessage + ": " + contextMessage, filename, function, + lineNumber, nullptr); } /** diff --git a/wpilibc/src/main/native/include/DriverStation.h b/wpilibc/src/main/native/include/DriverStation.h index 28678e466c..a585ff0839 100644 --- a/wpilibc/src/main/native/include/DriverStation.h +++ b/wpilibc/src/main/native/include/DriverStation.h @@ -14,7 +14,7 @@ #include #include -#include +#include #include #include "ErrorBase.h" @@ -35,10 +35,11 @@ class DriverStation : public ErrorBase, public RobotStateInterface { ~DriverStation() override; static DriverStation& GetInstance(); - static void ReportError(llvm::StringRef error); - static void ReportWarning(llvm::StringRef error); - static void ReportError(bool isError, int code, llvm::StringRef error, - llvm::StringRef location, llvm::StringRef stack); + static void ReportError(const llvm::Twine& error); + static void ReportWarning(const llvm::Twine& error); + static void ReportError(bool isError, int code, const llvm::Twine& error, + const llvm::Twine& location, + const llvm::Twine& stack); static constexpr int kJoystickPorts = 6; @@ -124,8 +125,8 @@ class DriverStation : public ErrorBase, public RobotStateInterface { private: DriverStation(); - void ReportJoystickUnpluggedError(llvm::StringRef message); - void ReportJoystickUnpluggedWarning(llvm::StringRef message); + void ReportJoystickUnpluggedError(const llvm::Twine& message); + void ReportJoystickUnpluggedWarning(const llvm::Twine& message); void Run(); void UpdateControlWord(bool force, HAL_ControlWord& controlWord) const; diff --git a/wpilibc/src/main/native/include/Error.h b/wpilibc/src/main/native/include/Error.h index 815dc721fa..92d3a639dd 100644 --- a/wpilibc/src/main/native/include/Error.h +++ b/wpilibc/src/main/native/include/Error.h @@ -12,6 +12,7 @@ #include #include +#include #ifdef _WIN32 #include @@ -46,8 +47,8 @@ class Error { const ErrorBase* GetOriginatingObject() const; double GetTimestamp() const; void Clear(); - void Set(Code code, llvm::StringRef contextMessage, llvm::StringRef filename, - llvm::StringRef function, int lineNumber, + void Set(Code code, const llvm::Twine& contextMessage, + llvm::StringRef filename, llvm::StringRef function, int lineNumber, const ErrorBase* originatingObject); private: diff --git a/wpilibc/src/main/native/include/ErrorBase.h b/wpilibc/src/main/native/include/ErrorBase.h index 2c3899f49c..52bbfcd453 100644 --- a/wpilibc/src/main/native/include/ErrorBase.h +++ b/wpilibc/src/main/native/include/ErrorBase.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include "Base.h" @@ -81,32 +82,33 @@ class ErrorBase { virtual Error& GetError(); virtual const Error& GetError() const; - virtual void SetErrnoError(llvm::StringRef contextMessage, + virtual void SetErrnoError(const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const; - virtual void SetImaqError(int success, llvm::StringRef contextMessage, + virtual void SetImaqError(int success, const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const; - virtual void SetError(Error::Code code, llvm::StringRef contextMessage, + virtual void SetError(Error::Code code, const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const; virtual void SetErrorRange(Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, - llvm::StringRef contextMessage, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const; - virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code, - llvm::StringRef contextMessage, + virtual void SetWPIError(const llvm::Twine& errorMessage, Error::Code code, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber) const; virtual void CloneError(const ErrorBase& rhs) const; virtual void ClearError() const; virtual bool StatusIsFatal() const; - static void SetGlobalError(Error::Code code, llvm::StringRef contextMessage, + static void SetGlobalError(Error::Code code, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber); - static void SetGlobalWPIError(llvm::StringRef errorMessage, - llvm::StringRef contextMessage, + static void SetGlobalWPIError(const llvm::Twine& errorMessage, + const llvm::Twine& contextMessage, llvm::StringRef filename, llvm::StringRef function, int lineNumber); static Error& GetGlobalError();