From 1604ae5b979dd60f4bb589248d717be8f0d23efa Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 11 Jul 2022 11:45:08 +0000 Subject: [PATCH] dawn/node: Work around NodeJS brokenness with printing long strings. Attempting to print a long string can cause output to go missing. Split long strings up into 4k chunks. Change-Id: Ibcb07459bd8fb4a1a11b6f8db41c8378274a6d09 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/95940 Kokoro: Kokoro Reviewed-by: Corentin Wallez Commit-Queue: Ben Clayton --- src/dawn/node/binding/GPUDevice.cpp | 53 +++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/dawn/node/binding/GPUDevice.cpp b/src/dawn/node/binding/GPUDevice.cpp index 5239ce16e1..206d5af42e 100644 --- a/src/dawn/node/binding/GPUDevice.cpp +++ b/src/dawn/node/binding/GPUDevice.cpp @@ -41,6 +41,53 @@ namespace wgpu::binding { namespace { +// Returns a string representation of the WGPULoggingType +const char* str(WGPULoggingType ty) { + switch (ty) { + case WGPULoggingType_Verbose: + return "verbose"; + case WGPULoggingType_Info: + return "info"; + case WGPULoggingType_Warning: + return "warning"; + case WGPULoggingType_Error: + return "error"; + default: + return "unknown"; + } +} + +// Returns a string representation of the WGPUErrorType +const char* str(WGPUErrorType ty) { + switch (ty) { + case WGPUErrorType_NoError: + return "no error"; + case WGPUErrorType_Validation: + return "validation"; + case WGPUErrorType_OutOfMemory: + return "out of memory"; + case WGPUErrorType_Unknown: + return "unknown"; + case WGPUErrorType_DeviceLost: + return "device lost"; + default: + return "unknown"; + } +} + +// There's something broken with Node when attempting to write more than 65536 bytes to cout. +// Split the string up into writes of 4k chunks . +// Likely related: https://github.com/nodejs/node/issues/12921 +void chunkedWrite(const char* msg) { + while (true) { + auto n = printf("%.4096s", msg); + if (n == 0) { + break; + } + msg += n; + } +} + class DeviceLostInfo : public interop::GPUDeviceLostInfo { public: DeviceLostInfo(interop::GPUDeviceLostReason reason, std::string message) @@ -88,12 +135,14 @@ GPUDevice::GPUDevice(Napi::Env env, wgpu::Device device) lost_promise_(env, PROMISE_INFO) { device_.SetLoggingCallback( [](WGPULoggingType type, char const* message, void* userdata) { - std::cout << type << ": " << message << std::endl; + printf("%s:\n", str(type)); + chunkedWrite(message); }, nullptr); device_.SetUncapturedErrorCallback( [](WGPUErrorType type, char const* message, void* userdata) { - std::cout << type << ": " << message << std::endl; + printf("%s:\n", str(type)); + chunkedWrite(message); }, nullptr);