v_utils/utils/
mod.rs

1pub mod eyre;
2pub use eyre::*;
3
4pub mod format;
5pub use format::*;
6
7pub mod info_size;
8pub use info_size::*;
9
10pub mod serde;
11pub use serde::*;
12
13/// Macro for logging to both stdout and tracing info
14/// Usage: log!("message") or log!("format {}", arg)
15#[macro_export]
16macro_rules! log {
17	($($arg:tt)*) => {{
18		println!($($arg)*);
19		tracing::info!($($arg)*);
20	}};
21}
22
23/// Macro for logging to both stderr and tracing debug
24/// Usage: elog!("message") or elog!("format {}", arg)
25#[macro_export]
26macro_rules! elog {
27	($($arg:tt)*) => {{
28		eprintln!($($arg)*);
29		tracing::debug!($($arg)*);
30	}};
31}
32
33#[cfg(feature = "tracing")]
34pub mod tracing;
35#[cfg(feature = "tracing")]
36pub use tracing::*;
37
38/// # HACK
39/// Assumes that `color_eyre` is in scope
40///
41/// Optionally pass `option_env!("LOG_DIRECTIVES")` as the first argument to embed compile-time log directives.
42#[cfg(all(feature = "tracing", feature = "xdg"))]
43#[macro_export]
44macro_rules! clientside {
45	() => {
46		color_eyre::install().unwrap();
47		v_utils::utils::init_subscriber(
48			v_utils::utils::LogDestination::xdg(env!("CARGO_PKG_NAME"))
49				.stderr_errors(true)
50				.compiled_directives(option_env!("LOG_DIRECTIVES")),
51		);
52	};
53	($fname:expr) => {
54		color_eyre::install().unwrap();
55		v_utils::utils::init_subscriber(
56			v_utils::utils::LogDestination::xdg(env!("CARGO_PKG_NAME"))
57				.fname($fname)
58				.stderr_errors(true)
59				.compiled_directives(option_env!("LOG_DIRECTIVES")),
60		);
61	};
62}
63
64/// Fallback when xdg is not available - logs to stdout
65#[cfg(all(feature = "tracing", not(feature = "xdg")))]
66#[macro_export]
67macro_rules! clientside {
68	() => {
69		eprintln!("[v_utils] Warning: `xdg` feature not enabled, logging to stdout instead of file. Add `xdg` feature to v_utils dependency to enable file logging.");
70		color_eyre::install().unwrap();
71		v_utils::utils::init_subscriber(v_utils::utils::LogDestination::default().compiled_directives(option_env!("LOG_DIRECTIVES")));
72	};
73	($fname:expr) => {
74		eprintln!("[v_utils] Warning: `xdg` feature not enabled, logging to stdout instead of file. Add `xdg` feature to v_utils dependency to enable file logging.");
75		color_eyre::install().unwrap();
76		v_utils::utils::init_subscriber(v_utils::utils::LogDestination::default().compiled_directives(option_env!("LOG_DIRECTIVES")));
77	};
78}
79
80/// **Warning**: Consider using `strum` crate instead - this macro is likely redundant for most use cases.
81#[macro_export]
82macro_rules! define_str_enum {
83  ($(#[$meta:meta])* $vis:vis enum $name:ident {
84    $($(#[$variant_meta:meta])* $variant:ident => $str:expr),* $(,)?
85  }) => {
86    $(#[$meta])*
87    $vis enum $name {
88      $($(#[$variant_meta])* $variant),*
89    }
90
91    impl std::fmt::Display for $name {
92      fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        match self {
94          $(Self::$variant => write!(f, "{}", $str)),*
95        }
96      }
97    }
98
99    impl std::str::FromStr for $name {
100      type Err = eyre::Report;
101
102      fn from_str(s: &str) -> Result<Self, Self::Err> {
103        match s {
104          $($str => Ok(Self::$variant)),*,
105          _ => eyre::bail!("Invalid {} string: {}", stringify!($name).to_lowercase(), s),
106        }
107      }
108    }
109  };
110}