Skip to content

Commit be8e7c6

Browse files
Made logging logic dynamic to log string length
Reviewed By: emilsjolander Differential Revision: D6784491 fbshipit-source-id: 26e4520a84be355ff992b808297ce7a95b3d09e3
1 parent 6fa039d commit be8e7c6

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ static int YGJNILogFunc(const YGConfigRef config,
158158
YGLogLevel level,
159159
const char *format,
160160
va_list args) {
161-
char buffer[256];
162-
int result = vsnprintf(buffer, sizeof(buffer), format, args);
161+
int result = vsnprintf(NULL, 0, format, args);
162+
std::vector<char> buffer(1 + result);
163+
vsnprintf(buffer.data(), buffer.size(), format, args);
163164

164165
static auto logFunc =
165166
findClassStatic("com/facebook/yoga/YogaLogger")
@@ -170,10 +171,12 @@ static int YGJNILogFunc(const YGConfigRef config,
170171

171172
if (auto obj = YGNodeJobject(node)->lockLocal()) {
172173
auto jlogger = reinterpret_cast<global_ref<jobject> *>(YGConfigGetContext(config));
173-
logFunc(jlogger->get(),
174-
obj,
175-
logLevelFromInt(JYogaLogLevel::javaClassStatic(), static_cast<jint>(level)),
176-
Environment::current()->NewStringUTF(buffer));
174+
logFunc(
175+
jlogger->get(),
176+
obj,
177+
logLevelFromInt(
178+
JYogaLogLevel::javaClassStatic(), static_cast<jint>(level)),
179+
Environment::current()->NewStringUTF(buffer.data()));
177180
}
178181

179182
return result;

ReactCommon/yoga/yoga/YGNodePrint.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ static bool areFourValuesEqual(const std::array<YGValue, YGEdgeCount>& four) {
2929
}
3030

3131
static void appendFormatedString(string* str, const char* fmt, ...) {
32-
char buffer[1024];
3332
va_list args;
3433
va_start(args, fmt);
3534
va_list argsCopy;
3635
va_copy(argsCopy, args);
36+
std::vector<char> buf(1 + vsnprintf(NULL, 0, fmt, args));
3737
va_end(args);
38-
vsnprintf(buffer, 1024, fmt, argsCopy);
38+
vsnprintf(buf.data(), buf.size(), fmt, argsCopy);
3939
va_end(argsCopy);
40-
string result = string(buffer);
40+
string result = string(buf.begin(), buf.end() - 1);
4141
str->append(result);
4242
}
4343

0 commit comments

Comments
 (0)