Replace wpi::errs() usage with fmtlib (#5560)

This will make migration to C++23's std::print() easier later.

Fixes #5556.
This commit is contained in:
Tyler Veness
2023-08-29 08:56:07 -07:00
committed by GitHub
parent ab3bf39e0e
commit d7264ff597
2 changed files with 29 additions and 21 deletions

View File

@@ -8,6 +8,7 @@
#include <thread>
#include <vector>
#include <fmt/core.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/StringExtras.h>
#include <wpi/fmt/raw_ostream.h>
@@ -63,10 +64,6 @@ struct CameraConfig {
std::vector<CameraConfig> cameras;
wpi::raw_ostream& ParseError() {
return wpi::errs() << "config error in '" << configFile << "': ";
}
bool ReadCameraConfig(const wpi::json& config) {
CameraConfig c;
@@ -74,7 +71,8 @@ bool ReadCameraConfig(const wpi::json& config) {
try {
c.name = config.at("name").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "could not read camera name: " << e.what() << '\n';
fmt::print(stderr, "config error in '{}': could not read camera name: {}\n",
configFile, e.what());
return false;
}
@@ -82,8 +80,9 @@ bool ReadCameraConfig(const wpi::json& config) {
try {
c.path = config.at("path").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "camera '" << c.name
<< "': could not read path: " << e.what() << '\n';
fmt::print(stderr,
"config error in '{}': camera '{}': could not read path: {}\n",
configFile, c.name, e.what());
return false;
}
@@ -98,8 +97,7 @@ bool ReadConfig() {
std::error_code ec;
wpi::raw_fd_istream is(configFile, ec);
if (ec) {
wpi::errs() << "could not open '" << configFile << "': " << ec.message()
<< '\n';
fmt::print(stderr, "could not open '{}': {}\n", configFile, ec.message());
return false;
}
@@ -108,13 +106,15 @@ bool ReadConfig() {
try {
j = wpi::json::parse(is);
} catch (const wpi::json::parse_error& e) {
fmt::print(ParseError(), "byte {}: {}\n", e.byte, e.what());
fmt::print(stderr, "config error in '{}': byte {}: {}\n", configFile,
e.byte, e.what());
return false;
}
// top level must be an object
if (!j.is_object()) {
ParseError() << "must be JSON object\n";
fmt::print(stderr, "config error in '{}': must be JSON object\n",
configFile);
return false;
}
@@ -122,7 +122,8 @@ bool ReadConfig() {
try {
team = j.at("team").get<unsigned int>();
} catch (const wpi::json::exception& e) {
ParseError() << "could not read team number: " << e.what() << '\n';
fmt::print(stderr, "config error in '{}': could not read team number: {}\n",
configFile, e.what());
return false;
}
@@ -135,10 +136,14 @@ bool ReadConfig() {
} else if (wpi::equals_lower(str, "server")) {
server = true;
} else {
ParseError() << "could not understand ntmode value '" << str << "'\n";
fmt::print(
stderr,
"config error in '{}': could not understand ntmode value '{}'\n",
configFile, str);
}
} catch (const wpi::json::exception& e) {
ParseError() << "could not read ntmode: " << e.what() << '\n';
fmt::print(stderr, "config error in '{}': could not read ntmode: {}\n",
configFile, e.what());
}
}
@@ -150,7 +155,8 @@ bool ReadConfig() {
}
}
} catch (const wpi::json::exception& e) {
ParseError() << "could not read cameras: " << e.what() << '\n';
fmt::print(stderr, "config error in '{}': could not read cameras: {}\n",
configFile, e.what());
return false;
}