Skip to content

Commit

Permalink
When debugger is attached continue to receive console messages
Browse files Browse the repository at this point in the history
Summary:
I noticed an issue when the hermes debugger was attached. Console messages would no longer appear in the terminal. This is because the hermes inspector overrides the console object with its own to intercept messages and send them to the debug client.
With this change I made it so that messages are sent to the original console as well instead of completely replacing it.

Changelog:
[General][Fixed] - When Hermes debugger is enabled continue to send log messages to the console

Reviewed By: mhorowitz

Differential Revision: D21509895

fbshipit-source-id: 6d91c4b82682e404679533be14b3e5f12e687e79
  • Loading branch information
MartinSherburn authored and facebook-github-bot committed May 13, 2020
1 parent e3f4a7b commit 77ef8f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
52 changes: 35 additions & 17 deletions ReactCommon/hermes/inspector/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Inspector::~Inspector() {

void Inspector::installConsoleFunction(
jsi::Object &console,
std::shared_ptr<jsi::Object> &originalConsole,
const std::string &name,
const std::string &chromeTypeDefault = "") {
jsi::Runtime &rt = adapter_->getRuntime();
Expand All @@ -150,11 +151,22 @@ void Inspector::installConsoleFunction(
rt,
nameID,
1,
[weakInspector, chromeType](
[weakInspector, originalConsole, name, chromeType](
jsi::Runtime &runtime,
const jsi::Value &thisVal,
const jsi::Value *args,
size_t count) {
if (originalConsole) {
auto val = originalConsole->getProperty(runtime, name.c_str());
if (val.isObject()) {
auto obj = val.getObject(runtime);
if (obj.isFunction(runtime)) {
auto func = obj.getFunction(runtime);
func.call(runtime, args, count);
}
}
}

if (auto inspector = weakInspector.lock()) {
jsi::Array argsArray(runtime, count);
for (size_t index = 0; index < count; ++index)
Expand All @@ -170,22 +182,28 @@ void Inspector::installConsoleFunction(
void Inspector::installLogHandler() {
jsi::Runtime &rt = adapter_->getRuntime();
auto console = jsi::Object(rt);
installConsoleFunction(console, "assert");
installConsoleFunction(console, "clear");
installConsoleFunction(console, "debug");
installConsoleFunction(console, "dir");
installConsoleFunction(console, "dirxml");
installConsoleFunction(console, "error");
installConsoleFunction(console, "group", "startGroup");
installConsoleFunction(console, "groupCollapsed", "startGroupCollapsed");
installConsoleFunction(console, "groupEnd", "endGroup");
installConsoleFunction(console, "info");
installConsoleFunction(console, "log");
installConsoleFunction(console, "profile");
installConsoleFunction(console, "profileEnd");
installConsoleFunction(console, "table");
installConsoleFunction(console, "trace");
installConsoleFunction(console, "warn", "warning");
auto val = rt.global().getProperty(rt, "console");
std::shared_ptr<jsi::Object> originalConsole;
if (val.isObject()) {
originalConsole = std::make_shared<jsi::Object>(val.getObject(rt));
}
installConsoleFunction(console, originalConsole, "assert");
installConsoleFunction(console, originalConsole, "clear");
installConsoleFunction(console, originalConsole, "debug");
installConsoleFunction(console, originalConsole, "dir");
installConsoleFunction(console, originalConsole, "dirxml");
installConsoleFunction(console, originalConsole, "error");
installConsoleFunction(console, originalConsole, "group", "startGroup");
installConsoleFunction(
console, originalConsole, "groupCollapsed", "startGroupCollapsed");
installConsoleFunction(console, originalConsole, "groupEnd", "endGroup");
installConsoleFunction(console, originalConsole, "info");
installConsoleFunction(console, originalConsole, "log");
installConsoleFunction(console, originalConsole, "profile");
installConsoleFunction(console, originalConsole, "profileEnd");
installConsoleFunction(console, originalConsole, "table");
installConsoleFunction(console, originalConsole, "trace");
installConsoleFunction(console, originalConsole, "warn", "warning");
rt.global().setProperty(rt, "console", console);
}

Expand Down
1 change: 1 addition & 0 deletions ReactCommon/hermes/inspector/Inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class Inspector : public facebook::hermes::debugger::EventObserver,

void installConsoleFunction(
jsi::Object &console,
std::shared_ptr<jsi::Object> &originalConsole,
const std::string &name,
const std::string &chromeType);

Expand Down

0 comments on commit 77ef8f8

Please sign in to comment.