2021-04-18 20:35:29 -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.
|
|
|
|
|
|
|
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
|
|
|
|
|
|
#include <hal/DriverStation.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
|
|
|
|
#include <wpi/StackTrace.h>
|
2021-06-01 21:50:35 -07:00
|
|
|
#include <wpi/fs.h>
|
2021-04-18 20:35:29 -07:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
RuntimeError::RuntimeError(int32_t code, std::string&& loc, std::string&& stack,
|
|
|
|
|
std::string&& message)
|
|
|
|
|
: runtime_error{std::move(message)}, m_data{std::make_shared<Data>()} {
|
2021-04-18 20:35:29 -07:00
|
|
|
m_data->code = code;
|
2021-05-23 19:33:33 -07:00
|
|
|
m_data->loc = std::move(loc);
|
2021-04-18 20:35:29 -07:00
|
|
|
m_data->stack = stack;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
RuntimeError::RuntimeError(int32_t code, const char* fileName, int lineNumber,
|
|
|
|
|
const char* funcName, std::string&& stack,
|
|
|
|
|
std::string&& message)
|
2021-06-01 21:50:35 -07:00
|
|
|
: RuntimeError{
|
|
|
|
|
code,
|
|
|
|
|
fmt::format("{} [{}:{}]", funcName,
|
|
|
|
|
fs::path{fileName}.filename().string(), lineNumber),
|
|
|
|
|
std::move(stack), std::move(message)} {}
|
2021-04-18 20:35:29 -07:00
|
|
|
|
|
|
|
|
void RuntimeError::Report() const {
|
|
|
|
|
HAL_SendError(m_data->code < 0, m_data->code, 0, what(), m_data->loc.c_str(),
|
|
|
|
|
m_data->stack.c_str(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 09:56:54 -07:00
|
|
|
const char* frc::GetErrorMessage(int32_t* code) {
|
|
|
|
|
switch (*code) {
|
2021-04-18 20:35:29 -07:00
|
|
|
#define S(label, offset, message) \
|
2021-05-24 23:36:26 -07:00
|
|
|
case err::label: \
|
2021-04-18 20:35:29 -07:00
|
|
|
return message;
|
|
|
|
|
#include "frc/WPIErrors.mac"
|
2021-05-24 23:36:26 -07:00
|
|
|
#undef S
|
|
|
|
|
#define S(label, offset, message) \
|
|
|
|
|
case warn::label: \
|
|
|
|
|
return message;
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/WPIWarnings.mac"
|
|
|
|
|
#undef S
|
|
|
|
|
default:
|
2021-04-29 09:56:54 -07:00
|
|
|
return HAL_GetLastError(code);
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
void frc::ReportErrorV(int32_t status, const char* fileName, int lineNumber,
|
|
|
|
|
const char* funcName, fmt::string_view format,
|
|
|
|
|
fmt::format_args args) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (status == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-23 19:33:33 -07:00
|
|
|
fmt::memory_buffer out;
|
|
|
|
|
fmt::format_to(out, "{}: ", GetErrorMessage(&status));
|
|
|
|
|
fmt::vformat_to(out, format, args);
|
|
|
|
|
out.push_back('\0');
|
|
|
|
|
HAL_SendError(status < 0, status, 0, out.data(), funcName,
|
|
|
|
|
wpi::GetStackTrace(2).c_str(), 1);
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
RuntimeError frc::MakeErrorV(int32_t status, const char* fileName,
|
|
|
|
|
int lineNumber, const char* funcName,
|
|
|
|
|
fmt::string_view format, fmt::format_args args) {
|
|
|
|
|
fmt::memory_buffer out;
|
|
|
|
|
fmt::format_to(out, "{}: ", GetErrorMessage(&status));
|
|
|
|
|
fmt::vformat_to(out, format, args);
|
|
|
|
|
return RuntimeError{status,
|
|
|
|
|
fileName,
|
|
|
|
|
lineNumber,
|
|
|
|
|
funcName,
|
|
|
|
|
wpi::GetStackTrace(2),
|
|
|
|
|
fmt::to_string(out)};
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|