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
use std::collections::HashMap;
use std::sync::Mutex;
use bytes::{BufMut, Bytes, BytesMut};
use lazy_static::lazy_static;
use tokio::task_local;
lazy_static! {
static ref LOG_BUFFER: Mutex<HashMap<String, BytesMut>> = Mutex::new(HashMap::new());
}
task_local! {
#[allow(missing_docs)]
pub static LOG_ID: String;
}
pub fn fetch_buffer_contents(id: &String) -> Bytes {
let mut inner = LOG_BUFFER.lock().unwrap();
let buffer = inner.entry(id.clone())
.or_insert(BytesMut::with_capacity(256));
buffer.split().freeze()
}
pub fn write_to_log_buffer(buf: &[u8]) {
let id = LOG_ID.try_with(|id| id.clone()).unwrap_or("global".into());
let mut inner = LOG_BUFFER.lock().unwrap();
let buffer = inner.entry(id)
.or_insert(BytesMut::with_capacity(256));
buffer.put(buf);
}