Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_logger"
version = "0.6.3"
version = "0.7.0"
edition = "2021"
authors = ["Jacob <RegenJacob@gmx.de>"]
license = "MIT"
Expand All @@ -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"
Expand Down
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![doc = include_str!("../README.md")]
mod ui;

use std::collections::{HashMap, VecDeque};
use std::sync::LazyLock;
use std::sync::Mutex;

use hashbrown::HashMap;
pub use ui::logger_ui;
pub use ui::LoggerUi;

Expand All @@ -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.
Expand All @@ -42,7 +50,7 @@ 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
Expand All @@ -59,19 +67,20 @@ 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_boxed_logger(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) {
if self.enabled(record.metadata()) {
if let Ok(ref mut logger) = LOGGER.lock() {
logger.logs.push(Record {
logger.logs.push_back(Record {
level: record.level(),
message: record.args().to_string(),
target: record.target().to_string(),
Expand Down Expand Up @@ -123,14 +132,14 @@ struct Record {
}

struct Logger {
logs: Vec<Record>,
logs: VecDeque<Record>,
categories: HashMap<String, bool>,
max_category_length: usize,
start_time: chrono::DateTime<chrono::Local>,
}
static LOGGER: LazyLock<Mutex<Logger>> = LazyLock::new(|| {
Mutex::new(Logger {
logs: Vec::new(),
logs: VecDeque::new(),
categories: HashMap::new(),
max_category_length: 0,
start_time: chrono::Local::now(),
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl LoggerUi {

let mut logs_displayed: usize = 0;

let time_padding = logger.logs.last().map_or(0, |record| {
let time_padding = logger.logs.back().map_or(0, |record| {
format_time(record.time, &self.style, logger.start_time).len()
});

Expand Down
Loading