Skip to content

Commit

Permalink
Improved the helper context.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent ec8eaef commit c8b65bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
34 changes: 26 additions & 8 deletions copper_helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@ use copper_clock::RobotClock;
use copper_log::default_log_index_dir;
use copper_log_runtime::{ExtraTextLogger, LoggerRuntime};
use copper_traits::{CuResult, UnifiedLogType};
use copper_unifiedlog::{stream_write, UnifiedLogger, UnifiedLoggerBuilder};
use copper_unifiedlog::{stream_write, UnifiedLogger, UnifiedLoggerBuilder, UnifiedLoggerWrite};
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use std::path::Path;
use std::sync::{Arc, Mutex};

/// Just a simple struct to hold the various bits needed to run a Copper application.
pub struct CopperContext {
pub unified_logger: Arc<Mutex<UnifiedLoggerWrite>>,
pub logger_runtime: LoggerRuntime,
pub clock: RobotClock,
}

/// 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,
pub fn basic_copper_setup(
unifiedlogger_output_path: &Path,
text_log: bool,
) -> CuResult<LoggerRuntime> {
) -> CuResult<CopperContext> {
let UnifiedLogger::Write(logger) = UnifiedLoggerBuilder::new()
.write(true)
.create(true)
.file_path(datalogger_output_path)
.file_path(unifiedlogger_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(), UnifiedLogType::StructuredLogLine, 1024);
let unified_logger = Arc::new(Mutex::new(logger));
let stream = stream_write(
unified_logger.clone(),
UnifiedLogType::StructuredLogLine,
1024,
);

let extra = if text_log {
let slow_text_logger = TermLogger::new(
Expand All @@ -49,5 +60,12 @@ pub fn basic_logger_runtime_setup(
} else {
None
};
Ok(LoggerRuntime::init(RobotClock::default(), stream, extra))

let clock = RobotClock::default();
let structured_logging = LoggerRuntime::init(clock.clone(), stream, extra);
Ok(CopperContext {
unified_logger: unified_logger.clone(),
logger_runtime: structured_logging,
clock,
})
}
4 changes: 2 additions & 2 deletions copper_log_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use copper::monitoring::ScopedAllocCounter;
use copper_helpers::basic_logger_runtime_setup;
use copper_helpers::basic_copper_setup;
use copper_log_derive::debug;
use serde::Serialize;
use std::path::PathBuf;

fn main() {
let rt = basic_logger_runtime_setup(&PathBuf::from("/tmp/teststructlog.copper"), true)
let _ = basic_copper_setup(&PathBuf::from("/tmp/teststructlog.copper"), true)
.expect("Failed to setup logger.");
debug!("Logger created.");

Expand Down
10 changes: 4 additions & 6 deletions examples/cu_caterpillar/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub mod tasks;

use copper::clock::ClockProvider;
use copper_derive::copper_runtime;
use copper_helpers::basic_logger_runtime_setup;
use copper_helpers::basic_copper_setup;
use copper_log_derive::debug;
use std::path::PathBuf;
use std::thread::sleep;
Expand All @@ -12,11 +11,10 @@ use std::time::Duration;
struct CaterpillarApplication {}

fn main() {
let logger_runtime =
basic_logger_runtime_setup(&PathBuf::from("/tmp/caterpillar.copper"), true)
.expect("Failed to setup logger.");
let copper_ctx = basic_copper_setup(&PathBuf::from("/tmp/caterpillar.copper"), true)
.expect("Failed to setup logger.");
debug!("Logger created.");
let clock = logger_runtime.get_clock();
let clock = copper_ctx.clock;
debug!("Creating application... ");
let mut application =
CaterpillarApplication::new(clock.clone()).expect("Failed to create runtime.");
Expand Down

0 comments on commit c8b65bf

Please sign in to comment.