Skip to content

Commit f6282e6

Browse files
rigor789edusperoni
authored andcommitted
feat: split long logs into multiple instead of truncating
1 parent 5a6c2ee commit f6282e6

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

NativeScript/runtime/Console.cpp

+39-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ void Console::AttachInspectorClient(v8_inspector::JsV8InspectorClient* aInspecto
3939
inspector = aInspector;
4040
}
4141

42+
void Console::SplitAndLogInChunks(std::string message) {
43+
auto messageLength = message.length();
44+
int maxStringLength = 1000; // technically 1024, but let's have some room :)
45+
46+
if (messageLength < maxStringLength) {
47+
// print normally
48+
Log("%s", message.c_str());
49+
} else {
50+
// split into chunks
51+
for (int i = 0; i < messageLength; i += maxStringLength) {
52+
std::string messagePart = message.substr(i, maxStringLength);
53+
Log("%s", messagePart.c_str());
54+
}
55+
}
56+
}
57+
4258
void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
4359
// TODO: implement 'forceLog' override option like android has, to force logs in prod if desired
4460
if (!RuntimeConfig.LogToSystemConsole) {
@@ -66,7 +82,22 @@ void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
6682
ConsoleAPIType method = VerbosityToInspectorMethod(verbosityLevel);
6783
SendToDevToolsFrontEnd(method, args);
6884
std::string msgWithVerbosity = "CONSOLE " + verbosityLevelUpper + ": " + msgToLog;
69-
Log("%s", msgWithVerbosity.c_str());
85+
86+
SplitAndLogInChunks(msgWithVerbosity);
87+
// //Log("%s", msgWithVerbosity.c_str());
88+
// auto messageLength = msgWithVerbosity.length();
89+
// int maxStringLength = 1000; // technically 1024, but let's have some room :)
90+
91+
// if (messageLength < maxStringLength) {
92+
// // print normally
93+
// Log("%s", msgWithVerbosity.c_str());
94+
// } else {
95+
// // split into chunks
96+
// for (int i = 0; i < messageLength; i += maxStringLength) {
97+
// std::string messagePart = msgWithVerbosity.substr(i, maxStringLength);
98+
// Log("%s", messagePart.c_str());
99+
// }
100+
// }
70101
}
71102

72103
void Console::AssertCallback(const FunctionCallbackInfo<Value>& args) {
@@ -92,7 +123,9 @@ void Console::AssertCallback(const FunctionCallbackInfo<Value>& args) {
92123
std::string log = ss.str();
93124

94125
SendToDevToolsFrontEnd(ConsoleAPIType::kAssert, args);
95-
Log("%s", log.c_str());
126+
127+
SplitAndLogInChunks(log);
128+
// Log("%s", log.c_str());
96129
}
97130
}
98131

@@ -163,7 +196,8 @@ void Console::DirCallback(const FunctionCallbackInfo<Value>& args) {
163196

164197
std::string msgToLog = ss.str();
165198
SendToDevToolsFrontEnd(ConsoleAPIType::kDir, args);
166-
Log("%s", msgToLog.c_str());
199+
SplitAndLogInChunks(msgToLog);
200+
// Log("%s", msgToLog.c_str());
167201
}
168202

169203
void Console::TimeCallback(const FunctionCallbackInfo<Value>& args) {
@@ -224,7 +258,8 @@ void Console::TimeEndCallback(const FunctionCallbackInfo<Value>& args) {
224258

225259
std::string msgToLog = ss.str();
226260
SendToDevToolsFrontEnd(isolate, ConsoleAPIType::kTimeEnd, msgToLog);
227-
Log("%s", msgToLog.c_str());
261+
SplitAndLogInChunks(msgToLog);
262+
// Log("%s", msgToLog.c_str());
228263
}
229264

230265
void Console::AttachLogFunction(Local<Context> context, Local<Object> console, const std::string name, v8::FunctionCallback callback) {

NativeScript/runtime/Console.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Console {
1515
using ConsoleAPIType = v8_inspector::ConsoleAPIType;
1616

1717
static void AttachLogFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> console, const std::string name, v8::FunctionCallback callback = Console::LogCallback);
18+
static void SplitAndLogInChunks(std::string message);
1819
static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
1920
static void AssertCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
2021
static void DirCallback(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)