Skip to content

Commit

Permalink
WIP on copper log
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 24, 2024
1 parent c18335f commit c28a88f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions copper_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ proc-macro = true
proc-macro2 = { version = "1.0.83", features = ["span-locations"] }
quote = "1.0.36"
syn = { version = "2.0.65", features = ["full"] }
rkv = { version = "0.19.0", features = ["lmdb"] }
lazy_static = { version = "1.4.0" }
83 changes: 83 additions & 0 deletions copper_log/src/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use lazy_static::lazy_static;
use rkv::backend::{BackendEnvironment, Lmdb, LmdbDatabase};
use rkv::backend::{LmdbEnvironment, LmdbRwTransaction};
use rkv::{Manager, MultiStore, Rkv, SingleStore, StoreOptions, Value, Writer};
use std::path::Path;
use std::sync::Mutex;

type SStore = SingleStore<LmdbDatabase>;
type MStore = MultiStore<LmdbDatabase>;

lazy_static! {
static ref RKV: Mutex<Rkv<LmdbEnvironment>> = {
let outdir = std::env::var("OUT_DIR").expect("no OUT_DIR set, build.rs must be broken");
let path = Path::new(&outdir).join("copper_log_index.lmdb");
//if !path.exists() {
// fs::create_dir_all(path).unwrap();
//}
let env = Rkv::new::<Lmdb>(&path).unwrap();
Mutex::new(env)
};
static ref DBS: Mutex<(SStore, SStore, SStore, MStore)> = {
let env = RKV.lock().unwrap();
let counter = env.open_single("counter", StoreOptions::create()).unwrap();
let index_to_string = env.open_single("index_to_string", StoreOptions::create()).unwrap();
let string_to_index = env.open_single("string_to_index", StoreOptions::create()).unwrap();
let index_to_callsites = env.open_multi("index_to_callsites", StoreOptions::create()).unwrap();

Mutex::new((counter, index_to_string, string_to_index, index_to_callsites))
};
}

fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option<u64> {
let mut env = RKV.lock().unwrap();
let (counter_store, index_to_string, string_to_index, index_to_callsite) =
&mut *DBS.lock().unwrap();
// If this string already exists in the store, return the index
{
let reader = env.read().unwrap();
// check if log_string is already in the string_to_index store
if let Ok(Some(Value::U64(index))) = string_to_index.get(&reader, log_string) {
return Some(index);
};
}
let mut writer = env.write().unwrap();
let next_index = get_next_index(&mut writer, counter_store).unwrap();
// Insert the new string into the store
index_to_string
.put(
&mut writer,
next_index.to_le_bytes(),
&Value::Str(log_string),
)
.unwrap();
string_to_index
.put(&mut writer, log_string, &Value::U64(next_index))
.unwrap();
index_to_callsite
.put(
&mut writer,
next_index.to_le_bytes(),
&Value::Str(format!("{}:{}", filename, line_number).as_str()),
)
.unwrap();
None
}

const COUNTER_KEY: &str = "__counter__";
fn get_next_index(
writer: &mut Writer<LmdbRwTransaction>,
counter_store: &SStore,
) -> Result<u64, Box<dyn std::error::Error>> {
// Get the current counter value
let current_counter = match counter_store.get(writer, COUNTER_KEY)? {
Some(Value::U64(value)) => value as u64,
_ => 0,
};

// Increment the counter
let next_counter = current_counter + 1;
counter_store.put(writer, COUNTER_KEY, &Value::U64(next_counter as u64))?;

Ok(next_counter)
}
5 changes: 3 additions & 2 deletions copper_log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod index;

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{parse_macro_input, LitStr};

Expand All @@ -13,7 +14,7 @@ pub fn debug(input: TokenStream) -> TokenStream {
let expanded = quote! {
{
let log_message = #message;
println!("Log: {:?}", log_message);
println!("{}:{} Log: {:?}", file!(), line!(), log_message);
log_message
}
};
Expand Down

0 comments on commit c28a88f

Please sign in to comment.