Mercurial > crates > nonstick
annotate src/logging.rs @ 98:b87100c5eed4
Start on environment variables, and make pointers nicer.
This starts work on the PAM environment handling, and in so doing,
introduces the CHeapBox and CHeapString structs. These are analogous
to Box and CString, but they're located on the C heap rather than
being Rust-managed memory.
This is because environment variables deal with even more pointers
and it turns out we can lose a lot of manual freeing using homemade
smart pointers.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Tue, 24 Jun 2025 04:25:25 -0400 |
parents | 5ddbcada30f2 |
children | dfcd96a74ac4 |
rev | line source |
---|---|
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
1 //! PAM logging variables and macros. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
2 //! |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
3 //! PAM implementations usually include the ability to log to syslog in a way |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 //! that is associated with the log entry itself. This module defines the enums |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 //! and macros for logging. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 //! |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 //! For more details, see [`PamShared::log`](crate::PamShared::log). |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 //! |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 //! We can't use [the `log` crate](https://docs.rs/log) because that requires |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 //! that any `Log` implementors be `Sync` and `Send`, and a PAM handle |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 //! may be neither. Furthermore, PAM handles are passed to PAM modules in |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 //! dynamic libraries, and `log` doesn't work across dynamic linking boundaries. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 //! |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 //! A `PamShared` implementation may still use the `log` crate on the backend, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 //! and may even itself implement `log::Log`, but that interface is not exposed |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 //! to the generic PAM user. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 #[cfg(feature = "link")] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 mod levels { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 pub use internal::*; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 #[cfg(pam_impl = "linux-pam")] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 mod internal { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 use crate::libpam::pam_ffi; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 pub const ERROR: u32 = pam_ffi::LOG_ERR; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 pub const WARN: u32 = pam_ffi::LOG_WARNING; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 pub const INFO: u32 = pam_ffi::LOG_INFO; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 pub const DEBUG: u32 = pam_ffi::LOG_DEBUG; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 #[cfg(pam_impl = "openpam")] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 mod internal { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 use crate::libpam::pam_ffi; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 pub const ERROR: u32 = pam_ffi::PAM_LOG_ERROR; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 pub const WARN: u32 = pam_ffi::PAM_LOG_NOTICE; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 pub const INFO: u32 = pam_ffi::PAM_LOG_VERBOSE; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 pub const DEBUG: u32 = pam_ffi::PAM_LOG_DEBUG; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
38 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
39 #[cfg(not(feature = "link"))] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 mod levels { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 pub const ERROR: u32 = 2255887; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 pub const WARN: u32 = 7265000; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 pub const INFO: u32 = 7762323; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 pub const DEBUG: u32 = 8675309; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 /// An entry to be added to the log. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 /// The levels are in descending order of importance and correspond roughly |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 /// to the similarly-named levels in the `log` crate. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 /// In all implementations, these are ordered such that `Error`, `Warning`, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 /// `Info`, and `Debug` are in ascending order. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 #[derive(Debug, PartialEq, Ord, PartialOrd, Eq)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 #[repr(u32)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 pub enum Level { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 Error = levels::ERROR, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 Warning = levels::WARN, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 Info = levels::INFO, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 Debug = levels::DEBUG, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 /// Here's the guts of the logger thingy. You shouldn't be using this! |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 #[doc(hidden)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 macro_rules! __log_internal { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 ($handle:expr, $level:ident, $($arg:tt)+) => { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 $handle.log($crate::logging::Level::$level, &format!($($arg)+)); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
72 /// Logs a message at error level via the given PAM handle. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
73 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
79 /// # fn _test(pam_handle: impl nonstick::PamShared) { |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
80 /// # let load_error = "xxx"; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
81 /// nonstick::error!(pam_handle, "error loading data from data source: {load_error}"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
82 /// // Will log a message like "error loading data from data source: timed out" |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 /// // at ERROR level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
84 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
87 macro_rules! error { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Error, $($arg)+);}} |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 /// Logs a message at warning level via the given PAM handle. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
90 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
96 /// # fn _test(pam_handle: impl nonstick::PamShared) { |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 /// # let latency_ms = "xxx"; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 /// nonstick::warn!(pam_handle, "loading took too long: {latency_ms} ms"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 /// // Will log a message like "loading took too long: 495 ms" |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 /// // at WARN level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
101 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 macro_rules! warn { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Warning, $($arg)+);}} |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 /// Logs a message at info level via the given PAM handle. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
113 /// # fn _test(pam_handle: impl nonstick::PamShared) { |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 /// nonstick::info!(pam_handle, "using remote backend to load user data"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
115 /// // Will log a message like "using remote backend to load user data" |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
116 /// // at INFO level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
117 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
118 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
120 macro_rules! info { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Info, $($arg)+);}} |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
121 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
122 /// Logs a message at debug level via the given PAM handle. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
123 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
124 /// This level specially includes file/line/column information. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
125 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
126 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
127 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
128 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
129 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
130 /// # fn _test(pam_handle: impl nonstick::PamShared) { |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
131 /// # let userinfo_url = "https://zombo.com/"; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
132 /// nonstick::debug!(pam_handle, "making HTTP GET request to {userinfo_url}"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
133 /// // Will log a message like |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
134 /// // "pam_http/lib.rs:39:14: making HTTP GET request to https://zombo.com/" |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
135 /// // at DEBUG level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
136 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
137 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
138 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
139 macro_rules! debug {($handle:expr, $($arg:tt)+) => { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
140 $crate::__log_internal!( |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
141 $handle, Debug, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
142 "{}:{}:{}: {}", file!(), line!(), column!(), format_args!($($arg)+), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
143 ); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
144 }} |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
145 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
146 #[cfg(test)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
147 mod tests { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
148 use super::*; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
149 use regex::Regex; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
150 use std::cell::RefCell; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
151 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
152 #[test] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
153 fn test_order() { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
154 assert!(Level::Error < Level::Warning); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
155 assert!(Level::Warning < Level::Info); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
156 assert!(Level::Info < Level::Debug); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
157 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
158 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
159 #[test] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
160 fn test_logging() { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
161 struct Logger(RefCell<Vec<(Level, String)>>); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
162 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
163 impl Logger { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
164 fn log(&self, level: Level, text: &str) { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
165 self.0.borrow_mut().push((level, text.to_owned())) |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
166 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
167 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
168 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
169 let logger = Logger(Default::default()); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
170 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
171 let something = Level::Error; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
172 error!(logger, "here is another thing: {}", 99); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
173 warn!(logger, "watch out!"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
174 info!(logger, "here is some info: {info}", info = "information"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
175 debug!(logger, "here is something: {something:?}"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
176 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
177 let mut logged = logger.0.into_inner(); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
178 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
179 let (last_level, last_string) = logged.pop().unwrap(); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
180 assert_eq!(Level::Debug, last_level); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
181 let expr = Regex::new(r"^[^:]+:\d+:\d+: here is something: Error$").unwrap(); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
182 assert!( |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
183 expr.is_match(&last_string), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
184 "{last_string:?} did not match {expr:?}" |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
185 ); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
186 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
187 assert_eq!( |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
188 vec![ |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
189 (Level::Error, "here is another thing: 99".to_owned()), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
190 (Level::Warning, "watch out!".to_owned()), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
191 (Level::Info, "here is some info: information".to_owned()), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
192 ], |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
193 logged |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
194 ); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
195 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
196 } |