Skip to content

Commit

Permalink
Added a basic logger helper for users to get started easily
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent ca7fdd2 commit 118818a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ members = [
"examples/pluginload",
"examples/simplelogger",
"examples/v4lsrc"
]
, "copper_helpers"]
resolver = "2"


Expand Down
2 changes: 1 addition & 1 deletion copper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ copper-traits = { path = "../copper_traits" }
copper-log = { path = "../copper_log" }
copper-log-derive = { path = "../copper_log_derive" }
copper-log-runtime = { path = "../copper_log_runtime" }
copper-clock = { path = "../copper_clock" }
copper-clock = { path = "../copper_clock" }
2 changes: 1 addition & 1 deletion copper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod copperlist;
pub mod config;
pub mod copperlist;
pub mod curuntime;
pub mod cutask;
pub mod monitoring;
Expand Down
6 changes: 3 additions & 3 deletions copper_datalogger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::{File, OpenOptions};
use std::io;
use std::io::BufReader;
use std::io::Read;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::slice::from_raw_parts_mut;
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -153,8 +153,8 @@ impl DataLoggerBuilder {
}
}

pub fn file_path(mut self, file_path: &PathBuf) -> Self {
self.file_path = Some(file_path.clone());
pub fn file_path(mut self, file_path: &Path) -> Self {
self.file_path = Some(file_path.to_path_buf());
self
}

Expand Down
16 changes: 16 additions & 0 deletions copper_helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "copper-helpers"
version = "0.1.0"
authors = ["Guillaume Binet <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
keywords = ["robotics", "middleware", "copper", "real-time"]
categories = ["science::robotics"]

[dependencies]
copper-traits = { path = "../copper_traits" }
copper-datalogger = { path = "../copper_datalogger" }
copper-log = { path = "../copper_log" }
copper-log-runtime = { path = "../copper_log_runtime" }
copper-clock = { path = "../copper_clock" }
simplelog = "0.12.2"
53 changes: 53 additions & 0 deletions copper_helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use std::path::Path;
use std::sync::{Arc, Mutex};

/// This is a basic setup for a copper application to get you started.
/// Duplicate and customize as needed when your needs grow.
///
/// text_log: if true, the log will be printed to the console as a simple log.
/// It is useful to debug an application in real-time but should be set to false in production
/// as it is an order of magnitude slower than the default copper structured logging.
/// It will create a LoggerRuntime that can be used as a robot clock source too.
pub fn basic_logger_runtime_setup(
datalogger_output_path: &Path,
text_log: bool,
) -> CuResult<LoggerRuntime> {
let DataLogger::Write(logger) = DataLoggerBuilder::new()
.write(true)
.create(true)
.file_path(datalogger_output_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 stream = stream_write(data_logger.clone(), DataLogType::StructuredLogLine, 1024);

let extra = if text_log {
let slow_text_logger = TermLogger::new(
LevelFilter::Debug,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
);

// This is the path to the index file that was created at build time.
// depending if we build with debug pick it from debug or release:
let log_index_path = default_log_index_dir();

let extra_text_logger: ExtraTextLogger =
ExtraTextLogger::new(log_index_path, slow_text_logger);
Some(extra_text_logger)
} else {
None
};
Ok(LoggerRuntime::init(RobotClock::default(), stream, extra))
}
5 changes: 2 additions & 3 deletions examples/cu_caterpillar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ categories = ["science::robotics"]
[dependencies]
copper-derive = { path = "../../copper_derive" }
copper = { path = "../../copper" }
copper-helpers = { path = "../../copper_helpers" }
copper-log = { path = "../../copper_log" }
copper-log-derive = { path = "../../copper_log_derive" }
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"
serde = { version = "1.0.203", features = ["derive"] }
45 changes: 8 additions & 37 deletions examples/cu_caterpillar/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use copper::clock::{OptionCuTime, RobotClock};
use copper::clock::{ClockProvider, OptionCuTime, RobotClock};
use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle};
use copper::{CuResult, DataLogType};
use copper_datalogger::{stream_write, DataLogger, DataLoggerBuilder};
use copper::CuResult;
use copper_derive::copper_runtime;
use copper_log::default_log_index_dir;
use copper_helpers::basic_logger_runtime_setup;
use copper_log_derive::debug;
use copper_log_runtime::{ExtraTextLogger, LoggerRuntime};
use cu_rp_gpio::RPGpioMsg;
use serde::{Deserialize, Serialize};
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;

Expand Down Expand Up @@ -76,40 +72,15 @@ impl CuTask for CaterpillarTask {
}

fn main() {
let path: PathBuf = PathBuf::from("/tmp/caterpillar.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 stream = stream_write(data_logger.clone(), DataLogType::StructuredLogLine, 1024);

let slow_text_logger = TermLogger::new(
LevelFilter::Debug,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
);

// This is the path to the index file that was created at build time.
// 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();
let _needed = LoggerRuntime::init(clock.clone(), stream, Some(extra)); // with the slow textual logger on top.
let logger_runtime =
basic_logger_runtime_setup(&PathBuf::from("/tmp/caterpillar.copper"), true)
.expect("Failed to setup logger.");
let clock = logger_runtime.get_clock();
debug!("Application created.");
let mut application =
TheVeryHungryCaterpillar::new(clock.clone()).expect("Failed to create runtime.");
debug!("Running... starting clock: {}.", clock.now());
application.run(2).expect("Failed to run application.");
debug!("End of program.");
drop(_needed);
debug!("End of program: {}.", clock.now());
sleep(Duration::from_secs(1));
}

0 comments on commit 118818a

Please sign in to comment.