Skip to content

Commit 5733af1

Browse files
authored
fix: properly convert objective-c logs into os_log (#301)
1 parent aa34e6a commit 5733af1

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

NativeScript/runtime/Helpers.h

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -240,47 +240,27 @@ static inline void TNS_FormatAndLog(NSString* fmt, ...) {
240240
va_list ap;
241241
va_start(ap, fmt);
242242

243-
// Convert NSString format to C string and delegate to the main implementation
244-
const char* cFmt = [fmt UTF8String];
245-
if (!cFmt) {
246-
va_end(ap);
243+
// Use NSString's formatting to handle both C and Objective-C format
244+
// specifiers
245+
NSString* formattedString =
246+
[[NSString alloc] initWithFormat:fmt arguments:ap];
247+
va_end(ap);
248+
249+
if (!formattedString) {
247250
return;
248251
}
249252

250-
// Fast path: try a reasonably sized stack buffer first
251-
const int STACK_BUF_SIZE = 1024;
252-
char stack_buf[STACK_BUF_SIZE];
253-
254-
va_list ap_copy;
255-
va_copy(ap_copy, ap);
256-
int needed = vsnprintf(stack_buf, STACK_BUF_SIZE, cFmt, ap_copy);
257-
va_end(ap_copy);
258-
259-
if (needed < 0) {
260-
va_end(ap);
253+
// Convert to C string for logging
254+
const char* cStr = [formattedString UTF8String];
255+
if (!cStr) {
261256
return;
262257
}
263258

264-
if (needed < STACK_BUF_SIZE) {
265-
// Message fit into stack buffer
266-
#if TNS_HAVE_OS_LOG
267-
os_log(OS_LOG_DEFAULT, "%{public}s", stack_buf);
268-
#else
269-
NSLog(@"%s", stack_buf);
270-
#endif
271-
} else {
272-
// Needs heap allocation
273-
std::vector<char> buffer((size_t)needed + 1);
274-
vsnprintf(buffer.data(), buffer.size(), cFmt, ap);
275-
276259
#if TNS_HAVE_OS_LOG
277-
os_log(OS_LOG_DEFAULT, "%{public}s", buffer.data());
260+
os_log(OS_LOG_DEFAULT, "%{public}s", cStr);
278261
#else
279-
NSLog(@"%s", buffer.data());
262+
NSLog(@"%s", cStr);
280263
#endif
281-
}
282-
283-
va_end(ap);
284264
}
285265
#endif
286266

@@ -337,7 +317,7 @@ static inline void TNS_FormatAndLog(const char* fmt, ...) {
337317
}
338318

339319
// Keep the existing Log(...) macro name for call-site compatibility.
340-
#define Log(...) TNS_FormatAndLog(__VA_ARGS__)
320+
#define Log(...) tns::TNS_FormatAndLog(__VA_ARGS__)
341321

342322
v8::Local<v8::String> JsonStringifyObject(v8::Local<v8::Context> context,
343323
v8::Local<v8::Value> value,

0 commit comments

Comments
 (0)