numcodecs_wasm_logging/
lib.rs

1//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
2//!
3//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
4//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
5//!
6//! [MSRV]: https://img.shields.io/badge/MSRV-1.82.0-blue
7//! [repo]: https://github.com/juntyr/numcodecs-rs
8//!
9//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-wasm-logging
10//! [crates.io]: https://crates.io/crates/numcodecs-wasm-logging
11//!
12//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-wasm-logging
13//! [docs.rs]: https://docs.rs/numcodecs-wasm-logging/
14//!
15//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
16//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_wasm_logging
17//!
18//! Enable logging for wasm32-compiled [`StaticCodec`]s for the [`numcodecs`]
19//! API.
20
21use std::sync::Once;
22
23use numcodecs::{
24    AnyArray, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, StaticCodec, StaticCodecConfig,
25};
26
27#[derive(Clone)]
28/// Wrapper for a [`StaticCodec`] that automatically installs a logger.
29pub struct LoggingCodec<T: StaticCodec>(pub T);
30
31impl<T: StaticCodec> Codec for LoggingCodec<T> {
32    type Error = T::Error;
33
34    #[inline]
35    fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
36        ensure_logger();
37
38        self.0.encode(data)
39    }
40
41    #[inline]
42    fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
43        ensure_logger();
44
45        self.0.decode(encoded)
46    }
47
48    #[inline]
49    fn decode_into(
50        &self,
51        encoded: AnyArrayView,
52        decoded: AnyArrayViewMut,
53    ) -> Result<(), Self::Error> {
54        ensure_logger();
55
56        self.0.decode_into(encoded, decoded)
57    }
58}
59
60impl<T: StaticCodec> StaticCodec for LoggingCodec<T> {
61    type Config<'de> = T::Config<'de>;
62
63    const CODEC_ID: &'static str = T::CODEC_ID;
64
65    #[inline]
66    fn from_config(config: Self::Config<'_>) -> Self {
67        ensure_logger();
68
69        Self(T::from_config(config))
70    }
71
72    #[inline]
73    fn get_config(&self) -> StaticCodecConfig<Self> {
74        ensure_logger();
75
76        StaticCodecConfig::new(self.0.get_config().config)
77    }
78}
79
80// The logger init could also be implemented using a no-mangle `_initialize`
81// function, but that would require unsafe and be less explicit
82// https://github.com/bytecodealliance/wasm-tools/pull/1747
83fn ensure_logger() {
84    static LOGGER_INIT: Once = Once::new();
85
86    LOGGER_INIT.call_once(|| {
87        #[expect(clippy::expect_used)]
88        // failing to install the logger is a bug and we cannot continue
89        wasi_logger::Logger::install().expect("failed to install wasi_logger::Logger");
90
91        log::set_max_level(log::LevelFilter::Trace);
92    });
93}