Skip to content

Commit

Permalink
sniffer: fix mangled lines on stdout output
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmouchet committed Jan 6, 2024
1 parent 4764ae4 commit cb3d588
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 0 additions & 1 deletion apps/caracal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ int main(int argc, char** argv) {
spdlog::set_default_logger(spdlog::stderr_color_st(""));

spdlog::info("Reading from stdin, press CTRL+D to stop...");
std::ios::sync_with_stdio(false);
caracal::Prober::probe(config, std::cin);
} catch (const std::exception& e) {
auto type = caracal::Utilities::demangle(typeid(e).name());
Expand Down
11 changes: 10 additions & 1 deletion src/sniffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <filesystem>
#include <iostream>
#include <optional>
#ifndef __APPLE__
// Currently not available on macOS
#include <syncstream>
#endif
#include <thread>

namespace fs = std::filesystem;
Expand Down Expand Up @@ -70,14 +74,19 @@ void Sniffer::start() noexcept {
std::cout << (Reply::csv_header() + "\n");
auto handler = [this](Tins::Packet &packet) {
auto reply = Parser::parse(packet);
#ifdef __APPLE__
auto stdout = std::cout;
#else
auto stdout = std::osyncstream(std::cout);
#endif

if (reply && (!integrity_check_ || reply->is_valid(caracal_id_))) {
spdlog::trace(reply.value());
statistics_.icmp_messages_all.insert(reply->reply_src_addr);
if (reply->is_time_exceeded()) {
statistics_.icmp_messages_path.insert(reply->reply_src_addr);
}
std::cout << (reply->to_csv(meta_round_.value_or("1")) + "\n");
stdout << (reply->to_csv(meta_round_.value_or("1")) + "\n");
} else {
auto data = packet.pdu()->serialize();
spdlog::trace("invalid_packet_hex={:02x}", fmt::join(data, ""));
Expand Down

0 comments on commit cb3d588

Please sign in to comment.