Skip to content

Commit

Permalink
User friendliness: exposed the default log index
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent 7c80485 commit ca7fdd2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
22 changes: 22 additions & 0 deletions copper_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ pub use copper_value as value;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Display;
use std::path::{Path, PathBuf};
use strfmt::strfmt;
use value::Value;

/// The name of the directory where the log index is stored.
const INDEX_DIR_NAME: &str = "copper_log_index";

#[allow(dead_code)]
pub const ANONYMOUS: u32 = 0;

Expand Down Expand Up @@ -90,3 +94,21 @@ pub fn rebuild_logline(all_interned_strings: &Vec<String>, entry: &CuLogEntry) -
)
})
}

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
}

/// Convenience function to returns the default path for the log index directory.
pub fn default_log_index_dir() -> PathBuf {
let outdir = std::env::var("OUT_DIR").expect("no OUT_DIR set, build.rs must be broken");
let outdir_path = Path::new(&outdir);
let target_dir = parent_n_times(&outdir_path, 3)
.unwrap()
.join(INDEX_DIR_NAME);
target_dir
}
19 changes: 3 additions & 16 deletions copper_log_derive/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use copper_log::default_log_index_dir;
use lazy_static::lazy_static;
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, 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 {
Expand All @@ -20,21 +18,10 @@ macro_rules! build_log {
};
}

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 outdir_path = Path::new(&outdir);
let target_dir = parent_n_times(&outdir_path, 3)
.unwrap()
.join(INDEX_DIR_NAME);

let target_dir = default_log_index_dir();

// Should never happen I believe.
if !target_dir.exists() {
Expand Down
4 changes: 1 addition & 3 deletions examples/cu_caterpillar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ copper-log-runtime = { path = "../../copper_log_runtime" }
copper-datalogger = { path = "../../copper_datalogger" }
cu-rp-gpio = { path = "../cu_rp_gpio" }
serde = { version = "1.0.203", features = ["derive"] }
simplelog = "0.12.2"


simplelog = "0.12.2"
7 changes: 3 additions & 4 deletions examples/cu_caterpillar/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::env;
fn main() {
println!(
"cargo:rustc-env=OUT_DIR={}",
std::env::var("OUT_DIR").unwrap()
);
let out_dir = env::var("OUT_DIR").unwrap();
println!("cargo:rustc-env=OUT_DIR={}", out_dir);
}
8 changes: 3 additions & 5 deletions examples/cu_caterpillar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle};
use copper::{CuResult, DataLogType};
use copper_datalogger::{stream_write, DataLogger, DataLoggerBuilder};
use copper_derive::copper_runtime;
use copper_log::default_log_index_dir;
use copper_log_derive::debug;
use copper_log_runtime::{ExtraTextLogger, LoggerRuntime};
use cu_rp_gpio::RPGpioMsg;
Expand Down Expand Up @@ -89,7 +90,6 @@ fn main() {
let data_logger = Arc::new(Mutex::new(logger));
let stream = stream_write(data_logger.clone(), DataLogType::StructuredLogLine, 1024);

//
let slow_text_logger = TermLogger::new(
LevelFilter::Debug,
Config::default(),
Expand All @@ -98,10 +98,8 @@ fn main() {
);

// This is the path to the index file that was created at build time.
let log_index_path = PathBuf::from("../..").join("target/debug/copper_log_index");
println!("log_index_path: {:?}", log_index_path);

// .join("target/debug/copper_log_index");
// depending if we build with debug pick it from debug or release:
let log_index_path = default_log_index_dir();

let extra: ExtraTextLogger = ExtraTextLogger::new(log_index_path, slow_text_logger);
let clock = RobotClock::default();
Expand Down

0 comments on commit ca7fdd2

Please sign in to comment.