Skip to content

Commit f247b40

Browse files
committed
feat(console): split long messages for os_log and report errors as errors
os_log truncates messages at about 1 KB. They now go out as chunks of at most 900 bytes, cut at line breaks where possible and never inside a UTF-8 sequence. console.error uses OS_LOG_TYPE_ERROR. When the CLI writes logChunkMarkers to the app's package.json and stdout is /dev/null, each chunk ends with an invisible marker that lets the CLI rebuild the original message. Other readers see plain split lines: older CLIs never set the key, and an attached console fails the stdout check.
1 parent 3ae16f1 commit f247b40

5 files changed

Lines changed: 120 additions & 7 deletions

File tree

‎NativeScript/runtime/Console.cpp‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ namespace {
2525
struct ConsoleTimersState {
2626
robin_hood::unordered_map<std::string, double> startedAt;
2727
};
28+
29+
// Errors get the error log type so log readers can tell severity apart. The
30+
// default handle is kept on purpose: a subsystem would show up in the text of
31+
// syslog-style output, which older CLIs parse.
32+
void LogConsoleMessage(bool isError, const std::string& prefix,
33+
const std::string& message) {
34+
#if TNS_HAVE_OS_LOG
35+
TNS_WriteToOsLog(message.c_str(),
36+
isError ? OS_LOG_TYPE_ERROR : OS_LOG_TYPE_DEFAULT, prefix);
37+
#else
38+
Log("%s%s", prefix.c_str(), message.c_str());
39+
#endif
40+
}
2841
} // namespace
2942

