1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! The global debug log. Listeners are either trait objects or plain functions,
//! and messages are also sent on a broadcast channel for anyone who'd rather
//! subscribe.
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, OnceLock, RwLock};
use tokio::sync::broadcast;
/// Capacity of the broadcast channel.
const BROADCAST_CAPACITY: usize = 1024;
/// A message that was written to the debug log.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DebugLogMessage {
/// The category of the message.
pub category: String,
/// The message itself.
pub message: String,
}
/// A debug log listener.
pub trait IDebugListener: Send + Sync {
/// Called for every line written to the debug log.
fn write_line(&self, category: &str, msg: &str);
}
/// The global state of the debug log.
struct DebugLogState {
/// Whether debug logging is enabled.
enabled: AtomicBool,
/// The registered listener objects.
listeners: RwLock<Vec<Arc<dyn IDebugListener>>>,
/// The registered listener functions, with their handles.
functions: RwLock<Vec<(u64, Arc<dyn Fn(&str, &str) + Send + Sync>)>>,
/// The handle the next listener function gets.
next_function_id: AtomicU64,
/// The channel log messages are broadcast on.
sender: broadcast::Sender<DebugLogMessage>,
}
impl DebugLogState {
fn new() -> DebugLogState {
let (sender, _) = broadcast::channel(BROADCAST_CAPACITY);
DebugLogState {
// On in debug builds, off in release builds.
enabled: AtomicBool::new(cfg!(debug_assertions)),
listeners: RwLock::new(Vec::new()),
functions: RwLock::new(Vec::new()),
next_function_id: AtomicU64::new(1),
sender,
}
}
}
/// The global debug log state, created on first use.
fn state() -> &'static DebugLogState {
static STATE: OnceLock<DebugLogState> = OnceLock::new();
STATE.get_or_init(DebugLogState::new)
}
/// The global debug log.
#[derive(Debug)]
pub struct DebugLog;
impl DebugLog {
/// Whether debug logging is on. Defaults to on in debug builds and off in
/// release builds.
pub fn enabled() -> bool {
state().enabled.load(Ordering::Relaxed)
}
/// Turns debug logging on or off.
pub fn set_enabled(enabled: bool) {
state().enabled.store(enabled, Ordering::Relaxed);
}
/// Adds a listener.
pub fn add_listener(listener: Arc<dyn IDebugListener>) {
state().listeners.write().unwrap_or_else(|error| error.into_inner()).push(listener);
}
/// Adds a listener function. Closures can't be compared, so use the
/// returned handle with [`DebugLog::remove_listener_function`] to remove it.
pub fn add_listener_function(
action: impl Fn(&str, &str) + Send + Sync + 'static,
) -> u64 {
let id = state().next_function_id.fetch_add(1, Ordering::Relaxed);
state()
.functions
.write()
.unwrap_or_else(|error| error.into_inner())
.push((id, Arc::new(action)));
id
}
/// Removes a listener (compared by identity).
pub fn remove_listener(listener: &Arc<dyn IDebugListener>) {
let mut listeners = state().listeners.write().unwrap_or_else(|error| error.into_inner());
if let Some(index) = listeners
.iter()
.position(|registered| Arc::ptr_eq(registered, listener))
{
listeners.remove(index);
}
}
/// Removes a listener function added with
/// [`DebugLog::add_listener_function`].
pub fn remove_listener_function(id: u64) {
let mut functions = state().functions.write().unwrap_or_else(|error| error.into_inner());
if let Some(index) = functions.iter().position(|(registered, _)| *registered == id) {
functions.remove(index);
}
}
/// Removes all listeners.
pub fn clear_listeners() {
state()
.listeners
.write()
.unwrap_or_else(|error| error.into_inner())
.clear();
state()
.functions
.write()
.unwrap_or_else(|error| error.into_inner())
.clear();
}
/// Subscribes to debug log messages. A receiver that falls behind gets
/// `RecvError::Lagged` and misses messages.
pub fn subscribe() -> broadcast::Receiver<DebugLogMessage> {
state().sender.subscribe()
}
/// Writes a line to the debug log.
pub fn write_line(category: &str, msg: &str) {
let state = state();
if !state.enabled.load(Ordering::Relaxed) {
return;
}
let message = DebugLogMessage {
category: category.to_string(),
message: msg.to_string(),
};
// Sending only fails when nobody's subscribed, which is fine.
let _ = state.sender.send(message);
let listeners = state.listeners.read().unwrap_or_else(|error| error.into_inner());
for listener in listeners.iter() {
listener.write_line(category, msg);
}
drop(listeners);
let functions = state.functions.read().unwrap_or_else(|error| error.into_inner());
for (_, action) in functions.iter() {
action(category, msg);
}
}
/// Writes a line to the debug log, filling `{0}`, `{1}`, ... in `msg` from
/// `args` (`{{` and `}}` are literal braces). Format specifiers like
/// `{0:X}` aren't supported and are left as they are.
pub fn write_line_args(category: &str, msg: &str, args: &[&str]) {
DebugLog::write_line(category, &format_with_args(msg, args));
}
/// If `condition` is false, writes `message` to the debug log.
pub fn assert(condition: bool, category: &str, message: &str) {
if !condition {
DebugLog::write_line(category, &format!("Assertion Failed! {message}"));
}
}
}
/// Fills `{0}`, `{1}`, ... in `msg` from `args`. `{{` and `}}` are literal
/// braces; placeholders with a format specifier or alignment are left as they
/// are.
pub fn format_with_args(msg: &str, args: &[&str]) -> String {
let mut result = String::with_capacity(msg.len());
let mut characters = msg.chars().peekable();
while let Some(character) = characters.next() {
match character {
'{' => {
if characters.peek() == Some(&'{') {
characters.next();
result.push('{');
continue;
}
let mut placeholder = String::new();
let mut closed = false;
for character in characters.by_ref() {
if character == '}' {
closed = true;
break;
}
placeholder.push(character);
}
if !closed {
// Unterminated placeholder: copy the rest as-is.
result.push('{');
result.push_str(&placeholder);
break;
}
match placeholder.parse::<usize>() {
Ok(index) => match args.get(index) {
Some(value) => result.push_str(value),
None => {
result.push('{');
result.push_str(&placeholder);
result.push('}');
}
},
Err(_) => {
result.push('{');
result.push_str(&placeholder);
result.push('}');
}
}
}
'}' => {
if characters.peek() == Some(&'}') {
characters.next();
result.push('}');
continue;
}
result.push('}');
}
_ => result.push(character),
}
}
result
}