ring_log/
lib.rs

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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::cell::UnsafeCell;
use std::fs::File;
use std::io::Write;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::{self};

#[derive(Clone)]
pub enum LogTo {
    Ephemeral,
    File,
}

struct LogEntry {
    closure: Box<dyn FnOnce() -> String + Send>,
    log_to: LogTo,
}

struct RingBuffer {
    buffer: Vec<AtomicUsize>,
    entries: Vec<UnsafeCell<Option<LogEntry>>>,
    head: AtomicUsize,
    tail: AtomicUsize,
    size: usize,
    is_empty: AtomicBool,
    shutdown: AtomicBool,
}

unsafe impl Sync for RingBuffer {}

impl RingBuffer {
    fn new(size: usize) -> Self {
        RingBuffer {
            size,
            buffer: (0..size).map(|_| AtomicUsize::new(0)).collect(),
            entries: (0..size).map(|_| UnsafeCell::new(None)).collect(),
            head: AtomicUsize::new(0),
            tail: AtomicUsize::new(0),
            is_empty: AtomicBool::new(true),
            shutdown: AtomicBool::new(false),
        }
    }

    fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Release);
    }

    fn should_shutdown(&self) -> bool {
        self.shutdown.load(Ordering::Acquire)
    }

    fn is_empty(&self) -> bool {
        self.is_empty.load(Ordering::Acquire)
    }

    fn push(&self, entry: &mut LogEntry) -> bool {
        let mut head = self.head.load(Ordering::Relaxed);
        loop {
            let next = (head + 1) % self.size;
            if next == self.tail.load(Ordering::Relaxed) {
                return false; // Buffer is full
            }
            match self
                .head
                .compare_exchange(head, next, Ordering::Release, Ordering::Relaxed)
            {
                Ok(_) => {
                    unsafe {
                        // TODO
                        *self.entries[head].get() = Some(std::mem::replace(
                            entry,
                            LogEntry {
                                closure: Box::new(|| "Error: LogEntry moved".to_string()),
                                log_to: entry.log_to.clone(),
                            },
                        ));
                    }
                    self.buffer[head].store(1, Ordering::Release);
                    self.is_empty.store(false, Ordering::Release);
                    return true;
                }
                Err(x) => head = x,
            }
        }
    }

    fn pop(&self) -> Option<LogEntry> {
        let mut tail = self.tail.load(Ordering::Relaxed);
        loop {
            if self.buffer[tail].load(Ordering::Acquire) == 0 {
                return None; // Buffer is empty
            }
            let next = (tail + 1) % self.size;
            match self
                .tail
                .compare_exchange_weak(tail, next, Ordering::Release, Ordering::Relaxed)
            {
                Ok(_) => {
                    let entry = unsafe { (*self.entries[tail].get()).take() };
                    self.buffer[tail].store(0, Ordering::Release);
                    if next == self.head.load(Ordering::Relaxed) {
                        self.is_empty.store(true, Ordering::Release);
                    }
                    return entry;
                }
                Err(x) => tail = x,
            }
        }
    }

    fn wait_for_new_entries(&self) {
        while self.is_empty() && !self.should_shutdown() {
            thread::yield_now();
        }
    }
}

pub struct Logger {
    buffer: Arc<RingBuffer>,
    file: Option<File>,
    log_to: LogTo,
    with_time: bool,
}

#[derive(Clone, Copy)]
pub struct LoggerFileOptions {
    path: &'static str,
    append_mode: bool,
}

impl Logger {
    pub fn builder(size: usize, log_op: Option<LoggerFileOptions>) -> Self {
        let buffer = Arc::new(RingBuffer::new(size));
        let buffer_clone = buffer.clone();
        thread::spawn(move || {
            let mut file = None;
            if let Some(op) = log_op {
                file = Some(Logger::open_log_file(op));
            }
            loop {
                if let Some(entry) = buffer_clone.pop() {
                    let mut message = (entry.closure)();

                    match entry.log_to {
                        LogTo::File => {
                            message.push('\n');
                            let f = file.as_mut().unwrap();
                            f.write_all(message.as_bytes()).unwrap();
                            f.flush().unwrap();
                        }
                        LogTo::Ephemeral => println!("{}", message),
                    };
                } else if buffer_clone.should_shutdown() && buffer_clone.is_empty() {
                    break;
                } else {
                    buffer_clone.wait_for_new_entries();
                }
            }
        });

        let file = log_op.map(Logger::open_log_file);

        Logger {
            buffer,
            file,
            log_to: log_op.map_or(LogTo::Ephemeral, |_| LogTo::File),
            with_time: false,
        }
    }

