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
This commit is contained in:
Pritesh Nandgaonkar
2018-01-23 06:35:51 -08:00
committed by Facebook Github Bot
parent 4f92ae46ef
commit f5f8105b57
2 changed files with 12 additions and 9 deletions

View File

@@ -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<char> 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<global_ref<jobject> *>(YGConfigGetContext(config));
logFunc(jlogger->get(),
obj,
logLevelFromInt(JYogaLogLevel::javaClassStatic(), static_cast<jint>(level)),
Environment::current()->NewStringUTF(buffer));
logFunc(
jlogger->get(),
obj,
logLevelFromInt(
JYogaLogLevel::javaClassStatic(), static_cast<jint>(level)),
Environment::current()->NewStringUTF(buffer.data()));
}
return result;

View File

@@ -29,15 +29,15 @@ static bool areFourValuesEqual(const std::array<YGValue, YGEdgeCount>& 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<char> 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);
}