webdav_server/log.rs
1use std::fmt::Display;
2
3/// A lightweight, zero-dependency macro for printing structured debug output to stdout.
4///
5/// This macro is a no-op in release builds (`#[cfg(debug_assertions)]` guards every call).
6/// Output is formatted with a right-aligned "worker" label followed by the message.
7///
8/// # Examples
9///
10/// ```rust
11/// use webdav_server::log;
12///
13/// // Simple message with no label
14/// log!("Server started");
15///
16/// // Labeled message (label is right-aligned to 12 characters)
17/// log!("SERVER", "Listening on port 6969");
18/// log!("STORAGE", format!("committed: {}", "some_file.bin"));
19/// ```
20#[macro_export]
21#[deprecated(
22 since = "0.1.1",
23 note = "use info!(), error!(), warn!(), fatal!() macro instead"
24)]
25macro_rules! log {
26 ($msg:expr $(,)?) => {{
27 #[cfg(debug_assertions)]
28 {
29 println!("{:>12} {}", "", $msg);
30 }
31 }};
32
33 ($worker:expr, $msg:expr $(,)?) => {{
34 #[cfg(debug_assertions)]
35 {
36 println!("\r\x1b[1m\x1b[34m{:>12}\x1b[0m -> {}", $worker, $msg);
37 }
38 }};
39}
40
41/// Extension trait that adds ANSI color-formatting methods to any `Display` type.
42///
43/// This is intended for use inside `log!` calls to highlight important parts of
44/// a message. All methods return a new `String` with the appropriate ANSI escape
45/// codes applied.
46///
47/// # Examples
48///
49/// ```rust
50/// use webdav_server::log::Color;
51///
52/// let msg = "something went wrong".error();
53/// let user = "user_42".bold();
54/// ```
55pub trait Color {
56 /// Wraps the value in bold ANSI escape codes.
57 fn bold(&self) -> String;
58 /// Wraps the value in italic (dim) ANSI escape codes.
59 fn italic(&self) -> String;
60 /// Wraps the value in yellow ANSI escape codes, used for warnings.
61 fn warn(&self) -> String;
62 /// Wraps the value in red ANSI escape codes, used for errors.
63 fn error(&self) -> String;
64 /// Wraps the value in grey ANSI escape codes, used for low-priority debug output.
65 fn debug(&self) -> String;
66 /// Wraps the value in cyan ANSI escape codes, used for informational messages.
67 fn info(&self) -> String;
68}
69
70impl<T> Color for T
71where
72 T: Display,
73{
74 fn bold(&self) -> String {
75 format!("\x1b[1m{self}\x1b[0m")
76 }
77
78 fn debug(&self) -> String {
79 format!("\x1b[90m{self}\x1b[0m")
80 }
81
82 fn error(&self) -> String {
83 format!("\x1b[31m{self}\x1b[0m")
84 }
85
86 fn info(&self) -> String {
87 format!("\x1b[36m{self}\x1b[0m")
88 }
89
90 fn italic(&self) -> String {
91 format!("\x1b[2m{self}\x1b[0m")
92 }
93
94 fn warn(&self) -> String {
95 format!("\x1b[33m{self}\x1b[0m")
96 }
97}