diff --git a/Cargo.toml b/Cargo.toml index a0e1fc5..dbb8b4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "egui_logger" -version = "0.6.3" +version = "0.7.0" edition = "2021" authors = ["Jacob "] license = "MIT" @@ -16,7 +16,6 @@ include = ["src/*.rs", "Cargo.toml", "LICENSE"] log = "0.4" egui = "0.31" regex = "1.11" -hashbrown = "0.15" [dev-dependencies] eframe = "0.31" diff --git a/src/lib.rs b/src/lib.rs index 8697f8e..44c0391 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ #![doc = include_str!("../README.md")] mod ui; +use std::collections::HashMap; use std::sync::LazyLock; use std::sync::Mutex; -use hashbrown::HashMap; pub use ui::logger_ui; pub use ui::LoggerUi; @@ -21,7 +21,15 @@ const LEVELS: [log::Level; log::Level::Trace as usize] = [ /// The logger for egui /// You might want to use [`builder()`] instead. /// To get a builder with default values. -pub struct EguiLogger; +pub struct EguiLogger { + max_level: log::LevelFilter, +} + +impl EguiLogger { + fn new(max_level: log::LevelFilter) -> Self { + Self { max_level } + } +} /// The builder for the logger. /// You can use [`builder()`] to get an instance of this. @@ -42,11 +50,11 @@ impl Builder { /// Useful if you want to add it to a multi-logger. /// See [here](https://github.com/RegenJacob/egui_logger/blob/main/examples/multi_log.rs) for an example. pub fn build(self) -> EguiLogger { - EguiLogger + EguiLogger::new(self.max_level) } /// Sets the max level for the logger - /// this only has an effect when calling [`init()`]. + /// this only has an effect when calling [init](Self::init). /// /// Defaults to [Debug](`log::LevelFilter::Debug`). pub fn max_level(mut self, max_level: log::LevelFilter) -> Self { @@ -59,13 +67,14 @@ impl Builder { /// /// The max level is the [max_level](Self::max_level) field. pub fn init(self) -> Result<(), SetLoggerError> { - log::set_logger(&EguiLogger).map(|()| log::set_max_level(self.max_level)) + log::set_max_level(self.max_level); + log::set_logger(Box::leak(Box::new(self.build()))) } } impl log::Log for EguiLogger { fn enabled(&self, metadata: &log::Metadata) -> bool { - metadata.level() <= log::STATIC_MAX_LEVEL + metadata.level() <= self.max_level } fn log(&self, record: &log::Record) { diff --git a/src/ui.rs b/src/ui.rs index 0a49b21..21f4861 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -267,7 +267,7 @@ impl LoggerUi { // Filter out log levels that are disabled via regex or log level if (!self.search_term.is_empty() && !self.match_string(&raw_text)) - || !(self.loglevels[record.level as usize - 1]) + || !self.loglevels[record.level as usize - 1] { return; } @@ -300,7 +300,7 @@ impl LoggerUi { ui.horizontal(|ui| { ui.label(format!("Log size: {}", logger.logs.len())); ui.label(format!("Displayed: {}", logs_displayed)); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| { if ui.button("Copy").clicked() { let mut out_string = String::new(); logger