python interface for core and logging
cargo devel CI / cargo CI (push) Successful in 4m59s Details

This commit is contained in:
Christoph J. Scherr 2024-03-01 20:15:16 +01:00
parent e8be5388b1
commit 10ec77aef0
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
9 changed files with 130 additions and 44 deletions

View File

@ -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 <software@cscherr.de>"]
license = "MIT"

View File

@ -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 {
() => {{
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}", "=")
}

View File

@ -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<PathBuf>, max_level: Option<Level>, uptime: bool) -> Result<()> {
Self::init_customized(
pub fn build(log_dir: Option<PathBuf>, max_level: Option<Level>, uptime: bool) -> Result<Self> {
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<Level>) -> Result<()> {
Self::init_customized(
pub fn build_mini(max_level: Option<Level>) -> Result<Self> {
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<Self> {
// 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 {

View File

@ -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());
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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"]

View File

@ -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(())
}

View File

@ -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(())
}

View File

@ -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(())
}

View File

@ -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<Level> 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<origin::Logger> for Logger {
fn from(inner: origin::Logger) -> Self {
Self { inner }
}
}
#[pymethods]
impl Logger {
#[new]
pub fn build(
log_dir: Option<PathBuf>,
max_level: Option<Level>,
uptime: Option<bool>,
) -> anyhow::Result<Self> {
// 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::<Logger>()?;
parent.add_submodule(module)?;
Ok(())
}