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
75mod console;
76pub use self::console::*;
77
78pub mod levels;
79
80pub mod prelude {
81 pub use super::console::*;
82 pub use super::log::{
83 log_debug, log_error, log_info, log_trace, log_warn, set_log_level, Level, LevelFilter,
84 };
85}
86
87#[cfg(test)]
88mod test {
89 use crate::*;
90 use std::sync::Arc;
91
92 #[test]
93 fn log_sink_test() {
94 pub struct MyStruct;
95 impl Sink for MyStruct {
96 fn write(
97 &self,
98 target: Option<&str>,
99 level: Level,
100 args: &std::fmt::Arguments<'_>,
101 ) -> bool {
102 println!("target: {target:?}");
103 println!("level: {level:?}");
104 println!("args: {args:?}");
105 true
106 }
107 }
108
109 let my_struct = Arc::new(MyStruct {});
110 workflow_log::pipe(Some(my_struct));
111 log_trace!("test msg");
112 }
113}