mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11: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:
@@ -4,8 +4,10 @@
|
||||
|
||||
#include "HALSimWS.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/uv/util.h>
|
||||
|
||||
#include "HALSimWSClientConnection.h"
|
||||
@@ -22,7 +24,7 @@ HALSimWS::HALSimWS(wpi::uv::Loop& loop, ProviderContainer& providers,
|
||||
m_providers(providers),
|
||||
m_simDevicesProvider(simDevicesProvider) {
|
||||
m_loop.error.connect([](uv::Error err) {
|
||||
wpi::errs() << "HALSim WS Client libuv Error: " << err.str() << "\n";
|
||||
fmt::print(stderr, "HALSim WS Client libuv Error: {}\n", err.str());
|
||||
});
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
@@ -50,7 +52,7 @@ bool HALSimWS::Initialize() {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
|
||||
fmt::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -83,13 +85,12 @@ void HALSimWS::Start() {
|
||||
m_connect_timer->Start(uv::Timer::Time(kTcpConnectAttemptTimeout));
|
||||
});
|
||||
|
||||
m_tcp_client->closed.connect(
|
||||
[]() { wpi::outs() << "TCP connection closed\n"; });
|
||||
m_tcp_client->closed.connect([]() { std::puts("TCP connection closed"); });
|
||||
|
||||
// Set up the connection timer
|
||||
wpi::outs() << "HALSimWS Initialized\n";
|
||||
wpi::outs() << "Will attempt to connect to ws://" << m_host << ":" << m_port
|
||||
<< m_uri << "\n";
|
||||
std::puts("HALSimWS Initialized");
|
||||
fmt::print("Will attempt to connect to ws://{}:{}{}\n", m_host, m_port,
|
||||
m_uri);
|
||||
|
||||
// Set up the timer to attempt connection
|
||||
m_connect_timer->timeout.connect([this] { AttemptConnect(); });
|
||||
@@ -102,7 +103,7 @@ void HALSimWS::Start() {
|
||||
void HALSimWS::AttemptConnect() {
|
||||
m_connect_attempts++;
|
||||
|
||||
wpi::outs() << "Connection Attempt " << m_connect_attempts << "\n";
|
||||
fmt::print("Connection Attempt {}\n", m_connect_attempts);
|
||||
|
||||
struct sockaddr_in dest;
|
||||
uv::NameToAddr(m_host, m_port, &dest);
|
||||
@@ -167,6 +168,6 @@ void HALSimWS::OnNetValueChanged(const wpi::json& msg) {
|
||||
provider->OnNetValueChanged(msg.at("data"));
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
|
||||
fmt::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
#include "HALSimWSClientConnection.h"
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <cstdio>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
|
||||
#include "HALSimWS.h"
|
||||
@@ -17,29 +19,29 @@ void HALSimWSClientConnection::Initialize() {
|
||||
// Get a shared pointer to ourselves
|
||||
auto self = this->shared_from_this();
|
||||
|
||||
auto ws =
|
||||
wpi::WebSocket::CreateClient(*m_stream, m_client->GetTargetUri(),
|
||||
wpi::Twine{m_client->GetTargetHost()} + ":" +
|
||||
wpi::Twine{m_client->GetTargetPort()});
|
||||
auto ws = wpi::WebSocket::CreateClient(
|
||||
*m_stream, m_client->GetTargetUri(),
|
||||
fmt::format("{}:{}", m_client->GetTargetHost(),
|
||||
m_client->GetTargetPort()));
|
||||
|
||||
ws->SetData(self);
|
||||
|
||||
m_websocket = ws.get();
|
||||
|
||||
// Hook up events
|
||||
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
||||
m_websocket->open.connect_extended([this](auto conn, auto) {
|
||||
conn.disconnect();
|
||||
|
||||
if (!m_client->RegisterWebsocket(shared_from_this())) {
|
||||
wpi::errs() << "Unable to register websocket\n";
|
||||
std::fputs("Unable to register websocket\n", stderr);
|
||||
return;
|
||||
}
|
||||
|
||||
m_ws_connected = true;
|
||||
wpi::outs() << "HALSimWS: WebSocket Connected\n";
|
||||
std::puts("HALSimWS: WebSocket Connected");
|
||||
});
|
||||
|
||||
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
|
||||
m_websocket->text.connect([this](auto msg, bool) {
|
||||
if (!m_ws_connected) {
|
||||
return;
|
||||
}
|
||||
@@ -50,7 +52,7 @@ void HALSimWSClientConnection::Initialize() {
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
wpi::errs() << err << "\n";
|
||||
fmt::print(stderr, "{}\n", err);
|
||||
m_websocket->Fail(1003, err);
|
||||
return;
|
||||
}
|
||||
@@ -58,9 +60,9 @@ void HALSimWSClientConnection::Initialize() {
|
||||
m_client->OnNetValueChanged(j);
|
||||
});
|
||||
|
||||
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
||||
m_websocket->closed.connect([this](uint16_t, auto) {
|
||||
if (m_ws_connected) {
|
||||
wpi::outs() << "HALSimWS: Websocket Disconnected\n";
|
||||
std::puts("HALSimWS: Websocket Disconnected");
|
||||
m_ws_connected = false;
|
||||
|
||||
m_client->CloseWebsocket(shared_from_this());
|
||||
@@ -90,8 +92,8 @@ void HALSimWSClientConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::errs() << err.str() << "\n";
|
||||
wpi::errs().flush();
|
||||
fmt::print(stderr, "{}\n", err.str());
|
||||
std::fflush(stderr);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// 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 <cstdio>
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Extensions.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "HALSimWSClient.h"
|
||||
|
||||
@@ -19,7 +19,7 @@ __declspec(dllexport)
|
||||
#endif
|
||||
|
||||
int HALSIM_InitExtension(void) {
|
||||
wpi::outs() << "HALSim WS Client Extension Initializing\n";
|
||||
std::puts("HALSim WS Client Extension Initializing");
|
||||
|
||||
HAL_OnShutdown(nullptr, [](void*) { gClient.reset(); });
|
||||
|
||||
@@ -28,7 +28,7 @@ __declspec(dllexport)
|
||||
return -1;
|
||||
}
|
||||
|
||||
wpi::outs() << "HALSim WS Client Extension Initialized\n";
|
||||
std::puts("HALSim WS Client Extension Initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user