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 <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-07-11 11:45:08 +00:00 committed by Dawn LUCI CQ
parent af13cc714b
commit 1604ae5b97
1 changed files with 51 additions and 2 deletions

View File

@ -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);