Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 15 additions & 6 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;
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,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 {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down