Simple log file

This commit is contained in:
eraden 2023-05-25 23:51:26 +02:00
parent 84e5e3343a
commit ccf033eeed
3 changed files with 1210 additions and 48 deletions

1190
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,4 +10,9 @@ futures-util = { version = "0.3.28" }
futures = { version = "0.3.28" }
futures-async-runtime = { version = "0.2.1" }
tracing = { version = "0" }
serde = { version = "1.0.162" }
serde = { version = "1.0.162", features = ['derive'] }
flumedb = "*"
serde_json = "*"
async-std = { version = "*", features = ["attributes"] }
tide = "*"
gumdrop = "*"

View File

@ -1,3 +1,60 @@
fn main() {
println!("Hello, world!");
use flumedb::*;
use gumdrop::Options;
#[derive(Debug, thiserror::Error)]
enum Error {}
#[derive(Options)]
struct Opts {
help: bool,
log_path: String,
bind: String,
listen_port: u16,
broadcast_port: u16,
}
#[async_std::main]
async fn main() -> Result<(), Error> {
let path = {
let args = std::env::args();
let mut iter = args.into_iter();
iter
.next();
iter.next()
.expect("Log file path not found. Please privide path to log file")
};
eprintln!("File: {path}");
let mut log = OffsetLog::<u32>::from_file(
std::fs::OpenOptions::new()
.append(true)
.create(true)
.write(true)
.read(true)
.open(&path)
.expect("Failed to open log file"),
)
.expect("Failed to open log file");
let len = log.iter().count();
log.append(&len.to_le_bytes()).expect("Failed to write");
// Read the entry at offset 0
let r = log.read(0).expect("Failed to read at 0");
// `r.data` is a json string in a standard ssb log.
// `r.next` is the offset of the next entry.
let r = log.read(r.next);
log.iter().map(|r| {
eprintln!("{r:?}");
let (vec, _) = r.data.split_at(std::mem::size_of::<usize>());
usize::from_le_bytes(vec.try_into().unwrap())
})
.for_each(|v| eprintln!("value {v:?}"));
// log.iter()
// .map(|e| serde_json::from_slice::<serde_json::Value>(&e.data).unwrap())
// .for_each(|v| println!("{}", serde_json::to_string_pretty(&v).unwrap()));
Ok(())
}