From f5f8105b576f59158a9b4a708edb1a2c1dd8d98c Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 23 Jan 2018 06:35:51 -0800 Subject: [PATCH] Made logging logic dynamic to log string length Summary: Previously the logging logic assumed fixed number of characters in the string to be logged. With this diff the logging logic is made dynamic, catering to variable length of the string to be logged Reviewed By: emilsjolander Differential Revision: D6784491 fbshipit-source-id: 26e4520a84be355ff992b808297ce7a95b3d09e3 --- java/jni/YGJNI.cpp | 15 +++++++++------ yoga/YGNodePrint.cpp | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index a7676a97..fd2ed9c3 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -158,8 +158,9 @@ static int YGJNILogFunc(const YGConfigRef config, YGLogLevel level, const char *format, va_list args) { - char buffer[256]; - int result = vsnprintf(buffer, sizeof(buffer), format, args); + int result = vsnprintf(NULL, 0, format, args); + std::vector buffer(1 + result); + vsnprintf(buffer.data(), buffer.size(), format, args); static auto logFunc = findClassStatic("com/facebook/yoga/YogaLogger") @@ -170,10 +171,12 @@ static int YGJNILogFunc(const YGConfigRef config, if (auto obj = YGNodeJobject(node)->lockLocal()) { auto jlogger = reinterpret_cast *>(YGConfigGetContext(config)); - logFunc(jlogger->get(), - obj, - logLevelFromInt(JYogaLogLevel::javaClassStatic(), static_cast(level)), - Environment::current()->NewStringUTF(buffer)); + logFunc( + jlogger->get(), + obj, + logLevelFromInt( + JYogaLogLevel::javaClassStatic(), static_cast(level)), + Environment::current()->NewStringUTF(buffer.data())); } return result; diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index 0951e955..6e504714 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -29,15 +29,15 @@ static bool areFourValuesEqual(const std::array& four) { } static void appendFormatedString(string* str, const char* fmt, ...) { - char buffer[1024]; va_list args; va_start(args, fmt); va_list argsCopy; va_copy(argsCopy, args); + std::vector buf(1 + vsnprintf(NULL, 0, fmt, args)); va_end(args); - vsnprintf(buffer, 1024, fmt, argsCopy); + vsnprintf(buf.data(), buf.size(), fmt, argsCopy); va_end(argsCopy); - string result = string(buffer); + string result = string(buf.begin(), buf.end() - 1); str->append(result); }