mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Auto merge of #27088 - jdm:group, r=ferjm
Implement Console grouping APIs. These are used by Hubs and other sites we want to run. --- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #9274 - [x] These changes do not require tests because we can't test stdout content or devtools messages.
This commit is contained in:
commit
cbbbe16936
4 changed files with 76 additions and 59 deletions
|
@ -52,88 +52,83 @@ where
|
|||
f()
|
||||
}
|
||||
|
||||
fn console_messages(global: &GlobalScope, messages: &[DOMString], level: LogLevel) {
|
||||
console_message(global, DOMString::from(messages.join(" ")), level)
|
||||
}
|
||||
|
||||
fn console_message(global: &GlobalScope, message: DOMString, level: LogLevel) {
|
||||
with_stderr_lock(move || {
|
||||
let prefix = global.current_group_label().unwrap_or_default();
|
||||
let message = DOMString::from(format!("{}{}", prefix, message));
|
||||
println!("{}", message);
|
||||
Console::send_to_devtools(global, level, message);
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl Console {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/log
|
||||
pub fn Log(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
for message in messages {
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Log, message);
|
||||
}
|
||||
})
|
||||
console_messages(global, &messages, LogLevel::Log)
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console
|
||||
pub fn Debug(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
for message in messages {
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Debug, message);
|
||||
}
|
||||
})
|
||||
console_messages(global, &messages, LogLevel::Debug)
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/info
|
||||
pub fn Info(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
for message in messages {
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Info, message);
|
||||
}
|
||||
})
|
||||
console_messages(global, &messages, LogLevel::Info)
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/warn
|
||||
pub fn Warn(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
for message in messages {
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Warn, message);
|
||||
}
|
||||
})
|
||||
console_messages(global, &messages, LogLevel::Warn)
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/error
|
||||
pub fn Error(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
for message in messages {
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Error, message);
|
||||
}
|
||||
})
|
||||
console_messages(global, &messages, LogLevel::Error)
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/assert
|
||||
pub fn Assert(global: &GlobalScope, condition: bool, message: Option<DOMString>) {
|
||||
with_stderr_lock(move || {
|
||||
if !condition {
|
||||
let message = message.unwrap_or_else(|| DOMString::from("no message"));
|
||||
println!("Assertion failed: {}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Error, message);
|
||||
}
|
||||
})
|
||||
let message = DOMString::from(format!("Assertion failed: {}", message));
|
||||
console_message(global, message, LogLevel::Error)
|
||||
};
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/time
|
||||
pub fn Time(global: &GlobalScope, label: DOMString) {
|
||||
with_stderr_lock(move || {
|
||||
if let Ok(()) = global.time(label.clone()) {
|
||||
let message = DOMString::from(format!("{}: timer started", label));
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Log, message);
|
||||
console_message(global, message, LogLevel::Log);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd
|
||||
pub fn TimeEnd(global: &GlobalScope, label: DOMString) {
|
||||
with_stderr_lock(move || {
|
||||
if let Ok(delta) = global.time_end(&label) {
|
||||
let message = DOMString::from(format!("{}: {}ms", label, delta));
|
||||
println!("{}", message);
|
||||
Self::send_to_devtools(global, LogLevel::Log, message);
|
||||
};
|
||||
})
|
||||
console_message(global, message, LogLevel::Log);
|
||||
}
|
||||
}
|
||||
|
||||
// https://console.spec.whatwg.org/#group
|
||||
pub fn Group(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
global.push_console_group(DOMString::from(messages.join(" ")));
|
||||
}
|
||||
|
||||
// https://console.spec.whatwg.org/#groupcollapsed
|
||||
pub fn GroupCollapsed(global: &GlobalScope, messages: Vec<DOMString>) {
|
||||
global.push_console_group(DOMString::from(messages.join(" ")));
|
||||
}
|
||||
|
||||
// https://console.spec.whatwg.org/#groupend
|
||||
pub fn GroupEnd(global: &GlobalScope) {
|
||||
global.pop_console_group();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -295,6 +295,9 @@ pub struct GlobalScope {
|
|||
|
||||
/// currect https state (from previous request)
|
||||
https_state: Cell<HttpsState>,
|
||||
|
||||
/// The stack of active group labels for the Console APIs.
|
||||
console_group_stack: DomRefCell<Vec<DOMString>>,
|
||||
}
|
||||
|
||||
/// A wrapper for glue-code between the ipc router and the event-loop.
|
||||
|
@ -744,6 +747,7 @@ impl GlobalScope {
|
|||
gpu_id_hub,
|
||||
frozen_supported_performance_entry_types: DomRefCell::new(Default::default()),
|
||||
https_state: Cell::new(HttpsState::None),
|
||||
console_group_stack: DomRefCell::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2934,6 +2938,21 @@ impl GlobalScope {
|
|||
pub fn wgpu_id_hub(&self) -> Arc<Mutex<Identities>> {
|
||||
self.gpu_id_hub.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn current_group_label(&self) -> Option<DOMString> {
|
||||
self.console_group_stack
|
||||
.borrow()
|
||||
.last()
|
||||
.map(|label| DOMString::from(format!("[{}]", label)))
|
||||
}
|
||||
|
||||
pub(crate) fn push_console_group(&self, group: DOMString) {
|
||||
self.console_group_stack.borrow_mut().push(group);
|
||||
}
|
||||
|
||||
pub(crate) fn pop_console_group(&self) {
|
||||
let _ = self.console_group_stack.borrow_mut().pop();
|
||||
}
|
||||
}
|
||||
|
||||
fn timestamp_in_ms(time: Timespec) -> u64 {
|
||||
|
|
|
@ -1,25 +1,27 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
/*
|
||||
* References:
|
||||
* MDN Docs - https://developer.mozilla.org/en-US/docs/Web/API/console
|
||||
* Draft Spec - https://sideshowbarker.github.io/console-spec/
|
||||
*
|
||||
* © Copyright 2014 Mozilla Foundation.
|
||||
*/
|
||||
|
||||
// https://console.spec.whatwg.org/
|
||||
|
||||
[ClassString="Console",
|
||||
Exposed=(Window,Worker,Worklet),
|
||||
ProtoObjectHack]
|
||||
namespace console {
|
||||
// These should be DOMString message, DOMString message2, ...
|
||||
// Logging
|
||||
void log(DOMString... messages);
|
||||
void debug(DOMString... messages);
|
||||
void info(DOMString... messages);
|
||||
void warn(DOMString... messages);
|
||||
void error(DOMString... messages);
|
||||
void assert(boolean condition, optional DOMString message);
|
||||
|
||||
// Grouping
|
||||
void group(DOMString... data);
|
||||
void groupCollapsed(DOMString... data);
|
||||
void groupEnd();
|
||||
|
||||
// Timing
|
||||
void time(DOMString message);
|
||||
void timeEnd(DOMString message);
|
||||
};
|
||||
|
|
|
@ -90,6 +90,7 @@ WEBIDL_STANDARDS = [
|
|||
b"//drafts.csswg.org",
|
||||
b"//drafts.css-houdini.org",
|
||||
b"//drafts.fxtf.org",
|
||||
b"//console.spec.whatwg.org",
|
||||
b"//encoding.spec.whatwg.org",
|
||||
b"//fetch.spec.whatwg.org",
|
||||
b"//html.spec.whatwg.org",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue