1use crate::{EventStream, Sink};
10use std::cell::{Cell, RefCell};
11use std::rc::Rc;
12
13#[derive(Clone, Debug, PartialEq, Eq)]
15pub enum DiagnosticEvent {
16 TransactionStarted {
18 id: u64,
20 },
21 NodeProcessed {
23 node: u64,
25 rank: usize,
27 signal: bool,
29 },
30 ObserverFired {
32 observer: u64,
34 },
35 DeferredWriteQueued {
37 transaction: u64,
39 },
40 CycleRejected,
42 TransactionEnded {
44 id: u64,
46 processed: usize,
48 fired: usize,
50 deferred: usize,
52 },
53}
54
55thread_local! {
56 static DIAGNOSTICS: RefCell<Option<Rc<Diagnostics>>> = const { RefCell::new(None) };
57 static FLUSHING: Cell<bool> = const { Cell::new(false) };
58}
59
60pub struct Diagnostics {
62 buffer: RefCell<Vec<DiagnosticEvent>>,
63 sink: Sink<DiagnosticEvent>,
64}
65
66impl Diagnostics {
67 pub fn install() -> Rc<Self> {
70 assert!(
71 DIAGNOSTICS.with(|d| d.borrow().is_none()),
72 "diagnostics already installed on this thread"
73 );
74 let diagnostics = Rc::new(Self {
75 buffer: RefCell::new(Vec::new()),
76 sink: Sink::new(),
77 });
78 DIAGNOSTICS.with(|d| *d.borrow_mut() = Some(diagnostics.clone()));
79 diagnostics
80 }
81
82 pub fn uninstall(&self) {
84 let is_current = DIAGNOSTICS.with(|d| {
85 d.borrow()
86 .as_ref()
87 .is_some_and(|current| std::ptr::eq(Rc::as_ptr(current), self))
88 });
89 if is_current {
90 DIAGNOSTICS.with(|d| *d.borrow_mut() = None);
91 }
92 }
93
94 pub fn events(&self) -> EventStream<DiagnosticEvent> {
96 self.sink.stream()
97 }
98
99 pub fn flush(&self) {
102 if FLUSHING.get() {
103 return;
104 }
105 FLUSHING.set(true);
106 let events: Vec<DiagnosticEvent> = std::mem::take(&mut *self.buffer.borrow_mut());
107 for event in events {
108 self.sink.send(event);
109 }
110 FLUSHING.set(false);
111 }
112
113 pub(crate) fn emit(event: DiagnosticEvent) {
114 if FLUSHING.get() {
115 return;
116 }
117 DIAGNOSTICS.with(|d| {
118 if let Some(diagnostics) = d.borrow().as_ref() {
119 diagnostics.buffer.borrow_mut().push(event);
120 }
121 });
122 }
123
124 pub(crate) fn flush_global() {
125 DIAGNOSTICS.with(|d| {
126 if let Some(diagnostics) = d.borrow().as_ref() {
127 diagnostics.flush();
128 }
129 });
130 }
131}
132
133#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
135pub enum LogLevel {
136 Error,
138 Warn,
140 Info,
142 Debug,
144}
145
146#[derive(Clone, Debug, PartialEq, Eq)]
148pub struct LogRecord {
149 pub level: LogLevel,
151 pub message: String,
153}
154
155thread_local! {
156 static LOGGER: RefCell<Option<Rc<Logger>>> = const { RefCell::new(None) };
157}
158
159pub struct Logger {
161 min_level: Cell<LogLevel>,
162 sink: Sink<LogRecord>,
163}
164
165impl Logger {
166 pub fn install(min_level: LogLevel) -> Rc<Self> {
168 assert!(
169 LOGGER.with(|l| l.borrow().is_none()),
170 "logger already installed on this thread"
171 );
172 let logger = Rc::new(Self {
173 min_level: Cell::new(min_level),
174 sink: Sink::new(),
175 });
176 LOGGER.with(|l| *l.borrow_mut() = Some(logger.clone()));
177 logger
178 }
179
180 pub fn uninstall(&self) {
182 let is_current = LOGGER.with(|l| {
183 l.borrow()
184 .as_ref()
185 .is_some_and(|current| std::ptr::eq(Rc::as_ptr(current), self))
186 });
187 if is_current {
188 LOGGER.with(|l| *l.borrow_mut() = None);
189 }
190 }
191
192 pub fn records(&self) -> EventStream<LogRecord> {
194 self.sink.stream()
195 }
196
197 pub fn set_min_level(&self, level: LogLevel) {
199 self.min_level.set(level);
200 }
201
202 pub fn log(&self, level: LogLevel, message: impl Into<String>) {
204 if level <= self.min_level.get() {
205 self.sink.send(LogRecord {
206 level,
207 message: message.into(),
208 });
209 }
210 }
211
212 pub fn debug(&self, message: impl Into<String>) {
214 self.log(LogLevel::Debug, message);
215 }
216
217 pub fn info(&self, message: impl Into<String>) {
219 self.log(LogLevel::Info, message);
220 }
221
222 pub fn warn(&self, message: impl Into<String>) {
224 self.log(LogLevel::Warn, message);
225 }
226
227 pub fn error(&self, message: impl Into<String>) {
229 self.log(LogLevel::Error, message);
230 }
231}
232
233pub fn log(level: LogLevel, message: impl Into<String>) {
235 LOGGER.with(|l| {
236 if let Some(logger) = l.borrow().as_ref() {
237 logger.log(level, message);
238 }
239 });
240}
241
242pub fn log_debug(message: impl Into<String>) {
244 log(LogLevel::Debug, message);
245}
246
247pub fn log_info(message: impl Into<String>) {
249 log(LogLevel::Info, message);
250}
251
252pub fn log_warn(message: impl Into<String>) {
254 log(LogLevel::Warn, message);
255}
256
257pub fn log_error(message: impl Into<String>) {
259 log(LogLevel::Error, message);
260}