Use Twine for error message inputs.

This commit is contained in:
Peter Johnson
2017-12-01 21:31:35 -08:00
parent ab137abab5
commit fe53dd2f28
6 changed files with 69 additions and 68 deletions

View File

@@ -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);

View File

@@ -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() {

View File

@@ -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<wpi::mutex> 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<wpi::mutex> 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<wpi::mutex> 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<wpi::mutex> 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<wpi::mutex> 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);
}
/**