feat: tracing to the Home Assistant logger

This commit is contained in:
2025-03-17 21:48:50 -04:00
parent 2a99e67d7c
commit c127e478b8
2 changed files with 92 additions and 6 deletions

View File

@@ -4,20 +4,32 @@ use home_assistant::home_assistant::HomeAssistant;
use pyo3::prelude::*;
use shadow_rs::shadow;
use tokio::time::interval;
use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing::{level_filters::LevelFilter, Level};
use tracing_subscriber::{
fmt::{self, format::FmtSpan},
layer::SubscriberExt,
registry,
util::SubscriberInitExt,
Layer,
};
use tracing_to_home_assistant::TracingToHomeAssistant;
mod arbitrary;
mod home_assistant;
mod python_utils;
mod tracing_to_home_assistant;
shadow!(build_info);
async fn real_main(home_assistant: HomeAssistant) -> ! {
tracing_subscriber::fmt()
.with_max_level(Level::TRACE)
.with_span_events(FmtSpan::ACTIVE)
registry()
.with(
fmt::layer()
.pretty()
.with_span_events(FmtSpan::ACTIVE)
.with_filter(LevelFilter::from_level(Level::TRACE)),
)
.with(TracingToHomeAssistant)
.init();
let built_at = build_info::BUILD_TIME;

View File

@@ -0,0 +1,74 @@
use std::fmt::Debug;
use pyo3::Python;
use tracing::{
field::{Field, Visit},
Event, Level, Subscriber,
};
use tracing_subscriber::{layer::Context, Layer};
use crate::home_assistant::logger::{HassLogger, LogData};
pub struct TracingToHomeAssistant;
impl<S: Subscriber> Layer<S> for TracingToHomeAssistant {
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
let meta = event.metadata();
let file = meta.file();
let level = meta.level();
let line = meta.line();
let target = meta.target();
let mut msg = String::new();
struct StringVisitor<'a> {
s: &'a mut String,
}
impl<'a> Visit for StringVisitor<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
let field_name = field.name();
if field_name != "message" {
self.s.push_str(field_name);
self.s.push_str(" = ");
}
self.s.push_str(&format!("{value:?}"));
self.s.push_str(", ");
}
}
let mut visitor = StringVisitor { s: &mut msg };
event.record(&mut visitor);
if let Some(file) = file {
msg.push_str(&format!("at {file}"));
if let Some(line) = line {
msg.push_str(&format!(":{line}"));
}
}
let args = vec![];
let log_data: Option<LogData<()>> = None;
Python::with_gil(|py| {
let Ok(hass_logger) = HassLogger::new(py, target) else {
return;
};
if *level == Level::TRACE {
// Errors are ignored because there's nowhere to report them besides
// through the tracer itself!
let _ = hass_logger.debug(py, &msg, args, log_data);
} else if *level == Level::DEBUG {
let _ = hass_logger.debug(py, &msg, args, log_data);
} else if *level == Level::INFO {
let _ = hass_logger.info(py, &msg, args, log_data);
} else if *level == Level::WARN {
let _ = hass_logger.warning(py, &msg, args, log_data);
} else if *level == Level::ERROR {
let _ = hass_logger.error(py, &msg, args, log_data);
} else {
unreachable!("those are all 5 levels");
}
});
}
}