[hal] Add HAL_GetLastError to enable better error messages from HAL calls (#3320)

This uses thread local storage so a full error string can be provided, not just an error code.
This commit is contained in:
Thad House
2021-04-29 09:56:54 -07:00
committed by GitHub
parent ced654880c
commit 5127380727
10 changed files with 81 additions and 13 deletions

View File

@@ -0,0 +1,41 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include "hal/Errors.h"
#include "hal/HALBase.h"
namespace {
struct LastErrorStorage {
int32_t status;
wpi::SmallString<512> message;
};
} // namespace
static LastErrorStorage& GetThreadLastError() {
thread_local LastErrorStorage lastError;
return lastError;
}
namespace hal {
void SetLastError(int32_t status, const wpi::Twine& value) {
LastErrorStorage& lastError = GetThreadLastError();
lastError.message.clear();
value.toVector(lastError.message);
lastError.status = status;
}
} // namespace hal
extern "C" {
const char* HAL_GetLastError(int32_t* status) {
if (*status == HAL_USE_LAST_ERROR) {
LastErrorStorage& lastError = GetThreadLastError();
*status = lastError.status;
return lastError.message.c_str();
}
return HAL_GetErrorMessage(*status);
}
} // extern "C"

View File

@@ -85,7 +85,7 @@ void ThrowUncleanStatusException(JNIEnv* env, wpi::StringRef msg,
void ThrowAllocationException(JNIEnv* env, int32_t minRange, int32_t maxRange,
int32_t requestedValue, int32_t status) {
const char* message = HAL_GetErrorMessage(status);
const char* message = HAL_GetLastError(&status);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message
@@ -96,7 +96,7 @@ void ThrowAllocationException(JNIEnv* env, int32_t minRange, int32_t maxRange,
}
void ThrowHalHandleException(JNIEnv* env, int32_t status) {
const char* message = HAL_GetErrorMessage(status);
const char* message = HAL_GetLastError(&status);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message;
@@ -110,7 +110,7 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
}
const char* message = HAL_GetErrorMessage(status);
const char* message = HAL_GetLastError(&status);
if (doThrow && status < 0) {
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
@@ -119,7 +119,11 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
} else {
std::string func;
auto stack = GetJavaStackTrace(env, &func, "edu.wpi.first");
HAL_SendError(1, status, 0, message, func.c_str(), stack.c_str(), 1);
// Make a copy of message for safety, calling back into the HAL might
// invalidate the string.
wpi::SmallString<256> lastMessage{wpi::StringRef{message}};
HAL_SendError(1, status, 0, lastMessage.c_str(), func.c_str(),
stack.c_str(), 1);
}
}