1use std::sync::Arc;
18use std::sync::atomic::{AtomicU64, Ordering};
19
20#[derive(Debug)]
22pub struct WriteError {
23 pub backend: &'static str,
25 pub source: std::io::Error,
27 pub events_lost: u64,
30}
31
32impl std::fmt::Display for WriteError {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(
35 f,
36 "{}: audit write failed, {} event(s) lost: {}",
37 self.backend, self.events_lost, self.source
38 )
39 }
40}
41
42impl std::error::Error for WriteError {
43 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44 Some(&self.source)
45 }
46}
47
48pub trait ErrorSink: Send + Sync {
54 fn on_error(&self, error: &WriteError);
56}
57
58#[derive(Debug, Clone, Copy, Default)]
61pub struct IgnoreErrors;
62
63impl ErrorSink for IgnoreErrors {
64 fn on_error(&self, _error: &WriteError) {}
65}
66
67#[derive(Debug, Clone, Copy, Default)]
73pub struct StderrErrorSink;
74
75impl ErrorSink for StderrErrorSink {
76 fn on_error(&self, error: &WriteError) {
77 eprintln!("ash-log: {error}");
80 }
81}
82
83#[derive(Debug, Default)]
89pub struct CountingErrorSink {
90 failures: AtomicU64,
91 events_lost: AtomicU64,
92}
93
94impl CountingErrorSink {
95 #[must_use]
97 pub fn new() -> Self {
98 Self::default()
99 }
100
101 #[must_use]
103 pub fn failures(&self) -> u64 {
104 self.failures.load(Ordering::Relaxed)
105 }
106
107 #[must_use]
109 pub fn events_lost(&self) -> u64 {
110 self.events_lost.load(Ordering::Relaxed)
111 }
112
113 #[must_use]
118 pub fn had_failures(&self) -> bool {
119 self.failures() > 0
120 }
121}
122
123impl ErrorSink for CountingErrorSink {
124 fn on_error(&self, error: &WriteError) {
125 self.failures.fetch_add(1, Ordering::Relaxed);
126 self.events_lost
127 .fetch_add(error.events_lost, Ordering::Relaxed);
128 }
129}
130
131pub struct FnErrorSink<F: Fn(&WriteError) + Send + Sync>(F);
145
146impl<F: Fn(&WriteError) + Send + Sync> FnErrorSink<F> {
147 pub const fn new(f: F) -> Self {
149 Self(f)
150 }
151}
152
153impl<F: Fn(&WriteError) + Send + Sync> ErrorSink for FnErrorSink<F> {
154 fn on_error(&self, error: &WriteError) {
155 (self.0)(error);
156 }
157}
158
159impl<T: ErrorSink + ?Sized> ErrorSink for Arc<T> {
160 fn on_error(&self, error: &WriteError) {
161 (**self).on_error(error);
162 }
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 fn error(events_lost: u64) -> WriteError {
170 WriteError {
171 backend: "TestBackend",
172 source: std::io::Error::other("disk full"),
173 events_lost,
174 }
175 }
176
177 #[test]
178 fn test_counting_sink_accumulates_losses() {
179 let sink = CountingErrorSink::new();
180 assert!(!sink.had_failures());
181
182 sink.on_error(&error(1));
183 sink.on_error(&error(64));
184
185 assert_eq!(sink.failures(), 2);
186 assert_eq!(sink.events_lost(), 65, "a batch loss counts every event");
187 assert!(sink.had_failures());
188 }
189
190 #[test]
191 fn test_error_displays_backend_and_loss() {
192 let rendered = error(3).to_string();
193 assert!(
194 rendered.contains("TestBackend"),
195 "names the failing backend"
196 );
197 assert!(rendered.contains('3'), "states how many events were lost");
198 assert!(rendered.contains("disk full"), "includes the cause");
199 }
200
201 #[test]
202 fn test_fn_sink_receives_errors() {
203 let count = Arc::new(AtomicU64::new(0));
204 let seen = count.clone();
205 let sink = FnErrorSink::new(move |_| {
206 seen.fetch_add(1, Ordering::Relaxed);
207 });
208
209 sink.on_error(&error(1));
210 assert_eq!(count.load(Ordering::Relaxed), 1);
211 }
212}