diff --git a/Cargo.toml b/Cargo.toml index 4b97d69..97dde94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ default-members = [".", "members/libpt-core"] [workspace.package] publish = true -version = "0.3.11" +version = "0.3.12" edition = "2021" authors = ["Christoph J. Scherr "] license = "MIT" diff --git a/members/libpt-core/src/printing.rs b/members/libpt-core/src/printing.rs index 1c2d465..808de0b 100644 --- a/members/libpt-core/src/printing.rs +++ b/members/libpt-core/src/printing.rs @@ -1,21 +1,11 @@ //! # tools that make printing stuff better -// reimport our macros to this module, so the user does not get confused when importing the macros -pub use crate::divider; -pub use crate::print_divider; - /// Quickly get a one line visual divider -#[macro_export] -macro_rules! divider { - () => {{ - format!("{:=^80}", "=") - }}; +pub fn divider() -> String { + format!("{:=^80}", "=") } /// Quickly print a one line visual divider -#[macro_export] -macro_rules! print_divider { - () => {{ - println!("{}", divider!()) - }}; +pub fn print_divider() { + println!("{:=^80}", "=") } diff --git a/members/libpt-log/src/lib.rs b/members/libpt-log/src/lib.rs index 0bfb5c3..1a06b08 100644 --- a/members/libpt-log/src/lib.rs +++ b/members/libpt-log/src/lib.rs @@ -45,19 +45,13 @@ pub struct Logger; /// ## Main implementation impl Logger { - /// ## create a `Logger` - /// - /// Creates a new uninitialized [`Logger`] object. - pub fn new() -> Self { - Logger {} - } /// ## initializes the logger /// /// Will enable the logger to be used. /// /// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control - pub fn init(log_dir: Option, max_level: Option, uptime: bool) -> Result<()> { - Self::init_customized( + pub fn build(log_dir: Option, max_level: Option, uptime: bool) -> Result { + Self::build_customized( log_dir.is_some(), log_dir.unwrap_or(PathBuf::from(DEFAULT_LOG_DIR)), true, @@ -80,8 +74,8 @@ impl Logger { /// useful in cases with only one sender to the logging framework. /// /// Assumes some defaults, use [`init_customized`](Self::init_customized) for more control - pub fn init_mini(max_level: Option) -> Result<()> { - Self::init_customized( + pub fn build_mini(max_level: Option) -> Result { + Self::build_customized( false, PathBuf::from(DEFAULT_LOG_DIR), true, @@ -103,7 +97,7 @@ impl Logger { /// ## initializes the logger /// /// Will enable the logger to be used. - pub fn init_customized( + pub fn build_customized( log_to_file: bool, log_dir: PathBuf, ansi: bool, @@ -117,7 +111,7 @@ impl Logger { pretty: bool, show_time: bool, uptime: bool, // uptime instead of system time - ) -> Result<()> { + ) -> Result { // only init if no init has been performed yet if INITIALIZED.load(Ordering::Relaxed) { warn!("trying to reinitialize the logger, ignoring"); @@ -205,7 +199,7 @@ impl Logger { } } INITIALIZED.store(true, Ordering::Relaxed); - Ok(()) + Ok(Logger {}) } /// ## logging at [`Level::ERROR`] @@ -245,12 +239,6 @@ impl Logger { } } -impl Default for Logger { - fn default() -> Self { - Self::new() - } -} - impl fmt::Debug for Logger { /// ## DEBUG representation for [`Logger`] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/members/libpt-net/src/monitoring/uptime.rs b/members/libpt-net/src/monitoring/uptime.rs index 590d252..ad9259e 100644 --- a/members/libpt-net/src/monitoring/uptime.rs +++ b/members/libpt-net/src/monitoring/uptime.rs @@ -29,7 +29,7 @@ use std::time::SystemTime; use serde::{Deserialize, Serialize}; use serde_json; -use libpt_core::divider; +use libpt_core::printing::divider; // TYPES ///////////////////////////////////////////////////////////////////////////////////////// @@ -245,7 +245,7 @@ fn display_uptime_status( match_format_duration_since(last_uptime) ); debug!("\n{}", status); - info!("{}", divider!()); + info!("{}", divider()); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/members/libpt-py/Cargo.toml b/members/libpt-py/Cargo.toml index fca6d7b..1e97a36 100644 --- a/members/libpt-py/Cargo.toml +++ b/members/libpt-py/Cargo.toml @@ -12,7 +12,8 @@ crate-type = ["cdylib", "rlib"] [dependencies] libpt = { version = "0.3.11", path = "../.." } -pyo3 = "0.19.0" +pyo3 = { version = "0.19.0", features = ["full"] } +anyhow.workspace = true [features] default = ["log", "core", "full"] diff --git a/members/libpt-py/src/core/mod.rs b/members/libpt-py/src/core/mod.rs index 039806d..901d86f 100644 --- a/members/libpt-py/src/core/mod.rs +++ b/members/libpt-py/src/core/mod.rs @@ -1,11 +1,11 @@ use pyo3::prelude::*; +mod printing; + /// implement a python module in Rust -#[pymodule] -#[pyo3(name = "core")] -pub fn submodule(py: Python, m: &PyModule) -> PyResult<()> { - let submodule = PyModule::new(py, "submodule")?; - submodule.add("super_useful_constant", "important")?; - m.add_submodule(m)?; +pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> { + let module = PyModule::new(py, "core")?; + printing::submodule(py, module)?; + parent.add_submodule(module)?; Ok(()) } diff --git a/members/libpt-py/src/core/printing.rs b/members/libpt-py/src/core/printing.rs new file mode 100644 index 0000000..146451c --- /dev/null +++ b/members/libpt-py/src/core/printing.rs @@ -0,0 +1,24 @@ +use pyo3::prelude::*; + +use libpt::core::printing as origin; + +/// Quickly get a one line visual divider +#[pyfunction] +pub fn divider() -> String { + origin::divider() +} + +/// Quickly print a one line visual divider +#[pyfunction] +pub fn print_divider() { + origin::print_divider() +} + +/// implement a python module in Rust +pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> { + let module = PyModule::new(py, "printing")?; + module.add_function(wrap_pyfunction!(divider, module)?)?; + module.add_function(wrap_pyfunction!(print_divider, module)?)?; + parent.add_submodule(module)?; + Ok(()) +} diff --git a/members/libpt-py/src/lib.rs b/members/libpt-py/src/lib.rs index cc2445b..c595ba1 100644 --- a/members/libpt-py/src/lib.rs +++ b/members/libpt-py/src/lib.rs @@ -1,8 +1,9 @@ //! Python bindings for [`libpt`](libpt) -use libpt; #[cfg(feature = "core")] mod core; +#[cfg(feature = "log")] +mod log; use pyo3::prelude::*; @@ -19,5 +20,6 @@ fn libpt_py(py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(version, m)?)?; #[cfg(feature = "core")] core::submodule(py, m)?; + log::submodule(py, m)?; Ok(()) } diff --git a/members/libpt-py/src/log/mod.rs b/members/libpt-py/src/log/mod.rs index e69de29..ff47c92 100644 --- a/members/libpt-py/src/log/mod.rs +++ b/members/libpt-py/src/log/mod.rs @@ -0,0 +1,81 @@ +use std::path::PathBuf; + +use pyo3::prelude::*; + +use libpt::log as origin; + +#[derive(Clone)] +#[pyclass] +pub enum Level { + Error, + Warn, + Info, + Debug, + Trace, +} + +impl From for origin::Level { + fn from(value: Level) -> Self { + match value { + Level::Error => origin::Level::ERROR, + Level::Warn => origin::Level::WARN, + Level::Info => origin::Level::INFO, + Level::Debug => origin::Level::DEBUG, + Level::Trace => origin::Level::TRACE, + } + } +} + +#[pyclass] +pub struct Logger { + inner: origin::Logger, +} + +impl From for Logger { + fn from(inner: origin::Logger) -> Self { + Self { inner } + } +} + +#[pymethods] +impl Logger { + #[new] + pub fn build( + log_dir: Option, + max_level: Option, + uptime: Option, + ) -> anyhow::Result { + // concert our wrapper type + let max_level = max_level.map(origin::Level::from); + Ok(origin::Logger::build(log_dir, max_level, uptime.unwrap_or(false))?.into()) + } + + /// ## logging at [`Level::ERROR`] + pub fn error(&self, printable: String) { + self.inner.error(printable) + } + /// ## logging at [`Level::WARN`] + pub fn warn(&self, printable: String) { + self.inner.warn(printable) + } + /// ## logging at [`Level::INFO`] + pub fn info(&self, printable: String) { + self.inner.info(printable) + } + /// ## logging at [`Level::DEBUG`] + pub fn debug(&self, printable: String) { + self.inner.debug(printable) + } + /// ## logging at [`Level::StringRACE`] + pub fn trace(&self, printable: String) { + self.inner.trace(printable) + } +} + +/// implement a python module in Rust +pub fn submodule(py: Python, parent: &PyModule) -> PyResult<()> { + let module = PyModule::new(py, "log")?; + module.add_class::()?; + parent.add_submodule(module)?; + Ok(()) +}