[wpinet] uv::Stream: Add logging for Write and TryWrite

This commit is contained in:
Peter Johnson
2024-01-19 17:37:40 -08:00
parent 42c41785ac
commit 6fab87fa4c

View File

@@ -4,7 +4,9 @@
#include "wpinet/uv/Stream.h"
#include <wpi/Logger.h>
#include <wpi/SmallVector.h>
#include <wpi/raw_ostream.h>
using namespace wpi;
using namespace wpi::uv;
@@ -86,11 +88,38 @@ void Stream::StartRead() {
});
}
static std::string BufsToString(std::span<const Buffer> bufs) {
std::string str;
wpi::raw_string_ostream stros{str};
size_t count = 0;
for (auto buf : bufs) {
for (auto ch : buf.bytes()) {
stros << fmt::format("{:02x},", static_cast<unsigned int>(ch) & 0xff);
if (count++ > 30) {
goto extra;
}
}
}
goto done;
extra: {
size_t total = 0;
for (auto buf : bufs) {
total += buf.len;
}
stros << fmt::format("... (total {})", total);
}
done:
return str;
}
void Stream::Write(std::span<const Buffer> bufs,
const std::shared_ptr<WriteReq>& req) {
if (IsLoopClosing()) {
return;
}
if (auto logger = GetLogger()) {
WPI_DEBUG4(*logger, "uv::Write({})", BufsToString(bufs));
}
if (Invoke(&uv_write, req->GetRaw(), GetRawStream(), bufs.data(), bufs.size(),
[](uv_write_t* r, int status) {
auto& h = *static_cast<WriteReq*>(r->data);
@@ -113,6 +142,9 @@ int Stream::TryWrite(std::span<const Buffer> bufs) {
if (IsLoopClosing()) {
return UV_ECANCELED;
}
if (auto logger = GetLogger()) {
WPI_DEBUG4(*logger, "uv::TryWrite({})", BufsToString(bufs));
}
int val = uv_try_write(GetRawStream(), bufs.data(), bufs.size());
if (val == UV_EAGAIN) {
return 0;