[wpiutil] Upgrade to fmt 10.2.1, add wpi::print (#6161)

We now use a wrapper (wpi::print) to catch exceptions since we can't patch
std::print() to not throw when we ultimately migrate to it.

fmtlib and std format/print throw the same exceptions and always have. We previously patched fmt::print() to not throw a write failure exception, but we can't do that for std::print(); wpi::print() is the migration plan.
This commit is contained in:
Tyler Veness
2024-05-12 06:25:42 -07:00
committed by GitHub
parent 6c9dcc157e
commit d88c71ffdc
99 changed files with 1374 additions and 1130 deletions

View File

@@ -6,10 +6,10 @@
#include <chrono>
#include <fmt/format.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/fmt/raw_ostream.h>
#include <wpi/print.h>
#include <wpinet/HttpUtil.h>
#include <wpinet/TCPAcceptor.h>
#include <wpinet/raw_socket_istream.h>
@@ -135,7 +135,7 @@ class MjpegServerImpl::ConnThread : public wpi::SafeThread {
static void SendHeader(wpi::raw_ostream& os, int code,
std::string_view codeText, std::string_view contentType,
std::string_view extra = {}) {
fmt::print(os, "HTTP/1.0 {} {}\r\n", code, codeText);
wpi::print(os, "HTTP/1.0 {} {}\r\n", code, codeText);
os << "Connection: close\r\n"
"Server: CameraServer/1.0\r\n"
"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, "
@@ -302,7 +302,7 @@ bool MjpegServerImpl::ConnThread::ProcessCommand(wpi::raw_ostream& os,
case CS_PROP_INTEGER:
case CS_PROP_ENUM: {
if (auto v = wpi::parse_integer<int>(value, 10)) {
fmt::print(response, "{}: {}\r\n", param, v.value());
wpi::print(response, "{}: {}\r\n", param, v.value());
SDEBUG4("HTTP parameter \"{}\" value {}", param, value);
source.SetProperty(prop, v.value(), &status);
} else {
@@ -356,10 +356,10 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
continue;
}
auto kind = source.GetPropertyKind(prop);
fmt::print(os, "<p /><label for=\"{0}\">{0}</label>\n", name);
wpi::print(os, "<p /><label for=\"{0}\">{0}</label>\n", name);
switch (kind) {
case CS_PROP_BOOLEAN:
fmt::print(os,
wpi::print(os,
"<input id=\"{0}\" type=\"checkbox\" "
"onclick=\"update('{0}', this.checked ? 1 : 0)\" ",
name);
@@ -374,12 +374,12 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
auto min = source.GetPropertyMin(prop, &status);
auto max = source.GetPropertyMax(prop, &status);
auto step = source.GetPropertyStep(prop, &status);
fmt::print(os,
wpi::print(os,
"<input type=\"range\" min=\"{1}\" max=\"{2}\" "
"value=\"{3}\" id=\"{0}\" step=\"{4}\" "
"oninput=\"updateInt('#{0}op', '{0}', value)\" />\n",
name, min, max, valI, step);
fmt::print(os, "<output for=\"{0}\" id=\"{0}op\">{1}</output>\n", name,
wpi::print(os, "<output for=\"{0}\" id=\"{0}op\">{1}</output>\n", name,
valI);
break;
}
@@ -397,25 +397,25 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
for (char ch : *choice) {
ch_name.push_back(wpi::isPrint(ch) ? ch : ' ');
}
fmt::print(os,
wpi::print(os,
"<input id=\"{0}{1}\" type=\"radio\" name=\"{0}\" "
"value=\"{2}\" onclick=\"update('{0}', {1})\"",
name, j, ch_name.str());
if (j == valE) {
os << " checked";
}
fmt::print(os, " /><label for=\"{}{}\">{}</label>\n", name, j,
wpi::print(os, " /><label for=\"{}{}\">{}</label>\n", name, j,
ch_name.str());
}
break;
}
case CS_PROP_STRING: {
wpi::SmallString<128> strval_buf;
fmt::print(os,
wpi::print(os,
"<input type=\"text\" id=\"{0}box\" name=\"{0}\" "
"value=\"{1}\" />\n",
name, source.GetStringProperty(prop, strval_buf, &status));
fmt::print(os,
wpi::print(os,
"<input type=\"button\" value =\"Submit\" "
"onclick=\"update('{0}', {0}box.value)\" />\n",
name);
@@ -471,7 +471,7 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
os << "unknown";
break;
}
fmt::print(os, "</td><td>{}</td><td>{}</td><td>{}</td></tr>", mode.width,
wpi::print(os, "</td><td>{}</td><td>{}</td><td>{}</td></tr>", mode.width,
mode.height, mode.fps);
}
os << "</table>\n";
@@ -500,21 +500,21 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os,
wpi::SmallString<128> name_buf;
auto name = source.GetPropertyName(prop, name_buf, &status);
auto kind = source.GetPropertyKind(prop);
fmt::print(os, "\n\"name\": \"{}\"", name);
fmt::print(os, ",\n\"id\": \"{}\"", prop);
fmt::print(os, ",\n\"type\": \"{}\"", static_cast<int>(kind));
fmt::print(os, ",\n\"min\": \"{}\"", source.GetPropertyMin(prop, &status));
fmt::print(os, ",\n\"max\": \"{}\"", source.GetPropertyMax(prop, &status));
fmt::print(os, ",\n\"step\": \"{}\"",
wpi::print(os, "\n\"name\": \"{}\"", name);
wpi::print(os, ",\n\"id\": \"{}\"", prop);
wpi::print(os, ",\n\"type\": \"{}\"", static_cast<int>(kind));
wpi::print(os, ",\n\"min\": \"{}\"", source.GetPropertyMin(prop, &status));
wpi::print(os, ",\n\"max\": \"{}\"", source.GetPropertyMax(prop, &status));
wpi::print(os, ",\n\"step\": \"{}\"",
source.GetPropertyStep(prop, &status));
fmt::print(os, ",\n\"default\": \"{}\"",
wpi::print(os, ",\n\"default\": \"{}\"",
source.GetPropertyDefault(prop, &status));
os << ",\n\"value\": \"";
switch (kind) {
case CS_PROP_BOOLEAN:
case CS_PROP_INTEGER:
case CS_PROP_ENUM:
fmt::print(os, "{}", source.GetProperty(prop, &status));
wpi::print(os, "{}", source.GetProperty(prop, &status));
break;
case CS_PROP_STRING: {
wpi::SmallString<128> strval_buf;
@@ -544,7 +544,7 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os,
for (char ch : *choice) {
ch_name.push_back(std::isprint(ch) ? ch : ' ');
}
fmt::print(os, "\"{}\": \"{}\"", j, ch_name.str());
wpi::print(os, "\"{}\": \"{}\"", j, ch_name.str());
}
os << "}\n";
}
@@ -586,9 +586,9 @@ void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os,
os << "unknown";
break;
}
fmt::print(os, "\",\n\"width\": \"{}\"", mode.width);
fmt::print(os, ",\n\"height\": \"{}\"", mode.height);
fmt::print(os, ",\n\"fps\": \"{}\"", mode.fps);
wpi::print(os, "\",\n\"width\": \"{}\"", mode.width);
wpi::print(os, ",\n\"height\": \"{}\"", mode.height);
wpi::print(os, ",\n\"fps\": \"{}\"", mode.fps);
os << '}';
}
os << "\n]\n}\n";
@@ -773,8 +773,8 @@ void MjpegServerImpl::ConnThread::SendStream(wpi::raw_socket_ostream& os) {
header.clear();
oss << "\r\n--" BOUNDARY "\r\n"
<< "Content-Type: image/jpeg\r\n";
fmt::print(oss, "Content-Length: {}\r\n", size);
fmt::print(oss, "X-Timestamp: {}\r\n", timestamp);
wpi::print(oss, "Content-Length: {}\r\n", size);
wpi::print(oss, "X-Timestamp: {}\r\n", timestamp);
oss << "\r\n";
os << oss.str();
if (addDHT) {