Skip to content

Commit

Permalink
Made logging logic dynamic to log string length
Browse files Browse the repository at this point in the history
Reviewed By: emilsjolander

Differential Revision: D6784491

fbshipit-source-id: 26e4520a84be355ff992b808297ce7a95b3d09e3
  • Loading branch information
priteshrnandgaonkar authored and facebook-github-bot committed Jan 23, 2018
1 parent 6fa039d commit be8e7c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions ReactCommon/yoga/yoga/YGNodePrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit be8e7c6

Please sign in to comment.