mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +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,10 +4,10 @@
|
||||
|
||||
#include "WebServerClientTest.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
#include <wpi/uv/util.h>
|
||||
|
||||
@@ -20,14 +20,12 @@ namespace wpilibws {
|
||||
// Create Web Socket and specify event callbacks
|
||||
void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
const std::string& uri) {
|
||||
std::stringstream ss;
|
||||
ss << host << ":" << port;
|
||||
wpi::outs() << "Will attempt to connect to: " << ss.str() << uri << "\n";
|
||||
m_websocket =
|
||||
wpi::WebSocket::CreateClient(*m_tcp_client.get(), uri, ss.str());
|
||||
fmt::print("Will attempt to connect to: {}:{}{}\n", host, port, uri);
|
||||
m_websocket = wpi::WebSocket::CreateClient(*m_tcp_client.get(), uri,
|
||||
fmt::format("{}:{}", host, port));
|
||||
|
||||
// Hook up events
|
||||
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
||||
m_websocket->open.connect_extended([this](auto conn, auto) {
|
||||
conn.disconnect();
|
||||
m_buffers = std::make_unique<BufferPool>();
|
||||
|
||||
@@ -38,17 +36,17 @@ void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
});
|
||||
|
||||
m_ws_connected = true;
|
||||
wpi::errs() << "WebServerClientTest: WebSocket Connected\n";
|
||||
std::fputs("WebServerClientTest: WebSocket Connected\n", stderr);
|
||||
});
|
||||
|
||||
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
|
||||
m_websocket->text.connect([this](auto msg, bool) {
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} 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;
|
||||
}
|
||||
@@ -56,9 +54,9 @@ void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
m_json = j;
|
||||
});
|
||||
|
||||
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
||||
m_websocket->closed.connect([this](uint16_t, auto) {
|
||||
if (m_ws_connected) {
|
||||
wpi::errs() << "WebServerClientTest: Websocket Disconnected\n";
|
||||
std::fputs("WebServerClientTest: Websocket Disconnected\n", stderr);
|
||||
m_ws_connected = false;
|
||||
}
|
||||
});
|
||||
@@ -67,11 +65,11 @@ void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
// Create tcp client, specify callbacks, and create timers for loop
|
||||
bool WebServerClientTest::Initialize() {
|
||||
m_loop.error.connect(
|
||||
[](uv::Error err) { wpi::errs() << "uv Error: " << err.str() << "\n"; });
|
||||
[](uv::Error err) { fmt::print(stderr, "uv Error: {}\n", err.str()); });
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
if (!m_tcp_client) {
|
||||
wpi::errs() << "ERROR: Could not create TCP Client\n";
|
||||
std::fputs("ERROR: Could not create TCP Client\n", stderr);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -90,7 +88,7 @@ bool WebServerClientTest::Initialize() {
|
||||
});
|
||||
|
||||
m_tcp_client->closed.connect(
|
||||
[]() { wpi::errs() << "TCP connection closed\n"; });
|
||||
[]() { std::fputs("TCP connection closed\n", stderr); });
|
||||
|
||||
// Set up the connection timer
|
||||
m_connect_timer = uv::Timer::Create(m_loop);
|
||||
@@ -101,18 +99,17 @@ bool WebServerClientTest::Initialize() {
|
||||
m_connect_timer->timeout.connect([this] { AttemptConnect(); });
|
||||
m_connect_timer->Start(uv::Timer::Time(0));
|
||||
|
||||
wpi::outs() << "WebServerClientTest Initialized\n";
|
||||
std::puts("WebServerClientTest Initialized");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebServerClientTest::AttemptConnect() {
|
||||
m_connect_attempts++;
|
||||
wpi::outs() << "Test Client Connection Attempt " << m_connect_attempts
|
||||
<< "\n";
|
||||
fmt::print("Test Client Connection Attempt {}\n", m_connect_attempts);
|
||||
|
||||
if (m_connect_attempts >= 5) {
|
||||
wpi::errs() << "Test Client Timeout. Unable to connect\n";
|
||||
std::fputs("Test Client Timeout. Unable to connect\n", stderr);
|
||||
m_loop.Stop();
|
||||
return;
|
||||
}
|
||||
@@ -127,7 +124,7 @@ void WebServerClientTest::AttemptConnect() {
|
||||
|
||||
void WebServerClientTest::SendMessage(const wpi::json& msg) {
|
||||
if (msg.empty()) {
|
||||
wpi::errs() << "Message to send is empty\n";
|
||||
std::fputs("Message to send is empty\n", stderr);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,8 +144,8 @@ void WebServerClientTest::SendMessage(const wpi::json& msg) {
|
||||
m_buffers->Release(bufs);
|
||||
}
|
||||
if (err) {
|
||||
wpi::errs() << err.str() << "\n";
|
||||
wpi::errs().flush();
|
||||
fmt::print(stderr, "{}\n", err.str());
|
||||
std::fflush(stderr);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user