tools: use Log instead of fmt::print
Since fmt::print does not flush the buffer, I added a Log macro to
ensure the output is immediately flushed.
Not needed for print on stderr as std lib already flush it.
Change-Id: Ieb7c04f9f191f1982643b005fae2d4e944a3a1fa
diff --git a/tools/dnc/dnc.cpp b/tools/dnc/dnc.cpp
index 86bb159..18860ae 100644
--- a/tools/dnc/dnc.cpp
+++ b/tools/dnc/dnc.cpp
@@ -127,7 +127,7 @@
auto it = authorized_services.find(ip);
if (it == authorized_services.end()) {
// Reject the connection if the ip is not authorized
- fmt::print("Rejecting connection to {}:{}", ip, port);
+ Log("Rejecting connection to {}:{}", ip, port);
return false;
}
@@ -135,10 +135,10 @@
const auto &ports = it->second;
if (std::find(ports.begin(), ports.end(), port) == ports.end()) {
// Reject the connection if the port is not authorized
- fmt::print("Rejecting connection to {}:{}", ip, port);
+ Log("Rejecting connection to {}:{}", ip, port);
return false;
}
- fmt::print("Accepting connection to {}:{}", ip, port);
+ Log("Accepting connection to {}:{}", ip, port);
return true;
});
@@ -151,7 +151,7 @@
}
try {
auto parsedName = parseName(name);
- fmt::print("Connecting to {}:{}", parsedName.first, parsedName.second);
+ Log("Connecting to {}:{}", parsedName.first, parsedName.second);
asio::ip::tcp::resolver resolver(*ioContext);
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(parsedName.first,
@@ -167,7 +167,7 @@
[this, socket, mtlxSocket](const std::error_code& error,
const asio::ip::tcp::endpoint& ep) {
if (!error) {
- fmt::print("Connected!\n");
+ Log("Connected!\n");
mtlxSocket->setOnRecv([socket, this](const uint8_t* data, size_t size) {
auto data_copy = std::make_shared<std::vector<uint8_t>>(data,
data + size);
@@ -176,7 +176,7 @@
[data_copy, this](const std::error_code& error,
std::size_t bytesWritten) {
if (error) {
- fmt::print("Write error: {}\n", error.message());
+ Log("Write error: {}\n", error.message());
}
});
@@ -186,13 +186,13 @@
auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE);
readFromPipe(mtlxSocket, socket, buffer);
} else {
- fmt::print("Connection error: {}\n", error.message());
+ Log("Connection error: {}\n", error.message());
mtlxSocket->shutdown();
}
});
} catch (std::exception& e) {
- fmt::print("Exception: {}\n", e.what());
+ Log("Exception: {}\n", e.what());
}
});
}
@@ -211,7 +211,7 @@
{
std::condition_variable cv;
auto name = fmt::format("nc://{:s}:{:d}", remote_host, remote_port);
- fmt::print("Requesting socket: %s\n", name.c_str());
+ Log("Requesting socket: %s\n", name.c_str());
connectionManager->connectDevice(
peer_id, name, [&](std::shared_ptr<ChannelSocket> socket, const dht::InfoHash&) {
if (socket) {
@@ -230,7 +230,7 @@
readFromPipe(socket, stdinPipe, buffer);
socket->onShutdown([this]() {
- fmt::print("Exit program\n");
+ Log("Exit program\n");
ioContext->stop();
});
}
@@ -238,7 +238,7 @@
connectionManager->onConnectionReady(
[&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> mtlxSocket) {
- fmt::print("Connected!\n");
+ Log("Connected!\n");
});
}