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:
Peter Johnson
2021-06-06 16:13:58 -07:00
committed by GitHub
parent 4f1cecb8e7
commit b2c3b2dd8e
441 changed files with 5061 additions and 9749 deletions

View File

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