From a0a353b6ef2426fd13687645ff8a95bfbdceb489 Mon Sep 17 00:00:00 2001 From: Guillaume Binet Date: Fri, 31 May 2024 10:41:47 -0400 Subject: [PATCH] General cleanup better feedback at interned string build up removed warnings changed the logic of the callsites, it was not the right signature --- copper/src/curuntime.rs | 8 +-- copper/src/monitoring.rs | 4 +- copper_log/src/index.rs | 60 +++++++++++------- copper_log/src/lib.rs | 11 +++- .../test/copper_log_index/lock.mdb | Bin 8192 -> 8192 bytes copper_log_runtime/src/lib.rs | 1 + copper_log_test/build.rs | 5 +- copper_log_test/src/main.rs | 1 - copper_value/src/lib.rs | 2 - 9 files changed, 55 insertions(+), 37 deletions(-) diff --git a/copper/src/curuntime.rs b/copper/src/curuntime.rs index 5a50f80ae..a5767fb19 100644 --- a/copper/src/curuntime.rs +++ b/copper/src/curuntime.rs @@ -51,7 +51,7 @@ mod tests { pub struct TestSource {} impl CuTaskLifecycle for TestSource { - fn new(config: Option<&NodeInstanceConfig>) -> CuResult + fn new(_config: Option<&NodeInstanceConfig>) -> CuResult where Self: Sized, { @@ -61,7 +61,7 @@ mod tests { impl CuSrcTask for TestSource { type Payload = (); - fn process(&mut self, empty_msg: &mut CuMsg) -> CuResult<()> { + fn process(&mut self, _empty_msg: &mut CuMsg) -> CuResult<()> { Ok(()) } } @@ -69,7 +69,7 @@ mod tests { pub struct TestSink {} impl CuTaskLifecycle for TestSink { - fn new(config: Option<&NodeInstanceConfig>) -> CuResult + fn new(_config: Option<&NodeInstanceConfig>) -> CuResult where Self: Sized, { @@ -80,7 +80,7 @@ mod tests { impl CuSinkTask for TestSink { type Input = (); - fn process(&mut self, input: &CuMsg) -> CuResult<()> { + fn process(&mut self, _input: &CuMsg) -> CuResult<()> { Ok(()) } } diff --git a/copper/src/monitoring.rs b/copper/src/monitoring.rs index 4641ce8ba..22d2f4eed 100644 --- a/copper/src/monitoring.rs +++ b/copper/src/monitoring.rs @@ -81,7 +81,7 @@ impl Drop for ScopedAllocCounter { pub struct MonitoringTask {} impl CuTaskLifecycle for MonitoringTask { - fn new(config: Option<&NodeInstanceConfig>) -> CuResult + fn new(_config: Option<&NodeInstanceConfig>) -> CuResult where Self: Sized, { @@ -102,7 +102,7 @@ impl CuTaskLifecycle for MonitoringTask { impl CuSrcTask for MonitoringTask { type Payload = (); - fn process(&mut self, empty_msg: &mut CuMsg) -> CuResult<()> { + fn process(&mut self, _empty_msg: &mut CuMsg) -> CuResult<()> { Ok(()) } } diff --git a/copper_log/src/index.rs b/copper_log/src/index.rs index 2ab0d26cf..7f3bb7bff 100644 --- a/copper_log/src/index.rs +++ b/copper_log/src/index.rs @@ -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; type MStore = MultiStore; 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 { + 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> = { 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::(&path).unwrap(); Mutex::new(env) }; @@ -51,7 +80,7 @@ pub fn intern_string(s: &str) -> Option { 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); }; } @@ -67,29 +96,12 @@ pub fn intern_string(s: &str) -> Option { 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 { - 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 { + intern_string(format!("{}:{}", filename, line_number).as_str()) } const COUNTER_KEY: &str = "__counter__"; diff --git a/copper_log/src/lib.rs b/copper_log/src/lib.rs index 49b616a30..60919b00e 100644 --- a/copper_log/src/lib.rs +++ b/copper_log/src/lib.rs @@ -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; @@ -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."); @@ -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! { diff --git a/copper_log_reader/test/copper_log_index/lock.mdb b/copper_log_reader/test/copper_log_index/lock.mdb index 827941176e894a54234d63d0fcb834e911708c13..b2de4835a80372d1828c7aa8a38f3a40baf4eddc 100644 GIT binary patch delta 18 ZcmZp0XmFS?iRD2SU){!q@$wTBxB*Bo2T1?` delta 18 ZcmZp0XmFS?iAAB}clE}F@$wTBxB*DH2Y~

Sender CuResult<()> { if let Some(queue) = QUEUE.get() { diff --git a/copper_log_test/build.rs b/copper_log_test/build.rs index e61c5f35f..f17b87124 100644 --- a/copper_log_test/build.rs +++ b/copper_log_test/build.rs @@ -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); } diff --git a/copper_log_test/src/main.rs b/copper_log_test/src/main.rs index fc007a7b1..34b32a348 100644 --- a/copper_log_test/src/main.rs +++ b/copper_log_test/src/main.rs @@ -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}; diff --git a/copper_value/src/lib.rs b/copper_value/src/lib.rs index 85c200304..1f0d00211 100644 --- a/copper_value/src/lib.rs +++ b/copper_value/src/lib.rs @@ -12,8 +12,6 @@ mod benc; mod de; mod ser; -pub use bdec::*; -pub use benc::*; pub use de::*; pub use ser::*;