mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Use std::string_view and fmtlib across all libraries (#3402)
- Twine, StringRef, Format, and NativeFormatting have been removed - Logging now uses fmtlib style formatting - Nearly all uses of wpi::outs/errs have been replaced with fmt::print() or std::puts()/std::fputs() (for unformatted strings). - A wpi/fmt/raw_ostream.h header has been added to enable fmt::print() with wpi::raw_ostream
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// 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 <fmt/format.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "hal/Errors.h"
|
||||
#include "hal/HALBase.h"
|
||||
@@ -21,31 +21,30 @@ static LastErrorStorage& GetThreadLastError() {
|
||||
}
|
||||
|
||||
namespace hal {
|
||||
void SetLastError(int32_t* status, const wpi::Twine& value) {
|
||||
void SetLastError(int32_t* status, std::string_view value) {
|
||||
LastErrorStorage& lastError = GetThreadLastError();
|
||||
lastError.message.clear();
|
||||
value.toVector(lastError.message);
|
||||
lastError.message = value;
|
||||
lastError.status = *status;
|
||||
*status = HAL_USE_LAST_ERROR;
|
||||
}
|
||||
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastErrorIndexOutOfRange(int32_t* status, std::string_view message,
|
||||
int32_t minimum, int32_t maximum,
|
||||
int32_t requested) {
|
||||
SetLastError(status, message + "\n Status: " + wpi::Twine(*status) +
|
||||
"\n Minimum: " + wpi::Twine(minimum) +
|
||||
" Maximum: " + wpi::Twine(maximum) +
|
||||
" Reequested: " + wpi::Twine(requested));
|
||||
SetLastError(
|
||||
status,
|
||||
fmt::format("{}\n Status: {}\n Minimum: {} Maximum: {} Requested: {}",
|
||||
message, *status, minimum, maximum, requested));
|
||||
}
|
||||
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
|
||||
void SetLastErrorPreviouslyAllocated(int32_t* status, std::string_view message,
|
||||
int32_t channel,
|
||||
const wpi::Twine& previousAllocation) {
|
||||
hal::SetLastError(
|
||||
status,
|
||||
message + " " + wpi::Twine(channel) +
|
||||
" previously allocated.\nLocation of the previous allocation:\n" +
|
||||
previousAllocation + "\nLocation of the current allocation:");
|
||||
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));
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
|
||||
#include "hal/cpp/fpga_clock.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "hal/HALBase.h"
|
||||
|
||||
@@ -19,11 +20,12 @@ fpga_clock::time_point fpga_clock::now() noexcept {
|
||||
int32_t status = 0;
|
||||
uint64_t currentTime = HAL_GetFPGATime(&status);
|
||||
if (status != 0) {
|
||||
wpi::errs()
|
||||
<< "Call to HAL_GetFPGATime failed in fpga_clock::now() with status "
|
||||
<< status
|
||||
<< ". Initialization might have failed. Time will not be correct\n";
|
||||
wpi::errs().flush();
|
||||
fmt::print(
|
||||
stderr,
|
||||
"Call to HAL_GetFPGATime failed in fpga_clock::now() with status {}. "
|
||||
"Initialization might have failed. Time will not be correct\n",
|
||||
status);
|
||||
std::fflush(stderr);
|
||||
return epoch();
|
||||
}
|
||||
return time_point(std::chrono::microseconds(currentTime));
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/jni_util.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "HALUtil.h"
|
||||
#include "edu_wpi_first_hal_CANAPIJNI.h"
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/jni_util.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "HALUtil.h"
|
||||
#include "edu_wpi_first_hal_can_CANJNI.h"
|
||||
@@ -65,8 +63,8 @@ Java_edu_wpi_first_hal_can_CANJNI_FRCNetCommCANSessionMuxReceiveMessage
|
||||
return nullptr;
|
||||
}
|
||||
return MakeJByteArray(env,
|
||||
wpi::StringRef{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(dataSize)});
|
||||
std::string_view{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(dataSize)});
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "HALUtil.h"
|
||||
@@ -227,13 +228,11 @@ Java_edu_wpi_first_hal_HAL_getJoystickAxes
|
||||
|
||||
jsize javaSize = env->GetArrayLength(axesArray);
|
||||
if (axes.count > javaSize) {
|
||||
wpi::SmallString<128> errStr;
|
||||
wpi::raw_svector_ostream oss{errStr};
|
||||
oss << "Native array size larger then passed in java array size "
|
||||
<< "Native Size: " << static_cast<int>(axes.count)
|
||||
<< " Java Size: " << static_cast<int>(javaSize);
|
||||
|
||||
ThrowIllegalArgumentException(env, errStr.str());
|
||||
ThrowIllegalArgumentException(
|
||||
env,
|
||||
fmt::format("Native array size larger then passed in java array "
|
||||
"size\nNative Size: {} Java Size: {}",
|
||||
static_cast<int>(axes.count), static_cast<int>(javaSize)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -256,13 +255,11 @@ Java_edu_wpi_first_hal_HAL_getJoystickPOVs
|
||||
|
||||
jsize javaSize = env->GetArrayLength(povsArray);
|
||||
if (povs.count > javaSize) {
|
||||
wpi::SmallString<128> errStr;
|
||||
wpi::raw_svector_ostream oss{errStr};
|
||||
oss << "Native array size larger then passed in java array size "
|
||||
<< "Native Size: " << static_cast<int>(povs.count)
|
||||
<< " Java Size: " << static_cast<int>(javaSize);
|
||||
|
||||
ThrowIllegalArgumentException(env, errStr.str());
|
||||
ThrowIllegalArgumentException(
|
||||
env,
|
||||
fmt::format("Native array size larger then passed in java array "
|
||||
"size\nNative Size: {} Java Size: {}",
|
||||
static_cast<int>(povs.count), static_cast<int>(javaSize)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/jni_util.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "edu_wpi_first_hal_HALUtil.h"
|
||||
#include "hal/CAN.h"
|
||||
@@ -72,7 +71,7 @@ static const JExceptionInit exceptions[] = {
|
||||
|
||||
namespace hal {
|
||||
|
||||
void ThrowUncleanStatusException(JNIEnv* env, wpi::StringRef msg,
|
||||
void ThrowUncleanStatusException(JNIEnv* env, std::string_view msg,
|
||||
int32_t status) {
|
||||
static jmethodID func =
|
||||
env->GetMethodID(uncleanStatusExCls, "<init>", "(ILjava/lang/String;)V");
|
||||
@@ -85,20 +84,14 @@ void ThrowUncleanStatusException(JNIEnv* env, wpi::StringRef msg,
|
||||
|
||||
void ThrowAllocationException(JNIEnv* env, const char* lastError,
|
||||
int32_t status) {
|
||||
wpi::SmallString<1024> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
|
||||
oss << "Code: " << status << '\n' << lastError;
|
||||
|
||||
allocationExCls.Throw(env, buf.c_str());
|
||||
allocationExCls.Throw(env,
|
||||
fmt::format("Code: {}\n{}", status, lastError).c_str());
|
||||
}
|
||||
|
||||
void ThrowHalHandleException(JNIEnv* env, int32_t status) {
|
||||
const char* message = HAL_GetLastError(&status);
|
||||
wpi::SmallString<1024> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message;
|
||||
halHandleExCls.Throw(env, buf.c_str());
|
||||
halHandleExCls.Throw(env,
|
||||
fmt::format(" Code: {}. {}", status, message).c_str());
|
||||
}
|
||||
|
||||
void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
|
||||
@@ -111,16 +104,14 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
|
||||
return;
|
||||
}
|
||||
if (doThrow && status < 0) {
|
||||
wpi::SmallString<1024> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message;
|
||||
ThrowUncleanStatusException(env, buf.c_str(), status);
|
||||
ThrowUncleanStatusException(
|
||||
env, fmt::format(" Code: {}. {}", status, message).c_str(), status);
|
||||
} else {
|
||||
std::string func;
|
||||
auto stack = GetJavaStackTrace(env, &func, "edu.wpi.first");
|
||||
// Make a copy of message for safety, calling back into the HAL might
|
||||
// invalidate the string.
|
||||
wpi::SmallString<256> lastMessage{wpi::StringRef{message}};
|
||||
std::string lastMessage{message};
|
||||
HAL_SendError(1, status, 0, lastMessage.c_str(), func.c_str(),
|
||||
stack.c_str(), 1);
|
||||
}
|
||||
@@ -141,10 +132,8 @@ void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
|
||||
ThrowHalHandleException(env, status);
|
||||
return;
|
||||
}
|
||||
wpi::SmallString<1024> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << lastError;
|
||||
ThrowUncleanStatusException(env, buf.c_str(), status);
|
||||
ThrowUncleanStatusException(
|
||||
env, fmt::format(" Code: {}. {}", status, lastError).c_str(), status);
|
||||
}
|
||||
|
||||
void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
@@ -181,10 +170,8 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
}
|
||||
case HAL_ERR_CANSessionMux_NotAllowed:
|
||||
case kRIOStatusFeatureNotSupported: {
|
||||
wpi::SmallString<100> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
oss << "MessageID = " << message_id;
|
||||
canMessageNotAllowedExCls.Throw(env, buf.c_str());
|
||||
canMessageNotAllowedExCls.Throw(
|
||||
env, fmt::format("MessageID = {}", message_id).c_str());
|
||||
break;
|
||||
}
|
||||
case HAL_ERR_CANSessionMux_NotInitialized:
|
||||
@@ -200,16 +187,14 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
wpi::SmallString<100> buf;
|
||||
wpi::raw_svector_ostream oss(buf);
|
||||
oss << "Fatal status code detected: " << status;
|
||||
uncleanStatusExCls.Throw(env, buf.c_str());
|
||||
uncleanStatusExCls.Throw(
|
||||
env, fmt::format("Fatal status code detected: {}", status).c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ThrowIllegalArgumentException(JNIEnv* env, wpi::StringRef msg) {
|
||||
void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg) {
|
||||
illegalArgExCls.Throw(env, msg);
|
||||
}
|
||||
|
||||
@@ -266,9 +251,9 @@ void SetMatchInfoObject(JNIEnv* env, jobject matchStatus,
|
||||
|
||||
env->CallVoidMethod(
|
||||
matchStatus, func, MakeJString(env, matchInfo.eventName),
|
||||
MakeJString(env, wpi::StringRef{reinterpret_cast<const char*>(
|
||||
matchInfo.gameSpecificMessage),
|
||||
matchInfo.gameSpecificMessageSize}),
|
||||
MakeJString(env,
|
||||
{reinterpret_cast<const char*>(matchInfo.gameSpecificMessage),
|
||||
matchInfo.gameSpecificMessageSize}),
|
||||
static_cast<jint>(matchInfo.matchNumber),
|
||||
static_cast<jint>(matchInfo.replayNumber),
|
||||
static_cast<jint>(matchInfo.matchType));
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <jni.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wpi/StringRef.h>
|
||||
#include <string_view>
|
||||
|
||||
struct HAL_MatchInfo;
|
||||
struct HAL_Value;
|
||||
@@ -51,7 +51,7 @@ inline bool CheckCANStatus(JNIEnv* env, int32_t status, int32_t message_id) {
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
void ThrowIllegalArgumentException(JNIEnv* env, wpi::StringRef msg);
|
||||
void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg);
|
||||
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
|
||||
double upper);
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ Java_edu_wpi_first_hal_SimDeviceJNI_createSimValueEnum
|
||||
if (!elem) {
|
||||
return 0;
|
||||
}
|
||||
arr.push_back(JStringRef{env, elem}.str());
|
||||
arr.emplace_back(JStringRef{env, elem}.str());
|
||||
}
|
||||
wpi::SmallVector<const char*, 8> carr;
|
||||
for (auto&& val : arr) {
|
||||
@@ -129,7 +129,7 @@ Java_edu_wpi_first_hal_SimDeviceJNI_createSimValueEnumDouble
|
||||
if (!elem) {
|
||||
return 0;
|
||||
}
|
||||
arr.push_back(JStringRef{env, elem}.str());
|
||||
arr.emplace_back(JStringRef{env, elem}.str());
|
||||
}
|
||||
|
||||
wpi::SmallVector<const char*, 8> carr;
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "SimulatorJNI.h"
|
||||
@@ -46,18 +48,18 @@ void BufferCallbackStore::performCallback(const char* name, uint8_t* buffer,
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
auto toCallbackArr =
|
||||
MakeJByteArray(env, wpi::StringRef{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(length)});
|
||||
auto toCallbackArr = MakeJByteArray(
|
||||
env, std::string_view{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(length)});
|
||||
|
||||
env->CallVoidMethod(m_call, sim::GetBufferCallback(), MakeJString(env, name),
|
||||
toCallbackArr, static_cast<jint>(length));
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "SimulatorJNI.h"
|
||||
@@ -45,13 +47,13 @@ void CallbackStore::performCallback(const char* name, const HAL_Value* value) {
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
env->CallVoidMethod(m_call, sim::GetNotifyCallback(), MakeJString(env, name),
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "SimulatorJNI.h"
|
||||
@@ -47,18 +49,18 @@ void ConstBufferCallbackStore::performCallback(const char* name,
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
auto toCallbackArr =
|
||||
MakeJByteArray(env, wpi::StringRef{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(length)});
|
||||
auto toCallbackArr = MakeJByteArray(
|
||||
env, std::string_view{reinterpret_cast<const char*>(buffer),
|
||||
static_cast<size_t>(length)});
|
||||
|
||||
env->CallVoidMethod(m_call, sim::GetConstBufferCallback(),
|
||||
MakeJString(env, name), toCallbackArr,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <utility>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
@@ -134,13 +135,13 @@ void DeviceCallbackStore::performCallback(const char* name,
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
env->CallVoidMethod(m_call, simDeviceCallbackCallback, MakeJString(env, name),
|
||||
@@ -168,13 +169,13 @@ void ValueCallbackStore::performCallback(const char* name,
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
auto [value1, value2] = ToValue12(value);
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <wpi/jni_util.h>
|
||||
|
||||
#include "SimulatorJNI.h"
|
||||
@@ -47,13 +49,13 @@ int32_t SpiReadAutoReceiveBufferCallbackStore::performCallback(
|
||||
didAttachThread = true;
|
||||
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
||||
// Failed to attach, log and return
|
||||
wpi::outs() << "Failed to attach\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Failed to attach");
|
||||
std::fflush(stdout);
|
||||
return -1;
|
||||
}
|
||||
} else if (tryGetEnv == JNI_EVERSION) {
|
||||
wpi::outs() << "Invalid JVM Version requested\n";
|
||||
wpi::outs().flush();
|
||||
std::puts("Invalid JVM Version requested");
|
||||
std::fflush(stdout);
|
||||
}
|
||||
|
||||
auto toCallbackArr = MakeJIntArray(
|
||||
|
||||
Reference in New Issue
Block a user