mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Replace std::stringstream with llvm::raw_svector_ostream (#345)
A few locations were changed to use std::ostringstream.
This commit is contained in:
committed by
Peter Johnson
parent
7006672b06
commit
b433d98c02
@@ -7,13 +7,14 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <jni.h>
|
||||
#include "HAL/cpp/Log.h"
|
||||
|
||||
#include "edu_wpi_first_wpilibj_can_CANJNI.h"
|
||||
|
||||
#include "FRC_NetworkCommunication/CANSessionMux.h"
|
||||
#include "HAL/CAN.h"
|
||||
#include "HAL/cpp/Log.h"
|
||||
#include "HALUtil.h"
|
||||
#include "edu_wpi_first_wpilibj_can_CANJNI.h"
|
||||
#include "llvm/SmallString.h"
|
||||
#include "llvm/raw_ostream.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
@@ -44,14 +45,15 @@ Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetCommCANSessionMuxSendMessage(
|
||||
(uint8_t *)(data ? env->GetDirectBufferAddress(data) : 0);
|
||||
uint8_t dataSize = (uint8_t)(data ? env->GetDirectBufferCapacity(data) : 0);
|
||||
|
||||
CANJNI_LOG(logDEBUG) << "Message ID " << std::hex << messageID;
|
||||
CANJNI_LOG(logDEBUG) << "Message ID ";
|
||||
CANJNI_LOG(logDEBUG).write_hex(messageID);
|
||||
|
||||
if (logDEBUG <= canJNILogLevel) {
|
||||
if (dataBuffer) {
|
||||
std::ostringstream str;
|
||||
str << std::setfill('0') << std::hex;
|
||||
llvm::SmallString<128> buf;
|
||||
llvm::raw_svector_ostream str(buf);
|
||||
for (int32_t i = 0; i < dataSize; i++) {
|
||||
str << std::setw(2) << (int)dataBuffer[i] << ' ';
|
||||
str.write_hex(dataBuffer[i]) << ' ';
|
||||
}
|
||||
|
||||
Log().Get(logDEBUG) << "Data: " << str.str();
|
||||
@@ -93,13 +95,20 @@ Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetCommCANSessionMuxReceiveMessage(
|
||||
FRC_NetworkCommunication_CANSessionMux_receiveMessage(
|
||||
messageIDPtr, messageIDMask, buffer, &dataSize, timeStampPtr, &status);
|
||||
|
||||
CANJNI_LOG(logDEBUG) << "Message ID " << std::hex << *messageIDPtr;
|
||||
CANJNI_LOG(logDEBUG) << "Message ID ";
|
||||
CANJNI_LOG(logDEBUG).write_hex(*messageIDPtr);
|
||||
|
||||
if (logDEBUG <= canJNILogLevel) {
|
||||
std::ostringstream str;
|
||||
str << std::setfill('0') << std::hex;
|
||||
llvm::SmallString<128> buf;
|
||||
llvm::raw_svector_ostream str(buf);
|
||||
|
||||
for (int32_t i = 0; i < dataSize; i++) {
|
||||
str << std::setw(2) << (int)buffer[i] << ' ';
|
||||
// Pad one-digit data with a zero
|
||||
if (buffer[i] <= 16) {
|
||||
str << '0';
|
||||
}
|
||||
|
||||
str.write_hex(buffer[i]) << ' ';
|
||||
}
|
||||
|
||||
Log().Get(logDEBUG) << "Data: " << str.str();
|
||||
|
||||
@@ -59,20 +59,21 @@ namespace frc {
|
||||
void ThrowAllocationException(JNIEnv *env, int32_t minRange, int32_t maxRange,
|
||||
int32_t requestedValue, int32_t status) {
|
||||
const char *message = HAL_GetErrorMessage(status);
|
||||
char *buf = new char[strlen(message) + 100];
|
||||
sprintf(buf,
|
||||
" Code: $%d. %s, Minimum Value: %d, Maximum Value: %d, Requested Value: %d",
|
||||
status, message, minRange, maxRange, requestedValue);
|
||||
allocationExCls.Throw(env, buf);
|
||||
delete[] buf;
|
||||
llvm::SmallString<1024> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message << ", Minimum Value: "
|
||||
<< minRange << ", Maximum Value: " << maxRange << ", Requested Value: "
|
||||
<< requestedValue;
|
||||
env->ThrowNew(allocationExCls, buf.c_str());
|
||||
allocationExCls.Throw(env, buf.c_str());
|
||||
}
|
||||
|
||||
void ThrowHalHandleException(JNIEnv *env, int32_t status) {
|
||||
const char *message = HAL_GetErrorMessage(status);
|
||||
char *buf = new char[strlen(message) + 30];
|
||||
sprintf(buf, " Code: $%d. %s", status, message);
|
||||
halHandleExCls.Throw(env, buf);
|
||||
delete[] buf;
|
||||
llvm::SmallString<1024> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message;
|
||||
halHandleExCls.Throw(env, buf.c_str());
|
||||
}
|
||||
|
||||
constexpr const char wpilibjPrefix[] = "edu.wpi.first.wpilibj";
|
||||
@@ -84,10 +85,10 @@ void ReportError(JNIEnv *env, int32_t status, bool do_throw) {
|
||||
}
|
||||
const char *message = HAL_GetErrorMessage(status);
|
||||
if (do_throw && status < 0) {
|
||||
char *buf = new char[strlen(message) + 30];
|
||||
sprintf(buf, " Code: %d. %s", status, message);
|
||||
runtimeExCls.Throw(env, buf);
|
||||
delete[] buf;
|
||||
llvm::SmallString<1024> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message;
|
||||
runtimeExCls.Throw(env, buf.c_str());
|
||||
} else {
|
||||
std::string func;
|
||||
auto stack = GetJavaStackTrace<wpilibjPrefix>(env, &func);
|
||||
@@ -107,10 +108,10 @@ void ThrowError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange,
|
||||
ThrowHalHandleException(env, status);
|
||||
}
|
||||
const char *message = HAL_GetErrorMessage(status);
|
||||
char *buf = new char[strlen(message) + 30];
|
||||
sprintf(buf, " Code: %d. %s", status, message);
|
||||
runtimeExCls.Throw(env, buf);
|
||||
delete[] buf;
|
||||
llvm::SmallString<1024> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << " Code: " << status << ". " << message;
|
||||
runtimeExCls.Throw(env, buf.c_str());
|
||||
}
|
||||
|
||||
void ReportCANError(JNIEnv *env, int32_t status, int message_id) {
|
||||
@@ -143,9 +144,10 @@ void ReportCANError(JNIEnv *env, int32_t status, int message_id) {
|
||||
}
|
||||
case ERR_CANSessionMux_NotAllowed:
|
||||
case kRIOStatusFeatureNotSupported: {
|
||||
char buf[100];
|
||||
sprintf(buf, "MessageID = %d", message_id);
|
||||
canMessageNotAllowedExCls.Throw(env, buf);
|
||||
llvm::SmallString<100> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << "MessageID = " << message_id;
|
||||
canMessageNotAllowedExCls.Throw(env, buf.c_str());
|
||||
break;
|
||||
}
|
||||
case ERR_CANSessionMux_NotInitialized:
|
||||
@@ -160,9 +162,10 @@ void ReportCANError(JNIEnv *env, int32_t status, int message_id) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
char buf[100];
|
||||
sprintf(buf, "Fatal status code detected: %d", status);
|
||||
uncleanStatusExCls.Throw(env, buf);
|
||||
llvm::SmallString<100> buf;
|
||||
llvm::raw_svector_ostream oss(buf);
|
||||
oss << "Fatal status code detected: " << status;
|
||||
uncleanStatusExCls.Throw(env, buf.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user