feat: suppress devtools console logging (#49359)

* feat: suppress devtools console logging

Co-authored-by: clavin <clavin@electronjs.org>

* Emit messages as-is in testing builds

Co-authored-by: clavin <clavin@electronjs.org>

* Promote `DCHECK_IS_ON()` to preprocessor check

Co-authored-by: clavin <clavin@electronjs.org>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
This commit is contained in:
trop[bot]
2026-01-20 10:06:35 +01:00
committed by GitHub
parent d4a52baa2f
commit 3138ac5fd2
2 changed files with 35 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
#include "base/base64.h"
#include "base/containers/span.h"
#include "base/dcheck_is_on.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h"
#include "base/strings/pattern.h"
@@ -957,6 +958,35 @@ bool InspectableWebContents::HandleKeyboardEvent(
return !delegate || delegate->HandleKeyboardEvent(source, event);
}
bool InspectableWebContents::DidAddMessageToConsole(
content::WebContents* source,
blink::mojom::ConsoleMessageLevel log_level,
const std::u16string& message,
int32_t line_no,
const std::u16string& source_id) {
// Suppress Chromium's default logging of DevTools frontend console messages
// into native logs for the managed DevTools WebContents. Can be overridden by
// enabling verbose logging.
if (source == managed_devtools_web_contents_.get()) {
#if DCHECK_IS_ON()
// In debug/testing builds, let logging through.
return false;
#endif
if (VLOG_IS_ON(1)) {
// Match Chromium's `content::LogConsoleMessage()` output format, but emit
// it as a verbose log.
logging::LogMessage("CONSOLE", line_no, logging::LOGGING_VERBOSE).stream()
<< "\"" << message << "\", source: " << source_id << " (" << line_no
<< ")";
}
return true; // Suppress the default logging.
}
return false; // Allow the default logging.
}
void InspectableWebContents::CloseContents(content::WebContents* source) {
// This is where the devtools closes itself (by clicking the x button).
CloseDevTools();

View File

@@ -221,6 +221,11 @@ class InspectableWebContents
// content::WebContentsDelegate:
bool HandleKeyboardEvent(content::WebContents*,
const input::NativeWebKeyboardEvent&) override;
bool DidAddMessageToConsole(content::WebContents* source,
blink::mojom::ConsoleMessageLevel log_level,
const std::u16string& message,
int32_t line_no,
const std::u16string& source_id) override;
void CloseContents(content::WebContents* source) override;
std::unique_ptr<content::EyeDropper> OpenEyeDropper(
content::RenderFrameHost* frame,