diff options
author | Rakhi Sharma <atbrakhi@gmail.com> | 2023-04-29 12:40:25 +0200 |
---|---|---|
committer | Martin Robinson <mrobinson@igalia.com> | 2023-05-02 17:02:02 +0200 |
commit | 7d7abef602f943ba8035cf8c3e3f5eb4c13124ef (patch) | |
tree | 855b5d5fbc51b689bd3304b4dc36dd2d32d228d2 | |
parent | 06873da56625640f252814707befa96bf130dad4 (diff) | |
download | servo-7d7abef602f943ba8035cf8c3e3f5eb4c13124ef.tar.gz servo-7d7abef602f943ba8035cf8c3e3f5eb4c13124ef.zip |
Move logers into seperate logger files
Constellation seems to have lots of code that could be moved into
seperate files. `FromScriptLogger` and `FromCompositorLogger` seem
to qualify for that.
Moved FromScriptLogger and FromScriptLogger to logger file.
-rw-r--r-- | components/constellation/constellation.rs | 110 | ||||
-rw-r--r-- | components/constellation/lib.rs | 6 | ||||
-rw-r--r-- | components/constellation/logging.rs | 126 | ||||
-rw-r--r-- | docs/HACKING_QUICKSTART.md | 2 |
4 files changed, 130 insertions, 114 deletions
diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index 2d30004746e..88c0af2e202 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -102,7 +102,6 @@ use crate::session_history::{ }; use crate::timer_scheduler::TimerScheduler; use background_hang_monitor::HangMonitorRegister; -use backtrace::Backtrace; use bluetooth_traits::BluetoothRequest; use canvas_traits::canvas::{CanvasId, CanvasMsg}; use canvas_traits::webgl::WebGLThreads; @@ -127,7 +126,6 @@ use ipc_channel::Error as IpcError; use keyboard_types::webdriver::Event as WebDriverInputEvent; use keyboard_types::KeyboardEvent; use layout_traits::LayoutThreadFactory; -use log::{Level, LevelFilter, Log, Metadata, Record}; use media::{GLPlayerThreads, WindowGLContext}; use msg::constellation_msg::{ BackgroundHangMonitorControlMsg, BackgroundHangMonitorRegister, HangMonitorAlert, @@ -168,7 +166,6 @@ use script_traits::{SWManagerMsg, SWManagerSenders, UpdatePipelineIdReason, WebD use serde::{Deserialize, Serialize}; use servo_config::{opts, pref}; use servo_rand::{random, Rng, ServoRng, SliceRandom}; -use servo_remutex::ReentrantMutex; use servo_url::{Host, ImmutableOrigin, ServoUrl}; use std::borrow::{Cow, ToOwned}; use std::collections::hash_map::Entry; @@ -613,113 +610,6 @@ enum ExitPipelineMode { Force, } -/// The constellation uses logging to perform crash reporting. -/// The constellation receives all `warn!`, `error!` and `panic!` messages, -/// and generates a crash report when it receives a panic. - -/// A logger directed at the constellation from content processes -#[derive(Clone)] -pub struct FromScriptLogger { - /// A channel to the constellation - pub script_to_constellation_chan: Arc<ReentrantMutex<ScriptToConstellationChan>>, -} - -impl FromScriptLogger { - /// Create a new constellation logger. - pub fn new(script_to_constellation_chan: ScriptToConstellationChan) -> FromScriptLogger { - FromScriptLogger { - script_to_constellation_chan: Arc::new(ReentrantMutex::new( - script_to_constellation_chan, - )), - } - } - - /// The maximum log level the constellation logger is interested in. - pub fn filter(&self) -> LevelFilter { - LevelFilter::Warn - } -} - -impl Log for FromScriptLogger { - fn enabled(&self, metadata: &Metadata) -> bool { - metadata.level() <= Level::Warn - } - - fn log(&self, record: &Record) { - if let Some(entry) = log_entry(record) { - debug!("Sending log entry {:?}.", entry); - let thread_name = thread::current().name().map(ToOwned::to_owned); - let msg = FromScriptMsg::LogEntry(thread_name, entry); - let chan = self - .script_to_constellation_chan - .lock() - .unwrap_or_else(|err| err.into_inner()); - let _ = chan.send(msg); - } - } - - fn flush(&self) {} -} - -/// A logger directed at the constellation from the compositor -#[derive(Clone)] -pub struct FromCompositorLogger { - /// A channel to the constellation - pub constellation_chan: Arc<ReentrantMutex<Sender<FromCompositorMsg>>>, -} - -impl FromCompositorLogger { - /// Create a new constellation logger. - pub fn new(constellation_chan: Sender<FromCompositorMsg>) -> FromCompositorLogger { - FromCompositorLogger { - constellation_chan: Arc::new(ReentrantMutex::new(constellation_chan)), - } - } - - /// The maximum log level the constellation logger is interested in. - pub fn filter(&self) -> LevelFilter { - LevelFilter::Warn - } -} - -impl Log for FromCompositorLogger { - fn enabled(&self, metadata: &Metadata) -> bool { - metadata.level() <= Level::Warn - } - - fn log(&self, record: &Record) { - if let Some(entry) = log_entry(record) { - debug!("Sending log entry {:?}.", entry); - let top_level_id = TopLevelBrowsingContextId::installed(); - let thread_name = thread::current().name().map(ToOwned::to_owned); - let msg = FromCompositorMsg::LogEntry(top_level_id, thread_name, entry); - let chan = self - .constellation_chan - .lock() - .unwrap_or_else(|err| err.into_inner()); - let _ = chan.send(msg); - } - } - - fn flush(&self) {} -} - -/// Rust uses `Record` for storing logging, but servo converts that to -/// a `LogEntry`. We do this so that we can record panics as well as log -/// messages, and because `Record` does not implement serde (de)serialization, -/// so cannot be used over an IPC channel. -fn log_entry(record: &Record) -> Option<LogEntry> { - match record.level() { - Level::Error if thread::panicking() => Some(LogEntry::Panic( - format!("{}", record.args()), - format!("{:?}", Backtrace::new()), - )), - Level::Error => Some(LogEntry::Error(format!("{}", record.args()))), - Level::Warn => Some(LogEntry::Warn(format!("{}", record.args()))), - _ => None, - } -} - /// The number of warnings to include in each crash report. const WARNINGS_BUFFER_SIZE: usize = 32; diff --git a/components/constellation/lib.rs b/components/constellation/lib.rs index 92495c50cd9..837c7235169 100644 --- a/components/constellation/lib.rs +++ b/components/constellation/lib.rs @@ -14,6 +14,7 @@ extern crate serde; mod browsingcontext; mod constellation; mod event_loop; +mod logging; mod network_listener; mod pipeline; mod sandboxing; @@ -21,8 +22,7 @@ mod serviceworker; mod session_history; mod timer_scheduler; -pub use crate::constellation::{ - Constellation, FromCompositorLogger, FromScriptLogger, InitialConstellationState, -}; +pub use crate::constellation::{Constellation, InitialConstellationState}; +pub use crate::logging::{FromCompositorLogger, FromScriptLogger}; pub use crate::pipeline::UnprivilegedPipelineContent; pub use crate::sandboxing::{content_process_sandbox_profile, UnprivilegedContent}; diff --git a/components/constellation/logging.rs b/components/constellation/logging.rs new file mode 100644 index 00000000000..c267565658f --- /dev/null +++ b/components/constellation/logging.rs @@ -0,0 +1,126 @@ +/* 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/. */ + +use backtrace::Backtrace; +use compositing::ConstellationMsg as FromCompositorMsg; +use crossbeam_channel::Sender; +use log::{Level, LevelFilter, Log, Metadata, Record}; +use msg::constellation_msg::TopLevelBrowsingContextId; +use script_traits::{LogEntry, ScriptMsg as FromScriptMsg, ScriptToConstellationChan}; +use servo_remutex::ReentrantMutex; +use std::borrow::ToOwned; +use std::sync::Arc; +use std::thread; + +/// The constellation uses logging to perform crash reporting. +/// The constellation receives all `warn!`, `error!` and `panic!` messages, +/// and generates a crash report when it receives a panic. + +/// A logger directed at the constellation from content processes +/// #[derive(Clone)] +pub struct FromScriptLogger { + /// A channel to the constellation + pub script_to_constellation_chan: Arc<ReentrantMutex<ScriptToConstellationChan>>, +} + +/// The constellation uses logging to perform crash reporting. +/// The constellation receives all `warn!`, `error!` and `panic!` messages, +/// and generates a crash report when it receives a panic. + +/// A logger directed at the constellation from content processes +impl FromScriptLogger { + /// Create a new constellation logger. + pub fn new(script_to_constellation_chan: ScriptToConstellationChan) -> FromScriptLogger { + FromScriptLogger { + script_to_constellation_chan: Arc::new(ReentrantMutex::new( + script_to_constellation_chan, + )), + } + } + + /// The maximum log level the constellation logger is interested in. + pub fn filter(&self) -> LevelFilter { + LevelFilter::Warn + } +} + +impl Log for FromScriptLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= Level::Warn + } + + fn log(&self, record: &Record) { + if let Some(entry) = log_entry(record) { + debug!("Sending log entry {:?}.", entry); + let thread_name = thread::current().name().map(ToOwned::to_owned); + let msg = FromScriptMsg::LogEntry(thread_name, entry); + let chan = self + .script_to_constellation_chan + .lock() + .unwrap_or_else(|err| err.into_inner()); + let _ = chan.send(msg); + } + } + + fn flush(&self) {} +} + +/// A logger directed at the constellation from the compositor +#[derive(Clone)] +pub struct FromCompositorLogger { + /// A channel to the constellation + pub constellation_chan: Arc<ReentrantMutex<Sender<FromCompositorMsg>>>, +} + +impl FromCompositorLogger { + /// Create a new constellation logger. + pub fn new(constellation_chan: Sender<FromCompositorMsg>) -> FromCompositorLogger { + FromCompositorLogger { + constellation_chan: Arc::new(ReentrantMutex::new(constellation_chan)), + } + } + + /// The maximum log level the constellation logger is interested in. + pub fn filter(&self) -> LevelFilter { + LevelFilter::Warn + } +} + +impl Log for FromCompositorLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= Level::Warn + } + + fn log(&self, record: &Record) { + if let Some(entry) = log_entry(record) { + debug!("Sending log entry {:?}.", entry); + let top_level_id = TopLevelBrowsingContextId::installed(); + let thread_name = thread::current().name().map(ToOwned::to_owned); + let msg = FromCompositorMsg::LogEntry(top_level_id, thread_name, entry); + let chan = self + .constellation_chan + .lock() + .unwrap_or_else(|err| err.into_inner()); + let _ = chan.send(msg); + } + } + + fn flush(&self) {} +} + +/// Rust uses `Record` for storing logging, but servo converts that to +/// a `LogEntry`. We do this so that we can record panics as well as log +/// messages, and because `Record` does not implement serde (de)serialization, +/// so cannot be used over an IPC channel. +fn log_entry(record: &Record) -> Option<LogEntry> { + match record.level() { + Level::Error if thread::panicking() => Some(LogEntry::Panic( + format!("{}", record.args()), + format!("{:?}", Backtrace::new()), + )), + Level::Error => Some(LogEntry::Error(format!("{}", record.args()))), + Level::Warn => Some(LogEntry::Warn(format!("{}", record.args()))), + _ => None, + } +} diff --git a/docs/HACKING_QUICKSTART.md b/docs/HACKING_QUICKSTART.md index 3dce59cd575..ac8cf3e00fa 100644 --- a/docs/HACKING_QUICKSTART.md +++ b/docs/HACKING_QUICKSTART.md @@ -240,7 +240,7 @@ Using `RUST_LOG="debug"` is usually the very first thing you might want to do if RUST_LOG="debug" ./mach run -d -- -i -y 1 /tmp/a.html 2>&1 | ts -s "%.S: " | tee /tmp/log.txt ``` -You can filter by crate or module, for example `RUST_LOG="layout::inline=debug" ./mach run …`. Check the [env_logger](https://doc.rust-lang.org/log/env_logger/index.html) documentation for more details. +You can filter by crate or module, for example `RUST_LOG="layout::inline=debug" ./mach run …`. Check the [env_logger](https://docs.rs/env_logger) documentation for more details. Use `RUST_BACKTRACE=1` to dump the backtrace when Servo panics. |