Files
PlantCtrl/Software/MainBoard/rust/src/webserver/get_log.rs

66 lines
1.7 KiB
Rust

use crate::fat_error::FatResult;
use crate::log::LOG_ACCESS;
use alloc::string::String;
use alloc::vec::Vec;
use edge_http::io::server::Connection;
use edge_nal::io::{Read, Write};
use serde::Serialize;
pub(crate) async fn get_log<T, const N: usize>(
conn: &mut Connection<'_, T, N>,
) -> FatResult<Option<u32>>
where
T: Read + Write,
{
let log = LOG_ACCESS.lock().await.get();
conn.initiate_response(
200,
Some("OK"),
&[
("Content-Type", "text/javascript"),
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "*"),
("Access-Control-Allow-Methods", "*"),
],
)
.await?;
conn.write_all("[".as_bytes()).await?;
let mut append = false;
for entry in log {
if append {
conn.write_all(",".as_bytes()).await?;
}
append = true;
let json = serde_json::to_string(&entry)?;
conn.write_all(json.as_bytes()).await?;
}
conn.write_all("]".as_bytes()).await?;
Ok(Some(200))
}
#[derive(Serialize)]
struct LiveLogEntry {
seq: u64,
text: String,
}
#[derive(Serialize)]
struct LiveLogResponse {
entries: Vec<LiveLogEntry>,
dropped: bool,
next_seq: u64,
}
pub(crate) async fn get_live_log(after: Option<u64>) -> FatResult<Option<String>> {
let (raw_entries, dropped, next_seq) = crate::log::INTERCEPTOR.get_live_logs(after);
let response = LiveLogResponse {
entries: raw_entries
.into_iter()
.map(|(seq, text)| LiveLogEntry { seq, text })
.collect(),
dropped,
next_seq,
};
Ok(Some(serde_json::to_string(&response)?))
}