Skip to main content

workflow_log/
lib.rs

1//!
2//! [<img alt="github" src="https://img.shields.io/badge/github-workflow--rs-8da0cb?style=for-the-badge&labelColor=555555&color=8da0cb&logo=github" height="20">](https://github.com/workflow-rs/workflow-rs)
3//! [<img alt="crates.io" src="https://img.shields.io/crates/v/workflow-log.svg?maxAge=2592000&style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/workflow-log)
4//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-workflow--log-56c2a5?maxAge=2592000&style=for-the-badge&logo=docs.rs" height="20">](https://docs.rs/workflow-log)
5//! <img alt="license" src="https://img.shields.io/crates/l/workflow-log.svg?maxAge=2592000&color=6ac&style=for-the-badge&logoColor=fff" height="20">
6//! <img src="https://img.shields.io/badge/platform- native -informational?style=for-the-badge&color=50a0f0" height="20">
7//! <img src="https://img.shields.io/badge/platform- wasm32/browser -informational?style=for-the-badge&color=50a0f0" height="20">
8//! <img src="https://img.shields.io/badge/platform- wasm32/node.js -informational?style=for-the-badge&color=50a0f0" height="20">
9//! <img src="https://img.shields.io/badge/platform- solana_os -informational?style=for-the-badge&color=50a0f0" height="20">
10//!
11//! [`workflow_log`] is a part of the [`workflow-rs`](https://crates.io/workflow-rs)
12//! framework, subset of which is designed to function uniformally across multiple
13//! environments including native Rust, WASM-browser and Solana OS targets.
14//!
15//! When you application is built in the native application environment
16//! macros such as `log_info!()` will invoke `println!()`, in WASM they will
17//! invoke `console.log()` and under Solana they will invoke `sol_log()`
18//! (used by `msg!()` macro)
19//!
20//! `workflow-log` macros operate the same way as regular functions such as
21//! `println!()`
22//!
23//! The following core macros are available:
24//! - `log_trace!()`
25//! - `log_debug!()`
26//! - `log_info!()`
27//! - `log_warn()`
28//! - `log_error!()`
29//!
30//! # Redirecting log output
31//!
32//! This crate allows you to configure a log sink that will receive
33//! all log messages from your application.  This is useful to route log messages
34//! to an external receiver or, for example, store logs to a file.
35//!
36//! Log sink can be installed using [`workflow_log::pipe`] function and supplying
37//! it with an Arc of the [`workflow_log::Sink`] trait.  The trait function
38//! [`workflow_log::Sink::write`] should return `true` to indicate the the text
39//! should be outputed to the console, or `false` to prevent further output
40//! (i.e. to consume the log text)
41//!
42//! ## Example:
43//!
44//! ```
45//! use workflow_log::*;
46//! use std::sync::Arc;
47//!
48//! pub struct MyStruct;
49//! impl Sink for MyStruct {
50//!     fn write(&self, target: Option<&str>, level:Level, args : &std::fmt::Arguments<'_>) -> bool {
51//!         
52//!         println!("target: {target:?}");
53//!         println!("level: {level:?}");
54//!         println!("args: {args:?}");
55//!
56//!         // return true to continue output
57//!         // return false to prevent further output
58//!         true
59//!     }
60//! }
61//!
62//! let my_struct = Arc::new(MyStruct{});
63//! workflow_log::pipe(Some(my_struct));
64//! log_trace!("test msg");
65//! ```
66//!
67//! To can disable the sink by supplying [`Option::None`] to [`workflow_log::pipe`].  
68//!
69
70extern crate self as workflow_log;
71
72mod log;
73pub use self::log::*;
74
75#[cfg(not(target_arch = "bpf"))]
76mod hex;
77
78mod console;
79pub use self::console::*;
80
81/// Self-contained [`Level`](levels::Level) and [`LevelFilter`](levels::LevelFilter)
82/// definitions, mirroring the `log` crate for use on targets where that crate is
83/// not embedded (e.g. BPF).
84pub mod levels;
85
86/// Convenient re-exports of the most commonly used logging macros, level types
87/// and console helpers for glob importing.
88pub mod prelude {
89    pub use super::console::*;
90    pub use super::log::{
91        Level, LevelFilter, log_debug, log_error, log_info, log_trace, log_warn, set_log_level,
92    };
93}
94
95#[cfg(test)]
96mod test {
97    use crate::*;
98    use std::sync::Arc;
99
100    #[test]
101    fn log_sink_test() {
102        pub struct MyStruct;
103        impl Sink for MyStruct {
104            fn write(
105                &self,
106                target: Option<&str>,
107                level: Level,
108                args: &std::fmt::Arguments<'_>,
109            ) -> bool {
110                println!("target: {target:?}");
111                println!("level: {level:?}");
112                println!("args: {args:?}");
113                true
114            }
115        }
116
117        let my_struct = Arc::new(MyStruct {});
118        workflow_log::pipe(Some(my_struct));
119        log_trace!("test msg");
120    }
121}