Compare commits
2 Commits
2a99e67d7c
...
a25079c813
Author | SHA1 | Date | |
---|---|---|---|
a25079c813 | |||
c127e478b8 |
28
Cargo.lock
generated
28
Cargo.lock
generated
@@ -841,9 +841,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3"
|
name = "pyo3"
|
||||||
version = "0.23.5"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872"
|
checksum = "7f1c6c3591120564d64db2261bec5f910ae454f01def849b9c22835a84695e86"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -861,9 +861,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-async-runtimes"
|
name = "pyo3-async-runtimes"
|
||||||
version = "0.23.0"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "977dc837525cfd22919ba6a831413854beb7c99a256c03bf8624ad707e45810e"
|
checksum = "dd0b83dc42f9d41f50d38180dad65f0c99763b65a3ff2a81bf351dd35a1df8bf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
@@ -874,9 +874,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-build-config"
|
name = "pyo3-build-config"
|
||||||
version = "0.23.5"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb"
|
checksum = "e9b6c2b34cf71427ea37c7001aefbaeb85886a074795e35f161f5aecc7620a7a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
@@ -884,9 +884,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-ffi"
|
name = "pyo3-ffi"
|
||||||
version = "0.23.5"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d"
|
checksum = "5507651906a46432cdda02cd02dd0319f6064f1374c9147c45b978621d2c3a9c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"pyo3-build-config",
|
"pyo3-build-config",
|
||||||
@@ -894,9 +894,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-macros"
|
name = "pyo3-macros"
|
||||||
version = "0.23.5"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da"
|
checksum = "b0d394b5b4fd8d97d48336bb0dd2aebabad39f1d294edd6bcd2cccf2eefe6f42"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"pyo3-macros-backend",
|
"pyo3-macros-backend",
|
||||||
@@ -906,9 +906,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pyo3-macros-backend"
|
name = "pyo3-macros-backend"
|
||||||
version = "0.23.5"
|
version = "0.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028"
|
checksum = "fd72da09cfa943b1080f621f024d2ef7e2773df7badd51aa30a2be1f8caa7c8e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
@@ -1209,9 +1209,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "target-lexicon"
|
name = "target-lexicon"
|
||||||
version = "0.12.16"
|
version = "0.13.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
|
checksum = "e502f78cdbb8ba4718f566c418c52bc729126ffd16baee5baa718cf25dd5a69a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
|
@@ -20,8 +20,12 @@ derive_more = { version = "2.0.1", features = [
|
|||||||
] }
|
] }
|
||||||
ijson = "0.1.4"
|
ijson = "0.1.4"
|
||||||
itertools = "0.14.0"
|
itertools = "0.14.0"
|
||||||
pyo3 = { version = "0.23.0", features = ["chrono", "chrono-tz"] }
|
pyo3 = { version = "0.24.0", features = [
|
||||||
pyo3-async-runtimes = { version = "0.23.0", features = ["tokio-runtime"] }
|
"auto-initialize",
|
||||||
|
"chrono",
|
||||||
|
"chrono-tz",
|
||||||
|
] }
|
||||||
|
pyo3-async-runtimes = { version = "0.24.0", features = ["tokio-runtime"] }
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
shadow-rs = { version = "1.0.1", default-features = false }
|
shadow-rs = { version = "1.0.1", default-features = false }
|
||||||
snafu = "0.8.5"
|
snafu = "0.8.5"
|
||||||
|
24
src/lib.rs
24
src/lib.rs
@@ -4,20 +4,32 @@ use home_assistant::home_assistant::HomeAssistant;
|
|||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use shadow_rs::shadow;
|
use shadow_rs::shadow;
|
||||||
use tokio::time::interval;
|
use tokio::time::interval;
|
||||||
use tracing::Level;
|
use tracing::{level_filters::LevelFilter, Level};
|
||||||
use tracing_subscriber::fmt::format::FmtSpan;
|
use tracing_subscriber::{
|
||||||
|
fmt::{self, format::FmtSpan},
|
||||||
|
layer::SubscriberExt,
|
||||||
|
registry,
|
||||||
|
util::SubscriberInitExt,
|
||||||
|
Layer,
|
||||||
|
};
|
||||||
|
use tracing_to_home_assistant::TracingToHomeAssistant;
|
||||||
|
|
||||||
mod arbitrary;
|
mod arbitrary;
|
||||||
mod home_assistant;
|
mod home_assistant;
|
||||||
mod python_utils;
|
mod python_utils;
|
||||||
|
mod tracing_to_home_assistant;
|
||||||
|
|
||||||
shadow!(build_info);
|
shadow!(build_info);
|
||||||
|
|
||||||
async fn real_main(home_assistant: HomeAssistant) -> ! {
|
async fn real_main(home_assistant: HomeAssistant) -> ! {
|
||||||
tracing_subscriber::fmt()
|
registry()
|
||||||
.with_max_level(Level::TRACE)
|
.with(
|
||||||
.with_span_events(FmtSpan::ACTIVE)
|
fmt::layer()
|
||||||
.pretty()
|
.pretty()
|
||||||
|
.with_span_events(FmtSpan::ACTIVE)
|
||||||
|
.with_filter(LevelFilter::from_level(Level::TRACE)),
|
||||||
|
)
|
||||||
|
.with(TracingToHomeAssistant)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
let built_at = build_info::BUILD_TIME;
|
let built_at = build_info::BUILD_TIME;
|
||||||
|
74
src/tracing_to_home_assistant.rs
Normal file
74
src/tracing_to_home_assistant.rs
Normal 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");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user