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.
This commit is contained in:
Rakhi Sharma 2023-04-29 12:40:25 +02:00 committed by Martin Robinson
parent 06873da566
commit 7d7abef602
4 changed files with 130 additions and 114 deletions

View file

@ -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;

View file

@ -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};

View file

@ -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,
}
}

View file

@ -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.