From 23cb213d3f2f8d8376dc0b081a8e6df15129f8be Mon Sep 17 00:00:00 2001 From: Florian Fischer <florian.fl.fischer@fau.de> Date: Thu, 8 Apr 2021 10:42:07 +0200 Subject: [PATCH] [EchoClient] improve unexpected echo output Build up output string and don't incrementally output it to stderr to hopefully prevent error messages interleaving. Only output the least significant byte of the unsigned int cast. Always use two digits per byte and remove the whitespace. Should have used printf in the first place. --- apps/EchoClient.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/apps/EchoClient.cpp b/apps/EchoClient.cpp index bd4551d3..43dc49f3 100644 --- a/apps/EchoClient.cpp +++ b/apps/EchoClient.cpp @@ -10,11 +10,12 @@ #include <algorithm> // for find #include <atomic> -#include <cerrno> // for errno, ECANCELED -#include <chrono> // for nanoseconds, duration, durat... -#include <cstdint> // for uint64_t, int32_t -#include <cstdlib> // for size_t, strtol, exit, EXIT_F... -#include <cstring> // for memcmp +#include <cerrno> // for errno, ECANCELED +#include <chrono> // for nanoseconds, duration, durat... +#include <cstdint> // for uint64_t, int32_t +#include <cstdlib> // for size_t, strtol, exit, EXIT_F... +#include <cstring> // for memcmp +#include <iomanip> #include <iostream> // for operator<<, basic_ostream, endl #include <string> // for allocator, string, char_traits #include <thread> @@ -177,18 +178,21 @@ static void clientFunc(uint64_t client_id, Semaphore& readySem, Semaphore& start } if (memcmp(outBuf, inBuf, size) != 0) { - std::cerr << "got unexpected echo from server" << std::endl; - std::cerr << "expected: " << std::hex; + std::stringstream sst; + sst << "got unexpected echo from server" << std::endl; + sst << "expected: "; for (unsigned i = 0; i < size; ++i) { - std::cerr << (unsigned)outBuf[i] << " "; + sst << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned)outBuf[i]); } - std::cerr << std::endl; + sst << std::endl; - std::cerr << "received: " << std::hex; + sst << "received: "; for (unsigned i = 0; i < size; ++i) { - std::cerr << (unsigned)inBuf[i] << " "; + sst << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned)inBuf[i]); + sst << (unsigned)inBuf[i] << " "; } - std::cerr << std::endl; + sst << std::endl; + std::cerr << sst.str(); DIE; } -- GitLab