Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions NativeScript/runtime/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,47 +240,27 @@ static inline void TNS_FormatAndLog(NSString* fmt, ...) {
va_list ap;
va_start(ap, fmt);

// Convert NSString format to C string and delegate to the main implementation
const char* cFmt = [fmt UTF8String];
if (!cFmt) {
va_end(ap);
// Use NSString's formatting to handle both C and Objective-C format
// specifiers
NSString* formattedString =
[[NSString alloc] initWithFormat:fmt arguments:ap];
va_end(ap);

if (!formattedString) {
return;
}

// Fast path: try a reasonably sized stack buffer first
const int STACK_BUF_SIZE = 1024;
char stack_buf[STACK_BUF_SIZE];

va_list ap_copy;
va_copy(ap_copy, ap);
int needed = vsnprintf(stack_buf, STACK_BUF_SIZE, cFmt, ap_copy);
va_end(ap_copy);

if (needed < 0) {
va_end(ap);
// Convert to C string for logging
const char* cStr = [formattedString UTF8String];
if (!cStr) {
return;
}

if (needed < STACK_BUF_SIZE) {
// Message fit into stack buffer
#if TNS_HAVE_OS_LOG
os_log(OS_LOG_DEFAULT, "%{public}s", stack_buf);
#else
NSLog(@"%s", stack_buf);
#endif
} else {
// Needs heap allocation
std::vector<char> buffer((size_t)needed + 1);
vsnprintf(buffer.data(), buffer.size(), cFmt, ap);

#if TNS_HAVE_OS_LOG
os_log(OS_LOG_DEFAULT, "%{public}s", buffer.data());
os_log(OS_LOG_DEFAULT, "%{public}s", cStr);
#else
NSLog(@"%s", buffer.data());
NSLog(@"%s", cStr);
#endif
}

va_end(ap);
}
#endif

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

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

v8::Local<v8::String> JsonStringifyObject(v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
Expand Down