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

@@ -2,14 +2,14 @@
// 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 <iostream>
#include <thread>
#include <fmt/format.h>
#include <hal/DriverStation.h>
#include <hal/HALBase.h>
#include <hal/Main.h>
#include <wpi/Format.h>
#include <wpi/raw_ostream.h>
extern "C" int HALSIM_InitExtension(void);
@@ -25,9 +25,9 @@ int main() {
while (cycleCount < 1000) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cycleCount++;
std::cout << "Count: " << cycleCount << std::endl;
fmt::print("Count: {}\n", cycleCount);
}
std::cout << "DONE" << std::endl;
std::puts("DONE");
HAL_ExitMain();
}

View File

@@ -6,12 +6,15 @@
#include <uv.h>
#include <string_view>
#include <fmt/format.h>
#include <wpi/MimeTypes.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/UrlParser.h>
#include <wpi/fs.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Request.h>
@@ -19,7 +22,7 @@ namespace uv = wpi::uv;
using namespace wpilibws;
bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef protocol) {
bool HALSimHttpConnection::IsValidWsUpgrade(std::string_view protocol) {
if (m_request.GetUrl() != m_server->GetServerUri()) {
MySendError(404, "invalid websocket address");
return false;
@@ -29,7 +32,7 @@ bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef protocol) {
}
void HALSimHttpConnection::ProcessWsUpgrade() {
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
m_websocket->open.connect_extended([this](auto conn, auto) {
conn.disconnect(); // one-shot
if (!m_server->RegisterWebsocket(shared_from_this())) {
@@ -40,11 +43,11 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
Log(200);
m_isWsConnected = true;
wpi::errs() << "HALWebSim: websocket connected\n";
std::fputs("HALWebSim: websocket connected\n", stderr);
});
// parse incoming JSON, dispatch to parent
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
m_websocket->text.connect([this](auto msg, bool) {
if (!m_isWsConnected) {
return;
}
@@ -61,10 +64,10 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
m_server->OnNetValueChanged(j);
});
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
m_websocket->closed.connect([this](uint16_t, auto) {
// unset the global, allow another websocket to connect
if (m_isWsConnected) {
wpi::errs() << "HALWebSim: websocket disconnected\n";
std::fputs("HALWebSim: websocket disconnected\n", stderr);
m_isWsConnected = false;
m_server->CloseWebsocket(shared_from_this());
@@ -91,30 +94,28 @@ void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
}
if (err) {
wpi::errs() << err.str() << "\n";
wpi::errs().flush();
fmt::print(stderr, "{}\n", err.str());
std::fflush(stderr);
}
});
});
}
void HALSimHttpConnection::SendFileResponse(int code,
const wpi::Twine& codeText,
const wpi::Twine& contentType,
const wpi::Twine& filename,
const wpi::Twine& extraHeader) {
std::string fn = filename.str();
void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
std::string_view contentType,
std::string_view filename,
std::string_view extraHeader) {
std::error_code ec;
// get file size
auto size = fs::file_size(fn, ec);
auto size = fs::file_size(filename, ec);
if (ec) {
MySendError(404, "error getting file size");
return;
}
// open file
wpi::raw_fd_istream is{fn, ec, true};
wpi::raw_fd_istream is{filename, ec, true};
if (ec) {
MySendError(404, "error opening file");
return;
@@ -157,23 +158,23 @@ void HALSimHttpConnection::ProcessRequest() {
return;
}
wpi::StringRef path;
std::string_view path;
if (url.HasPath()) {
path = url.GetPath();
}
if (m_request.GetMethod() == wpi::HTTP_GET && path.startswith("/") &&
!path.contains("..") && !path.contains("//")) {
if (m_request.GetMethod() == wpi::HTTP_GET && wpi::starts_with(path, '/') &&
!wpi::contains(path, "..") && !wpi::contains(path, "//")) {
// convert to fs native representation
fs::path nativePath;
if (path.startswith("/user/")) {
nativePath = fs::path{std::string{m_server->GetWebrootSys()}} /
fs::path{std::string{path.drop_front(6)},
fs::path::format::generic_format};
if (wpi::starts_with(path, "/user/")) {
nativePath =
fs::path{m_server->GetWebrootSys()} /
fs::path{wpi::drop_front(path, 6), fs::path::format::generic_format};
} else {
nativePath = fs::path{std::string{m_server->GetWebrootSys()}} /
fs::path{std::string{path.drop_front(1)},
fs::path::format::generic_format};
nativePath =
fs::path{m_server->GetWebrootSys()} /
fs::path{wpi::drop_front(path, 1), fs::path::format::generic_format};
}
if (fs::is_directory(nativePath)) {
@@ -181,7 +182,7 @@ void HALSimHttpConnection::ProcessRequest() {
}
if (!fs::exists(nativePath) || fs::is_directory(nativePath)) {
MySendError(404, "Resource '" + path + "' not found");
MySendError(404, fmt::format("Resource '{}' not found", path));
} else {
auto contentType = wpi::MimeTypeFromPath(nativePath.string());
SendFileResponse(200, "OK", contentType, nativePath.string());
@@ -191,14 +192,13 @@ void HALSimHttpConnection::ProcessRequest() {
}
}
void HALSimHttpConnection::MySendError(int code, const wpi::Twine& message) {
void HALSimHttpConnection::MySendError(int code, std::string_view message) {
Log(code);
SendError(code, message);
}
void HALSimHttpConnection::Log(int code) {
auto method = wpi::http_method_str(m_request.GetMethod());
wpi::errs() << method << " " << m_request.GetUrl() << " HTTP/"
<< m_request.GetMajor() << "." << m_request.GetMinor() << " "
<< code << "\n";
fmt::print(stderr, "{} {} HTTP/{}.{} {}\n", method, m_request.GetUrl(),
m_request.GetMajor(), m_request.GetMinor(), code);
}

View File

@@ -4,8 +4,8 @@
#include "HALSimWeb.h"
#include <fmt/format.h>
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include <wpi/UrlParser.h>
#include <wpi/WebSocketServer.h>
#include <wpi/fs.h>
@@ -25,7 +25,7 @@ HALSimWeb::HALSimWeb(wpi::uv::Loop& loop, ProviderContainer& providers,
m_providers(providers),
m_simDevicesProvider(simDevicesProvider) {
m_loop.error.connect([](uv::Error err) {
wpi::errs() << "HALSim WS Server libuv ERROR: " << err.str() << '\n';
fmt::print(stderr, "HALSim WS Server libuv ERROR: {}\n", err.str());
});
m_server = uv::Tcp::Create(m_loop);
@@ -70,7 +70,7 @@ bool HALSimWeb::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 {
@@ -98,8 +98,8 @@ void HALSimWeb::Start() {
// start listening for incoming connections
m_server->Listen();
wpi::outs() << "Listening at http://localhost:" << m_port << "\n";
wpi::outs() << "WebSocket URI: " << m_uri << "\n";
fmt::print("Listening at http://localhost:{}\n", m_port);
fmt::print("WebSocket URI: {}\n", m_uri);
}
bool HALSimWeb::RegisterWebsocket(
@@ -154,6 +154,6 @@ void HALSimWeb::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());
}
}

View File

@@ -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 "HALSimWSServer.h"
@@ -19,7 +19,7 @@ extern "C" {
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
wpi::outs() << "Websocket WS Server Initializing.\n";
std::puts("Websocket WS Server Initializing.");
HAL_OnShutdown(nullptr, [](void*) { gServer.reset(); });
@@ -28,7 +28,7 @@ __declspec(dllexport)
return -1;
}
wpi::outs() << "Websocket WS Server Initialized!\n";
std::puts("Websocket WS Server Initialized!");
return 0;
}
} // extern "C"

View File

@@ -6,6 +6,7 @@
#include <cinttypes>
#include <memory>
#include <string_view>
#include <utility>
#include <HALSimBaseWebSocketConnection.h>
@@ -38,14 +39,13 @@ class HALSimHttpConnection
protected:
void ProcessRequest() override;
bool IsValidWsUpgrade(wpi::StringRef protocol) override;
bool IsValidWsUpgrade(std::string_view protocol) override;
void ProcessWsUpgrade() override;
void SendFileResponse(int code, const wpi::Twine& codeText,
const wpi::Twine& contentType,
const wpi::Twine& filename,
const wpi::Twine& extraHeader = wpi::Twine{});
void SendFileResponse(int code, std::string_view codeText,
std::string_view contentType, std::string_view filename,
std::string_view extraHeader = {});
void MySendError(int code, const wpi::Twine& message);
void MySendError(int code, std::string_view message);
void Log(int code);
private:

View File

@@ -11,7 +11,6 @@
#include <WSBaseProvider.h>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/StringRef.h>
#include <wpi/uv/Async.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
@@ -42,9 +41,9 @@ class HALSimWeb : public std::enable_shared_from_this<HALSimWeb> {
// network -> sim
void OnNetValueChanged(const wpi::json& msg);
wpi::StringRef GetWebrootSys() const { return m_webroot_sys; }
wpi::StringRef GetWebrootUser() const { return m_webroot_user; }
wpi::StringRef GetServerUri() const { return m_uri; }
const std::string& GetWebrootSys() const { return m_webroot_sys; }
const std::string& GetWebrootUser() const { return m_webroot_user; }
const std::string& GetServerUri() const { return m_uri; }
int GetServerPort() const { return m_port; }
wpi::uv::Loop& GetLoop() { return m_loop; }

View File

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

View File

@@ -4,11 +4,11 @@
#include <thread>
#include <fmt/format.h>
#include <hal/DriverStation.h>
#include <hal/HALBase.h>
#include <hal/Main.h>
#include <hal/simulation/DIOData.h>
#include <wpi/raw_ostream.h>
#include <wpi/uv/Loop.h>
#include "HALSimWSServer.h"
@@ -55,8 +55,8 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalOutput) {
return;
}
if (IsConnectedClientWS()) {
wpi::outs() << "***** Setting DIO value for pin " << PIN << " to "
<< (EXPECTED_VALUE ? "true" : "false") << "\n";
fmt::print("***** Setting DIO value for pin {} to {}\n", PIN,
(EXPECTED_VALUE ? "true" : "false"));
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
done = true;
}
@@ -83,7 +83,7 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalOutput) {
test_value = it.value();
}
} catch (wpi::json::exception& e) {
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
fmt::print(stderr, "Error with incoming message: {}\n", e.what());
}
// Compare results
@@ -109,7 +109,7 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalInput) {
wpi::json msg = {{"type", "DIO"},
{"device", std::to_string(PIN)},
{"data", {{"<>value", EXPECTED_VALUE}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
fmt::print("***** Input JSON: {}\n", msg.dump());
m_webserverClient->SendMessage(msg);
done = true;
}
@@ -144,7 +144,7 @@ TEST_F(WebServerIntegrationTest, DriverStation) {
{"type", "DriverStation"},
{"device", ""},
{"data", {{">enabled", EXPECTED_VALUE}, {">new_data", true}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
fmt::print("***** Input JSON: {}\n", msg.dump());
m_webserverClient->SendMessage(msg);
done = true;
}