Skip to content

Commit

Permalink
General cleanup
Browse files Browse the repository at this point in the history
better feedback at interned string build up
removed warnings
changed the logic of the callsites, it was not the right signature
  • Loading branch information
gbin committed May 31, 2024
1 parent 0595669 commit a0a353b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 37 deletions.
8 changes: 4 additions & 4 deletions copper/src/curuntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod tests {
pub struct TestSource {}

impl CuTaskLifecycle for TestSource {
fn new(config: Option<&NodeInstanceConfig>) -> CuResult<Self>
fn new(_config: Option<&NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Expand All @@ -61,15 +61,15 @@ mod tests {

impl CuSrcTask for TestSource {
type Payload = ();
fn process(&mut self, empty_msg: &mut CuMsg<Self::Payload>) -> CuResult<()> {
fn process(&mut self, _empty_msg: &mut CuMsg<Self::Payload>) -> CuResult<()> {
Ok(())
}
}

pub struct TestSink {}

impl CuTaskLifecycle for TestSink {
fn new(config: Option<&NodeInstanceConfig>) -> CuResult<Self>
fn new(_config: Option<&NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Expand All @@ -80,7 +80,7 @@ mod tests {
impl CuSinkTask for TestSink {
type Input = ();

fn process(&mut self, input: &CuMsg<Self::Input>) -> CuResult<()> {
fn process(&mut self, _input: &CuMsg<Self::Input>) -> CuResult<()> {
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions copper/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Drop for ScopedAllocCounter {
pub struct MonitoringTask {}

impl CuTaskLifecycle for MonitoringTask {
fn new(config: Option<&NodeInstanceConfig>) -> CuResult<Self>
fn new(_config: Option<&NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Expand All @@ -102,7 +102,7 @@ impl CuTaskLifecycle for MonitoringTask {
impl CuSrcTask for MonitoringTask {
type Payload = ();

fn process(&mut self, empty_msg: &mut CuMsg<Self::Payload>) -> CuResult<()> {
fn process(&mut self, _empty_msg: &mut CuMsg<Self::Payload>) -> CuResult<()> {
Ok(())
}
}
60 changes: 36 additions & 24 deletions copper_log/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,50 @@ use rkv::backend::{Lmdb, LmdbDatabase};
use rkv::backend::{LmdbEnvironment, LmdbRwTransaction};
use rkv::{MultiStore, Rkv, SingleStore, StoreOptions, Value, Writer};
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

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

const INDEX_DIR_NAME: &str = "copper_log_index";

const COLORED_PREFIX_BUILD_LOG: &str = "\x1b[32mCLog:\x1b[0m";

macro_rules! build_log {
($($arg:tt)*) => {
println!("{} {}", COLORED_PREFIX_BUILD_LOG, format!($($arg)*));
};
}

fn parent_n_times(path: &Path, n: usize) -> Option<PathBuf> {
let mut result = Some(path.to_path_buf());
for _ in 0..n {
result = result?.parent().map(PathBuf::from);
}
result
}

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");
let outdir_path = Path::new(&outdir);
let path = outdir_path.join(INDEX_DIR_NAME);
if !path.exists() {
fs::create_dir_all(&path).unwrap();
}
println!("CLog: Storing log index at: {:?}", path);
let target_dir_link = parent_n_times(&outdir_path, 3)
.unwrap()
.join(INDEX_DIR_NAME);
build_log!(
"=================================================================================="
);
build_log!("Path to interned strings storage: {:?}", target_dir_link);
build_log!(" [r] is reused index and [n] is new index.");
build_log!(
"=================================================================================="
);
let env = Rkv::new::<Lmdb>(&path).unwrap();
Mutex::new(env)
};
Expand Down Expand Up @@ -51,7 +80,7 @@ pub fn intern_string(s: &str) -> Option<IndexType> {
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, s) {
println!("CLog: Returning existing index #{} -> {}", index, s);
build_log!("#{:0>3} [r] -> {}.", index, s);
return Some(index as IndexType);
};
}
Expand All @@ -67,29 +96,12 @@ pub fn intern_string(s: &str) -> Option<IndexType> {
writer.commit().unwrap();
Some(next_index)
};
println!("CLog: Inserted #{} -> {}", index.unwrap(), s);
build_log!("#{:0>3} [n] -> {}.", index.unwrap(), s);
index
}

pub fn check_and_insert(filename: &str, line_number: u32, log_string: &str) -> Option<IndexType> {
let index = intern_string(log_string);
{
let (_, _, _, index_to_callsite) = &mut *DBS.lock().unwrap();
index?;
let lindex = index.unwrap();
let env = RKV.lock().unwrap();
let mut writer = env.write().unwrap();
index_to_callsite
.put(
&mut writer,
lindex.to_le_bytes(),
&Value::Str(format!("{}:{}", filename, line_number).as_str()),
)
.unwrap();
writer.commit().unwrap();
println!("CLog: Inserted #{} -> {}", lindex, log_string);
}
index
pub fn record_callsite(filename: &str, line_number: u32) -> Option<IndexType> {
intern_string(format!("{}:{}", filename, line_number).as_str())
}

const COUNTER_KEY: &str = "__counter__";
Expand Down
11 changes: 8 additions & 3 deletions copper_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod index;

extern crate proc_macro;

use crate::index::{check_and_insert, intern_string};
use crate::index::{intern_string, record_callsite};
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::Parser;
Expand All @@ -22,7 +22,7 @@ pub fn debug(input: TokenStream) -> TokenStream {
}) = msg_expr
{
let msg = msg.value();
let index = check_and_insert("dummy", 0, &msg).expect("Failed to insert log string.");
let index = intern_string(&msg).expect("Failed to insert log string.");
(index, msg)
} else {
panic!("The first parameter of the argument needs to be a string literal.");
Expand Down Expand Up @@ -65,7 +65,12 @@ pub fn debug(input: TokenStream) -> TokenStream {
let postfix = quote! {
// to do add conditional
println!("{} {}", msg, &log_entry);
copper_log_runtime::log(log_entry);
let r = copper_log_runtime::log(log_entry);
if let Err(e) = r {
eprintln!("Warning: Failed to log: {}", e);
let backtrace = std::backtrace::Backtrace::capture();
eprintln!("{:?}", backtrace);
}
};

let expanded = quote! {
Expand Down
Binary file modified copper_log_reader/test/copper_log_index/lock.mdb
Binary file not shown.
1 change: 1 addition & 0 deletions copper_log_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn initialize_queue(mut destination: impl Stream + 'static) -> Sender<CuLogEntry
}

/// Function called from generated code to log data.
/// It moves entry by design, it will be absorbed in the queue.
#[inline]
pub fn log(entry: CuLogEntry) -> CuResult<()> {
if let Some(queue) = QUEUE.get() {
Expand Down
5 changes: 4 additions & 1 deletion copper_log_test/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::env;
use std::path::Path;

const INDEX_DIR_NAME: &str = "copper_log_index";

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not defined");
println!("cargo:rustc-env=OUT_DIR={}", out_dir);
}
1 change: 0 additions & 1 deletion copper_log_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use copper::DataLogType;
use copper_datalogger::{stream, DataLogger};
use copper_log::debug;
use copper_log_runtime::LoggerRuntime;
use copper_value::to_value;
use serde::Serialize;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
Expand Down
2 changes: 0 additions & 2 deletions copper_value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ mod benc;
mod de;
mod ser;

pub use bdec::*;
pub use benc::*;
pub use de::*;
pub use ser::*;

Expand Down

0 comments on commit a0a353b

Please sign in to comment.