Skip to content

Commit 2c4337b

Browse files
committed
fix: Implement console.log inspector with Runtime protocol
Console messages used to be implemented with the Log protocol, which is no longer part of V8. The Runtime protocol provides an equivalent API, Runtime.consoleAPICalled. The Runtime protocol is part of the generated public sources, but the V8 inspector's RuntimeAgent is not. So, for now, we make use of V8's private API, casting V8Inspector* to V8InspectorImpl* and V8InspectorSession* to V8InspectorSessionImpl* in order to access their methods (for which we pulled in the corresponding V8 header files in the V8 sources update, earlier on this branch.)
1 parent f357ce6 commit 2c4337b

File tree

6 files changed

+64
-163
lines changed

6 files changed

+64
-163
lines changed

test-app/runtime/src/main/cpp/JsV8InspectorClient.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#include "JsV8InspectorClient.h"
22
#include <assert.h>
33
#include <include/libplatform/libplatform.h>
4+
#include <src/inspector/v8-console-message.h>
5+
#include <src/inspector/v8-inspector-impl.h>
6+
#include <src/inspector/v8-inspector-session-impl.h>
7+
#include <src/inspector/v8-runtime-agent-impl.h>
8+
#include <src/inspector/v8-stack-trace-impl.h>
9+
410
#include "Runtime.h"
511
#include "NativeScriptException.h"
612

713
#include "ArgConverter.h"
814
// #include "DOMDomainCallbackHandlers.h"
9-
// #include "LogAgentImpl.h"
1015
// #include "NetworkDomainCallbackHandlers.h"
1116

1217
using namespace std;
@@ -232,17 +237,26 @@ void JsV8InspectorClient::sendToFrontEndCallback(const v8::FunctionCallbackInfo<
232237
}
233238
}
234239

