Skip to content

Commit 2e47770

Browse files
motiz88facebook-github-bot
authored andcommitted
Inject log to easily identify modern CDP backend (#42383)
Summary: Pull Request resolved: #42383 Changelog: [Internal] During development / experimental rollout of the modern CDP backend, it can be helpful to have a user-visible message that makes it clear that the new backend is in use. Here, we add one using the [`Log.entryAdded`](https://chromedevtools.github.io/devtools-protocol/tot/Log/#type-LogEntry) CDP event. We also add some styling using [ANSI escape sequences](https://developer.chrome.com/docs/devtools/console/format-style#style-ansi) to make the message stand out from normal application logs. We could have used [`Runtime.consoleAPICalled`](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#event-consoleAPICalled) instead, but: 1. `Runtime.consoleAPICalled` requires an `executionContextId` which is not available at the `Page` level (it is a concept that's managed closer to the instance/VM), and it's slightly cleaner if we don't have to send a fake context ID. 2. It's slightly easier to follow the CDP dispatching logic / grep for relevant code if we use `Log` for "system logs" (from the Page) and reserve `Runtime` for real application logs from the instance/VM. NOTE: We'll probably want to remove this before the stable release. Reviewed By: huntie Differential Revision: D52894171 fbshipit-source-id: 3208e01f2ee31acef2e8cd58767f40ad724c9a39
1 parent 7886abd commit 2e47770

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

packages/react-native/ReactCommon/jsinspector-modern/PageAgent.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,48 @@
99
#include <folly/json.h>
1010
#include <jsinspector-modern/PageAgent.h>
1111

12+
#include <chrono>
13+
14+
using namespace std::chrono;
15+
using namespace std::literals::string_view_literals;
16+
1217
namespace facebook::react::jsinspector_modern {
1318

19+
#define ANSI_WEIGHT_BOLD "\x1B[1m"
20+
#define ANSI_WEIGHT_RESET "\x1B[22m"
21+
#define ANSI_STYLE_ITALIC "\x1B[3m"
22+
#define ANSI_STYLE_RESET "\x1B[23m"
23+
#define ANSI_COLOR_BG_YELLOW "\x1B[48;2;253;247;231m"
24+
25+
static constexpr auto kModernCDPBackendNotice =
26+
ANSI_COLOR_BG_YELLOW ANSI_WEIGHT_BOLD
27+
"NOTE:" ANSI_WEIGHT_RESET " You are using the " ANSI_STYLE_ITALIC
28+
"modern" ANSI_STYLE_RESET " CDP backend for React Native (PageTarget)."sv;
29+
1430
PageAgent::PageAgent(FrontendChannel frontendChannel)
1531
: frontendChannel_(frontendChannel) {}
1632

1733
void PageAgent::handleRequest(const cdp::PreparsedRequest& req) {
34+
if (req.method == "Log.enable") {
35+
// Send an "OK" response.
36+
frontendChannel_(
37+
folly::toJson(folly::dynamic::object("id", req.id)("result", nullptr)));
38+
39+
// Send a log entry identifying the modern CDP backend.
40+
frontendChannel_(
41+
folly::toJson(folly::dynamic::object("method", "Log.entryAdded")(
42+
"params",
43+
folly::dynamic::object(
44+
"entry",
45+
folly::dynamic::object(
46+
"timestamp",
47+
duration_cast<milliseconds>(
48+
system_clock::now().time_since_epoch())
49+
.count())("source", "other")(
50+
"level", "info")("text", kModernCDPBackendNotice)))));
51+
52+
return;
53+
}
1854
folly::dynamic res = folly::dynamic::object("id", req.id)(
1955
"error",
2056
folly::dynamic::object("code", -32601)(

packages/react-native/ReactCommon/jsinspector-modern/tests/PageTargetTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,25 @@ TEST_F(PageTargetProtocolTest, MalformedJson) {
102102
toPage_->sendMessage("{");
103103
}
104104

105+
TEST_F(PageTargetProtocolTest, InjectLogToIdentifyBackend) {
106+
InSequence s;
107+
108+
EXPECT_CALL(fromPage(), onMessage(JsonEq(R"({
109+
"id": 1,
110+
"result": null
111+
})")))
112+
.RetiresOnSaturation();
113+
114+
EXPECT_CALL(
115+
fromPage(),
116+
onMessage(JsonParsed(AllOf(
117+
AtJsonPtr("/method", "Log.entryAdded"),
118+
AtJsonPtr("/params/entry", Not(IsEmpty()))))))
119+
.RetiresOnSaturation();
120+
toPage_->sendMessage(R"({
121+
"id": 1,
122+
"method": "Log.enable"
123+
})");
124+
}
125+
105126
} // namespace facebook::react::jsinspector_modern

0 commit comments

Comments
 (0)