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: &str) -> Bytes {
let mut inner = LOG_BUFFER.lock().unwrap();
let buffer = inner.entry(id.to_string())
.or_insert_with(|| 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_else(|_| "global".into());
let mut inner = LOG_BUFFER.lock().unwrap();
let buffer = inner.entry(id)
.or_insert_with(|| BytesMut::with_capacity(256));
buffer.put(buf);
}