Skip to content

Commit

Permalink
decouple cu-consolemon from core crates
Browse files Browse the repository at this point in the history
  • Loading branch information
AS1100K committed Jan 17, 2025
1 parent a83bf44 commit a1379af
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 28 deletions.
6 changes: 2 additions & 4 deletions components/monitors/cu_consolemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,9 @@ impl CuMonitor for CuConsoleMon {

// Override the cu29-log-runtime Log Subscriber
#[cfg(debug_assertions)]
if cu29_log_runtime::EXTRA_TEXT_LOGGER
.set(Some(Box::new(log_subscriber) as Box<dyn Log>))
.is_err()
{
eprintln!("Failed to override default log subscriber of cu29-log-runtime\nIf you are using `basic_copper_setup` function provided by `cu29-helper` crate, please pass `monitor_subscriber` as true");
*cu29_log_runtime::EXTRA_TEXT_LOGGER.write().unwrap() =
Some(Box::new(log_subscriber) as Box<dyn Log>);
}

ui.run_app(&mut terminal).expect("Failed to run app");
Expand Down
2 changes: 1 addition & 1 deletion components/sources/cu_v4l/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod linux_impl {
TerminalMode::Mixed,
ColorChoice::Auto,
);
let _logger = LoggerRuntime::init(clock.clone(), NullLog {}, false, Some(*term_logger));
let _logger = LoggerRuntime::init(clock.clone(), NullLog {}, Some(*term_logger));

let rec = RecordingStreamBuilder::new("Camera Viz")
.spawn()
Expand Down
2 changes: 1 addition & 1 deletion core/cu29_export/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ mod tests {
};
let data_logger = Arc::new(Mutex::new(logger));
let stream = stream_write(data_logger.clone(), UnifiedLogType::StructuredLogLine, 1024);
let rt = LoggerRuntime::init(RobotClock::default(), stream, false, None::<NullLog>);
let rt = LoggerRuntime::init(RobotClock::default(), stream, None::<NullLog>);

let mut entry = CuLogEntry::new(4); // this is a "Just a String {}" log line
entry.add_param(0, Value::String("Parameter for the log line".into()));
Expand Down
7 changes: 1 addition & 6 deletions core/cu29_helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ pub fn basic_copper_setup(
let extra: Option<TermLogger> = None;

let clock = clock.unwrap_or_default();
#[cfg(debug_assertions)]
let monitor_subscriber = true;
#[cfg(not(debug_assertions))]
let monitor_subscriber = false;
let structured_logging =
LoggerRuntime::init(clock.clone(), structured_stream, monitor_subscriber, extra);
let structured_logging = LoggerRuntime::init(clock.clone(), structured_stream, extra);
Ok(CopperContext {
unified_logger: unified_logger.clone(),
logger_runtime: structured_logging,
Expand Down
1 change: 1 addition & 0 deletions core/cu29_log_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cu29-clock = { workspace = true }
bincode = { workspace = true }
smallvec = "1.13.2"
log = "0.4.22"
once_cell = "1.20"

[dev-dependencies]
cu29-value = { workspace = true }
26 changes: 11 additions & 15 deletions core/cu29_log_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use cu29_traits::{CuResult, WriteStream};
use log::Log;

#[cfg(debug_assertions)]
use cu29_log::format_logline;
#[cfg(debug_assertions)]
use log::Record;
#[cfg(debug_assertions)]
use std::collections::HashMap;
use {
cu29_log::format_logline, once_cell::sync::Lazy, std::collections::HashMap, std::sync::RwLock,
};

use std::fmt::{Debug, Formatter};
use std::fs::File;
Expand All @@ -36,9 +34,10 @@ type WriterPair = (Mutex<LogWriter>, RobotClock);
static WRITER: OnceLock<WriterPair> = OnceLock::new();

#[cfg(debug_assertions)]
pub static EXTRA_TEXT_LOGGER: OnceLock<Option<Box<dyn Log>>> = OnceLock::new();
pub static EXTRA_TEXT_LOGGER: Lazy<RwLock<Option<Box<dyn Log + 'static>>>> =
Lazy::new(|| RwLock::new(None));

pub struct NullLog {}
pub struct NullLog;
impl Log for NullLog {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
false
Expand All @@ -54,11 +53,9 @@ pub struct LoggerRuntime {}
impl LoggerRuntime {
/// destination is the binary stream in which we will log the structured log.
/// `extra_text_logger` is the logger that will log the text logs in real time. This is slow and only for debug builds.
/// `custom_logger`, if true _(only in `dev` profile)_ will not set `extra_text_logger` and will allow you to set the same using [EXTRA_TEXT_LOGGER]
pub fn init(
clock: RobotClock,
destination: impl WriteStream<CuLogEntry> + 'static,
#[allow(unused_variables)] custom_logger: bool,
#[allow(unused_variables)] extra_text_logger: Option<impl Log + 'static>,
) -> Self {
let runtime = LoggerRuntime {};
Expand All @@ -74,9 +71,8 @@ impl LoggerRuntime {
.unwrap();
}
#[cfg(debug_assertions)]
if !custom_logger {
let _ = EXTRA_TEXT_LOGGER
.set(extra_text_logger.map(|logger| Box::new(logger) as Box<dyn Log>));
if let Some(logger) = extra_text_logger {
*EXTRA_TEXT_LOGGER.write().unwrap() = Some(Box::new(logger) as Box<dyn Log>);
}

runtime
Expand Down Expand Up @@ -142,11 +138,11 @@ pub fn log_debug_mode(
) -> CuResult<()> {
log(entry)?;

let guarded_logger = EXTRA_TEXT_LOGGER.get();
let guarded_logger = EXTRA_TEXT_LOGGER.read().unwrap();
if guarded_logger.is_none() {
return Ok(());
}
if let Some(logger) = guarded_logger.unwrap() {
if let Some(logger) = guarded_logger.as_ref() {
let fstr = format_str.to_string();
// transform the slice into a hashmap
let params: Vec<String> = entry.params.iter().map(|v| v.to_string()).collect();
Expand All @@ -162,7 +158,7 @@ pub fn log_debug_mode(
.collect();
let logline = format_logline(entry.time, &fstr, params.as_slice(), &named_params)?;
logger.log(
&Record::builder()
&log::Record::builder()
.args(format_args!("{logline}"))
.level(log::Level::Info)
.target("cu29_log")
Expand Down
2 changes: 1 addition & 1 deletion examples/cu_standalone_structlog/src/structlog_perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let clock = RobotClock::new();
let bf = {
let writer = SimpleFileWriter::new(&PathBuf::from(LOG_FILE)).unwrap();
let _log_runtime = LoggerRuntime::init(clock.clone(), writer, false, None::<NullLog>);
let _log_runtime = LoggerRuntime::init(clock.clone(), writer, None::<NullLog>);
let bf: CuTime = clock.now();
for i in 0..1_000_000 {
debug!(
Expand Down

0 comments on commit a1379af

Please sign in to comment.