235-
void JsV8InspectorClient::consoleLogCallback(Isolate* isolate, const string& message, const string& logLevel) {
240+
void JsV8InspectorClient::consoleLogCallback(Isolate* isolate, ConsoleAPIType method, const std::vector<v8::Local<v8::Value>>& args) {
236241
if (!inspectorIsConnected()) {
237242
return;
238243
}
239244

240-
auto stack = v8::StackTrace::CurrentStackTrace(isolate, 1, v8::StackTrace::StackTraceOptions::kDetailed);
245+
// Note, here we access private V8 API
246+
auto* impl = reinterpret_cast<v8_inspector::V8InspectorImpl*>(instance->inspector_.get());
247+
auto* session = reinterpret_cast<v8_inspector::V8InspectorSessionImpl*>(instance->session_.get());
248+
249+
std::unique_ptr<V8StackTraceImpl> stack = impl->debugger()->captureStackTrace(false);
250+
251+
v8::Local<v8::Context> context = instance->context_.Get(instance->isolate_);
252+
const int contextId = V8ContextInfo::executionContextId(context);
241253

242-
auto frame = stack->GetFrame(isolate, 0);
254+
std::unique_ptr<v8_inspector::V8ConsoleMessage> msg =
255+
v8_inspector::V8ConsoleMessage::createForConsoleAPI(
256+
context, contextId, contextGroupId, impl, instance->currentTimeMS(),
257+
method, args, String16{}, std::move(stack));
243258

244-
// will be no-op in non-debuggable builds
245-
// v8_inspector::V8LogAgentImpl::EntryAdded(message, logLevel, ArgConverter::ConvertToString(frame->GetScriptNameOrSourceURL()), frame->GetLineNumber());
259+
session->runtimeAgent()->messageAdded(msg.get());
246260
}
247261

248262
void JsV8InspectorClient::attachInspectorCallbacks(Isolate* isolate,

test-app/runtime/src/main/cpp/JsV8InspectorClient.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
#define JSV8INSPECTORCLIENT_H_
33

44
#include <string>
5+
#include <vector>
6+
#include <src/inspector/v8-console-message.h>
57
#include "v8.h"
68
#include "JEnv.h"
7-
#include "src/inspector/v8-inspector-impl.h"
8-
#include "src/inspector/v8-inspector-session-impl.h"
99
#include "v8-inspector.h"
10-
#include "src/inspector/protocol/Forward.h"
1110

1211
using namespace v8_inspector;
1312

@@ -28,7 +27,7 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe
2827
void flushProtocolNotifications() override;
2928

3029
static void sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
31-
static void consoleLogCallback(v8::Isolate* isolate, const std::string& message, const std::string& logLevel);
30+
static void consoleLogCallback(v8::Isolate* isolate, ConsoleAPIType method, const std::vector<v8::Local<v8::Value>>& args);
3231

3332
// Overrides of V8InspectorClient
3433
void runMessageLoopOnPause(int context_group_id) override;

test-app/runtime/src/main/cpp/LogAgentImpl.cpp

Lines changed: 0 additions & 90 deletions
This file was deleted.

test-app/runtime/src/main/cpp/LogAgentImpl.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

test-app/runtime/src/main/cpp/console/Console.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
#include <chrono>
1010
#include <iomanip>
1111
#include <sstream>
12+
#include <string>
13+
#include <vector>
1214
#include <V8GlobalHelpers.h>
1315
#include <NativeScriptException.h>
16+
17+
#include "ArgConverter.h"
1418
#include "Console.h"
1519

1620
namespace tns {
@@ -66,10 +70,27 @@ void Console::sendToADBLogcat(const std::string& message, android_LogPriority lo
6670
}
6771
}
6872

69-
void Console::sendToDevToolsFrontEnd(v8::Isolate* isolate, const std::string& message, const std::string& logLevel) {
70-
if (m_callback != nullptr) {
71-
m_callback(isolate, message, logLevel);
73+
void Console::sendToDevToolsFrontEnd(v8::Isolate* isolate, ConsoleAPIType method, const v8::FunctionCallbackInfo<v8::Value>& args) {
74+
if (!m_callback) {
75+
return;
7276
}
77+
78+
std::vector<v8::Local<v8::Value>> arg_vector;
79+
unsigned nargs = args.Length();
80+
arg_vector.reserve(nargs);
81+
for (unsigned ix = 0; ix < nargs; ix++)
82+
arg_vector.push_back(args[ix]);
83+
84+
m_callback(isolate, method, arg_vector);
85+
}
86+
87+
void Console::sendToDevToolsFrontEnd(v8::Isolate* isolate, ConsoleAPIType method, const std::string& message) {
88+
if (!m_callback) {
89+
return;
90+
}
91+
92+
std::vector<v8::Local<v8::Value>> args{ArgConverter::ConvertToV8String(isolate, message)};
93+
m_callback(isolate, method, args);
7394
}
7495

7596
std::string transformJSObject(v8::Isolate* isolate, v8::Local<v8::Object> object) {
@@ -170,7 +191,7 @@ void Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
170191

171192
std::string log = assertionError.str();
172193
sendToADBLogcat(log, ANDROID_LOG_ERROR);
173-
sendToDevToolsFrontEnd(isolate, log, "error");
194+
sendToDevToolsFrontEnd(isolate, ConsoleAPIType::kAssert, info);
174195
}
175196
} catch (NativeScriptException& e) {
176197
e.ReThrowToV8();
@@ -191,7 +212,7 @@ void Console::errorCallback(const v8::FunctionCallbackInfo <v8::Value>& info) {
191212
log += buildLogString(info);
192213

193214
sendToADBLogcat(log, ANDROID_LOG_ERROR);
194-
sendToDevToolsFrontEnd(info.GetIsolate(), log, "error");
215+
sendToDevToolsFrontEnd(info.GetIsolate(), ConsoleAPIType::kError, info);
195216
} catch (NativeScriptException& e) {
196217
e.ReThrowToV8();
197218
} catch (std::exception e) {
@@ -211,7 +232,7 @@ void Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
211232
log += buildLogString(info);
212233

213234
sendToADBLogcat(log, ANDROID_LOG_INFO);
214-
sendToDevToolsFrontEnd(info.GetIsolate(), log, "info");
235+
sendToDevToolsFrontEnd(info.GetIsolate(), ConsoleAPIType::kInfo, info);
215236
} catch (NativeScriptException& e) {
216237
e.ReThrowToV8();
217238
} catch (std::exception e) {
@@ -231,7 +252,7 @@ void Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
231252
log += buildLogString(info);
232253

233254
sendToADBLogcat(log, ANDROID_LOG_INFO);
234-
sendToDevToolsFrontEnd(info.GetIsolate(), log, "info");
255+
sendToDevToolsFrontEnd(info.GetIsolate(), ConsoleAPIType::kLog, info);
235256
} catch (NativeScriptException& e) {
236257
e.ReThrowToV8();
237258
} catch (std::exception e) {
@@ -251,7 +272,7 @@ void Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
251272
log += buildLogString(info);
252273

253274
sendToADBLogcat(log, ANDROID_LOG_WARN);
254-
sendToDevToolsFrontEnd(info.GetIsolate(), log, "warning");
275+
sendToDevToolsFrontEnd(info.GetIsolate(), ConsoleAPIType::kWarning, info);
255276
} catch (NativeScriptException& e) {
256277
e.ReThrowToV8();
257278
} catch (std::exception e) {
@@ -325,7 +346,7 @@ void Console::dirCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
325346
std::string log = ss.str();
326347

327348
sendToADBLogcat(log, ANDROID_LOG_INFO);
328-
sendToDevToolsFrontEnd(isolate, log, "info");
349+
sendToDevToolsFrontEnd(isolate, ConsoleAPIType::kDir, info);
329350
} catch (NativeScriptException& e) {
330351
e.ReThrowToV8();
331352
} catch (std::exception e) {
@@ -406,7 +427,7 @@ void Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
406427

407428
std::string log = ss.str();
408429
__android_log_write(ANDROID_LOG_ERROR, LOG_TAG, log.c_str());
409-
sendToDevToolsFrontEnd(isolate, log, "error");
430+
sendToDevToolsFrontEnd(isolate, ConsoleAPIType::kTrace, info);
410431
} catch (NativeScriptException& e) {
411432
e.ReThrowToV8();
412433
} catch (std::exception e) {
@@ -480,7 +501,7 @@ void Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
480501
std::string warning = std::string("No such label '" + label + "' for console.timeEnd()");
481502

482503
__android_log_write(ANDROID_LOG_WARN, LOG_TAG, warning.c_str());
483-
sendToDevToolsFrontEnd(isolate, warning, "warning");
504+
sendToDevToolsFrontEnd(isolate, ConsoleAPIType::kWarning, warning);
484505

485506
return;
486507
}
@@ -499,7 +520,7 @@ void Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
499520
std::string log = ss.str();
500521

501522
__android_log_write(ANDROID_LOG_INFO, LOG_TAG, log.c_str());
502-
sendToDevToolsFrontEnd(isolate, log, "info");
523+
sendToDevToolsFrontEnd(isolate, ConsoleAPIType::kTimeEnd, log);
503524
} catch (NativeScriptException& e) {
504525
e.ReThrowToV8();
505526
} catch (std::exception e) {

test-app/runtime/src/main/cpp/console/Console.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
#include <include/v8.h>
99
#include <string>
10+
#include <vector>
1011
#include <ArgConverter.h>
1112
#include <android/log.h>
1213

14+
#include <src/inspector/v8-console-message.h>
15+
1316
namespace tns {
1417

15-
typedef void (*ConsoleCallback)(v8::Isolate* isolate, const std::string& message, const std::string& logLevel);
18+
typedef void (*ConsoleCallback)(v8::Isolate* isolate, v8_inspector::ConsoleAPIType method, const std::vector<v8::Local<v8::Value>>& args);
1619

1720
class Console {
1821
public:
@@ -31,6 +34,8 @@ class Console {
3134
static void onDisposeIsolate(v8::Isolate* isolate);
3235

3336
private:
37+
using ConsoleAPIType = v8_inspector::ConsoleAPIType;
38+
3439
static int m_maxLogcatObjectSize;
3540
static ConsoleCallback m_callback;
3641
static const char* LOG_TAG;
@@ -54,7 +59,8 @@ class Console {
5459
}
5560

5661
static void sendToADBLogcat(const std::string& log, android_LogPriority logPriority);
57-
static void sendToDevToolsFrontEnd(v8::Isolate* isolate, const std::string& message, const std::string& logLevel);
62+
static void sendToDevToolsFrontEnd(v8::Isolate* isolate, ConsoleAPIType method, const v8::FunctionCallbackInfo<v8::Value>& args);
63+
static void sendToDevToolsFrontEnd(v8::Isolate* isolate, ConsoleAPIType method, const std::string& args);
5864
};
5965

6066
}

0 commit comments

Comments
 (0)