Skip to content

Commit

Permalink
datalogger -> unifiedlog
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent e05dc4f commit d0a58fe
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
"copper_log_test",
"copper_log_runtime",
"copper_log_reader",
"copper_datalogger",
"copper_unifiedlog",
"copper_clock",
"copper_traits",
"examples/cu_rp_gpio",
Expand All @@ -17,7 +17,7 @@ members = [
"examples/pluginload",
"examples/simplelogger",
"examples/v4lsrc"
, "copper_helpers"]
, "copper_helpers"]
resolver = "2"


Expand Down
2 changes: 1 addition & 1 deletion copper_helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories = ["science::robotics"]

[dependencies]
copper-traits = { path = "../copper_traits" }
copper-datalogger = { path = "../copper_datalogger" }
copper-unifiedlog = { path = "../copper_unifiedlog" }
copper-log = { path = "../copper_log" }
copper-log-runtime = { path = "../copper_log_runtime" }
copper-clock = { path = "../copper_clock" }
Expand Down
4 changes: 2 additions & 2 deletions copper_helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use copper_clock::RobotClock;
use copper_datalogger::{stream_write, DataLogger, DataLoggerBuilder};
use copper_log::default_log_index_dir;
use copper_log_runtime::{ExtraTextLogger, LoggerRuntime};
use copper_traits::{CuResult, DataLogType};
use copper_unifiedlog::{stream_write, UnifiedLogger, UnifiedLoggerBuilder};
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use std::path::Path;
use std::sync::{Arc, Mutex};
Expand All @@ -18,7 +18,7 @@ pub fn basic_logger_runtime_setup(
datalogger_output_path: &Path,
text_log: bool,
) -> CuResult<LoggerRuntime> {
let DataLogger::Write(logger) = DataLoggerBuilder::new()
let UnifiedLogger::Write(logger) = UnifiedLoggerBuilder::new()
.write(true)
.create(true)
.file_path(datalogger_output_path)
Expand Down
2 changes: 1 addition & 1 deletion copper_log_reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "src/cli.rs"
copper-traits = { path = "../copper_traits" }
copper-log = { path = "../copper_log" }
copper-clock = { path = "../copper_clock" }
copper-datalogger = { path = "../copper_datalogger" }
copper-unifiedlog = { path = "../copper_unifiedlog" }
clap = { version = "4.5.7", features = ["derive"] }
rkv = { version = "0.19.0", features = ["lmdb"] }
byteorder = "1.5.0"
Expand Down
6 changes: 3 additions & 3 deletions copper_log_reader/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{Parser, Subcommand};
use copper_datalogger::{DataLogger, DataLoggerBuilder, DataLoggerIOReader};
use copper_log_reader::full_log_dump;
use copper_traits::DataLogType;
use copper_unifiedlog::{UnifiedLogger, UnifiedLoggerBuilder, UnifiedLoggerIOReader};
use std::io::Read;
use std::path::PathBuf;

Expand All @@ -28,14 +28,14 @@ fn main() {
PathBuf::from("test/test.copper")
};

let DataLogger::Read(dl) = DataLoggerBuilder::new()
let UnifiedLogger::Read(dl) = UnifiedLoggerBuilder::new()
.file_path(&datalog)
.build()
.expect("Failed to create logger")
else {
panic!("Failed to create logger");
};

let reader = DataLoggerIOReader::new(dl, DataLogType::StructuredLogLine);
let reader = UnifiedLoggerIOReader::new(dl, DataLogType::StructuredLogLine);
full_log_dump(reader, &index).expect("Failed to dump log");
}
11 changes: 3 additions & 8 deletions copper_log_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ categories = ["science::robotics"]

[dependencies]
copper = { path = "../copper" }
copper-log-derive = { path = "../copper_log_derive" }
copper-log = { path = "../copper_log" }
copper-log-runtime = { path = "../copper_log_runtime" }
copper-datalogger = { path = "../copper_datalogger" }
copper-value = { path = "../copper_value" }
serde = { version = "1.0.202", features = ["derive"] }

[build-dependencies]
copper-log-derive = { path = "../copper_log_derive" }

copper-log-runtime = { path = "../copper_log_runtime" }
copper-helpers = { path = "../copper_helpers" }
serde = { version = "1.0.202", features = ["derive"] }
30 changes: 7 additions & 23 deletions copper_log_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
use copper::clock::RobotClock;
use copper::DataLogType;
use copper_datalogger::{stream_write, DataLogger, DataLoggerBuilder};
use copper::monitoring::ScopedAllocCounter;
use copper_helpers::basic_logger_runtime_setup;
use copper_log_derive::debug;
use copper_log_runtime::LoggerRuntime;
use serde::Serialize;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