3043
void Console::Init(Local<Context> context) {
@@ -117,9 +130,8 @@ void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
117130

118131
ConsoleAPIType method = VerbosityToInspectorMethod(verbosityLevel);
119132
SendToDevToolsFrontEnd(method, args);
120-
std::string msgWithVerbosity =
121-
"CONSOLE " + verbosityLevelUpper + ": " + msgToLog;
122-
Log("%s", msgWithVerbosity.c_str());
133+
LogConsoleMessage(verbosityLevel == "error",
134+
"CONSOLE " + verbosityLevelUpper + ": ", msgToLog);
123135

124136
if (RuntimeConfig.IsDebug && Runtime::showErrorDisplay() &&
125137
verbosityLevel == "error" && hasStackTrace) {
@@ -158,7 +170,7 @@ void Console::AssertCallback(const FunctionCallbackInfo<Value>& args) {
158170
std::string log = ss.str();
159171

160172
SendToDevToolsFrontEnd(ConsoleAPIType::kAssert, args);
161-
Log("%s", log.c_str());
173+
LogConsoleMessage(true, "", log);
162174
}
163175
}
164176

‎NativeScript/runtime/Helpers.h‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern "C" void NSLog(CFStringRef format, ...);
3131
#define TNS_HAVE_OS_LOG 0
3232
#endif
3333

34+
#include "LogChunks.h"
35+
3436
namespace tns {
3537

3638
inline v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const std::string& value) {
@@ -353,6 +355,27 @@ void LogBacktrace(int skip = 1);
353355
// "%{public}s" argument to os_log. When os_log is not available we
354356
// fall back to NSLog (Objective-C) or CF/NSLog bridge in pure C++ builds.
355357

358+
// Whether split messages should carry markers for the CLI to stitch them with.
359+
bool ShouldMarkLogChunks();
360+
361+
#if TNS_HAVE_OS_LOG
362+
// The prefix is repeated on every chunk, so that each stays a complete log line.
363+
static inline void TNS_WriteToOsLog(const char* message, os_log_type_t type = OS_LOG_TYPE_DEFAULT,
364+
std::string_view prefix = "") {
365+
ForEachLogChunk(message, kMaxLogChunkBytes - prefix.size(),
366+
[&](std::string_view chunk, LogChunkBreak chunkBreak) {
367+
const char* marker = "";
368+
if (chunkBreak != LogChunkBreak::kNone && ShouldMarkLogChunks()) {
369+
marker = chunkBreak == LogChunkBreak::kLine ? kLogChunkLineMarker
370+
: kLogChunkJoinMarker;
371+
}
372+
os_log_with_type(OS_LOG_DEFAULT, type, "%{public}.*s%{public}.*s%{public}s",
373+
static_cast<int>(prefix.size()), prefix.data(),
374+
static_cast<int>(chunk.size()), chunk.data(), marker);
375+
});
376+
}
377+
#endif
378+
356379
#ifdef __OBJC__
357380
// Overload for Objective-C string literals (NSString*)
358381
static inline void TNS_FormatAndLog(NSString* fmt, ...) {
@@ -373,7 +396,7 @@ static inline void TNS_FormatAndLog(NSString* fmt, ...) {
373396
const char* cStr = [formattedString UTF8String];
374397
if (cStr) {
375398
#if TNS_HAVE_OS_LOG
376-
os_log(OS_LOG_DEFAULT, "%{public}s", cStr);
399+
TNS_WriteToOsLog(cStr);
377400
#else
378401
NSLog(@"%s", cStr);
379402
#endif
@@ -406,7 +429,7 @@ static inline void TNS_FormatAndLog(const char* fmt, ...) {
406429
if (needed < STACK_BUF_SIZE) {
407430
// Message fit into stack buffer — single formatting pass.
408431
#if TNS_HAVE_OS_LOG
409-
os_log(OS_LOG_DEFAULT, "%{public}s", stack_buf);
432+
TNS_WriteToOsLog(stack_buf);
410433
#else
411434
// Fall back to NSLog. Use Objective-C API when compiling as ObjC++.
412435
#ifdef __OBJC__
@@ -421,7 +444,7 @@ static inline void TNS_FormatAndLog(const char* fmt, ...) {
421444
vsnprintf(buffer.data(), buffer.size(), fmt, ap);
422445

423446
#if TNS_HAVE_OS_LOG
424-
os_log(OS_LOG_DEFAULT, "%{public}s", buffer.data());
447+
TNS_WriteToOsLog(buffer.data());
425448
#else
426449
// Fall back to NSLog. Use Objective-C API when compiling as ObjC++.
427450
#ifdef __OBJC__

‎NativeScript/runtime/Helpers.mm‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,27 @@ void WriteDebugLine(tns::LogCategory category, const char* message) {
493493
}
494494
}
495495

496+
static bool IsDevNull(int fd) {
497+
struct stat target;
498+
struct stat devNull;
499+
return fstat(fd, &target) == 0 && stat("/dev/null", &devNull) == 0 &&
500+
S_ISCHR(target.st_mode) && target.st_rdev == devNull.st_rdev;
501+
}
502+
503+
// Syslog-style output shows the markers as visible text and an attached console
504+
// shows them as they are, so they need a CLI that asked for them and no console.
505+
bool tns::ShouldMarkLogChunks() {
506+
if (RuntimeConfig.ApplicationPath.empty()) {
507+
return false;
508+
}
509+
510+
static const bool enabled = [] {
511+
id value = Runtime::GetAppConfigValue("logChunkMarkers");
512+
return value != nil && [value boolValue] && IsDevNull(STDOUT_FILENO);
513+
}();
514+
return enabled;
515+
}
516+
496517
void tns::EmitDebugLog(LogCategory category, const char* format, ...) {
497518
va_list ap;
498519
va_start(ap, format);

‎NativeScript/runtime/LogChunks.h‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef LogChunks_h
2+
#define LogChunks_h
3+
4+
#include <cstddef>
5+
#include <string_view>
6+
7+
namespace tns {
8+
9+
// os_log replaces whatever exceeds its buffer with "<…>". The buffer also holds
10+
// a few bytes of bookkeeping per argument, which left 996 bytes of text with two
11+
// arguments on iPadOS 26.7, so stay well below that.
12+
constexpr size_t kMaxLogChunkBytes = 900;
13+
14+
// How a chunk connects to the one after it.
15+
enum class LogChunkBreak { kNone, kJoin, kLine };
16+
17+
// Invisible, so that the chunks read normally wherever they are not stitched.
18+
// Control characters would not do: os_log escapes them.
19+
constexpr const char* kLogChunkJoinMarker = "\u2060";
20+
constexpr const char* kLogChunkLineMarker = "\u2061";
21+
22+
// Calls emit with consecutive pieces of message, each at most maxBytes long and
23+
// never ending inside a UTF-8 sequence. A piece ends at the last line break that
24+
// fits, so that lines stay intact whenever they can; that line break is dropped,
25+
// since every piece is logged as a line of its own.
26+
template <typename Emit>
27+
void ForEachLogChunk(std::string_view message, size_t maxBytes, Emit&& emit) {
28+
while (message.size() > maxBytes) {
29+
size_t end = message.rfind('\n', maxBytes);
30+
size_t next = end + 1;
31+
LogChunkBreak chunkBreak = LogChunkBreak::kLine;
32+
33+
if (end == std::string_view::npos || end == 0) {
34+
end = maxBytes;
35+
while (end > 0 && (static_cast<unsigned char>(message[end]) & 0xC0) == 0x80) {
36+
end--;
37+
}
38+
// Not UTF-8 at all: any cut is as good as another.
39+
if (end == 0) {
40+
end = maxBytes;
41+
}
42+
next = end;
43+
chunkBreak = LogChunkBreak::kJoin;
44+
}
45+
46+
emit(message.substr(0, end), chunkBreak);
47+
message.remove_prefix(next);
48+
}
49+
50+
emit(message, LogChunkBreak::kNone);
51+
}
52+
53+
} // namespace tns
54+
55+
#endif /* LogChunks_h */

‎v8ios.xcodeproj/project.pbxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@
818818
C2DDEB6A229EAC8200345BFE /* ArgConverter.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ArgConverter.mm; sourceTree = "<group>"; };
819819
C2DDEB6B229EAC8200345BFE /* Console.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Console.cpp; sourceTree = "<group>"; };
820820
C2DDEB6D229EAC8200345BFE /* Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Helpers.h; sourceTree = "<group>"; };
821+
A1C0DE5100000000000000A1 /* LogChunks.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LogChunks.h; sourceTree = "<group>"; };
821822
C2DDEB6E229EAC8200345BFE /* Metadata.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Metadata.h; sourceTree = "<group>"; };
822823
C2DDEB6F229EAC8200345BFE /* ArrayAdapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ArrayAdapter.h; sourceTree = "<group>"; };
823824
C2DDEB70229EAC8200345BFE /* Metadata.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Metadata.mm; sourceTree = "<group>"; };
@@ -1497,6 +1498,7 @@
14971498
C266569D22B282BA00EE15CC /* FunctionReference.h */,
14981499
C266569C22B282BA00EE15CC /* FunctionReference.cpp */,
14991500
C2DDEB6D229EAC8200345BFE /* Helpers.h */,
1501+
A1C0DE5100000000000000A1 /* LogChunks.h */,
15001502
C2DDEB7D229EAC8200345BFE /* Helpers.mm */,
15011503
AA5DBFD12EC191F8008D12F9 /* HttpLoader.h */,
15021504
AA5DBFD22EC191F8008D12F9 /* HttpLoader.mm */,

0 commit comments

Comments
 (0)