    fn open_log_file(op: LoggerFileOptions) -> File {
        File::options()
            .write(true)
            .append(op.append_mode)
            .create(true)
            .open(op.path)
            .unwrap()
    }

    #[track_caller]
    fn log<F, T>(&self, level: &'static str, f: F)
    where
        F: FnOnce() -> T + Send + 'static,
        T: AsRef<str>,
    {
        let location = std::panic::Location::caller();
        let file_line = format!("{}:{}", location.file(), location.line());
        let tt = self.with_time.clone();
        let mut entry = LogEntry {
            closure: Box::new(move || {
                let time = match tt {
                    true => format!(
                        "{}",
                        chrono::offset::Local::now().format("%Y-%m-%d %H:%M:%S ")
                    ),
                    false => String::new(),
                };
                let message = f();
                format!("{}{} {} {}", time, file_line, level, message.as_ref())
            }),
            log_to: self.log_to.clone(),
        };

        while !self.buffer.push(&mut entry) {
            thread::yield_now(); // Wait if the buffer is full
        }
    }

    pub fn with_log_type(mut self, t: LogTo) -> Self {
        self.log_to = t;
        self
    }

    pub fn with_time(mut self, time: bool) -> Self {
        self.with_time = time;
        self
    }

    /// Waits until all messages are logged
    pub fn shutdown(&self) {
        self.buffer.shutdown();
        while !self.buffer.is_empty() {
            thread::yield_now();
        }

        if let Some(ref file) = self.file {
            file.sync_all().unwrap();
        }
    }

    #[track_caller]
    pub fn info<F, T>(&self, f: F)
    where
        F: FnOnce() -> T + Send + 'static,
        T: AsRef<str>,
    {
        self.log("\x1b[32m[INFO]\x1b[0m", f);
    }

    #[track_caller]
    pub fn error<F, T>(&self, f: F)
    where
        F: FnOnce() -> T + Send + 'static,
        T: AsRef<str>,
    {
        self.log("\x1b[31m[ERROR]\x1b[0m", f);
    }

    #[track_caller]
    pub fn debug<F, T>(&self, f: F)
    where
        F: FnOnce() -> T + Send + 'static,
        T: AsRef<str>,
    {
        self.log("\x1b[36m[DEBUG]\x1b[0m", f);
    }

    #[track_caller]
    pub fn warning<F, T>(&self, f: F)
    where
        F: FnOnce() -> T + Send + 'static,
        T: AsRef<str>,
    {
        self.log("\x1b[33m[WARNING]\x1b[0m", f);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    fn setup() {
        fs::File::options()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open("log.txt")
            .unwrap();
    }

    fn teardown() {
        fs::remove_file("log.txt").unwrap();
    }

    #[test]
    fn run_test_sequentially() {
        simple_to_file();
        correct_ord();
        tt();
    }

    fn tt() {
        setup();
        let logger = Logger::builder(1024, None).with_time(true);
        logger.info(|| String::new());
        logger.info(|| String::from("hello"));
        logger.debug(|| "foo");
        let logger = logger.with_time(false);
        logger.error(|| "bar");
        logger.warning(|| "world");
        logger.shutdown();
        teardown();
    }

    fn simple_to_file() {
        setup();
        let o = LoggerFileOptions {
            path: "log.txt",
            append_mode: false,
        };
        let logger = Logger::builder(1024, Some(o)).with_time(false);
        logger.info(|| "to file".to_owned());
        logger.shutdown();
        let bytes = fs::read(o.path).unwrap();
        teardown();
        assert_eq!(
            String::from_utf8(bytes).unwrap(),
            "src/lib.rs:314 \u{1b}[32m[INFO]\u{1b}[0m to file\n".to_owned()
        );
    }

    fn correct_ord() {
        setup();
        let o = LoggerFileOptions {
            path: "log.txt",
            append_mode: false,
        };
        let logger = Logger::builder(1024 * 1024, Some(o));
        for i in 0..100_000 {
            logger.debug(move || format!("{}", i));
        }

        logger.shutdown();

        for (i, line) in fs::read_to_string("log.txt").unwrap().lines().enumerate() {
            assert_eq!(
                line,
                format!("src/lib.rs:332 \u{1b}[36m[DEBUG]\u{1b}[0m {}", i)
            );
        }
        teardown();
    }
}