Mercurial > crates > nonstick
annotate src/logging.rs @ 116:a12706e42c9d default tip
Logging, macros, and building:
- Changes logging API to accept the `Location` of the log statement.
Fixes OpenPAM implementation.
- Stops publicly exporting doc macros.
- Uses dlopen to detect the PAM library rather than header jankery.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Jun 2025 18:27:51 -0400 |
parents | 1e11a52b4665 |
children |
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 |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
103
diff
changeset
|
18 #[cfg(all(feature = "link", pam_impl = "openpam"))] |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 mod levels { |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
103
diff
changeset
|
20 use crate::libpam::pam_ffi; |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
21 pub const ERROR: i32 = pam_ffi::PAM_LOG_ERROR; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
22 pub const WARN: i32 = pam_ffi::PAM_LOG_NOTICE; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
23 pub const INFO: i32 = pam_ffi::PAM_LOG_VERBOSE; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
24 pub const DEBUG: i32 = pam_ffi::PAM_LOG_DEBUG; |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 } |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
103
diff
changeset
|
26 #[cfg(not(all(feature = "link", pam_impl = "openpam")))] |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 mod levels { |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
28 pub const ERROR: i32 = libc::LOG_ERR; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
29 pub const WARN: i32 = libc::LOG_WARNING; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
30 pub const INFO: i32 = libc::LOG_INFO; |
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
31 pub const DEBUG: i32 = libc::LOG_DEBUG; |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 /// 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
|
35 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 /// 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
|
37 /// 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
|
38 /// |
115
1e11a52b4665
Don't promise ordering for the log level.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
39 /// Their values are ordered monotonically, either increasing or decreasing, |
1e11a52b4665
Don't promise ordering for the log level.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
40 /// depending upon the implementation. |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
41 #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
113
178310336596
Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents:
108
diff
changeset
|
42 #[repr(i32)] |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 pub enum Level { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 Error = levels::ERROR, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 Warning = levels::WARN, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 Info = levels::INFO, |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 Debug = levels::DEBUG, |
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 |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
50 /// The location of a log entry. |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
51 #[derive(Clone, Copy, Debug, Default)] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
52 pub struct Location<'a> { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
53 pub file: &'a str, |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
54 pub line: u32, |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
55 pub function: &'a str, |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
56 _more: (), |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
57 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
58 |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
59 impl<'a> Location<'a> { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
60 pub fn new(file: &'a str, line: u32, function: &'a str) -> Self { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
61 Self {file, line, function, _more: ()} |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
62 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
63 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
64 |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
65 /// The [`Location`] where this macro is inserted. |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
66 #[doc(hidden)] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
67 #[macro_export] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
68 macro_rules! location { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
69 () => { $crate::logging::Location::new(file!(), line!(), $crate::__function!()) } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
70 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
71 |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
72 /// 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
|
73 #[doc(hidden)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 macro_rules! __log_internal { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 ($handle:expr, $level:ident, $($arg:tt)+) => { |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
77 $handle.log($crate::logging::Level::$level, $crate::location!(), &format!($($arg)+)); |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
79 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
80 |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
81 /// Ugly, hacky macro to get the current function name. |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
82 /// |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
83 /// [Stolen from Stack Overflow.][https://stackoverflow.com/a/40234666/39808] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
84 #[doc(hidden)] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
85 #[macro_export] |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
86 macro_rules! __function { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
87 () => {{ |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
88 fn p() {} |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
89 fn f<T>(_: T) -> &'static str { |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
90 ::std::any::type_name::<T>() |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
91 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
92 f(p).trim_end_matches("::p") |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
93 }} |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
94 } |
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
95 |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 /// 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
|
97 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
103 /// # 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
|
104 /// # let load_error = "xxx"; |
103
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
105 /// nonstick::error!( |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
106 /// pam_handle, |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
107 /// "error loading data from data source: {load_error}" |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
108 /// ); |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 /// // 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
|
110 /// // at ERROR level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
111 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 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
|
115 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
116 /// 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
|
117 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
118 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
120 /// # Example |
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 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
123 /// # fn _test(pam_handle: impl nonstick::PamShared) { |
103
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
124 /// # let (start, finish) = (0, 0); |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
125 /// nonstick::warn!( |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
126 /// pam_handle, |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
127 /// "loading took too long: {latency_ms} ms", |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
128 /// latency_ms = start - finish |
dfcd96a74ac4
write a truly prodigious amount of documentation
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
129 /// ); |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
130 /// // 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
|
131 /// // at WARN level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
132 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
133 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
134 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
135 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
|
136 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
137 /// 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
|
138 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
139 /// This supports `format!`-style formatting. |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
140 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
141 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
142 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
143 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
144 /// # 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
|
145 /// 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
|
146 /// // 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
|
147 /// // at INFO level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
148 /// # } |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
149 /// ``` |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
150 #[macro_export] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
151 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
|
152 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
153 /// 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
|
154 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
155 /// 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
|
156 /// This supports `format!`-style formatting. |
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 /// # Example |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
159 /// |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
160 /// ```no_run |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
161 /// # 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
|
162 /// # let userinfo_url = "https://zombo.com/"; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
163 /// 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
|
164 /// // Will log a message like |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
165 /// // making HTTP GET request to https://zombo.com/" |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
166 /// // at DEBUG level on syslog. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
92
diff
changeset
|
167 /// # } |
92
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 #[macro_export] |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
170 macro_rules! debug { ($handle:expr, $($arg:tt)+) => { $crate::__log_internal!($handle, Debug, $($arg)+);}} |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
171 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
172 #[cfg(test)] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
173 mod tests { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
174 use super::*; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
175 use regex::Regex; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
176 use std::cell::RefCell; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
177 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
178 #[test] |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
179 fn test_logging() { |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
180 struct Logger(RefCell<Vec<(Level, String)>>); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
181 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
182 impl Logger { |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
183 fn log(&self, level: Level, loc: Location<'_>, text: &str) { |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
184 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
|
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 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
188 let logger = Logger(Default::default()); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
189 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
190 let something = Level::Error; |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
191 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
|
192 warn!(logger, "watch out!"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
193 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
|
194 debug!(logger, "here is something: {something:?}"); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
195 |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
196 let logged = logger.0.into_inner(); |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
197 |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
198 assert_eq!( |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
199 vec![ |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
200 (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
|
201 (Level::Warning, "watch out!".to_owned()), |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
202 (Level::Info, "here is some info: information".to_owned()), |
116
a12706e42c9d
Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents:
115
diff
changeset
|
203 (Level::Debug, "here is something: Error".to_owned()), |
92
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
204 ], |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
205 logged |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
206 ); |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
207 } |
5ddbcada30f2
Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
208 } |