2021-04-29 09:56:54 -07:00
|
|
|
// 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.
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2021-04-29 09:56:54 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Errors.h"
|
|
|
|
|
#include "wpi/hal/HALBase.h"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/util/SmallString.hpp"
|
2021-04-29 09:56:54 -07:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
struct LastErrorStorage {
|
|
|
|
|
int32_t status;
|
|
|
|
|
wpi::SmallString<512> message;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
static LastErrorStorage& GetThreadLastError() {
|
|
|
|
|
thread_local LastErrorStorage lastError;
|
|
|
|
|
return lastError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace hal {
|
2021-06-06 16:13:58 -07:00
|
|
|
void SetLastError(int32_t* status, std::string_view value) {
|
2021-04-29 09:56:54 -07:00
|
|
|
LastErrorStorage& lastError = GetThreadLastError();
|
2021-06-06 16:13:58 -07:00
|
|
|
lastError.message = value;
|
2021-05-01 10:28:30 -07:00
|
|
|
lastError.status = *status;
|
|
|
|
|
*status = HAL_USE_LAST_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SetLastErrorIndexOutOfRange(int32_t* status, std::string_view message,
|
2021-05-01 10:28:30 -07:00
|
|
|
int32_t minimum, int32_t maximum,
|
|
|
|
|
int32_t requested) {
|
2021-06-06 16:13:58 -07:00
|
|
|
SetLastError(
|
|
|
|
|
status,
|
|
|
|
|
fmt::format("{}\n Status: {}\n Minimum: {} Maximum: {} Requested: {}",
|
|
|
|
|
message, *status, minimum, maximum, requested));
|
2021-05-01 10:28:30 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SetLastErrorPreviouslyAllocated(int32_t* status, std::string_view message,
|
2021-05-01 10:28:30 -07:00
|
|
|
int32_t channel,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view previousAllocation) {
|
|
|
|
|
hal::SetLastError(status,
|
|
|
|
|
fmt::format("{} {} previously allocated.\n"
|
|
|
|
|
"Location of the previous allocation:\n{}\n"
|
|
|
|
|
"Location of the current allocation:",
|
|
|
|
|
message, channel, previousAllocation));
|
2021-04-29 09:56:54 -07:00
|
|
|
}
|
|
|
|
|
} // 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"
|