Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add configurable logger feature #467

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions cursive-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ atty = "0.2"
[features]
markdown = ["pulldown-cmark"]
unstable_scroll = []
configurable-logger = []

[lib]
name = "cursive_core"
80 changes: 79 additions & 1 deletion cursive-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,41 @@ use std::sync::Mutex;
/// Saves all log records in a global deque.
///
/// Uses a `DebugView` to access it.
#[cfg(not(feature = "configurable-logger"))]
pub struct CursiveLogger;

/// Saves all log records in a global deque.
///
/// Uses a `DebugView` to access it.
#[cfg(feature = "configurable-logger")]
pub struct CursiveLogger {
level: log::Level,
}

#[cfg(not(feature = "configurable-logger"))]
impl Default for CursiveLogger {
fn default() -> CursiveLogger {
CursiveLogger
}
}

#[cfg(feature = "configurable-logger")]
impl Default for CursiveLogger {
fn default() -> CursiveLogger {
CursiveLogger {
level: log::Level::Debug,
}
}
}

#[cfg(not(feature = "configurable-logger"))]
static LOGGER: CursiveLogger = CursiveLogger;

#[cfg(feature = "configurable-logger")]
static LOGGER: CursiveLogger = CursiveLogger {
level: log::Level::Debug,
};

/// A log record.
pub struct Record {
/// Log level used for this record
Expand Down Expand Up @@ -43,6 +74,7 @@ pub fn log(record: &log::Record<'_>) {
});
}

#[cfg(not(feature = "configurable-logger"))]
impl log::Log for CursiveLogger {
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
true
Expand All @@ -55,6 +87,19 @@ impl log::Log for CursiveLogger {
fn flush(&self) {}
}

#[cfg(feature = "configurable-logger")]
impl log::Log for CursiveLogger {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
metadata.level() <= self.level
}

fn log(&self, record: &log::Record<'_>) {
log(record);
}

fn flush(&self) {}
}

/// Initialize the Cursive logger.
///
/// Make sure this is the only logger your are using.
Expand All @@ -72,14 +117,47 @@ pub fn init() {
log::set_max_level(log::LevelFilter::Trace);
}

/// Builder helper type for building a logger
///
/// Use Builder::default() to build a default-configured Builder object, which
///
/// * Logs with "debug" level
#[cfg(feature = "configurable-logger")]
pub struct Builder {
level: log::Level,
}

#[cfg(feature = "configurable-logger")]
impl Default for Builder {
fn default() -> Builder {
Builder {
level: log::Level::Debug,
}
}
}

#[cfg(feature = "configurable-logger")]
impl Builder {
/// initialize the logger
pub fn init(self) {
unimplemented!()
}

/// Set the level to log with
pub fn level(mut self, level: log::Level) -> Self {
self.level = level;
self
}
}

/// Return a logger that stores records in cursive's log queue.
///
/// These logs can then be read by a [`DebugView`](crate::views::DebugView).
///
/// An easier alternative might be to use [`init()`].
pub fn get_logger() -> CursiveLogger {
reserve_logs(1_000);
CursiveLogger
CursiveLogger::default()
}

/// Adds `n` more entries to cursive's log queue.
Expand Down
1 change: 1 addition & 0 deletions cursive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ crossterm-backend = ["crossterm"]
markdown = ["cursive_core/markdown"]
unstable_scroll = ["cursive_core/unstable_scroll"]
toml = ["cursive_core/toml"]
configurable-logger = ["cursive_core/configurable-logger"]

[lib]
name = "cursive"
Expand Down