fn main() {
let path: PathBuf = PathBuf::from("/tmp/teststructlog.copper");
let DataLogger::Write(logger) = DataLoggerBuilder::new()
.write(true)
.create(true)
.file_path(&path)
.preallocated_size(100000)
.build()
.expect("Failed to create logger")
else {
panic!("Failed to create logger")
};
let data_logger = Arc::new(Mutex::new(logger));
let clock = RobotClock::new();
let stream = stream_write(data_logger.clone(), DataLogType::StructuredLogLine, 1024);
let mut rt = LoggerRuntime::init(clock, stream, None);
let rt = basic_logger_runtime_setup(&PathBuf::from("/tmp/teststructlog.copper"), true)
.expect("Failed to setup logger.");
debug!("Logger created.");

#[derive(Serialize)]
struct Test {
a: i32,
b: i32,
}
let mytuple = (1, "toto", 3.34f64, true, 'a');
{
let hop = copper::monitoring::ScopedAllocCounter::new();
let hop = ScopedAllocCounter::new();
let gigantic_vec = vec![0u8; 1_000_000];
debug!("Just a string {}", "zarma");
debug!("anonymous param constants {} {}", 42u16, 43u8);
Expand All @@ -39,6 +25,4 @@ fn main() {
debug!("complex tuple", mytuple);
debug!("Struct", Test { a: 3, b: 4 });
}
debug!(" AFTER CLOSE {} ", "AFTER CLOSE");
rt.close();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "copper-datalogger"
name = "copper-unifiedlog"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
Expand Down
54 changes: 27 additions & 27 deletions copper_datalogger/src/lib.rs → copper_unifiedlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct SectionHeader {
/// A wrapper around a memory mapped file to write to.
struct MmapStream {
entry_type: DataLogType,
parent_logger: Arc<Mutex<DataLoggerWrite>>,
parent_logger: Arc<Mutex<UnifiedLoggerWrite>>,
current_slice: &'static mut [u8],
current_position: usize,
minimum_allocation_amount: usize,
Expand All @@ -52,7 +52,7 @@ struct MmapStream {
impl MmapStream {
fn new(
entry_type: DataLogType,
parent_logger: Arc<Mutex<DataLoggerWrite>>,
parent_logger: Arc<Mutex<UnifiedLoggerWrite>>,
current_slice: &'static mut [u8],
minimum_allocation_amount: usize,
) -> Self {
Expand Down Expand Up @@ -109,9 +109,9 @@ impl Drop for MmapStream {
}
}

/// Create a new stream to write to the datalogger.
/// Create a new stream to write to the unifiedlogger.
pub fn stream_write(
logger: Arc<Mutex<DataLoggerWrite>>,
logger: Arc<Mutex<UnifiedLoggerWrite>>,
entry_type: DataLogType,
minimum_allocation_amount: usize,
) -> impl WriteStream {
Expand All @@ -130,20 +130,20 @@ pub fn stream_write(
const DEFAULT_LOGGER_SIZE: usize = 1024 * 1024 * 1024; // 1GB

/// Holder of the read or write side of the datalogger.
pub enum DataLogger {
Read(DataLoggerRead),
Write(DataLoggerWrite),
pub enum UnifiedLogger {
Read(UnifiedLoggerRead),
Write(UnifiedLoggerWrite),
}

/// Use this builder to create a new DataLogger.
pub struct DataLoggerBuilder {
pub struct UnifiedLoggerBuilder {
file_path: Option<PathBuf>,
preallocated_size: Option<usize>,
write: bool,
create: bool,
}

impl DataLoggerBuilder {
impl UnifiedLoggerBuilder {
pub fn new() -> Self {
Self {
file_path: None,
Expand Down Expand Up @@ -173,7 +173,7 @@ impl DataLoggerBuilder {
self
}

pub fn build(self) -> io::Result<DataLogger> {
pub fn build(self) -> io::Result<UnifiedLogger> {
if self.create && !self.write {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
Expand Down Expand Up @@ -213,7 +213,7 @@ impl DataLoggerBuilder {
let nb_bytes = encode_into_slice(&main_header, &mut mmap[..], standard())
.expect("Failed to encode main header");
assert!(nb_bytes < page_size);
Ok(DataLogger::Write(DataLoggerWrite {
Ok(UnifiedLogger::Write(UnifiedLoggerWrite {
file,
mmap_buffer: mmap,
page_size,
Expand All @@ -233,7 +233,7 @@ impl DataLoggerBuilder {
"Invalid magic number in main header",
));
}
Ok(DataLogger::Read(DataLoggerRead {
Ok(UnifiedLogger::Read(UnifiedLoggerRead {
file,
mmap_buffer: mmap,
reading_position: main_header.first_section_offset as usize,
Expand All @@ -243,14 +243,14 @@ impl DataLoggerBuilder {
}

/// A read side of the datalogger.
pub struct DataLoggerRead {
pub struct UnifiedLoggerRead {
file: File,
mmap_buffer: Mmap,
reading_position: usize,
}

/// A write side of the datalogger.
pub struct DataLoggerWrite {
pub struct UnifiedLoggerWrite {
file: File,
mmap_buffer: MmapMut,
page_size: usize,
Expand All @@ -259,7 +259,7 @@ pub struct DataLoggerWrite {
flushed_until: usize,
}

impl DataLoggerWrite {
impl UnifiedLoggerWrite {
fn unsure_size(&mut self, size: usize) -> io::Result<()> {
// Here it is important that the memory map resizes in place.
// According to the documentation this is something unique to Linux to be able to do that.
Expand Down Expand Up @@ -346,7 +346,7 @@ impl DataLoggerWrite {
}
}

impl Drop for DataLoggerWrite {
impl Drop for UnifiedLoggerWrite {
fn drop(&mut self) {
self.add_section(DataLogType::LastEntry, 0);
self.flush();
Expand All @@ -356,7 +356,7 @@ impl Drop for DataLoggerWrite {
}
}

impl DataLoggerRead {
impl UnifiedLoggerRead {
pub fn read_next_section_type(
&mut self,
datalogtype: DataLogType,
Expand Down Expand Up @@ -433,16 +433,16 @@ impl DataLoggerRead {
}
}

/// This a a convience wrapper around the DataLoggerRead to implement the Read trait.
pub struct DataLoggerIOReader {
logger: DataLoggerRead,
/// This a a convience wrapper around the UnifiedLoggerRead to implement the Read trait.
pub struct UnifiedLoggerIOReader {
logger: UnifiedLoggerRead,
log_type: DataLogType,
buffer: Vec<u8>,
buffer_pos: usize,
}

impl DataLoggerIOReader {
pub fn new(logger: DataLoggerRead, log_type: DataLogType) -> Self {
impl UnifiedLoggerIOReader {
pub fn new(logger: UnifiedLoggerRead, log_type: DataLogType) -> Self {
Self {
logger,
log_type,
Expand All @@ -464,7 +464,7 @@ impl DataLoggerIOReader {
}
}

impl Read for DataLoggerIOReader {
impl Read for UnifiedLoggerIOReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.buffer_pos >= self.buffer.len() {
self.fill_buffer()?;
Expand All @@ -489,9 +489,9 @@ mod tests {
use super::*;
use std::path::PathBuf;
use tempfile::TempDir;
fn make_a_logger(tmp_dir: &TempDir) -> (Arc<Mutex<DataLoggerWrite>>, PathBuf) {
fn make_a_logger(tmp_dir: &TempDir) -> (Arc<Mutex<UnifiedLoggerWrite>>, PathBuf) {
let file_path = tmp_dir.path().join("test.bin");
let DataLogger::Write(data_logger) = DataLoggerBuilder::new()
let UnifiedLogger::Write(data_logger) = UnifiedLoggerBuilder::new()
.write(true)
.create(true)
.file_path(&file_path)
Expand All @@ -510,7 +510,7 @@ mod tests {
let tmp_dir = TempDir::new().expect("could not create a tmp dir");
let file_path = tmp_dir.path().join("test.bin");
let _used = {
let DataLogger::Write(mut logger) = DataLoggerBuilder::new()
let UnifiedLogger::Write(mut logger) = UnifiedLoggerBuilder::new()
.write(true)
.create(true)
.file_path(&file_path)
Expand Down Expand Up @@ -598,7 +598,7 @@ mod tests {
stream.log(&3u32).unwrap();
}
drop(logger);
let DataLogger::Read(mut dl) = DataLoggerBuilder::new()
let UnifiedLogger::Read(mut dl) = UnifiedLoggerBuilder::new()
.file_path(&f.to_path_buf())
.build()
.expect("Failed to build logger")
Expand Down

0 comments on commit d0a58fe

Please sign in to comment.