Update log crate to 0.4.22.
Update patch file so it applies.
This is required for migrating acpi, which needs log ^0.4.20.
Bug: http://b/339424309
Test: treehugger
Change-Id: I3a91eb39d18feab61207df000df7231fb76469f4
diff --git a/crates/log/.cargo-checksum.json b/crates/log/.cargo-checksum.json
index 96d7c07..42ea021 100644
--- a/crates/log/.cargo-checksum.json
+++ b/crates/log/.cargo-checksum.json
@@ -1 +1 @@
-{"files":{"CHANGELOG.md":"d96bb13d82aab1b8f01a077167eb3968fe27c4fbb7aa4933f0e257d42794eb76","Cargo.toml":"e0ff3b07a36ad23348c9cc7113ad787aefbc0c7b75517f5a68247863a4994e1c","LICENSE-APACHE":"7cfd738c53d61c79f07e348f622bf7707c9084237054d37fbe07788a75f5881c","LICENSE-MIT":"705aaaaecefa087c187bd2eacdb01b8ac1c5ee95b1d5d04d8d7efe0363207df8","README.md":"bd6ed9b3c1cf3329d7afca04eb03f5c36e75cf9cb0bbbffddb7f07f3f36f2ea0","benches/value.rs":"55bd5f795a524f0699b2858eb14cc456abebeb43d50c90d2bfc77cd7856e8050","build.rs":"91dd121dd1aa749c8c0c24bda658309d1313baa022afbc5af1fabd1104622376","src/kv/error.rs":"01d37bb1ecbb2ba1145cd6df0b0b9a3ea3f1a2c9586a4908d3256cb865365b7e","src/kv/key.rs":"e74b489cde28960f76d4038b02867b4cb724c206d31abd461abeaa04e73cf7ef","src/kv/mod.rs":"9d916f43fd9c2829991fcff6db32f23bb7864e1113a622d49e59f110e2f290cf","src/kv/source.rs":"3f7aadc5f30c0c821fd20d899d8529eba8088eeda0c8603814de689da25dd4e8","src/kv/value.rs":"3387875f0ffd5f4747f181c3bc5134974358993981e54b1f203d6dd835a50245","src/lib.rs":"69dd3c57d9c6438d2e5bda9dc657827dcd73561faff708358f2e6854f47a2a3d","src/macros.rs":"642640f48a9d61efed0213b3a61a2864e80e8f8ffb9541b7c44d3028d3a7f08c","src/serde.rs":"d8190561ed64398fe45142b261000255ea1bcda8707e6f1b07ef4b805da6829b","triagebot.toml":"2d1d46d069bc4e3739069676707ca2801e0e0ba58a784a264392362ff9ac4a77"},"package":"abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"}
\ No newline at end of file
+{"files":{"CHANGELOG.md":"df7d7ea4256611dd5e3bf160e39bb3f8b665c6805ae47fdbf28acf9f77245ffd","Cargo.toml":"2161251dd0dfbea680a9d5fd762973e343fc5215794681c5ffd641faab9a4e4c","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"a23bbe55ac94081711c081a63df10d324a8a26f4b836952cb3c45c9318a03152","benches/value.rs":"b613ff353d3cf0ef8cb98e4ca461ea929b8ba553fe299f2eb2942d77a5b1b6a0","src/__private_api.rs":"da677f1e29e3cb135c971247031bc0eb20324294ab5c1c74c5118f87e45518ae","src/kv/error.rs":"6dae12424164c33b93915f5e70bd6d99d616c969c8bfb543806721dd9b423981","src/kv/key.rs":"9439e91c3ab3f9574a6a11a0347c7b63fdf1652384a6b28411136e4373de2970","src/kv/mod.rs":"3521a5bcfd7f92dcfac6c3c948020d686fee696596c566333a27edbbcc8a4ea8","src/kv/source.rs":"73fbc180c824072d86f1f41f8c59c014db1d8988a86be38a9128d67d6aab06a5","src/kv/value.rs":"0aade52b8e3523a17d6114f8b664793862032a94ea1ee2a4f12a20dd729b92d4","src/lib.rs":"55c32130cd8b99cde2ea962a403cdade52d20e80088357ba2784ee53b2eb9a2c","src/macros.rs":"dfb98017d5f205fec632069ab857a18661d6d563cf5162eeef64d367cc3ad7f5","src/serde.rs":"35f520f62fdba0216ccee33e5b66ad8f81dee3af5b65b824f1816180c9350df5","triagebot.toml":"a135e10c777cd13459559bdf74fb704c1379af7c9b0f70bc49fa6f5a837daa81"},"package":"a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"}
\ No newline at end of file
diff --git a/crates/log/Android.bp b/crates/log/Android.bp
index c3a45f4..852bf3b 100644
--- a/crates/log/Android.bp
+++ b/crates/log/Android.bp
@@ -18,15 +18,10 @@
host_supported: true,
crate_name: "log",
cargo_env_compat: true,
- cargo_pkg_version: "0.4.17",
+ cargo_pkg_version: "0.4.22",
crate_root: "src/lib.rs",
- edition: "2015",
+ edition: "2021",
features: ["std"],
- cfgs: [
- "atomic_cas",
- "has_atomics",
- ],
- rustlibs: ["libcfg_if"],
apex_available: [
"//apex_available:platform",
"//apex_available:anyapex",
@@ -53,19 +48,14 @@
host_supported: true,
crate_name: "log",
cargo_env_compat: true,
- cargo_pkg_version: "0.4.17",
+ cargo_pkg_version: "0.4.22",
crate_root: "src/lib.rs",
- edition: "2015",
+ edition: "2021",
features: [
"max_level_info",
"release_max_level_info",
"std",
],
- cfgs: [
- "atomic_cas",
- "has_atomics",
- ],
- rustlibs: ["libcfg_if"],
apex_available: [
"//apex_available:platform",
"//apex_available:anyapex",
@@ -92,19 +82,14 @@
host_supported: true,
crate_name: "log",
cargo_env_compat: true,
- cargo_pkg_version: "0.4.17",
+ cargo_pkg_version: "0.4.22",
crate_root: "src/lib.rs",
- edition: "2015",
+ edition: "2021",
features: [
"max_level_off",
"release_max_level_off",
"std",
],
- cfgs: [
- "atomic_cas",
- "has_atomics",
- ],
- rustlibs: ["libcfg_if"],
apex_available: [
"//apex_available:platform",
"//apex_available:anyapex",
@@ -130,14 +115,9 @@
name: "liblog_rust_nostd",
crate_name: "log",
cargo_env_compat: true,
- cargo_pkg_version: "0.4.17",
+ cargo_pkg_version: "0.4.22",
crate_root: "src/lib.rs",
- edition: "2015",
- cfgs: [
- "atomic_cas",
- "has_atomics",
- ],
- rustlibs: ["libcfg_if"],
+ edition: "2021",
apex_available: [
"//apex_available:platform",
"//apex_available:anyapex",
diff --git a/crates/log/CHANGELOG.md b/crates/log/CHANGELOG.md
index 7d68f89..2c89834 100644
--- a/crates/log/CHANGELOG.md
+++ b/crates/log/CHANGELOG.md
@@ -1,241 +1,324 @@
-# Change Log
-
-## [Unreleased]
-
-## [0.4.17] - 2022-04-29
-
-* Update `kv_unstable` internal dependencies.
-
-## [0.4.16] - 2022-03-22
-
-* Fix a conflict with unqualified `Option` use in macros.
-
-## [0.4.15] - 2022-02-23
-
-* Silence a warning about the deprecated `spin_loop_hint`.
-* Relax ordering in the atomic `set_max_level` call.
-* Add thumbv4t-none-eabi to targets that don't support atomics
-* Allow levels to be iterated over.
-* Implement `Log` on some common wrapper types.
-* Improvements to test coverage.
-* Improvements to documentation.
-* Add key-value support to the `log!` macros.
-* Tighten `kv_unstable` internal dependencies so they don't bump past their current alpha.
-* Add a simple visit API to `kv_unstable`.
-* Support `NonZero*` integers as values in structured logging
-* Support static strings as keys in structured logging
-
-## [0.4.14] - 2021-01-27
-
-* Remove the `__private_api_log_lit` special case.
-* Fixed incorrect combination of `kv_unstable` and `std` features causing compile failures.
-* Remove unstable `Value::to_*` conversions that were incorrectly using `as`.
-* Rename unstable `Value::to_error` to `Value::to_borrowed_error`.
-
-## [0.4.13] - 2021-01-11
-
-* This is the same as `0.4.11`, except with a `kv_unstable_std` feature added to aid migrating current dependents to `0.4.14` (which was originally going to be `0.4.13` until it was decided to create a patch from `0.4.11` to minimize disruption).
-
-## [0.4.12] - 2020-12-24
-
-### New
-
-* Support platforms without atomics by racing instead of failing to compile
-* Implement `Log` for `Box<T: Log>`
-* Update `cfg-if` to `1.0`
-* Internal reworks of the structured logging API. Removed the `Fill` API
-and added `source::as_map` and `source::as_list` to easily serialize a `Source`
-as either a map of `{key: value, ..}` or as a list of `[(key, value), ..]`.
-
-### Fixed
-
-* Fixed deserialization of `LevelFilter` to use their `u64` index variants
-
-## [0.4.11] - 2020-07-09
-
-### New
-
-* Support coercing structured values into concrete types.
-* Reference the `win_dbg_logger` in the readme.
-
-### Fixed
-
-* Updates a few deprecated items used internally.
-* Fixed issues in docs and expands sections.
-* Show the correct build badge in the readme.
-* Fix up a possible inference breakage with structured value errors.
-* Respect formatting flags in structured value formatting.
-
-## [0.4.10] - 2019-12-16 (yanked)
-
-### Fixed
-
-* Fixed the `log!` macros so they work in expression context (this regressed in `0.4.9`, which has been yanked).
-
-## [0.4.9] - 2019-12-12 (yanked)
-
-### Minimum Supported Rust Version
-
-This release bumps the minimum compiler version to `1.31.0`. This was mainly needed for `cfg-if`,
-but between `1.16.0` and `1.31.0` there are a lot of language and library improvements we now
-take advantage of.
-
-### New
-
-* Unstable support for capturing key-value pairs in a record using the `log!` macros
-
-### Improved
-
-* Better documentation for max level filters.
-* Internal updates to line up with bumped MSRV
-
-## [0.4.8] - 2019-07-28
-
-### New
-
-* Support attempting to get `Record` fields as static strings.
-
-## [0.4.7] - 2019-07-06
-
-### New
-
-* Support for embedded environments with thread-unsafe initialization.
-* Initial unstable support for capturing structured data under the `kv_unstable`
-feature gate. This new API doesn't affect existing users and may change in future
-patches (so those changes may not appear in the changelog until it stabilizes).
-
-### Improved
-
-* Docs for using `log` with the 2018 edition.
-* Error messages for macros missing arguments.
-
-## [0.4.6] - 2018-10-27
-
-### Improved
-
-* Support 2018-style macro import for the `log_enabled!` macro.
-
-## [0.4.5] - 2018-09-03
-
-### Improved
-
-* Make `log`'s internal helper macros less likely to conflict with user-defined
- macros.
-
-## [0.4.4] - 2018-08-17
-
-### Improved
-
-* Support 2018-style imports of the log macros.
-
-## [0.4.3] - 2018-06-29
-
-### Improved
-
-* More code generation improvements.
-
-## [0.4.2] - 2018-06-05
-
-### Improved
-
-* Log invocations now generate less code.
-
-### Fixed
-
-* Example Logger implementations now properly set the max log level.
-
-## [0.4.1] - 2017-12-30
-
-### Fixed
-
-* Some doc links were fixed.
-
-## [0.4.0] - 2017-12-24
-
-The changes in this release include cleanup of some obscure functionality and a more robust public
-API designed to support bridges to other logging systems, and provide more flexibility to new
-features in the future.
-
-### Compatibility
-
-Vast portions of the Rust ecosystem use the 0.3.x release series of log, and we don't want to force
-the community to go through the pain of upgrading every crate to 0.4.x at the exact same time. Along
-with 0.4.0, we've published a new 0.3.9 release which acts as a "shim" over 0.4.0. This will allow
-crates using either version to coexist without losing messages from one side or the other.
-
-There is one caveat - a log message generated by a crate using 0.4.x but consumed by a logging
-implementation using 0.3.x will not have a file name or module path. Applications affected by this
-can upgrade their logging implementations to one using 0.4.x to avoid losing this information. The
-other direction does not lose any information, fortunately!
-
-**TL;DR** Libraries should feel comfortable upgrading to 0.4.0 without treating that as a breaking
-change. Applications may need to update their logging implementation (e.g. env-logger) to a newer
-version using log 0.4.x to avoid losing module and file information.
-
-### New
-
-* The crate is now `no_std` by default.
-* `Level` and `LevelFilter` now implement `Serialize` and `Deserialize` when the `serde` feature is
- enabled.
-* The `Record` and `Metadata` types can now be constructed by third-party code via a builder API.
-* The `logger` free function returns a reference to the logger implementation. This, along with the
- ability to construct `Record`s, makes it possible to bridge from another logging framework to
- this one without digging into the private internals of the crate. The standard `error!` `warn!`,
- etc, macros now exclusively use the public API of the crate rather than "secret" internal APIs.
-* `Log::flush` has been added to allow crates to tell the logging implementation to ensure that all
- "in flight" log events have been persisted. This can be used, for example, just before an
- application exits to ensure that asynchronous log sinks finish their work.
-
-### Removed
-
-* The `shutdown` and `shutdown_raw` functions have been removed. Supporting shutdown significantly
- complicated the implementation and imposed a performance cost on each logging operation.
-* The `log_panics` function and its associated `nightly` Cargo feature have been removed. Use the
- [log-panics](https://crates.io/crates/log-panics) instead.
-
-### Changed
-
-* The `Log` prefix has been removed from type names. For example, `LogLevelFilter` is now
- `LevelFilter`, and `LogRecord` is now `Record`.
-* The `MaxLogLevelFilter` object has been removed in favor of a `set_max_level` free function.
-* The `set_logger` free functions have been restructured. The logger is now directly passed to the
- functions rather than a closure which returns the logger. `set_logger` now takes a `&'static
- Log` and is usable in `no_std` contexts in place of the old `set_logger_raw`. `set_boxed_logger`
- is a convenience function which takes a `Box<Log>` but otherwise acts like `set_logger`. It
- requires the `std` feature.
-* The `file` and `module_path` values in `Record` no longer have the `'static` lifetime to support
- integration with other logging frameworks that don't provide a `'static` lifetime for the
- equivalent values.
-* The `file`, `line`, and `module_path` values in `Record` are now `Option`s to support integration
- with other logging frameworks that don't provide those values.
-
-### In the Future
-
-* We're looking to add support for *structured* logging - the inclusion of extra key-value pairs of
- information in a log event in addition to the normal string message. This should be able to be
- added in a backwards compatible manner to the 0.4.x series when the design is worked out.
-
-## Older
-
-Look at the [release tags] for information about older releases.
-
-[Unreleased]: https://github.com/rust-lang-nursery/log/compare/0.4.17...HEAD
-[0.4.17]: https://github.com/rust-lang-nursery/log/compare/0.4.16...0.4.17
-[0.4.16]: https://github.com/rust-lang-nursery/log/compare/0.4.15...0.4.16
-[0.4.15]: https://github.com/rust-lang-nursery/log/compare/0.4.13...0.4.15
-[0.4.14]: https://github.com/rust-lang-nursery/log/compare/0.4.13...0.4.14
-[0.4.13]: https://github.com/rust-lang-nursery/log/compare/0.4.11...0.4.13
-[0.4.12]: https://github.com/rust-lang-nursery/log/compare/0.4.11...0.4.12
-[0.4.11]: https://github.com/rust-lang-nursery/log/compare/0.4.10...0.4.11
-[0.4.10]: https://github.com/rust-lang-nursery/log/compare/0.4.9...0.4.10
-[0.4.9]: https://github.com/rust-lang-nursery/log/compare/0.4.8...0.4.9
-[0.4.8]: https://github.com/rust-lang-nursery/log/compare/0.4.7...0.4.8
-[0.4.7]: https://github.com/rust-lang-nursery/log/compare/0.4.6...0.4.7
-[0.4.6]: https://github.com/rust-lang-nursery/log/compare/0.4.5...0.4.6
-[0.4.5]: https://github.com/rust-lang-nursery/log/compare/0.4.4...0.4.5
-[0.4.4]: https://github.com/rust-lang-nursery/log/compare/0.4.3...0.4.4
-[0.4.3]: https://github.com/rust-lang-nursery/log/compare/0.4.2...0.4.3
-[0.4.2]: https://github.com/rust-lang-nursery/log/compare/0.4.1...0.4.2
-[0.4.1]: https://github.com/rust-lang-nursery/log/compare/0.4.0...0.4.1
-[0.4.0]: https://github.com/rust-lang-nursery/log/compare/0.3.8...0.4.0
-[release tags]: https://github.com/rust-lang-nursery/log/releases
+# Change Log
+
+## [Unreleased]
+
+## [0.4.22] - 2024-06-27
+
+## What's Changed
+* Add some clarifications to the library docs by @KodrAus in https://github.com/rust-lang/log/pull/620
+* Add links to `colog` crate by @chrivers in https://github.com/rust-lang/log/pull/621
+* adding line_number test + updating some testing infrastructure by @DIvkov575 in https://github.com/rust-lang/log/pull/619
+* Clarify the actual set of functions that can race in _racy variants by @KodrAus in https://github.com/rust-lang/log/pull/623
+* Replace deprecated std::sync::atomic::spin_loop_hint() by @Catamantaloedis in https://github.com/rust-lang/log/pull/625
+* Check usage of max_level features by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/627
+* Remove unneeded import by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/628
+* Loosen orderings for logger initialization in https://github.com/rust-lang/log/pull/632. Originally by @pwoolcoc in https://github.com/rust-lang/log/pull/599
+* Use Location::caller() for file and line info in https://github.com/rust-lang/log/pull/633. Originally by @Cassy343 in https://github.com/rust-lang/log/pull/520
+
+## New Contributors
+* @chrivers made their first contribution in https://github.com/rust-lang/log/pull/621
+* @DIvkov575 made their first contribution in https://github.com/rust-lang/log/pull/619
+* @Catamantaloedis made their first contribution in https://github.com/rust-lang/log/pull/625
+
+**Full Changelog**: https://github.com/rust-lang/log/compare/0.4.21...0.4.22
+
+## [0.4.21] - 2024-02-27
+
+## What's Changed
+* Minor clippy nits by @nyurik in https://github.com/rust-lang/log/pull/578
+* Simplify Display impl by @nyurik in https://github.com/rust-lang/log/pull/579
+* Set all crates to 2021 edition by @nyurik in https://github.com/rust-lang/log/pull/580
+* Various changes based on review by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/583
+* Fix typo in file_static() method doc by @dimo414 in https://github.com/rust-lang/log/pull/590
+* Specialize empty key value pairs by @EFanZh in https://github.com/rust-lang/log/pull/576
+* Fix incorrect lifetime in Value::to_str() by @peterjoel in https://github.com/rust-lang/log/pull/587
+* Remove some API of the key-value feature by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/585
+* Add logcontrol-log and log-reload by @swsnr in https://github.com/rust-lang/log/pull/595
+* Add Serialization section to kv::Value docs by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/593
+* Rename Value::to_str to to_cow_str by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/592
+* Clarify documentation and simplify initialization of `STATIC_MAX_LEVEL` by @ptosi in https://github.com/rust-lang/log/pull/594
+* Update docs to 2021 edition, test by @nyurik in https://github.com/rust-lang/log/pull/577
+* Add "alterable_logger" link to README.md by @brummer-simon in https://github.com/rust-lang/log/pull/589
+* Normalize line ending by @EFanZh in https://github.com/rust-lang/log/pull/602
+* Remove `ok_or` in favor of `Option::ok_or` by @AngelicosPhosphoros in https://github.com/rust-lang/log/pull/607
+* Use `Acquire` ordering for initialization check by @AngelicosPhosphoros in https://github.com/rust-lang/log/pull/610
+* Get structured logging API ready for stabilization by @KodrAus in https://github.com/rust-lang/log/pull/613
+
+## New Contributors
+* @nyurik made their first contribution in https://github.com/rust-lang/log/pull/578
+* @dimo414 made their first contribution in https://github.com/rust-lang/log/pull/590
+* @peterjoel made their first contribution in https://github.com/rust-lang/log/pull/587
+* @ptosi made their first contribution in https://github.com/rust-lang/log/pull/594
+* @brummer-simon made their first contribution in https://github.com/rust-lang/log/pull/589
+* @AngelicosPhosphoros made their first contribution in https://github.com/rust-lang/log/pull/607
+
+## [0.4.20] - 2023-07-11
+
+* Remove rustversion dev-dependency by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/568
+* Remove `local_inner_macros` usage by @EFanZh in https://github.com/rust-lang/log/pull/570
+
+## [0.4.19] - 2023-06-10
+
+* Use target_has_atomic instead of the old atomic_cas cfg by @GuillaumeGomez in https://github.com/rust-lang/log/pull/555
+* Put MSRV into Cargo.toml by @est31 in https://github.com/rust-lang/log/pull/557
+
+## [0.4.18] - 2023-05-28
+
+* fix markdown links (again) by @hellow554 in https://github.com/rust-lang/log/pull/513
+* add cargo doc to workflow by @hellow554 in https://github.com/rust-lang/log/pull/515
+* Apply Clippy lints by @hellow554 in https://github.com/rust-lang/log/pull/516
+* Replace ad-hoc eq_ignore_ascii_case with slice::eq_ignore_ascii_case by @glandium in https://github.com/rust-lang/log/pull/519
+* fix up windows targets by @KodrAus in https://github.com/rust-lang/log/pull/528
+* typo fix by @jiangying000 in https://github.com/rust-lang/log/pull/529
+* Remove dependency on cfg_if by @EriKWDev in https://github.com/rust-lang/log/pull/536
+* GitHub Workflows security hardening by @sashashura in https://github.com/rust-lang/log/pull/538
+* Fix build status badge by @atouchet in https://github.com/rust-lang/log/pull/539
+* Add call_logger to the documentation by @a1ecbr0wn in https://github.com/rust-lang/log/pull/547
+* Use stable internals for key-value API by @KodrAus in https://github.com/rust-lang/log/pull/550
+* Change wording of list of implementations by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/553
+* Add std-logger to list of implementations by @Thomasdezeeuw in https://github.com/rust-lang/log/pull/554
+* Add `set_max_level_racy` and gate `set_max_level` by @djkoloski in https://github.com/rust-lang/log/pull/544
+* [doc] src/lib.rs : prefix an unused variable with an underscore by @OccupyMars2025 in https://github.com/rust-lang/log/pull/561
+* [doc] src/macros.rs : correct grammar errors of an example in lib documentation by @OccupyMars2025 in https://github.com/rust-lang/log/pull/562
+
+## [0.4.17] - 2022-04-29
+
+* Update `kv_unstable` internal dependencies.
+
+## [0.4.16] - 2022-03-22
+
+* Fix a conflict with unqualified `Option` use in macros.
+
+## [0.4.15] - 2022-02-23
+
+* Silence a warning about the deprecated `spin_loop_hint`.
+* Relax ordering in the atomic `set_max_level` call.
+* Add thumbv4t-none-eabi to targets that don't support atomics
+* Allow levels to be iterated over.
+* Implement `Log` on some common wrapper types.
+* Improvements to test coverage.
+* Improvements to documentation.
+* Add key-value support to the `log!` macros.
+* Tighten `kv_unstable` internal dependencies so they don't bump past their current alpha.
+* Add a simple visit API to `kv_unstable`.
+* Support `NonZero*` integers as values in structured logging
+* Support static strings as keys in structured logging
+
+## [0.4.14] - 2021-01-27
+
+* Remove the `__private_api_log_lit` special case.
+* Fixed incorrect combination of `kv_unstable` and `std` features causing compile failures.
+* Remove unstable `Value::to_*` conversions that were incorrectly using `as`.
+* Rename unstable `Value::to_error` to `Value::to_borrowed_error`.
+
+## [0.4.13] - 2021-01-11
+
+* This is the same as `0.4.11`, except with a `kv_unstable_std` feature added to aid migrating current dependents to `0.4.14` (which was originally going to be `0.4.13` until it was decided to create a patch from `0.4.11` to minimize disruption).
+
+## [0.4.12] - 2020-12-24
+
+### New
+
+* Support platforms without atomics by racing instead of failing to compile
+* Implement `Log` for `Box<T: Log>`
+* Update `cfg-if` to `1.0`
+* Internal reworks of the structured logging API. Removed the `Fill` API
+and added `source::as_map` and `source::as_list` to easily serialize a `Source`
+as either a map of `{key: value, ..}` or as a list of `[(key, value), ..]`.
+
+### Fixed
+
+* Fixed deserialization of `LevelFilter` to use their `u64` index variants
+
+## [0.4.11] - 2020-07-09
+
+### New
+
+* Support coercing structured values into concrete types.
+* Reference the `win_dbg_logger` in the readme.
+
+### Fixed
+
+* Updates a few deprecated items used internally.
+* Fixed issues in docs and expands sections.
+* Show the correct build badge in the readme.
+* Fix up a possible inference breakage with structured value errors.
+* Respect formatting flags in structured value formatting.
+
+## [0.4.10] - 2019-12-16 (yanked)
+
+### Fixed
+
+* Fixed the `log!` macros so they work in expression context (this regressed in `0.4.9`, which has been yanked).
+
+## [0.4.9] - 2019-12-12 (yanked)
+
+### Minimum Supported Rust Version
+
+This release bumps the minimum compiler version to `1.31.0`. This was mainly needed for `cfg-if`,
+but between `1.16.0` and `1.31.0` there are a lot of language and library improvements we now
+take advantage of.
+
+### New
+
+* Unstable support for capturing key-value pairs in a record using the `log!` macros
+
+### Improved
+
+* Better documentation for max level filters.
+* Internal updates to line up with bumped MSRV
+
+## [0.4.8] - 2019-07-28
+
+### New
+
+* Support attempting to get `Record` fields as static strings.
+
+## [0.4.7] - 2019-07-06
+
+### New
+
+* Support for embedded environments with thread-unsafe initialization.
+* Initial unstable support for capturing structured data under the `kv_unstable`
+feature gate. This new API doesn't affect existing users and may change in future
+patches (so those changes may not appear in the changelog until it stabilizes).
+
+### Improved
+
+* Docs for using `log` with the 2018 edition.
+* Error messages for macros missing arguments.
+
+## [0.4.6] - 2018-10-27
+
+### Improved
+
+* Support 2018-style macro import for the `log_enabled!` macro.
+
+## [0.4.5] - 2018-09-03
+
+### Improved
+
+* Make `log`'s internal helper macros less likely to conflict with user-defined
+ macros.
+
+## [0.4.4] - 2018-08-17
+
+### Improved
+
+* Support 2018-style imports of the log macros.
+
+## [0.4.3] - 2018-06-29
+
+### Improved
+
+* More code generation improvements.
+
+## [0.4.2] - 2018-06-05
+
+### Improved
+
+* Log invocations now generate less code.
+
+### Fixed
+
+* Example Logger implementations now properly set the max log level.
+
+## [0.4.1] - 2017-12-30
+
+### Fixed
+
+* Some doc links were fixed.
+
+## [0.4.0] - 2017-12-24
+
+The changes in this release include cleanup of some obscure functionality and a more robust public
+API designed to support bridges to other logging systems, and provide more flexibility to new
+features in the future.
+
+### Compatibility
+
+Vast portions of the Rust ecosystem use the 0.3.x release series of log, and we don't want to force
+the community to go through the pain of upgrading every crate to 0.4.x at the exact same time. Along
+with 0.4.0, we've published a new 0.3.9 release which acts as a "shim" over 0.4.0. This will allow
+crates using either version to coexist without losing messages from one side or the other.
+
+There is one caveat - a log message generated by a crate using 0.4.x but consumed by a logging
+implementation using 0.3.x will not have a file name or module path. Applications affected by this
+can upgrade their logging implementations to one using 0.4.x to avoid losing this information. The
+other direction does not lose any information, fortunately!
+
+**TL;DR** Libraries should feel comfortable upgrading to 0.4.0 without treating that as a breaking
+change. Applications may need to update their logging implementation (e.g. env-logger) to a newer
+version using log 0.4.x to avoid losing module and file information.
+
+### New
+
+* The crate is now `no_std` by default.
+* `Level` and `LevelFilter` now implement `Serialize` and `Deserialize` when the `serde` feature is
+ enabled.
+* The `Record` and `Metadata` types can now be constructed by third-party code via a builder API.
+* The `logger` free function returns a reference to the logger implementation. This, along with the
+ ability to construct `Record`s, makes it possible to bridge from another logging framework to
+ this one without digging into the private internals of the crate. The standard `error!` `warn!`,
+ etc, macros now exclusively use the public API of the crate rather than "secret" internal APIs.
+* `Log::flush` has been added to allow crates to tell the logging implementation to ensure that all
+ "in flight" log events have been persisted. This can be used, for example, just before an
+ application exits to ensure that asynchronous log sinks finish their work.
+
+### Removed
+
+* The `shutdown` and `shutdown_raw` functions have been removed. Supporting shutdown significantly
+ complicated the implementation and imposed a performance cost on each logging operation.
+* The `log_panics` function and its associated `nightly` Cargo feature have been removed. Use the
+ [log-panics](https://crates.io/crates/log-panics) instead.
+
+### Changed
+
+* The `Log` prefix has been removed from type names. For example, `LogLevelFilter` is now
+ `LevelFilter`, and `LogRecord` is now `Record`.
+* The `MaxLogLevelFilter` object has been removed in favor of a `set_max_level` free function.
+* The `set_logger` free functions have been restructured. The logger is now directly passed to the
+ functions rather than a closure which returns the logger. `set_logger` now takes a `&'static
+ Log` and is usable in `no_std` contexts in place of the old `set_logger_raw`. `set_boxed_logger`
+ is a convenience function which takes a `Box<Log>` but otherwise acts like `set_logger`. It
+ requires the `std` feature.
+* The `file` and `module_path` values in `Record` no longer have the `'static` lifetime to support
+ integration with other logging frameworks that don't provide a `'static` lifetime for the
+ equivalent values.
+* The `file`, `line`, and `module_path` values in `Record` are now `Option`s to support integration
+ with other logging frameworks that don't provide those values.
+
+### In the Future
+
+* We're looking to add support for *structured* logging - the inclusion of extra key-value pairs of
+ information in a log event in addition to the normal string message. This should be able to be
+ added in a backwards compatible manner to the 0.4.x series when the design is worked out.
+
+## Older
+
+Look at the [release tags] for information about older releases.
+
+[Unreleased]: https://github.com/rust-lang-nursery/log/compare/0.4.21...HEAD
+[0.4.21]: https://github.com/rust-lang/log/compare/0.4.20...0.4.21
+[0.4.20]: https://github.com/rust-lang-nursery/log/compare/0.4.19...0.4.20
+[0.4.19]: https://github.com/rust-lang-nursery/log/compare/0.4.18...0.4.19
+[0.4.18]: https://github.com/rust-lang-nursery/log/compare/0.4.17...0.4.18
+[0.4.17]: https://github.com/rust-lang-nursery/log/compare/0.4.16...0.4.17
+[0.4.16]: https://github.com/rust-lang-nursery/log/compare/0.4.15...0.4.16
+[0.4.15]: https://github.com/rust-lang-nursery/log/compare/0.4.13...0.4.15
+[0.4.14]: https://github.com/rust-lang-nursery/log/compare/0.4.13...0.4.14
+[0.4.13]: https://github.com/rust-lang-nursery/log/compare/0.4.11...0.4.13
+[0.4.12]: https://github.com/rust-lang-nursery/log/compare/0.4.11...0.4.12
+[0.4.11]: https://github.com/rust-lang-nursery/log/compare/0.4.10...0.4.11
+[0.4.10]: https://github.com/rust-lang-nursery/log/compare/0.4.9...0.4.10
+[0.4.9]: https://github.com/rust-lang-nursery/log/compare/0.4.8...0.4.9
+[0.4.8]: https://github.com/rust-lang-nursery/log/compare/0.4.7...0.4.8
+[0.4.7]: https://github.com/rust-lang-nursery/log/compare/0.4.6...0.4.7
+[0.4.6]: https://github.com/rust-lang-nursery/log/compare/0.4.5...0.4.6
+[0.4.5]: https://github.com/rust-lang-nursery/log/compare/0.4.4...0.4.5
+[0.4.4]: https://github.com/rust-lang-nursery/log/compare/0.4.3...0.4.4
+[0.4.3]: https://github.com/rust-lang-nursery/log/compare/0.4.2...0.4.3
+[0.4.2]: https://github.com/rust-lang-nursery/log/compare/0.4.1...0.4.2
+[0.4.1]: https://github.com/rust-lang-nursery/log/compare/0.4.0...0.4.1
+[0.4.0]: https://github.com/rust-lang-nursery/log/compare/0.3.8...0.4.0
+[release tags]: https://github.com/rust-lang-nursery/log/releases
diff --git a/crates/log/Cargo.toml b/crates/log/Cargo.toml
index 5fed87f..313a005 100644
--- a/crates/log/Cargo.toml
+++ b/crates/log/Cargo.toml
@@ -10,10 +10,11 @@
# See Cargo.toml.orig for the original contents.
[package]
+edition = "2021"
+rust-version = "1.60.0"
name = "log"
-version = "0.4.17"
+version = "0.4.22"
authors = ["The Rust Project Developers"]
-build = "build.rs"
exclude = ["rfcs/**/*"]
description = """
A lightweight logging facade for Rust
@@ -29,14 +30,14 @@
features = [
"std",
"serde",
- "kv_unstable_std",
- "kv_unstable_sval",
- "kv_unstable_serde",
+ "kv_std",
+ "kv_sval",
+ "kv_serde",
]
[[test]]
-name = "filters"
-path = "tests/filters.rs"
+name = "integration"
+path = "tests/integration.rs"
harness = false
[[test]]
@@ -44,58 +45,84 @@
path = "tests/macros.rs"
harness = true
-[dependencies.cfg-if]
-version = "1.0"
-
[dependencies.serde]
version = "1.0"
optional = true
default-features = false
[dependencies.sval]
-version = "=1.0.0-alpha.5"
+version = "2.1"
+optional = true
+default-features = false
+
+[dependencies.sval_ref]
+version = "2.1"
optional = true
default-features = false
[dependencies.value-bag]
-version = "=1.0.0-alpha.9"
+version = "1.7"
+features = ["inline-i128"]
optional = true
default-features = false
-[dev-dependencies.rustversion]
-version = "1.0"
+[dev-dependencies.proc-macro2]
+version = "1.0.63"
+default-features = false
[dev-dependencies.serde]
version = "1.0"
features = ["derive"]
+[dev-dependencies.serde_json]
+version = "1.0"
+
[dev-dependencies.serde_test]
version = "1.0"
[dev-dependencies.sval]
-version = "=1.0.0-alpha.5"
-features = ["derive"]
+version = "2.1"
+
+[dev-dependencies.sval_derive]
+version = "2.1"
[dev-dependencies.value-bag]
-version = "=1.0.0-alpha.9"
+version = "1.7"
features = ["test"]
[features]
-kv_unstable = ["value-bag"]
-kv_unstable_serde = [
- "kv_unstable_std",
+kv = []
+kv_serde = [
+ "kv_std",
"value-bag/serde",
"serde",
]
-kv_unstable_std = [
+kv_std = [
"std",
- "kv_unstable",
+ "kv",
"value-bag/error",
]
-kv_unstable_sval = [
- "kv_unstable",
+kv_sval = [
+ "kv",
"value-bag/sval",
"sval",
+ "sval_ref",
+]
+kv_unstable = [
+ "kv",
+ "value-bag",
+]
+kv_unstable_serde = [
+ "kv_serde",
+ "kv_unstable_std",
+]
+kv_unstable_std = [
+ "kv_std",
+ "kv_unstable",
+]
+kv_unstable_sval = [
+ "kv_sval",
+ "kv_unstable",
]
max_level_debug = []
max_level_error = []
diff --git a/crates/log/LICENSE-APACHE b/crates/log/LICENSE-APACHE
index f47c941..16fe87b 100644
--- a/crates/log/LICENSE-APACHE
+++ b/crates/log/LICENSE-APACHE
@@ -1,201 +1,201 @@
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
-TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
-1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
-2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
-3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
-4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
-5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
-6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
-7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
-8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
-9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
-END OF TERMS AND CONDITIONS
-
-APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
-Copyright [yyyy] [name of copyright owner]
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/crates/log/LICENSE-MIT b/crates/log/LICENSE-MIT
index c0428e2..39d4bdb 100644
--- a/crates/log/LICENSE-MIT
+++ b/crates/log/LICENSE-MIT
@@ -1,25 +1,25 @@
-Copyright (c) 2014 The Rust Project Developers
-
-Permission is hereby granted, free of charge, to any
-person obtaining a copy of this software and associated
-documentation files (the "Software"), to deal in the
-Software without restriction, including without
-limitation the rights to use, copy, modify, merge,
-publish, distribute, sublicense, and/or sell copies of
-the Software, and to permit persons to whom the Software
-is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice
-shall be included in all copies or substantial portions
-of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
-ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
-TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
-PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
-IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-DEALINGS IN THE SOFTWARE.
+Copyright (c) 2014 The Rust Project Developers
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/crates/log/METADATA b/crates/log/METADATA
index 25d8912..36ec7a6 100644
--- a/crates/log/METADATA
+++ b/crates/log/METADATA
@@ -1,17 +1,17 @@
name: "log"
description: "A lightweight logging facade for Rust"
third_party {
- version: "0.4.17"
+ version: "0.4.22"
license_type: NOTICE
last_upgrade_date {
- year: 2022
- month: 12
- day: 12
+ year: 2024
+ month: 11
+ day: 1
}
homepage: "https://crates.io/crates/log"
identifier {
type: "Archive"
- value: "https://static.crates.io/crates/log/log-0.4.17.crate"
- version: "0.4.17"
+ value: "https://static.crates.io/crates/log/log-0.4.22.crate"
+ version: "0.4.22"
}
}
diff --git a/crates/log/README.md b/crates/log/README.md
index 7fa4353..d4a08b1 100644
--- a/crates/log/README.md
+++ b/crates/log/README.md
@@ -1,117 +1,130 @@
-log
-===
-
-A Rust library providing a lightweight logging *facade*.
-
-[](https://github.com/rust-lang/log/actions)
-[](https://crates.io/crates/log)
-[](https://docs.rs/log)
-
-
-* [`log` documentation](https://docs.rs/log)
-
-A logging facade provides a single logging API that abstracts over the actual
-logging implementation. Libraries can use the logging API provided by this
-crate, and the consumer of those libraries can choose the logging
-implementation that is most suitable for its use case.
-
-
-## Minimum supported `rustc`
-
-`1.31.0+`
-
-This version is explicitly tested in CI and may be bumped in any release as needed. Maintaining compatibility with older compilers is a priority though, so the bar for bumping the minimum supported version is set very high. Any changes to the supported minimum version will be called out in the release notes.
-
-## Usage
-
-### In libraries
-
-Libraries should link only to the `log` crate, and use the provided macros to
-log whatever information will be useful to downstream consumers:
-
-```toml
-[dependencies]
-log = "0.4"
-```
-
-```rust
-use log::{info, trace, warn};
-
-pub fn shave_the_yak(yak: &mut Yak) {
- trace!("Commencing yak shaving");
-
- loop {
- match find_a_razor() {
- Ok(razor) => {
- info!("Razor located: {}", razor);
- yak.shave(razor);
- break;
- }
- Err(err) => {
- warn!("Unable to locate a razor: {}, retrying", err);
- }
- }
- }
-}
-```
-
-### In executables
-
-In order to produce log output, executables have to use a logger implementation compatible with the facade.
-There are many available implementations to choose from, here are some of the most popular ones:
-
-* Simple minimal loggers:
- * [`env_logger`](https://docs.rs/env_logger/*/env_logger/)
- * [`simple_logger`](https://docs.rs/simple_logger/*/simple_logger/)
- * [`simplelog`](https://docs.rs/simplelog/*/simplelog/)
- * [`pretty_env_logger`](https://docs.rs/pretty_env_logger/*/pretty_env_logger/)
- * [`stderrlog`](https://docs.rs/stderrlog/*/stderrlog/)
- * [`flexi_logger`](https://docs.rs/flexi_logger/*/flexi_logger/)
-* Complex configurable frameworks:
- * [`log4rs`](https://docs.rs/log4rs/*/log4rs/)
- * [`fern`](https://docs.rs/fern/*/fern/)
-* Adaptors for other facilities:
- * [`syslog`](https://docs.rs/syslog/*/syslog/)
- * [`systemd-journal-logger`](https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/)
- * [`slog-stdlog`](https://docs.rs/slog-stdlog/*/slog_stdlog/)
- * [`android_log`](https://docs.rs/android_log/*/android_log/)
- * [`win_dbg_logger`](https://docs.rs/win_dbg_logger/*/win_dbg_logger/)
- * [`db_logger`](https://docs.rs/db_logger/*/db_logger/)
-* For WebAssembly binaries:
- * [`console_log`](https://docs.rs/console_log/*/console_log/)
-* For dynamic libraries:
- * You may need to construct [an FFI-safe wrapper over `log`](https://github.com/rust-lang/log/issues/421) to initialize in your libraries.
-* Utilities:
- * [`log_err`](https://docs.rs/log_err/*/log_err/)
-
-Executables should choose a logger implementation and initialize it early in the
-runtime of the program. Logger implementations will typically include a
-function to do this. Any log messages generated before the logger is
-initialized will be ignored.
-
-The executable itself may use the `log` crate to log as well.
-
-## Structured logging
-
-If you enable the `kv_unstable` feature, you can associate structured data with your log records:
-
-```rust
-use log::{info, trace, warn, as_serde, as_error};
-
-pub fn shave_the_yak(yak: &mut Yak) {
- trace!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving");
-
- loop {
- match find_a_razor() {
- Ok(razor) => {
- info!(razor = razor; "Razor located");
- yak.shave(razor);
- break;
- }
- Err(err) => {
- warn!(err = as_error!(err); "Unable to locate a razor, retrying");
- }
- }
- }
-}
-```
+log
+===
+
+A Rust library providing a lightweight logging *facade*.
+
+[](https://github.com/rust-lang/log/actions)
+[](https://crates.io/crates/log)
+[](https://docs.rs/log)
+
+
+* [`log` documentation](https://docs.rs/log)
+
+A logging facade provides a single logging API that abstracts over the actual
+logging implementation. Libraries can use the logging API provided by this
+crate, and the consumer of those libraries can choose the logging
+implementation that is most suitable for its use case.
+
+
+## Minimum supported `rustc`
+
+`1.60.0+`
+
+This version is explicitly tested in CI and may be bumped in any release as needed. Maintaining compatibility with older compilers is a priority though, so the bar for bumping the minimum supported version is set very high. Any changes to the supported minimum version will be called out in the release notes.
+
+## Usage
+
+### In libraries
+
+Libraries should link only to the `log` crate, and use the provided macros to
+log whatever information will be useful to downstream consumers:
+
+```toml
+[dependencies]
+log = "0.4"
+```
+
+```rust
+use log::{info, trace, warn};
+
+pub fn shave_the_yak(yak: &mut Yak) {
+ trace!("Commencing yak shaving");
+
+ loop {
+ match find_a_razor() {
+ Ok(razor) => {
+ info!("Razor located: {razor}");
+ yak.shave(razor);
+ break;
+ }
+ Err(err) => {
+ warn!("Unable to locate a razor: {err}, retrying");
+ }
+ }
+ }
+}
+```
+
+### In executables
+
+In order to produce log output, executables have to use a logger implementation compatible with the facade.
+There are many available implementations to choose from, here are some options:
+
+* Simple minimal loggers:
+ * [`env_logger`](https://docs.rs/env_logger/*/env_logger/)
+ * [`colog`](https://docs.rs/colog/*/colog/)
+ * [`simple_logger`](https://docs.rs/simple_logger/*/simple_logger/)
+ * [`simplelog`](https://docs.rs/simplelog/*/simplelog/)
+ * [`pretty_env_logger`](https://docs.rs/pretty_env_logger/*/pretty_env_logger/)
+ * [`stderrlog`](https://docs.rs/stderrlog/*/stderrlog/)
+ * [`flexi_logger`](https://docs.rs/flexi_logger/*/flexi_logger/)
+ * [`call_logger`](https://docs.rs/call_logger/*/call_logger/)
+ * [`std-logger`](https://docs.rs/std-logger/*/std_logger/)
+ * [`structured-logger`](https://docs.rs/structured-logger/latest/structured_logger/)
+* Complex configurable frameworks:
+ * [`log4rs`](https://docs.rs/log4rs/*/log4rs/)
+ * [`fern`](https://docs.rs/fern/*/fern/)
+* Adaptors for other facilities:
+ * [`syslog`](https://docs.rs/syslog/*/syslog/)
+ * [`systemd-journal-logger`](https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/)
+ * [`slog-stdlog`](https://docs.rs/slog-stdlog/*/slog_stdlog/)
+ * [`android_log`](https://docs.rs/android_log/*/android_log/)
+ * [`win_dbg_logger`](https://docs.rs/win_dbg_logger/*/win_dbg_logger/)
+ * [`db_logger`](https://docs.rs/db_logger/*/db_logger/)
+ * [`log-to-defmt`](https://docs.rs/log-to-defmt/*/log_to_defmt/)
+ * [`logcontrol-log`](https://docs.rs/logcontrol-log/*/logcontrol_log/)
+* For WebAssembly binaries:
+ * [`console_log`](https://docs.rs/console_log/*/console_log/)
+* For dynamic libraries:
+ * You may need to construct [an FFI-safe wrapper over `log`](https://github.com/rust-lang/log/issues/421) to initialize in your libraries.
+* Utilities:
+ * [`log_err`](https://docs.rs/log_err/*/log_err/)
+ * [`log-reload`](https://docs.rs/log-reload/*/log_reload/)
+ * [`alterable_logger`](https://docs.rs/alterable_logger/*/alterable_logger)
+
+Executables should choose a logger implementation and initialize it early in the
+runtime of the program. Logger implementations will typically include a
+function to do this. Any log messages generated before the logger is
+initialized will be ignored.
+
+The executable itself may use the `log` crate to log as well.
+
+## Structured logging
+
+If you enable the `kv` feature, you can associate structured data with your log records:
+
+```rust
+use log::{info, trace, warn};
+
+pub fn shave_the_yak(yak: &mut Yak) {
+ // `yak:serde` will capture `yak` using its `serde::Serialize` impl
+ //
+ // You could also use `:?` for `Debug`, or `:%` for `Display`. For a
+ // full list, see the `log` crate documentation
+ trace!(target = "yak_events", yak:serde; "Commencing yak shaving");
+
+ loop {
+ match find_a_razor() {
+ Ok(razor) => {
+ info!(razor; "Razor located");
+ yak.shave(razor);
+ break;
+ }
+ Err(e) => {
+ // `e:err` will capture `e` using its `std::error::Error` impl
+ warn!(e:err; "Unable to locate a razor, retrying");
+ }
+ }
+ }
+}
+```
diff --git a/crates/log/benches/value.rs b/crates/log/benches/value.rs
index 3dab3bf..3d0f18b 100644
--- a/crates/log/benches/value.rs
+++ b/crates/log/benches/value.rs
@@ -1,30 +1,27 @@
-#![cfg(feature = "kv_unstable")]
-#![feature(test)]
-
-extern crate log;
-extern crate test;
-
-use log::kv::Value;
-
-#[bench]
-fn u8_to_value(b: &mut test::Bencher) {
- b.iter(|| Value::from(1u8))
-}
-
-#[bench]
-fn u8_to_value_debug(b: &mut test::Bencher) {
- b.iter(|| Value::from_debug(&1u8))
-}
-
-#[bench]
-fn str_to_value_debug(b: &mut test::Bencher) {
- b.iter(|| Value::from_debug(&"a string"))
-}
-
-#[bench]
-fn custom_to_value_debug(b: &mut test::Bencher) {
- #[derive(Debug)]
- struct A;
-
- b.iter(|| Value::from_debug(&A))
-}
+#![cfg(feature = "kv")]
+#![feature(test)]
+
+use log::kv::Value;
+
+#[bench]
+fn u8_to_value(b: &mut test::Bencher) {
+ b.iter(|| Value::from(1u8));
+}
+
+#[bench]
+fn u8_to_value_debug(b: &mut test::Bencher) {
+ b.iter(|| Value::from_debug(&1u8));
+}
+
+#[bench]
+fn str_to_value_debug(b: &mut test::Bencher) {
+ b.iter(|| Value::from_debug(&"a string"));
+}
+
+#[bench]
+fn custom_to_value_debug(b: &mut test::Bencher) {
+ #[derive(Debug)]
+ struct A;
+
+ b.iter(|| Value::from_debug(&A));
+}
diff --git a/crates/log/build.rs b/crates/log/build.rs
deleted file mode 100644
index 30c7edb..0000000
--- a/crates/log/build.rs
+++ /dev/null
@@ -1,46 +0,0 @@
-//! This build script detects target platforms that lack proper support for
-//! atomics and sets `cfg` flags accordingly.
-
-use std::env;
-use std::str;
-
-fn main() {
- let target = match rustc_target() {
- Some(target) => target,
- None => return,
- };
-
- if target_has_atomic_cas(&target) {
- println!("cargo:rustc-cfg=atomic_cas");
- }
-
- if target_has_atomics(&target) {
- println!("cargo:rustc-cfg=has_atomics");
- }
-
- println!("cargo:rerun-if-changed=build.rs");
-}
-
-fn target_has_atomic_cas(target: &str) -> bool {
- match &target[..] {
- "thumbv6m-none-eabi"
- | "msp430-none-elf"
- | "riscv32i-unknown-none-elf"
- | "riscv32imc-unknown-none-elf" => false,
- _ => true,
- }
-}
-
-fn target_has_atomics(target: &str) -> bool {
- match &target[..] {
- "thumbv4t-none-eabi"
- | "msp430-none-elf"
- | "riscv32i-unknown-none-elf"
- | "riscv32imc-unknown-none-elf" => false,
- _ => true,
- }
-}
-
-fn rustc_target() -> Option<String> {
- env::var("TARGET").ok()
-}
diff --git a/crates/log/patches/0001-Enable-default-initializing-liblog_rust-to-write-to-.patch b/crates/log/patches/0001-Enable-default-initializing-liblog_rust-to-write-to-.patch
index 180c34c..bdd191e 100644
--- a/crates/log/patches/0001-Enable-default-initializing-liblog_rust-to-write-to-.patch
+++ b/crates/log/patches/0001-Enable-default-initializing-liblog_rust-to-write-to-.patch
@@ -37,54 +37,54 @@
+../../android_logger/src/lib.rs
\ No newline at end of file
diff --git b/src/lib.rs a/src/lib.rs
-index 4ead8262..8eb1c507 100644
+index 6b43a9ae..799b88e1 100644
--- b/src/lib.rs
+++ a/src/lib.rs
-@@ -344,6 +344,11 @@ mod serde;
- #[cfg(feature = "kv_unstable")]
- pub mod kv;
-
-+#[cfg(default_log_impl)]
-+extern crate once_cell;
-+#[cfg(default_log_impl)]
-+mod android_logger;
-+
- #[cfg(has_atomics)]
- use std::sync::atomic::{AtomicUsize, Ordering};
-
-@@ -405,7 +410,10 @@ const UNINITIALIZED: usize = 0;
- const INITIALIZING: usize = 1;
- const INITIALIZED: usize = 2;
-
-+#[cfg(not(default_log_impl))]
- static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
-+#[cfg(default_log_impl)]
-+static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
-
- static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
-
-@@ -1572,6 +1580,21 @@ impl error::Error for ParseLevelError {}
- /// If a logger has not been set, a no-op implementation is returned.
- pub fn logger() -> &'static dyn Log {
- if STATE.load(Ordering::SeqCst) != INITIALIZED {
-+ #[cfg(default_log_impl)]
-+ {
-+ // On Android, default to logging to logcat if not explicitly initialized. This
-+ // prevents logs from being dropped by default, which may happen unexpectedly in case
-+ // of using libraries from multiple linker namespaces and failing to initialize the
-+ // logger in each namespace. See b/294216366#comment7.
-+ use android_logger::{AndroidLogger, Config};
-+ use std::sync::OnceLock;
-+ static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
-+ return
-+ ANDROID_LOGGER.get_or_init(|| {
-+ // Pass all logs down to liblog - it does its own filtering.
-+ AndroidLogger::new(Config::default().with_max_level(LevelFilter::Trace))
-+ });
-+ }
- static NOP: NopLogger = NopLogger;
- &NOP
- } else {
+@@ -397,6 +397,11 @@ mod serde;
+ #[cfg(feature = "kv")]
+ pub mod kv;
+
++#[cfg(default_log_impl)]
++extern crate once_cell;
++#[cfg(default_log_impl)]
++mod android_logger;
++
+ #[cfg(target_has_atomic = "ptr")]
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+@@ -458,7 +463,10 @@ const UNINITIALIZED: usize = 0;
+ const INITIALIZING: usize = 1;
+ const INITIALIZED: usize = 2;
+
++#[cfg(not(default_log_impl))]
+ static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
++#[cfg(default_log_impl)]
++static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
+
+ static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
+
+@@ -1505,6 +1513,21 @@ pub fn logger() -> &'static dyn Log {
+ // write to the `LOGGER` static and initialization of the logger
+ // internal state synchronized with current thread.
+ if STATE.load(Ordering::Acquire) != INITIALIZED {
++ #[cfg(default_log_impl)]
++ {
++ // On Android, default to logging to logcat if not explicitly initialized. This
++ // prevents logs from being dropped by default, which may happen unexpectedly in case
++ // of using libraries from multiple linker namespaces and failing to initialize the
++ // logger in each namespace. See b/294216366#comment7.
++ use android_logger::{AndroidLogger, Config};
++ use std::sync::OnceLock;
++ static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
++ return
++ ANDROID_LOGGER.get_or_init(|| {
++ // Pass all logs down to liblog - it does its own filtering.
++ AndroidLogger::new(Config::default().with_max_level(LevelFilter::Trace))
++ });
++ }
+ static NOP: NopLogger = NopLogger;
+ &NOP
+ } else {
--
2.42.0.rc1.204.g551eb34607-goog
diff --git a/crates/log/rules.mk b/crates/log/rules.mk
index 2943283..ad13ff6 100644
--- a/crates/log/rules.mk
+++ b/crates/log/rules.mk
@@ -9,13 +9,8 @@
MODULE_RUST_CRATE_TYPES := rlib
MODULE_SRCS := $(LOCAL_DIR)/src/lib.rs
MODULE_ADD_IMPLICIT_DEPS := false
-MODULE_RUST_EDITION := 2015
-MODULE_RUSTFLAGS += \
- --cfg 'atomic_cas' \
- --cfg 'has_atomics'
-
+MODULE_RUST_EDITION := 2021
MODULE_LIBRARY_DEPS := \
- $(call FIND_CRATE,cfg-if) \
trusty/user/base/lib/libcompiler_builtins-rust \
trusty/user/base/lib/libcore-rust
diff --git a/crates/log/src/__private_api.rs b/crates/log/src/__private_api.rs
new file mode 100644
index 0000000..11bc2fc
--- /dev/null
+++ b/crates/log/src/__private_api.rs
@@ -0,0 +1,123 @@
+//! WARNING: this is not part of the crate's public API and is subject to change at any time
+
+use self::sealed::KVs;
+use crate::{Level, Metadata, Record};
+use std::fmt::Arguments;
+use std::panic::Location;
+pub use std::{format_args, module_path, stringify};
+
+#[cfg(not(feature = "kv"))]
+pub type Value<'a> = &'a str;
+
+mod sealed {
+ /// Types for the `kv` argument.
+ pub trait KVs<'a> {
+ fn into_kvs(self) -> Option<&'a [(&'a str, super::Value<'a>)]>;
+ }
+}
+
+// Types for the `kv` argument.
+
+impl<'a> KVs<'a> for &'a [(&'a str, Value<'a>)] {
+ #[inline]
+ fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
+ Some(self)
+ }
+}
+
+impl<'a> KVs<'a> for () {
+ #[inline]
+ fn into_kvs(self) -> Option<&'a [(&'a str, Value<'a>)]> {
+ None
+ }
+}
+
+// Log implementation.
+
+fn log_impl(
+ args: Arguments,
+ level: Level,
+ &(target, module_path, loc): &(&str, &'static str, &'static Location),
+ kvs: Option<&[(&str, Value)]>,
+) {
+ #[cfg(not(feature = "kv"))]
+ if kvs.is_some() {
+ panic!("key-value support is experimental and must be enabled using the `kv` feature")
+ }
+
+ let mut builder = Record::builder();
+
+ builder
+ .args(args)
+ .level(level)
+ .target(target)
+ .module_path_static(Some(module_path))
+ .file_static(Some(loc.file()))
+ .line(Some(loc.line()));
+
+ #[cfg(feature = "kv")]
+ builder.key_values(&kvs);
+
+ crate::logger().log(&builder.build());
+}
+
+pub fn log<'a, K>(
+ args: Arguments,
+ level: Level,
+ target_module_path_and_loc: &(&str, &'static str, &'static Location),
+ kvs: K,
+) where
+ K: KVs<'a>,
+{
+ log_impl(args, level, target_module_path_and_loc, kvs.into_kvs())
+}
+
+pub fn enabled(level: Level, target: &str) -> bool {
+ crate::logger().enabled(&Metadata::builder().level(level).target(target).build())
+}
+
+#[track_caller]
+pub fn loc() -> &'static Location<'static> {
+ Location::caller()
+}
+
+#[cfg(feature = "kv")]
+mod kv_support {
+ use crate::kv;
+
+ pub type Value<'a> = kv::Value<'a>;
+
+ // NOTE: Many functions here accept a double reference &&V
+ // This is so V itself can be ?Sized, while still letting us
+ // erase it to some dyn Trait (because &T is sized)
+
+ pub fn capture_to_value<'a, V: kv::ToValue + ?Sized>(v: &'a &'a V) -> Value<'a> {
+ v.to_value()
+ }
+
+ pub fn capture_debug<'a, V: core::fmt::Debug + ?Sized>(v: &'a &'a V) -> Value<'a> {
+ Value::from_debug(v)
+ }
+
+ pub fn capture_display<'a, V: core::fmt::Display + ?Sized>(v: &'a &'a V) -> Value<'a> {
+ Value::from_display(v)
+ }
+
+ #[cfg(feature = "kv_std")]
+ pub fn capture_error<'a>(v: &'a (dyn std::error::Error + 'static)) -> Value<'a> {
+ Value::from_dyn_error(v)
+ }
+
+ #[cfg(feature = "kv_sval")]
+ pub fn capture_sval<'a, V: sval::Value + ?Sized>(v: &'a &'a V) -> Value<'a> {
+ Value::from_sval(v)
+ }
+
+ #[cfg(feature = "kv_serde")]
+ pub fn capture_serde<'a, V: serde::Serialize + ?Sized>(v: &'a &'a V) -> Value<'a> {
+ Value::from_serde(v)
+ }
+}
+
+#[cfg(feature = "kv")]
+pub use self::kv_support::*;
diff --git a/crates/log/src/kv/error.rs b/crates/log/src/kv/error.rs
index 1adad23..7efa5af 100644
--- a/crates/log/src/kv/error.rs
+++ b/crates/log/src/kv/error.rs
@@ -1,90 +1,94 @@
-use std::fmt;
-
-/// An error encountered while working with structured data.
-#[derive(Debug)]
-pub struct Error {
- inner: Inner,
-}
-
-#[derive(Debug)]
-enum Inner {
- #[cfg(feature = "std")]
- Boxed(std_support::BoxedError),
- Msg(&'static str),
- Value(value_bag::Error),
- Fmt,
-}
-
-impl Error {
- /// Create an error from a message.
- pub fn msg(msg: &'static str) -> Self {
- Error {
- inner: Inner::Msg(msg),
- }
- }
-
- // Not public so we don't leak the `value_bag` API
- pub(super) fn from_value(err: value_bag::Error) -> Self {
- Error {
- inner: Inner::Value(err),
- }
- }
-
- // Not public so we don't leak the `value_bag` API
- pub(super) fn into_value(self) -> value_bag::Error {
- match self.inner {
- Inner::Value(err) => err,
- #[cfg(feature = "kv_unstable_std")]
- _ => value_bag::Error::boxed(self),
- #[cfg(not(feature = "kv_unstable_std"))]
- _ => value_bag::Error::msg("error inspecting a value"),
- }
- }
-}
-
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- use self::Inner::*;
- match &self.inner {
- #[cfg(feature = "std")]
- &Boxed(ref err) => err.fmt(f),
- &Value(ref err) => err.fmt(f),
- &Msg(ref msg) => msg.fmt(f),
- &Fmt => fmt::Error.fmt(f),
- }
- }
-}
-
-impl From<fmt::Error> for Error {
- fn from(_: fmt::Error) -> Self {
- Error { inner: Inner::Fmt }
- }
-}
-
-#[cfg(feature = "std")]
-mod std_support {
- use super::*;
- use std::{error, io};
-
- pub(super) type BoxedError = Box<dyn error::Error + Send + Sync>;
-
- impl Error {
- /// Create an error from a standard error type.
- pub fn boxed<E>(err: E) -> Self
- where
- E: Into<BoxedError>,
- {
- Error {
- inner: Inner::Boxed(err.into()),
- }
- }
- }
-
- impl error::Error for Error {}
-
- impl From<io::Error> for Error {
- fn from(err: io::Error) -> Self {
- Error::boxed(err)
- }
- }
-}
+use std::fmt;
+
+/// An error encountered while working with structured data.
+#[derive(Debug)]
+pub struct Error {
+ inner: Inner,
+}
+
+#[derive(Debug)]
+enum Inner {
+ #[cfg(feature = "std")]
+ Boxed(std_support::BoxedError),
+ Msg(&'static str),
+ #[cfg(feature = "value-bag")]
+ Value(crate::kv::value::inner::Error),
+ Fmt,
+}
+
+impl Error {
+ /// Create an error from a message.
+ pub fn msg(msg: &'static str) -> Self {
+ Error {
+ inner: Inner::Msg(msg),
+ }
+ }
+
+ // Not public so we don't leak the `crate::kv::value::inner` API
+ #[cfg(feature = "value-bag")]
+ pub(super) fn from_value(err: crate::kv::value::inner::Error) -> Self {
+ Error {
+ inner: Inner::Value(err),
+ }
+ }
+
+ // Not public so we don't leak the `crate::kv::value::inner` API
+ #[cfg(feature = "value-bag")]
+ pub(super) fn into_value(self) -> crate::kv::value::inner::Error {
+ match self.inner {
+ Inner::Value(err) => err,
+ #[cfg(feature = "kv_std")]
+ _ => crate::kv::value::inner::Error::boxed(self),
+ #[cfg(not(feature = "kv_std"))]
+ _ => crate::kv::value::inner::Error::msg("error inspecting a value"),
+ }
+ }
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use self::Inner::*;
+ match &self.inner {
+ #[cfg(feature = "std")]
+ Boxed(err) => err.fmt(f),
+ #[cfg(feature = "value-bag")]
+ Value(err) => err.fmt(f),
+ Msg(msg) => msg.fmt(f),
+ Fmt => fmt::Error.fmt(f),
+ }
+ }
+}
+
+impl From<fmt::Error> for Error {
+ fn from(_: fmt::Error) -> Self {
+ Error { inner: Inner::Fmt }
+ }
+}
+
+#[cfg(feature = "std")]
+mod std_support {
+ use super::*;
+ use std::{error, io};
+
+ pub(super) type BoxedError = Box<dyn error::Error + Send + Sync>;
+
+ impl Error {
+ /// Create an error from a standard error type.
+ pub fn boxed<E>(err: E) -> Self
+ where
+ E: Into<BoxedError>,
+ {
+ Error {
+ inner: Inner::Boxed(err.into()),
+ }
+ }
+ }
+
+ impl error::Error for Error {}
+
+ impl From<io::Error> for Error {
+ fn from(err: io::Error) -> Self {
+ Error::boxed(err)
+ }
+ }
+}
diff --git a/crates/log/src/kv/key.rs b/crates/log/src/kv/key.rs
index 0e688c1..9a64b95 100644
--- a/crates/log/src/kv/key.rs
+++ b/crates/log/src/kv/key.rs
@@ -1,172 +1,143 @@
-//! Structured keys.
-
-use std::borrow::Borrow;
-use std::cmp;
-use std::fmt;
-use std::hash;
-
-/// A type that can be converted into a [`Key`](struct.Key.html).
-pub trait ToKey {
- /// Perform the conversion.
- fn to_key(&self) -> Key;
-}
-
-impl<'a, T> ToKey for &'a T
-where
- T: ToKey + ?Sized,
-{
- fn to_key(&self) -> Key {
- (**self).to_key()
- }
-}
-
-impl<'k> ToKey for Key<'k> {
- fn to_key(&self) -> Key {
- Key { key: self.key }
- }
-}
-
-impl ToKey for str {
- fn to_key(&self) -> Key {
- Key::from_str(self)
- }
-}
-
-/// A key in a structured key-value pair.
-#[derive(Clone)]
-pub struct Key<'k> {
- key: &'k str,
-}
-
-impl<'k> Key<'k> {
- /// Get a key from a borrowed string.
- pub fn from_str(key: &'k str) -> Self {
- Key { key: key }
- }
-
- /// Get a borrowed string from this key.
- pub fn as_str(&self) -> &str {
- self.key
- }
-}
-
-impl<'k> fmt::Debug for Key<'k> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- self.key.fmt(f)
- }
-}
-
-impl<'k> fmt::Display for Key<'k> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- self.key.fmt(f)
- }
-}
-
-impl<'k> hash::Hash for Key<'k> {
- fn hash<H>(&self, state: &mut H)
- where
- H: hash::Hasher,
- {
- self.as_str().hash(state)
- }
-}
-
-impl<'k, 'ko> PartialEq<Key<'ko>> for Key<'k> {
- fn eq(&self, other: &Key<'ko>) -> bool {
- self.as_str().eq(other.as_str())
- }
-}
-
-impl<'k> Eq for Key<'k> {}
-
-impl<'k, 'ko> PartialOrd<Key<'ko>> for Key<'k> {
- fn partial_cmp(&self, other: &Key<'ko>) -> Option<cmp::Ordering> {
- self.as_str().partial_cmp(other.as_str())
- }
-}
-
-impl<'k> Ord for Key<'k> {
- fn cmp(&self, other: &Self) -> cmp::Ordering {
- self.as_str().cmp(other.as_str())
- }
-}
-
-impl<'k> AsRef<str> for Key<'k> {
- fn as_ref(&self) -> &str {
- self.as_str()
- }
-}
-
-impl<'k> Borrow<str> for Key<'k> {
- fn borrow(&self) -> &str {
- self.as_str()
- }
-}
-
-impl<'k> From<&'k str> for Key<'k> {
- fn from(s: &'k str) -> Self {
- Key::from_str(s)
- }
-}
-
-#[cfg(feature = "std")]
-mod std_support {
- use super::*;
-
- use std::borrow::Cow;
-
- impl ToKey for String {
- fn to_key(&self) -> Key {
- Key::from_str(self)
- }
- }
-
- impl<'a> ToKey for Cow<'a, str> {
- fn to_key(&self) -> Key {
- Key::from_str(self)
- }
- }
-}
-
-#[cfg(feature = "kv_unstable_sval")]
-mod sval_support {
- use super::*;
-
- extern crate sval;
-
- use self::sval::value::{self, Value};
-
- impl<'a> Value for Key<'a> {
- fn stream(&self, stream: &mut value::Stream) -> value::Result {
- self.key.stream(stream)
- }
- }
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-mod serde_support {
- use super::*;
-
- extern crate serde;
-
- use self::serde::{Serialize, Serializer};
-
- impl<'a> Serialize for Key<'a> {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- self.key.serialize(serializer)
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn key_from_string() {
- assert_eq!("a key", Key::from_str("a key").as_str());
- }
-}
+//! Structured keys.
+
+use std::borrow::Borrow;
+use std::fmt;
+
+/// A type that can be converted into a [`Key`](struct.Key.html).
+pub trait ToKey {
+ /// Perform the conversion.
+ fn to_key(&self) -> Key;
+}
+
+impl<'a, T> ToKey for &'a T
+where
+ T: ToKey + ?Sized,
+{
+ fn to_key(&self) -> Key {
+ (**self).to_key()
+ }
+}
+
+impl<'k> ToKey for Key<'k> {
+ fn to_key(&self) -> Key {
+ Key { key: self.key }
+ }
+}
+
+impl ToKey for str {
+ fn to_key(&self) -> Key {
+ Key::from_str(self)
+ }
+}
+
+/// A key in a key-value.
+// These impls must only be based on the as_str() representation of the key
+// If a new field (such as an optional index) is added to the key they must not affect comparison
+#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Key<'k> {
+ key: &'k str,
+}
+
+impl<'k> Key<'k> {
+ /// Get a key from a borrowed string.
+ pub fn from_str(key: &'k str) -> Self {
+ Key { key }
+ }
+
+ /// Get a borrowed string from this key.
+ pub fn as_str(&self) -> &str {
+ self.key
+ }
+}
+
+impl<'k> fmt::Display for Key<'k> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.key.fmt(f)
+ }
+}
+
+impl<'k> AsRef<str> for Key<'k> {
+ fn as_ref(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl<'k> Borrow<str> for Key<'k> {
+ fn borrow(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl<'k> From<&'k str> for Key<'k> {
+ fn from(s: &'k str) -> Self {
+ Key::from_str(s)
+ }
+}
+
+#[cfg(feature = "std")]
+mod std_support {
+ use super::*;
+
+ use std::borrow::Cow;
+
+ impl ToKey for String {
+ fn to_key(&self) -> Key {
+ Key::from_str(self)
+ }
+ }
+
+ impl<'a> ToKey for Cow<'a, str> {
+ fn to_key(&self) -> Key {
+ Key::from_str(self)
+ }
+ }
+}
+
+#[cfg(feature = "kv_sval")]
+mod sval_support {
+ use super::*;
+
+ use sval::Value;
+ use sval_ref::ValueRef;
+
+ impl<'a> Value for Key<'a> {
+ fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(
+ &'sval self,
+ stream: &mut S,
+ ) -> sval::Result {
+ self.key.stream(stream)
+ }
+ }
+
+ impl<'a> ValueRef<'a> for Key<'a> {
+ fn stream_ref<S: sval::Stream<'a> + ?Sized>(&self, stream: &mut S) -> sval::Result {
+ self.key.stream(stream)
+ }
+ }
+}
+
+#[cfg(feature = "kv_serde")]
+mod serde_support {
+ use super::*;
+
+ use serde::{Serialize, Serializer};
+
+ impl<'a> Serialize for Key<'a> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.key.serialize(serializer)
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn key_from_string() {
+ assert_eq!("a key", Key::from_str("a key").as_str());
+ }
+}
diff --git a/crates/log/src/kv/mod.rs b/crates/log/src/kv/mod.rs
index f96b821..1ccb825 100644
--- a/crates/log/src/kv/mod.rs
+++ b/crates/log/src/kv/mod.rs
@@ -1,26 +1,265 @@
-//! **UNSTABLE:** Structured key-value pairs.
-//!
-//! This module is unstable and breaking changes may be made
-//! at any time. See [the tracking issue](https://github.com/rust-lang-nursery/log/issues/328)
-//! for more details.
-//!
-//! Add the `kv_unstable` feature to your `Cargo.toml` to enable
-//! this module:
-//!
-//! ```toml
-//! [dependencies.log]
-//! features = ["kv_unstable"]
-//! ```
-
-mod error;
-mod key;
-pub mod source;
-
-pub mod value;
-
-pub use self::error::Error;
-pub use self::key::{Key, ToKey};
-pub use self::source::{Source, Visitor};
-
-#[doc(inline)]
-pub use self::value::{ToValue, Value};
+//! Structured logging.
+//!
+//! Add the `kv` feature to your `Cargo.toml` to enable
+//! this module:
+//!
+//! ```toml
+//! [dependencies.log]
+//! features = ["kv"]
+//! ```
+//!
+//! # Structured logging in `log`
+//!
+//! Structured logging enhances traditional text-based log records with user-defined
+//! attributes. Structured logs can be analyzed using a variety of data processing
+//! techniques, without needing to find and parse attributes from unstructured text first.
+//!
+//! In `log`, user-defined attributes are part of a [`Source`] on the log record.
+//! Each attribute is a key-value; a pair of [`Key`] and [`Value`]. Keys are strings
+//! and values are a datum of any type that can be formatted or serialized. Simple types
+//! like strings, booleans, and numbers are supported, as well as arbitrarily complex
+//! structures involving nested objects and sequences.
+//!
+//! ## Adding key-values to log records
+//!
+//! Key-values appear before the message format in the `log!` macros:
+//!
+//! ```
+//! # use log::info;
+//! info!(a = 1; "Something of interest");
+//! ```
+//!
+//! Key-values support the same shorthand identifer syntax as `format_args`:
+//!
+//! ```
+//! # use log::info;
+//! let a = 1;
+//!
+//! info!(a; "Something of interest");
+//! ```
+//!
+//! Values are capturing using the [`ToValue`] trait by default. To capture a value
+//! using a different trait implementation, use a modifier after its key. Here's how
+//! the same example can capture `a` using its `Debug` implementation instead:
+//!
+//! ```
+//! # use log::info;
+//! info!(a:? = 1; "Something of interest");
+//! ```
+//!
+//! The following capturing modifiers are supported:
+//!
+//! - `:?` will capture the value using `Debug`.
+//! - `:debug` will capture the value using `Debug`.
+//! - `:%` will capture the value using `Display`.
+//! - `:display` will capture the value using `Display`.
+//! - `:err` will capture the value using `std::error::Error` (requires the `kv_std` feature).
+//! - `:sval` will capture the value using `sval::Value` (requires the `kv_sval` feature).
+//! - `:serde` will capture the value using `serde::Serialize` (requires the `kv_serde` feature).
+//!
+//! ## Working with key-values on log records
+//!
+//! Use the [`Record::key_values`](../struct.Record.html#method.key_values) method to access key-values.
+//!
+//! Individual values can be pulled from the source by their key:
+//!
+//! ```
+//! # fn main() -> Result<(), log::kv::Error> {
+//! use log::kv::{Source, Key, Value};
+//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
+//!
+//! // info!(a = 1; "Something of interest");
+//!
+//! let a: Value = record.key_values().get(Key::from("a")).unwrap();
+//! assert_eq!(1, a.to_i64().unwrap());
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! All key-values can also be enumerated using a [`VisitSource`]:
+//!
+//! ```
+//! # fn main() -> Result<(), log::kv::Error> {
+//! use std::collections::BTreeMap;
+//!
+//! use log::kv::{self, Source, Key, Value, VisitSource};
+//!
+//! struct Collect<'kvs>(BTreeMap<Key<'kvs>, Value<'kvs>>);
+//!
+//! impl<'kvs> VisitSource<'kvs> for Collect<'kvs> {
+//! fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
+//! self.0.insert(key, value);
+//!
+//! Ok(())
+//! }
+//! }
+//!
+//! let mut visitor = Collect(BTreeMap::new());
+//!
+//! # let record = log::Record::builder().key_values(&[("a", 1), ("b", 2), ("c", 3)]).build();
+//! // info!(a = 1, b = 2, c = 3; "Something of interest");
+//!
+//! record.key_values().visit(&mut visitor)?;
+//!
+//! let collected = visitor.0;
+//!
+//! assert_eq!(
+//! vec!["a", "b", "c"],
+//! collected
+//! .keys()
+//! .map(|k| k.as_str())
+//! .collect::<Vec<_>>(),
+//! );
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! [`Value`]s have methods for conversions to common types:
+//!
+//! ```
+//! # fn main() -> Result<(), log::kv::Error> {
+//! use log::kv::{Source, Key};
+//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
+//!
+//! // info!(a = 1; "Something of interest");
+//!
+//! let a = record.key_values().get(Key::from("a")).unwrap();
+//!
+//! assert_eq!(1, a.to_i64().unwrap());
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! Values also have their own [`VisitValue`] type. Value visitors are a lightweight
+//! API for working with primitives types:
+//!
+//! ```
+//! # fn main() -> Result<(), log::kv::Error> {
+//! use log::kv::{self, Source, Key, VisitValue};
+//! # let record = log::Record::builder().key_values(&[("a", 1)]).build();
+//!
+//! struct IsNumeric(bool);
+//!
+//! impl<'kvs> VisitValue<'kvs> for IsNumeric {
+//! fn visit_any(&mut self, _value: kv::Value) -> Result<(), kv::Error> {
+//! self.0 = false;
+//! Ok(())
+//! }
+//!
+//! fn visit_u64(&mut self, _value: u64) -> Result<(), kv::Error> {
+//! self.0 = true;
+//! Ok(())
+//! }
+//!
+//! fn visit_i64(&mut self, _value: i64) -> Result<(), kv::Error> {
+//! self.0 = true;
+//! Ok(())
+//! }
+//!
+//! fn visit_u128(&mut self, _value: u128) -> Result<(), kv::Error> {
+//! self.0 = true;
+//! Ok(())
+//! }
+//!
+//! fn visit_i128(&mut self, _value: i128) -> Result<(), kv::Error> {
+//! self.0 = true;
+//! Ok(())
+//! }
+//!
+//! fn visit_f64(&mut self, _value: f64) -> Result<(), kv::Error> {
+//! self.0 = true;
+//! Ok(())
+//! }
+//! }
+//!
+//! // info!(a = 1; "Something of interest");
+//!
+//! let a = record.key_values().get(Key::from("a")).unwrap();
+//!
+//! let mut visitor = IsNumeric(false);
+//!
+//! a.visit(&mut visitor)?;
+//!
+//! let is_numeric = visitor.0;
+//!
+//! assert!(is_numeric);
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! To serialize a value to a format like JSON, you can also use either `serde` or `sval`:
+//!
+//! ```
+//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
+//! # #[cfg(feature = "serde")]
+//! # {
+//! # use log::kv::Key;
+//! #[derive(serde::Serialize)]
+//! struct Data {
+//! a: i32, b: bool,
+//! c: &'static str,
+//! }
+//!
+//! let data = Data { a: 1, b: true, c: "Some data" };
+//!
+//! # let source = [("a", log::kv::Value::from_serde(&data))];
+//! # let record = log::Record::builder().key_values(&source).build();
+//! // info!(a = data; "Something of interest");
+//!
+//! let a = record.key_values().get(Key::from("a")).unwrap();
+//!
+//! assert_eq!("{\"a\":1,\"b\":true,\"c\":\"Some data\"}", serde_json::to_string(&a)?);
+//! # }
+//! # Ok(())
+//! # }
+//! ```
+//!
+//! The choice of serialization framework depends on the needs of the consumer.
+//! If you're in a no-std environment, you can use `sval`. In other cases, you can use `serde`.
+//! Log producers and log consumers don't need to agree on the serialization framework.
+//! A value can be captured using its `serde::Serialize` implementation and still be serialized
+//! through `sval` without losing any structure or data.
+//!
+//! Values can also always be formatted using the standard `Debug` and `Display`
+//! traits:
+//!
+//! ```
+//! # use log::kv::Key;
+//! # #[derive(Debug)]
+//! struct Data {
+//! a: i32,
+//! b: bool,
+//! c: &'static str,
+//! }
+//!
+//! let data = Data { a: 1, b: true, c: "Some data" };
+//!
+//! # let source = [("a", log::kv::Value::from_debug(&data))];
+//! # let record = log::Record::builder().key_values(&source).build();
+//! // info!(a = data; "Something of interest");
+//!
+//! let a = record.key_values().get(Key::from("a")).unwrap();
+//!
+//! assert_eq!("Data { a: 1, b: true, c: \"Some data\" }", format!("{a:?}"));
+//! ```
+
+mod error;
+mod key;
+
+#[cfg(not(feature = "kv_unstable"))]
+mod source;
+#[cfg(not(feature = "kv_unstable"))]
+mod value;
+
+pub use self::error::Error;
+pub use self::key::{Key, ToKey};
+pub use self::source::{Source, VisitSource};
+pub use self::value::{ToValue, Value, VisitValue};
+
+#[cfg(feature = "kv_unstable")]
+pub mod source;
+#[cfg(feature = "kv_unstable")]
+pub mod value;
+
+#[cfg(feature = "kv_unstable")]
+pub use self::source::Visitor;
diff --git a/crates/log/src/kv/source.rs b/crates/log/src/kv/source.rs
index e3ecde9..f463e6d 100644
--- a/crates/log/src/kv/source.rs
+++ b/crates/log/src/kv/source.rs
@@ -1,754 +1,514 @@
-//! Sources for key-value pairs.
-
-#[cfg(feature = "kv_unstable_sval")]
-extern crate sval;
-
-#[cfg(feature = "kv_unstable_serde")]
-extern crate serde;
-
-use kv::{Error, Key, ToKey, ToValue, Value};
-use std::fmt;
-
-/// A source of key-value pairs.
-///
-/// The source may be a single pair, a set of pairs, or a filter over a set of pairs.
-/// Use the [`Visitor`](trait.Visitor.html) trait to inspect the structured data
-/// in a source.
-pub trait Source {
- /// Visit key-value pairs.
- ///
- /// A source doesn't have to guarantee any ordering or uniqueness of key-value pairs.
- /// If the given visitor returns an error then the source may early-return with it,
- /// even if there are more key-value pairs.
- ///
- /// # Implementation notes
- ///
- /// A source should yield the same key-value pairs to a subsequent visitor unless
- /// that visitor itself fails.
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error>;
-
- /// Get the value for a given key.
- ///
- /// If the key appears multiple times in the source then which key is returned
- /// is implementation specific.
- ///
- /// # Implementation notes
- ///
- /// A source that can provide a more efficient implementation of this method
- /// should override it.
- #[cfg(not(test))]
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- get_default(self, key)
- }
-
- #[cfg(test)]
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>>;
-
- /// Count the number of key-value pairs that can be visited.
- ///
- /// # Implementation notes
- ///
- /// A source that knows the number of key-value pairs upfront may provide a more
- /// efficient implementation.
- ///
- /// A subsequent call to `visit` should yield the same number of key-value pairs
- /// to the visitor, unless that visitor fails part way through.
- #[cfg(not(test))]
- fn count(&self) -> usize {
- count_default(self)
- }
-
- #[cfg(test)]
- fn count(&self) -> usize;
-}
-
-/// The default implemention of `Source::get`
-pub(crate) fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
- struct Get<'k, 'v> {
- key: Key<'k>,
- found: Option<Value<'v>>,
- }
-
- impl<'k, 'kvs> Visitor<'kvs> for Get<'k, 'kvs> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- if self.key == key {
- self.found = Some(value);
- }
-
- Ok(())
- }
- }
-
- let mut get = Get { key, found: None };
-
- let _ = source.visit(&mut get);
- get.found
-}
-
-/// The default implementation of `Source::count`.
-pub(crate) fn count_default(source: impl Source) -> usize {
- struct Count(usize);
-
- impl<'kvs> Visitor<'kvs> for Count {
- fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> {
- self.0 += 1;
-
- Ok(())
- }
- }
-
- let mut count = Count(0);
- let _ = source.visit(&mut count);
- count.0
-}
-
-impl<'a, T> Source for &'a T
-where
- T: Source + ?Sized,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- Source::visit(&**self, visitor)
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- Source::get(&**self, key)
- }
-
- fn count(&self) -> usize {
- Source::count(&**self)
- }
-}
-
-impl<K, V> Source for (K, V)
-where
- K: ToKey,
- V: ToValue,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- visitor.visit_pair(self.0.to_key(), self.1.to_value())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- if self.0.to_key() == key {
- Some(self.1.to_value())
- } else {
- None
- }
- }
-
- fn count(&self) -> usize {
- 1
- }
-}
-
-impl<S> Source for [S]
-where
- S: Source,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- for source in self {
- source.visit(visitor)?;
- }
-
- Ok(())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- for source in self {
- if let Some(found) = source.get(key.clone()) {
- return Some(found);
- }
- }
-
- None
- }
-
- fn count(&self) -> usize {
- self.len()
- }
-}
-
-impl<S> Source for Option<S>
-where
- S: Source,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- if let Some(ref source) = *self {
- source.visit(visitor)?;
- }
-
- Ok(())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- self.as_ref().and_then(|s| s.get(key))
- }
-
- fn count(&self) -> usize {
- self.as_ref().map(Source::count).unwrap_or(0)
- }
-}
-
-/// A visitor for the key-value pairs in a [`Source`](trait.Source.html).
-pub trait Visitor<'kvs> {
- /// Visit a key-value pair.
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
-}
-
-impl<'a, 'kvs, T> Visitor<'kvs> for &'a mut T
-where
- T: Visitor<'kvs> + ?Sized,
-{
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- (**self).visit_pair(key, value)
- }
-}
-
-impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugMap<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.entry(&key, &value);
- Ok(())
- }
-}
-
-impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugList<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.entry(&(key, value));
- Ok(())
- }
-}
-
-impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugSet<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.entry(&(key, value));
- Ok(())
- }
-}
-
-impl<'a, 'b: 'a, 'kvs> Visitor<'kvs> for fmt::DebugTuple<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.field(&key);
- self.field(&value);
- Ok(())
- }
-}
-
-#[cfg(feature = "std")]
-mod std_support {
- use super::*;
- use std::borrow::Borrow;
- use std::collections::{BTreeMap, HashMap};
- use std::hash::{BuildHasher, Hash};
-
- impl<S> Source for Box<S>
- where
- S: Source + ?Sized,
- {
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- Source::visit(&**self, visitor)
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- Source::get(&**self, key)
- }
-
- fn count(&self) -> usize {
- Source::count(&**self)
- }
- }
-
- impl<S> Source for Vec<S>
- where
- S: Source,
- {
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- Source::visit(&**self, visitor)
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- Source::get(&**self, key)
- }
-
- fn count(&self) -> usize {
- Source::count(&**self)
- }
- }
-
- impl<'kvs, V> Visitor<'kvs> for Box<V>
- where
- V: Visitor<'kvs> + ?Sized,
- {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- (**self).visit_pair(key, value)
- }
- }
-
- impl<K, V, S> Source for HashMap<K, V, S>
- where
- K: ToKey + Borrow<str> + Eq + Hash,
- V: ToValue,
- S: BuildHasher,
- {
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- for (key, value) in self {
- visitor.visit_pair(key.to_key(), value.to_value())?;
- }
- Ok(())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- HashMap::get(self, key.as_str()).map(|v| v.to_value())
- }
-
- fn count(&self) -> usize {
- self.len()
- }
- }
-
- impl<K, V> Source for BTreeMap<K, V>
- where
- K: ToKey + Borrow<str> + Ord,
- V: ToValue,
- {
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- for (key, value) in self {
- visitor.visit_pair(key.to_key(), value.to_value())?;
- }
- Ok(())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- BTreeMap::get(self, key.as_str()).map(|v| v.to_value())
- }
-
- fn count(&self) -> usize {
- self.len()
- }
- }
-
- #[cfg(test)]
- mod tests {
- use super::*;
- use kv::value::tests::Token;
- use std::collections::{BTreeMap, HashMap};
-
- #[test]
- fn count() {
- assert_eq!(1, Source::count(&Box::new(("a", 1))));
- assert_eq!(2, Source::count(&vec![("a", 1), ("b", 2)]));
- }
-
- #[test]
- fn get() {
- let source = vec![("a", 1), ("b", 2), ("a", 1)];
- assert_eq!(
- Token::I64(1),
- Source::get(&source, Key::from_str("a")).unwrap().to_token()
- );
-
- let source = Box::new(Option::None::<(&str, i32)>);
- assert!(Source::get(&source, Key::from_str("a")).is_none());
- }
-
- #[test]
- fn hash_map() {
- let mut map = HashMap::new();
- map.insert("a", 1);
- map.insert("b", 2);
-
- assert_eq!(2, Source::count(&map));
- assert_eq!(
- Token::I64(1),
- Source::get(&map, Key::from_str("a")).unwrap().to_token()
- );
- }
-
- #[test]
- fn btree_map() {
- let mut map = BTreeMap::new();
- map.insert("a", 1);
- map.insert("b", 2);
-
- assert_eq!(2, Source::count(&map));
- assert_eq!(
- Token::I64(1),
- Source::get(&map, Key::from_str("a")).unwrap().to_token()
- );
- }
- }
-}
-
-/// The result of calling `Source::as_map`.
-pub struct AsMap<S>(S);
-
-/// Visit this source as a map.
-pub fn as_map<S>(source: S) -> AsMap<S>
-where
- S: Source,
-{
- AsMap(source)
-}
-
-impl<S> Source for AsMap<S>
-where
- S: Source,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- self.0.visit(visitor)
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- self.0.get(key)
- }
-
- fn count(&self) -> usize {
- self.0.count()
- }
-}
-
-impl<S> fmt::Debug for AsMap<S>
-where
- S: Source,
-{
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let mut f = f.debug_map();
- self.0.visit(&mut f).map_err(|_| fmt::Error)?;
- f.finish()
- }
-}
-
-/// The result of calling `Source::as_list`
-pub struct AsList<S>(S);
-
-/// Visit this source as a list.
-pub fn as_list<S>(source: S) -> AsList<S>
-where
- S: Source,
-{
- AsList(source)
-}
-
-impl<S> Source for AsList<S>
-where
- S: Source,
-{
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- self.0.visit(visitor)
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- self.0.get(key)
- }
-
- fn count(&self) -> usize {
- self.0.count()
- }
-}
-
-impl<S> fmt::Debug for AsList<S>
-where
- S: Source,
-{
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let mut f = f.debug_list();
- self.0.visit(&mut f).map_err(|_| fmt::Error)?;
- f.finish()
- }
-}
-
-#[cfg(feature = "kv_unstable_sval")]
-mod sval_support {
- use super::*;
-
- use self::sval::value;
-
- impl<S> value::Value for AsMap<S>
- where
- S: Source,
- {
- fn stream(&self, stream: &mut value::Stream) -> value::Result {
- struct StreamVisitor<'a, 'b>(&'a mut value::Stream<'b>);
-
- impl<'a, 'b, 'kvs> Visitor<'kvs> for StreamVisitor<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.0
- .map_key(key)
- .map_err(|_| Error::msg("failed to stream map key"))?;
- self.0
- .map_value(value)
- .map_err(|_| Error::msg("failed to stream map value"))?;
- Ok(())
- }
- }
-
- stream
- .map_begin(Some(self.count()))
- .map_err(|_| self::sval::Error::msg("failed to begin map"))?;
-
- self.visit(&mut StreamVisitor(stream))
- .map_err(|_| self::sval::Error::msg("failed to visit key-values"))?;
-
- stream
- .map_end()
- .map_err(|_| self::sval::Error::msg("failed to end map"))
- }
- }
-
- impl<S> value::Value for AsList<S>
- where
- S: Source,
- {
- fn stream(&self, stream: &mut value::Stream) -> value::Result {
- struct StreamVisitor<'a, 'b>(&'a mut value::Stream<'b>);
-
- impl<'a, 'b, 'kvs> Visitor<'kvs> for StreamVisitor<'a, 'b> {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.0
- .seq_elem((key, value))
- .map_err(|_| Error::msg("failed to stream seq entry"))?;
- Ok(())
- }
- }
-
- stream
- .seq_begin(Some(self.count()))
- .map_err(|_| self::sval::Error::msg("failed to begin seq"))?;
-
- self.visit(&mut StreamVisitor(stream))
- .map_err(|_| self::sval::Error::msg("failed to visit key-values"))?;
-
- stream
- .seq_end()
- .map_err(|_| self::sval::Error::msg("failed to end seq"))
- }
- }
-
- #[cfg(test)]
- mod tests {
- use super::*;
-
- use self::sval::Value;
-
- use crate::kv::source;
-
- #[test]
- fn derive_stream() {
- #[derive(Value)]
- pub struct MyRecordAsMap<'a> {
- msg: &'a str,
- kvs: source::AsMap<&'a dyn Source>,
- }
-
- #[derive(Value)]
- pub struct MyRecordAsList<'a> {
- msg: &'a str,
- kvs: source::AsList<&'a dyn Source>,
- }
- }
- }
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-pub mod as_map {
- //! `serde` adapters for serializing a `Source` as a map.
-
- use super::*;
-
- use self::serde::{Serialize, Serializer};
-
- /// Serialize a `Source` as a map.
- pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
- where
- T: Source,
- S: Serializer,
- {
- as_map(source).serialize(serializer)
- }
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-pub mod as_list {
- //! `serde` adapters for serializing a `Source` as a list.
-
- use super::*;
-
- use self::serde::{Serialize, Serializer};
-
- /// Serialize a `Source` as a list.
- pub fn serialize<T, S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
- where
- T: Source,
- S: Serializer,
- {
- as_list(source).serialize(serializer)
- }
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-mod serde_support {
- use super::*;
-
- use self::serde::ser::{Error as SerError, Serialize, SerializeMap, SerializeSeq, Serializer};
-
- impl<T> Serialize for AsMap<T>
- where
- T: Source,
- {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- struct SerializerVisitor<'a, S>(&'a mut S);
-
- impl<'a, 'kvs, S> Visitor<'kvs> for SerializerVisitor<'a, S>
- where
- S: SerializeMap,
- {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.0
- .serialize_entry(&key, &value)
- .map_err(|_| Error::msg("failed to serialize map entry"))?;
- Ok(())
- }
- }
-
- let mut map = serializer.serialize_map(Some(self.count()))?;
-
- self.visit(&mut SerializerVisitor(&mut map))
- .map_err(|_| S::Error::custom("failed to visit key-values"))?;
-
- map.end()
- }
- }
-
- impl<T> Serialize for AsList<T>
- where
- T: Source,
- {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- struct SerializerVisitor<'a, S>(&'a mut S);
-
- impl<'a, 'kvs, S> Visitor<'kvs> for SerializerVisitor<'a, S>
- where
- S: SerializeSeq,
- {
- fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
- self.0
- .serialize_element(&(key, value))
- .map_err(|_| Error::msg("failed to serialize seq entry"))?;
- Ok(())
- }
- }
-
- let mut seq = serializer.serialize_seq(Some(self.count()))?;
-
- self.visit(&mut SerializerVisitor(&mut seq))
- .map_err(|_| S::Error::custom("failed to visit seq"))?;
-
- seq.end()
- }
- }
-
- #[cfg(test)]
- mod tests {
- use super::*;
-
- use self::serde::Serialize;
-
- use crate::kv::source;
-
- #[test]
- fn derive_serialize() {
- #[derive(Serialize)]
- pub struct MyRecordAsMap<'a> {
- msg: &'a str,
- #[serde(flatten)]
- #[serde(with = "source::as_map")]
- kvs: &'a dyn Source,
- }
-
- #[derive(Serialize)]
- pub struct MyRecordAsList<'a> {
- msg: &'a str,
- #[serde(flatten)]
- #[serde(with = "source::as_list")]
- kvs: &'a dyn Source,
- }
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use kv::value::tests::Token;
-
- #[test]
- fn source_is_object_safe() {
- fn _check(_: &dyn Source) {}
- }
-
- #[test]
- fn visitor_is_object_safe() {
- fn _check(_: &dyn Visitor) {}
- }
-
- #[test]
- fn count() {
- struct OnePair {
- key: &'static str,
- value: i32,
- }
-
- impl Source for OnePair {
- fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
- visitor.visit_pair(self.key.to_key(), self.value.to_value())
- }
-
- fn get<'v>(&'v self, key: Key) -> Option<Value<'v>> {
- get_default(self, key)
- }
-
- fn count(&self) -> usize {
- count_default(self)
- }
- }
-
- assert_eq!(1, Source::count(&("a", 1)));
- assert_eq!(2, Source::count(&[("a", 1), ("b", 2)] as &[_]));
- assert_eq!(0, Source::count(&Option::None::<(&str, i32)>));
- assert_eq!(1, Source::count(&OnePair { key: "a", value: 1 }));
- }
-
- #[test]
- fn get() {
- let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_];
- assert_eq!(
- Token::I64(1),
- Source::get(source, Key::from_str("a")).unwrap().to_token()
- );
- assert_eq!(
- Token::I64(2),
- Source::get(source, Key::from_str("b")).unwrap().to_token()
- );
- assert!(Source::get(&source, Key::from_str("c")).is_none());
-
- let source = Option::None::<(&str, i32)>;
- assert!(Source::get(&source, Key::from_str("a")).is_none());
- }
-
- #[test]
- fn as_map() {
- let _ = crate::kv::source::as_map(("a", 1));
- let _ = crate::kv::source::as_map(&("a", 1) as &dyn Source);
- }
-
- #[test]
- fn as_list() {
- let _ = crate::kv::source::as_list(("a", 1));
- let _ = crate::kv::source::as_list(&("a", 1) as &dyn Source);
- }
-}
+//! Sources for key-values.
+//!
+//! This module defines the [`Source`] type and supporting APIs for
+//! working with collections of key-values.
+
+use crate::kv::{Error, Key, ToKey, ToValue, Value};
+use std::fmt;
+
+/// A source of key-values.
+///
+/// The source may be a single pair, a set of pairs, or a filter over a set of pairs.
+/// Use the [`VisitSource`](trait.VisitSource.html) trait to inspect the structured data
+/// in a source.
+///
+/// A source is like an iterator over its key-values, except with a push-based API
+/// instead of a pull-based one.
+///
+/// # Examples
+///
+/// Enumerating the key-values in a source:
+///
+/// ```
+/// # fn main() -> Result<(), log::kv::Error> {
+/// use log::kv::{self, Source, Key, Value, VisitSource};
+///
+/// // A `VisitSource` that prints all key-values
+/// // VisitSources are fed the key-value pairs of each key-values
+/// struct Printer;
+///
+/// impl<'kvs> VisitSource<'kvs> for Printer {
+/// fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), kv::Error> {
+/// println!("{key}: {value}");
+///
+/// Ok(())
+/// }
+/// }
+///
+/// // A source with 3 key-values
+/// // Common collection types implement the `Source` trait
+/// let source = &[
+/// ("a", 1),
+/// ("b", 2),
+/// ("c", 3),
+/// ];
+///
+/// // Pass an instance of the `VisitSource` to a `Source` to visit it
+/// source.visit(&mut Printer)?;
+/// # Ok(())
+/// # }
+/// ```
+pub trait Source {
+ /// Visit key-values.
+ ///
+ /// A source doesn't have to guarantee any ordering or uniqueness of key-values.
+ /// If the given visitor returns an error then the source may early-return with it,
+ /// even if there are more key-values.
+ ///
+ /// # Implementation notes
+ ///
+ /// A source should yield the same key-values to a subsequent visitor unless
+ /// that visitor itself fails.
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error>;
+
+ /// Get the value for a given key.
+ ///
+ /// If the key appears multiple times in the source then which key is returned
+ /// is implementation specific.
+ ///
+ /// # Implementation notes
+ ///
+ /// A source that can provide a more efficient implementation of this method
+ /// should override it.
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ get_default(self, key)
+ }
+
+ /// Count the number of key-values that can be visited.
+ ///
+ /// # Implementation notes
+ ///
+ /// A source that knows the number of key-values upfront may provide a more
+ /// efficient implementation.
+ ///
+ /// A subsequent call to `visit` should yield the same number of key-values.
+ fn count(&self) -> usize {
+ count_default(self)
+ }
+}
+
+/// The default implementation of `Source::get`
+fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
+ struct Get<'k, 'v> {
+ key: Key<'k>,
+ found: Option<Value<'v>>,
+ }
+
+ impl<'k, 'kvs> VisitSource<'kvs> for Get<'k, 'kvs> {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ if self.key == key {
+ self.found = Some(value);
+ }
+
+ Ok(())
+ }
+ }
+
+ let mut get = Get { key, found: None };
+
+ let _ = source.visit(&mut get);
+ get.found
+}
+
+/// The default implementation of `Source::count`.
+fn count_default(source: impl Source) -> usize {
+ struct Count(usize);
+
+ impl<'kvs> VisitSource<'kvs> for Count {
+ fn visit_pair(&mut self, _: Key<'kvs>, _: Value<'kvs>) -> Result<(), Error> {
+ self.0 += 1;
+
+ Ok(())
+ }
+ }
+
+ let mut count = Count(0);
+ let _ = source.visit(&mut count);
+ count.0
+}
+
+impl<'a, T> Source for &'a T
+where
+ T: Source + ?Sized,
+{
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(&**self, visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(&**self, key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(&**self)
+ }
+}
+
+impl<K, V> Source for (K, V)
+where
+ K: ToKey,
+ V: ToValue,
+{
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ visitor.visit_pair(self.0.to_key(), self.1.to_value())
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ if self.0.to_key() == key {
+ Some(self.1.to_value())
+ } else {
+ None
+ }
+ }
+
+ fn count(&self) -> usize {
+ 1
+ }
+}
+
+impl<S> Source for [S]
+where
+ S: Source,
+{
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ for source in self {
+ source.visit(visitor)?;
+ }
+
+ Ok(())
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ for source in self {
+ if let Some(found) = source.get(key.clone()) {
+ return Some(found);
+ }
+ }
+
+ None
+ }
+
+ fn count(&self) -> usize {
+ self.iter().map(Source::count).sum()
+ }
+}
+
+impl<const N: usize, S> Source for [S; N]
+where
+ S: Source,
+{
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(self as &[_], visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(self as &[_], key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(self as &[_])
+ }
+}
+
+impl<S> Source for Option<S>
+where
+ S: Source,
+{
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ if let Some(source) = self {
+ source.visit(visitor)?;
+ }
+
+ Ok(())
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ self.as_ref().and_then(|s| s.get(key))
+ }
+
+ fn count(&self) -> usize {
+ self.as_ref().map_or(0, Source::count)
+ }
+}
+
+/// A visitor for the key-value pairs in a [`Source`](trait.Source.html).
+pub trait VisitSource<'kvs> {
+ /// Visit a key-value pair.
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error>;
+}
+
+impl<'a, 'kvs, T> VisitSource<'kvs> for &'a mut T
+where
+ T: VisitSource<'kvs> + ?Sized,
+{
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ (**self).visit_pair(key, value)
+ }
+}
+
+impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugMap<'a, 'b> {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ self.entry(&key, &value);
+ Ok(())
+ }
+}
+
+impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugList<'a, 'b> {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ self.entry(&(key, value));
+ Ok(())
+ }
+}
+
+impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugSet<'a, 'b> {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ self.entry(&(key, value));
+ Ok(())
+ }
+}
+
+impl<'a, 'b: 'a, 'kvs> VisitSource<'kvs> for fmt::DebugTuple<'a, 'b> {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ self.field(&key);
+ self.field(&value);
+ Ok(())
+ }
+}
+
+#[cfg(feature = "std")]
+mod std_support {
+ use super::*;
+ use std::borrow::Borrow;
+ use std::collections::{BTreeMap, HashMap};
+ use std::hash::{BuildHasher, Hash};
+ use std::rc::Rc;
+ use std::sync::Arc;
+
+ impl<S> Source for Box<S>
+ where
+ S: Source + ?Sized,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(&**self, visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(&**self, key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(&**self)
+ }
+ }
+
+ impl<S> Source for Arc<S>
+ where
+ S: Source + ?Sized,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(&**self, visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(&**self, key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(&**self)
+ }
+ }
+
+ impl<S> Source for Rc<S>
+ where
+ S: Source + ?Sized,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(&**self, visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(&**self, key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(&**self)
+ }
+ }
+
+ impl<S> Source for Vec<S>
+ where
+ S: Source,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ Source::visit(&**self, visitor)
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ Source::get(&**self, key)
+ }
+
+ fn count(&self) -> usize {
+ Source::count(&**self)
+ }
+ }
+
+ impl<'kvs, V> VisitSource<'kvs> for Box<V>
+ where
+ V: VisitSource<'kvs> + ?Sized,
+ {
+ fn visit_pair(&mut self, key: Key<'kvs>, value: Value<'kvs>) -> Result<(), Error> {
+ (**self).visit_pair(key, value)
+ }
+ }
+
+ impl<K, V, S> Source for HashMap<K, V, S>
+ where
+ K: ToKey + Borrow<str> + Eq + Hash,
+ V: ToValue,
+ S: BuildHasher,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ for (key, value) in self {
+ visitor.visit_pair(key.to_key(), value.to_value())?;
+ }
+ Ok(())
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ HashMap::get(self, key.as_str()).map(|v| v.to_value())
+ }
+
+ fn count(&self) -> usize {
+ self.len()
+ }
+ }
+
+ impl<K, V> Source for BTreeMap<K, V>
+ where
+ K: ToKey + Borrow<str> + Ord,
+ V: ToValue,
+ {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ for (key, value) in self {
+ visitor.visit_pair(key.to_key(), value.to_value())?;
+ }
+ Ok(())
+ }
+
+ fn get(&self, key: Key) -> Option<Value<'_>> {
+ BTreeMap::get(self, key.as_str()).map(|v| v.to_value())
+ }
+
+ fn count(&self) -> usize {
+ self.len()
+ }
+ }
+
+ #[cfg(test)]
+ mod tests {
+ use crate::kv::value;
+
+ use super::*;
+
+ #[test]
+ fn count() {
+ assert_eq!(1, Source::count(&Box::new(("a", 1))));
+ assert_eq!(2, Source::count(&vec![("a", 1), ("b", 2)]));
+ }
+
+ #[test]
+ fn get() {
+ let source = vec![("a", 1), ("b", 2), ("a", 1)];
+ assert_eq!(
+ value::inner::Token::I64(1),
+ Source::get(&source, Key::from_str("a")).unwrap().to_token()
+ );
+
+ let source = Box::new(None::<(&str, i32)>);
+ assert!(Source::get(&source, Key::from_str("a")).is_none());
+ }
+
+ #[test]
+ fn hash_map() {
+ let mut map = HashMap::new();
+ map.insert("a", 1);
+ map.insert("b", 2);
+
+ assert_eq!(2, Source::count(&map));
+ assert_eq!(
+ value::inner::Token::I64(1),
+ Source::get(&map, Key::from_str("a")).unwrap().to_token()
+ );
+ }
+
+ #[test]
+ fn btree_map() {
+ let mut map = BTreeMap::new();
+ map.insert("a", 1);
+ map.insert("b", 2);
+
+ assert_eq!(2, Source::count(&map));
+ assert_eq!(
+ value::inner::Token::I64(1),
+ Source::get(&map, Key::from_str("a")).unwrap().to_token()
+ );
+ }
+ }
+}
+
+// NOTE: Deprecated; but aliases can't carry this attribute
+#[cfg(feature = "kv_unstable")]
+pub use VisitSource as Visitor;
+
+#[cfg(test)]
+mod tests {
+ use crate::kv::value;
+
+ use super::*;
+
+ #[test]
+ fn source_is_object_safe() {
+ fn _check(_: &dyn Source) {}
+ }
+
+ #[test]
+ fn visitor_is_object_safe() {
+ fn _check(_: &dyn VisitSource) {}
+ }
+
+ #[test]
+ fn count() {
+ struct OnePair {
+ key: &'static str,
+ value: i32,
+ }
+
+ impl Source for OnePair {
+ fn visit<'kvs>(&'kvs self, visitor: &mut dyn VisitSource<'kvs>) -> Result<(), Error> {
+ visitor.visit_pair(self.key.to_key(), self.value.to_value())
+ }
+ }
+
+ assert_eq!(1, Source::count(&("a", 1)));
+ assert_eq!(2, Source::count(&[("a", 1), ("b", 2)] as &[_]));
+ assert_eq!(0, Source::count(&None::<(&str, i32)>));
+ assert_eq!(1, Source::count(&OnePair { key: "a", value: 1 }));
+ }
+
+ #[test]
+ fn get() {
+ let source = &[("a", 1), ("b", 2), ("a", 1)] as &[_];
+ assert_eq!(
+ value::inner::Token::I64(1),
+ Source::get(source, Key::from_str("a")).unwrap().to_token()
+ );
+ assert_eq!(
+ value::inner::Token::I64(2),
+ Source::get(source, Key::from_str("b")).unwrap().to_token()
+ );
+ assert!(Source::get(&source, Key::from_str("c")).is_none());
+
+ let source = None::<(&str, i32)>;
+ assert!(Source::get(&source, Key::from_str("a")).is_none());
+ }
+}
diff --git a/crates/log/src/kv/value.rs b/crates/log/src/kv/value.rs
index 9730f47..1511dd0 100644
--- a/crates/log/src/kv/value.rs
+++ b/crates/log/src/kv/value.rs
@@ -1,1034 +1,1394 @@
-//! Structured values.
-
-use std::fmt;
-
-extern crate value_bag;
-
-#[cfg(feature = "kv_unstable_sval")]
-extern crate sval;
-
-#[cfg(feature = "kv_unstable_serde")]
-extern crate serde;
-
-use self::value_bag::ValueBag;
-
-pub use kv::Error;
-
-/// A type that can be converted into a [`Value`](struct.Value.html).
-pub trait ToValue {
- /// Perform the conversion.
- fn to_value(&self) -> Value;
-}
-
-impl<'a, T> ToValue for &'a T
-where
- T: ToValue + ?Sized,
-{
- fn to_value(&self) -> Value {
- (**self).to_value()
- }
-}
-
-impl<'v> ToValue for Value<'v> {
- fn to_value(&self) -> Value {
- Value {
- inner: self.inner.clone(),
- }
- }
-}
-
-/// Get a value from a type implementing `std::fmt::Debug`.
-#[macro_export]
-macro_rules! as_debug {
- ($capture:expr) => {
- $crate::kv::Value::from_debug(&$capture)
- };
-}
-
-/// Get a value from a type implementing `std::fmt::Display`.
-#[macro_export]
-macro_rules! as_display {
- ($capture:expr) => {
- $crate::kv::Value::from_display(&$capture)
- };
-}
-
-/// Get a value from an error.
-#[cfg(feature = "kv_unstable_std")]
-#[macro_export]
-macro_rules! as_error {
- ($capture:expr) => {
- $crate::kv::Value::from_dyn_error(&$capture)
- };
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-/// Get a value from a type implementing `serde::Serialize`.
-#[macro_export]
-macro_rules! as_serde {
- ($capture:expr) => {
- $crate::kv::Value::from_serde(&$capture)
- };
-}
-
-/// Get a value from a type implementing `sval::value::Value`.
-#[cfg(feature = "kv_unstable_sval")]
-#[macro_export]
-macro_rules! as_sval {
- ($capture:expr) => {
- $crate::kv::Value::from_sval(&$capture)
- };
-}
-
-/// A value in a structured key-value pair.
-///
-/// # Capturing values
-///
-/// There are a few ways to capture a value:
-///
-/// - Using the `Value::capture_*` methods.
-/// - Using the `Value::from_*` methods.
-/// - Using the `ToValue` trait.
-/// - Using the standard `From` trait.
-///
-/// ## Using the `Value::capture_*` methods
-///
-/// `Value` offers a few constructor methods that capture values of different kinds.
-/// These methods require a `T: 'static` to support downcasting.
-///
-/// ```
-/// use log::kv::Value;
-///
-/// let value = Value::capture_debug(&42i32);
-///
-/// assert_eq!(Some(42), value.to_i64());
-/// ```
-///
-/// ## Using the `Value::from_*` methods
-///
-/// `Value` offers a few constructor methods that capture values of different kinds.
-/// These methods don't require `T: 'static`, but can't support downcasting.
-///
-/// ```
-/// use log::kv::Value;
-///
-/// let value = Value::from_debug(&42i32);
-///
-/// assert_eq!(None, value.to_i64());
-/// ```
-///
-/// ## Using the `ToValue` trait
-///
-/// The `ToValue` trait can be used to capture values generically.
-/// It's the bound used by `Source`.
-///
-/// ```
-/// # use log::kv::ToValue;
-/// let value = 42i32.to_value();
-///
-/// assert_eq!(Some(42), value.to_i64());
-/// ```
-///
-/// ```
-/// # use std::fmt::Debug;
-/// use log::kv::ToValue;
-///
-/// let value = (&42i32 as &dyn Debug).to_value();
-///
-/// assert_eq!(None, value.to_i64());
-/// ```
-///
-/// ## Using the standard `From` trait
-///
-/// Standard types that implement `ToValue` also implement `From`.
-///
-/// ```
-/// use log::kv::Value;
-///
-/// let value = Value::from(42i32);
-///
-/// assert_eq!(Some(42), value.to_i64());
-/// ```
-pub struct Value<'v> {
- inner: ValueBag<'v>,
-}
-
-impl<'v> Value<'v> {
- /// Get a value from a type implementing `ToValue`.
- pub fn from_any<T>(value: &'v T) -> Self
- where
- T: ToValue,
- {
- value.to_value()
- }
-
- /// Get a value from a type implementing `std::fmt::Debug`.
- pub fn capture_debug<T>(value: &'v T) -> Self
- where
- T: fmt::Debug + 'static,
- {
- Value {
- inner: ValueBag::capture_debug(value),
- }
- }
-
- /// Get a value from a type implementing `std::fmt::Display`.
- pub fn capture_display<T>(value: &'v T) -> Self
- where
- T: fmt::Display + 'static,
- {
- Value {
- inner: ValueBag::capture_display(value),
- }
- }
-
- /// Get a value from an error.
- #[cfg(feature = "kv_unstable_std")]
- pub fn capture_error<T>(err: &'v T) -> Self
- where
- T: std::error::Error + 'static,
- {
- Value {
- inner: ValueBag::capture_error(err),
- }
- }
-
- #[cfg(feature = "kv_unstable_serde")]
- /// Get a value from a type implementing `serde::Serialize`.
- pub fn capture_serde<T>(value: &'v T) -> Self
- where
- T: self::serde::Serialize + 'static,
- {
- Value {
- inner: ValueBag::capture_serde1(value),
- }
- }
-
- /// Get a value from a type implementing `sval::value::Value`.
- #[cfg(feature = "kv_unstable_sval")]
- pub fn capture_sval<T>(value: &'v T) -> Self
- where
- T: self::sval::value::Value + 'static,
- {
- Value {
- inner: ValueBag::capture_sval1(value),
- }
- }
-
- /// Get a value from a type implementing `std::fmt::Debug`.
- pub fn from_debug<T>(value: &'v T) -> Self
- where
- T: fmt::Debug,
- {
- Value {
- inner: ValueBag::from_debug(value),
- }
- }
-
- /// Get a value from a type implementing `std::fmt::Display`.
- pub fn from_display<T>(value: &'v T) -> Self
- where
- T: fmt::Display,
- {
- Value {
- inner: ValueBag::from_display(value),
- }
- }
-
- /// Get a value from a type implementing `serde::Serialize`.
- #[cfg(feature = "kv_unstable_serde")]
- pub fn from_serde<T>(value: &'v T) -> Self
- where
- T: self::serde::Serialize,
- {
- Value {
- inner: ValueBag::from_serde1(value),
- }
- }
-
- /// Get a value from a type implementing `sval::value::Value`.
- #[cfg(feature = "kv_unstable_sval")]
- pub fn from_sval<T>(value: &'v T) -> Self
- where
- T: self::sval::value::Value,
- {
- Value {
- inner: ValueBag::from_sval1(value),
- }
- }
-
- /// Get a value from a dynamic `std::fmt::Debug`.
- pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
- Value {
- inner: ValueBag::from_dyn_debug(value),
- }
- }
-
- /// Get a value from a dynamic `std::fmt::Display`.
- pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
- Value {
- inner: ValueBag::from_dyn_display(value),
- }
- }
-
- /// Get a value from a dynamic error.
- #[cfg(feature = "kv_unstable_std")]
- pub fn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
- Value {
- inner: ValueBag::from_dyn_error(err),
- }
- }
-
- /// Get a value from a type implementing `sval::value::Value`.
- #[cfg(feature = "kv_unstable_sval")]
- pub fn from_dyn_sval(value: &'v dyn self::sval::value::Value) -> Self {
- Value {
- inner: ValueBag::from_dyn_sval1(value),
- }
- }
-
- /// Get a value from an internal primitive.
- fn from_value_bag<T>(value: T) -> Self
- where
- T: Into<ValueBag<'v>>,
- {
- Value {
- inner: value.into(),
- }
- }
-
- /// Check whether this value can be downcast to `T`.
- pub fn is<T: 'static>(&self) -> bool {
- self.inner.is::<T>()
- }
-
- /// Try downcast this value to `T`.
- pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
- self.inner.downcast_ref::<T>()
- }
-
- /// Inspect this value using a simple visitor.
- pub fn visit(&self, visitor: impl Visit<'v>) -> Result<(), Error> {
- struct Visitor<V>(V);
-
- impl<'v, V> value_bag::visit::Visit<'v> for Visitor<V>
- where
- V: Visit<'v>,
- {
- fn visit_any(&mut self, value: ValueBag) -> Result<(), value_bag::Error> {
- self.0
- .visit_any(Value { inner: value })
- .map_err(Error::into_value)
- }
-
- fn visit_u64(&mut self, value: u64) -> Result<(), value_bag::Error> {
- self.0.visit_u64(value).map_err(Error::into_value)
- }
-
- fn visit_i64(&mut self, value: i64) -> Result<(), value_bag::Error> {
- self.0.visit_i64(value).map_err(Error::into_value)
- }
-
- fn visit_u128(&mut self, value: u128) -> Result<(), value_bag::Error> {
- self.0.visit_u128(value).map_err(Error::into_value)
- }
-
- fn visit_i128(&mut self, value: i128) -> Result<(), value_bag::Error> {
- self.0.visit_i128(value).map_err(Error::into_value)
- }
-
- fn visit_f64(&mut self, value: f64) -> Result<(), value_bag::Error> {
- self.0.visit_f64(value).map_err(Error::into_value)
- }
-
- fn visit_bool(&mut self, value: bool) -> Result<(), value_bag::Error> {
- self.0.visit_bool(value).map_err(Error::into_value)
- }
-
- fn visit_str(&mut self, value: &str) -> Result<(), value_bag::Error> {
- self.0.visit_str(value).map_err(Error::into_value)
- }
-
- fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), value_bag::Error> {
- self.0.visit_borrowed_str(value).map_err(Error::into_value)
- }
-
- fn visit_char(&mut self, value: char) -> Result<(), value_bag::Error> {
- self.0.visit_char(value).map_err(Error::into_value)
- }
-
- #[cfg(feature = "kv_unstable_std")]
- fn visit_error(
- &mut self,
- err: &(dyn std::error::Error + 'static),
- ) -> Result<(), value_bag::Error> {
- self.0.visit_error(err).map_err(Error::into_value)
- }
-
- #[cfg(feature = "kv_unstable_std")]
- fn visit_borrowed_error(
- &mut self,
- err: &'v (dyn std::error::Error + 'static),
- ) -> Result<(), value_bag::Error> {
- self.0.visit_borrowed_error(err).map_err(Error::into_value)
- }
- }
-
- self.inner
- .visit(&mut Visitor(visitor))
- .map_err(Error::from_value)
- }
-}
-
-impl<'v> fmt::Debug for Value<'v> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::Debug::fmt(&self.inner, f)
- }
-}
-
-impl<'v> fmt::Display for Value<'v> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- fmt::Display::fmt(&self.inner, f)
- }
-}
-
-impl ToValue for dyn fmt::Debug {
- fn to_value(&self) -> Value {
- Value::from_dyn_debug(self)
- }
-}
-
-impl ToValue for dyn fmt::Display {
- fn to_value(&self) -> Value {
- Value::from_dyn_display(self)
- }
-}
-
-#[cfg(feature = "kv_unstable_std")]
-impl ToValue for dyn std::error::Error + 'static {
- fn to_value(&self) -> Value {
- Value::from_dyn_error(self)
- }
-}
-
-#[cfg(feature = "kv_unstable_serde")]
-impl<'v> self::serde::Serialize for Value<'v> {
- fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
- where
- S: self::serde::Serializer,
- {
- self.inner.serialize(s)
- }
-}
-
-#[cfg(feature = "kv_unstable_sval")]
-impl<'v> self::sval::value::Value for Value<'v> {
- fn stream(&self, stream: &mut self::sval::value::Stream) -> self::sval::value::Result {
- self::sval::value::Value::stream(&self.inner, stream)
- }
-}
-
-#[cfg(feature = "kv_unstable_sval")]
-impl ToValue for dyn self::sval::value::Value {
- fn to_value(&self) -> Value {
- Value::from_dyn_sval(self)
- }
-}
-
-impl ToValue for str {
- fn to_value(&self) -> Value {
- Value::from(self)
- }
-}
-
-impl ToValue for u128 {
- fn to_value(&self) -> Value {
- Value::from(self)
- }
-}
-
-impl ToValue for i128 {
- fn to_value(&self) -> Value {
- Value::from(self)
- }
-}
-
-impl ToValue for std::num::NonZeroU128 {
- fn to_value(&self) -> Value {
- Value::from(self)
- }
-}
-
-impl ToValue for std::num::NonZeroI128 {
- fn to_value(&self) -> Value {
- Value::from(self)
- }
-}
-
-impl<'v> From<&'v str> for Value<'v> {
- fn from(value: &'v str) -> Self {
- Value::from_value_bag(value)
- }
-}
-
-impl<'v> From<&'v u128> for Value<'v> {
- fn from(value: &'v u128) -> Self {
- Value::from_value_bag(value)
- }
-}
-
-impl<'v> From<&'v i128> for Value<'v> {
- fn from(value: &'v i128) -> Self {
- Value::from_value_bag(value)
- }
-}
-
-impl<'v> From<&'v std::num::NonZeroU128> for Value<'v> {
- fn from(v: &'v std::num::NonZeroU128) -> Value<'v> {
- // SAFETY: `NonZeroU128` and `u128` have the same ABI
- Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroU128, &u128>(v) })
- }
-}
-
-impl<'v> From<&'v std::num::NonZeroI128> for Value<'v> {
- fn from(v: &'v std::num::NonZeroI128) -> Value<'v> {
- // SAFETY: `NonZeroI128` and `i128` have the same ABI
- Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroI128, &i128>(v) })
- }
-}
-
-impl ToValue for () {
- fn to_value(&self) -> Value {
- Value::from_value_bag(())
- }
-}
-
-impl<T> ToValue for Option<T>
-where
- T: ToValue,
-{
- fn to_value(&self) -> Value {
- match *self {
- Some(ref value) => value.to_value(),
- None => Value::from_value_bag(()),
- }
- }
-}
-
-macro_rules! impl_to_value_primitive {
- ($($into_ty:ty,)*) => {
- $(
- impl ToValue for $into_ty {
- fn to_value(&self) -> Value {
- Value::from(*self)
- }
- }
-
- impl<'v> From<$into_ty> for Value<'v> {
- fn from(value: $into_ty) -> Self {
- Value::from_value_bag(value)
- }
- }
- )*
- };
-}
-
-macro_rules! impl_to_value_nonzero_primitive {
- ($($into_ty:ident,)*) => {
- $(
- impl ToValue for std::num::$into_ty {
- fn to_value(&self) -> Value {
- Value::from(self.get())
- }
- }
-
- impl<'v> From<std::num::$into_ty> for Value<'v> {
- fn from(value: std::num::$into_ty) -> Self {
- Value::from(value.get())
- }
- }
- )*
- };
-}
-
-macro_rules! impl_value_to_primitive {
- ($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
- impl<'v> Value<'v> {
- $(
- #[doc = $doc]
- pub fn $into_name(&self) -> Option<$into_ty> {
- self.inner.$into_name()
- }
- )*
- }
- }
-}
-
-impl_to_value_primitive![usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32, f64, char, bool,];
-
-#[rustfmt::skip]
-impl_to_value_nonzero_primitive![
- NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64,
- NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64,
-];
-
-impl_value_to_primitive![
- #[doc = "Try convert this value into a `u64`."]
- to_u64 -> u64,
- #[doc = "Try convert this value into a `i64`."]
- to_i64 -> i64,
- #[doc = "Try convert this value into a `u128`."]
- to_u128 -> u128,
- #[doc = "Try convert this value into a `i128`."]
- to_i128 -> i128,
- #[doc = "Try convert this value into a `f64`."]
- to_f64 -> f64,
- #[doc = "Try convert this value into a `char`."]
- to_char -> char,
- #[doc = "Try convert this value into a `bool`."]
- to_bool -> bool,
-];
-
-impl<'v> Value<'v> {
- /// Try convert this value into an error.
- #[cfg(feature = "kv_unstable_std")]
- pub fn to_borrowed_error(&self) -> Option<&(dyn std::error::Error + 'static)> {
- self.inner.to_borrowed_error()
- }
-
- /// Try convert this value into a borrowed string.
- pub fn to_borrowed_str(&self) -> Option<&str> {
- self.inner.to_borrowed_str()
- }
-}
-
-#[cfg(feature = "kv_unstable_std")]
-mod std_support {
- use super::*;
-
- use std::borrow::Cow;
-
- impl<T> ToValue for Box<T>
- where
- T: ToValue + ?Sized,
- {
- fn to_value(&self) -> Value {
- (**self).to_value()
- }
- }
-
- impl ToValue for String {
- fn to_value(&self) -> Value {
- Value::from(&**self)
- }
- }
-
- impl<'v> ToValue for Cow<'v, str> {
- fn to_value(&self) -> Value {
- Value::from(&**self)
- }
- }
-
- impl<'v> Value<'v> {
- /// Try convert this value into a string.
- pub fn to_str(&self) -> Option<Cow<str>> {
- self.inner.to_str()
- }
- }
-
- impl<'v> From<&'v String> for Value<'v> {
- fn from(v: &'v String) -> Self {
- Value::from(&**v)
- }
- }
-}
-
-/// A visitor for a `Value`.
-pub trait Visit<'v> {
- /// Visit a `Value`.
- ///
- /// This is the only required method on `Visit` and acts as a fallback for any
- /// more specific methods that aren't overridden.
- /// The `Value` may be formatted using its `fmt::Debug` or `fmt::Display` implementation,
- /// or serialized using its `sval::Value` or `serde::Serialize` implementation.
- fn visit_any(&mut self, value: Value) -> Result<(), Error>;
-
- /// Visit an unsigned integer.
- fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
- self.visit_any(value.into())
- }
-
- /// Visit a signed integer.
- fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
- self.visit_any(value.into())
- }
-
- /// Visit a big unsigned integer.
- fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
- self.visit_any((&value).into())
- }
-
- /// Visit a big signed integer.
- fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
- self.visit_any((&value).into())
- }
-
- /// Visit a floating point.
- fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
- self.visit_any(value.into())
- }
-
- /// Visit a boolean.
- fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
- self.visit_any(value.into())
- }
-
- /// Visit a string.
- fn visit_str(&mut self, value: &str) -> Result<(), Error> {
- self.visit_any(value.into())
- }
-
- /// Visit a string.
- fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
- self.visit_str(value)
- }
-
- /// Visit a Unicode character.
- fn visit_char(&mut self, value: char) -> Result<(), Error> {
- let mut b = [0; 4];
- self.visit_str(&*value.encode_utf8(&mut b))
- }
-
- /// Visit an error.
- #[cfg(feature = "kv_unstable_std")]
- fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
- self.visit_any(Value::from_dyn_error(err))
- }
-
- /// Visit an error.
- #[cfg(feature = "kv_unstable_std")]
- fn visit_borrowed_error(
- &mut self,
- err: &'v (dyn std::error::Error + 'static),
- ) -> Result<(), Error> {
- self.visit_any(Value::from_dyn_error(err))
- }
-}
-
-impl<'a, 'v, T: ?Sized> Visit<'v> for &'a mut T
-where
- T: Visit<'v>,
-{
- fn visit_any(&mut self, value: Value) -> Result<(), Error> {
- (**self).visit_any(value)
- }
-
- fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
- (**self).visit_u64(value)
- }
-
- fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
- (**self).visit_i64(value)
- }
-
- fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
- (**self).visit_u128(value)
- }
-
- fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
- (**self).visit_i128(value)
- }
-
- fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
- (**self).visit_f64(value)
- }
-
- fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
- (**self).visit_bool(value)
- }
-
- fn visit_str(&mut self, value: &str) -> Result<(), Error> {
- (**self).visit_str(value)
- }
-
- fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
- (**self).visit_borrowed_str(value)
- }
-
- fn visit_char(&mut self, value: char) -> Result<(), Error> {
- (**self).visit_char(value)
- }
-
- #[cfg(feature = "kv_unstable_std")]
- fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
- (**self).visit_error(err)
- }
-
- #[cfg(feature = "kv_unstable_std")]
- fn visit_borrowed_error(
- &mut self,
- err: &'v (dyn std::error::Error + 'static),
- ) -> Result<(), Error> {
- (**self).visit_borrowed_error(err)
- }
-}
-
-#[cfg(test)]
-pub(crate) mod tests {
- use super::*;
-
- pub(crate) use super::value_bag::test::Token;
-
- impl<'v> Value<'v> {
- pub(crate) fn to_token(&self) -> Token {
- self.inner.to_token()
- }
- }
-
- fn unsigned() -> impl Iterator<Item = Value<'static>> {
- vec![
- Value::from(8u8),
- Value::from(16u16),
- Value::from(32u32),
- Value::from(64u64),
- Value::from(1usize),
- Value::from(std::num::NonZeroU8::new(8).unwrap()),
- Value::from(std::num::NonZeroU16::new(16).unwrap()),
- Value::from(std::num::NonZeroU32::new(32).unwrap()),
- Value::from(std::num::NonZeroU64::new(64).unwrap()),
- Value::from(std::num::NonZeroUsize::new(1).unwrap()),
- ]
- .into_iter()
- }
-
- fn signed() -> impl Iterator<Item = Value<'static>> {
- vec![
- Value::from(-8i8),
- Value::from(-16i16),
- Value::from(-32i32),
- Value::from(-64i64),
- Value::from(-1isize),
- Value::from(std::num::NonZeroI8::new(-8).unwrap()),
- Value::from(std::num::NonZeroI16::new(-16).unwrap()),
- Value::from(std::num::NonZeroI32::new(-32).unwrap()),
- Value::from(std::num::NonZeroI64::new(-64).unwrap()),
- Value::from(std::num::NonZeroIsize::new(-1).unwrap()),
- ]
- .into_iter()
- }
-
- fn float() -> impl Iterator<Item = Value<'static>> {
- vec![Value::from(32.32f32), Value::from(64.64f64)].into_iter()
- }
-
- fn bool() -> impl Iterator<Item = Value<'static>> {
- vec![Value::from(true), Value::from(false)].into_iter()
- }
-
- fn str() -> impl Iterator<Item = Value<'static>> {
- vec![Value::from("a string"), Value::from("a loong string")].into_iter()
- }
-
- fn char() -> impl Iterator<Item = Value<'static>> {
- vec![Value::from('a'), Value::from('â›°')].into_iter()
- }
-
- #[test]
- fn test_capture_fmt() {
- assert_eq!(Some(42u64), Value::capture_display(&42).to_u64());
- assert_eq!(Some(42u64), Value::capture_debug(&42).to_u64());
-
- assert!(Value::from_display(&42).to_u64().is_none());
- assert!(Value::from_debug(&42).to_u64().is_none());
- }
-
- #[cfg(feature = "kv_unstable_std")]
- #[test]
- fn test_capture_error() {
- let err = std::io::Error::from(std::io::ErrorKind::Other);
-
- assert!(Value::capture_error(&err).to_borrowed_error().is_some());
- assert!(Value::from_dyn_error(&err).to_borrowed_error().is_some());
- }
-
- #[cfg(feature = "kv_unstable_serde")]
- #[test]
- fn test_capture_serde() {
- assert_eq!(Some(42u64), Value::capture_serde(&42).to_u64());
-
- assert_eq!(Some(42u64), Value::from_serde(&42).to_u64());
- }
-
- #[cfg(feature = "kv_unstable_sval")]
- #[test]
- fn test_capture_sval() {
- assert_eq!(Some(42u64), Value::capture_sval(&42).to_u64());
-
- assert_eq!(Some(42u64), Value::from_sval(&42).to_u64());
- }
-
- #[test]
- fn test_to_value_display() {
- assert_eq!(42u64.to_value().to_string(), "42");
- assert_eq!(42i64.to_value().to_string(), "42");
- assert_eq!(42.01f64.to_value().to_string(), "42.01");
- assert_eq!(true.to_value().to_string(), "true");
- assert_eq!('a'.to_value().to_string(), "a");
- assert_eq!("a loong string".to_value().to_string(), "a loong string");
- assert_eq!(Some(true).to_value().to_string(), "true");
- assert_eq!(().to_value().to_string(), "None");
- assert_eq!(Option::None::<bool>.to_value().to_string(), "None");
- }
-
- #[test]
- fn test_to_value_structured() {
- assert_eq!(42u64.to_value().to_token(), Token::U64(42));
- assert_eq!(42i64.to_value().to_token(), Token::I64(42));
- assert_eq!(42.01f64.to_value().to_token(), Token::F64(42.01));
- assert_eq!(true.to_value().to_token(), Token::Bool(true));
- assert_eq!('a'.to_value().to_token(), Token::Char('a'));
- assert_eq!(
- "a loong string".to_value().to_token(),
- Token::Str("a loong string".into())
- );
- assert_eq!(Some(true).to_value().to_token(), Token::Bool(true));
- assert_eq!(().to_value().to_token(), Token::None);
- assert_eq!(Option::None::<bool>.to_value().to_token(), Token::None);
- }
-
- #[test]
- fn test_to_number() {
- for v in unsigned() {
- assert!(v.to_u64().is_some());
- assert!(v.to_i64().is_some());
- }
-
- for v in signed() {
- assert!(v.to_i64().is_some());
- }
-
- for v in unsigned().chain(signed()).chain(float()) {
- assert!(v.to_f64().is_some());
- }
-
- for v in bool().chain(str()).chain(char()) {
- assert!(v.to_u64().is_none());
- assert!(v.to_i64().is_none());
- assert!(v.to_f64().is_none());
- }
- }
-
- #[test]
- fn test_to_str() {
- for v in str() {
- assert!(v.to_borrowed_str().is_some());
-
- #[cfg(feature = "kv_unstable_std")]
- assert!(v.to_str().is_some());
- }
-
- let short_lived = String::from("short lived");
- let v = Value::from(&*short_lived);
-
- assert!(v.to_borrowed_str().is_some());
-
- #[cfg(feature = "kv_unstable_std")]
- assert!(v.to_str().is_some());
-
- for v in unsigned().chain(signed()).chain(float()).chain(bool()) {
- assert!(v.to_borrowed_str().is_none());
-
- #[cfg(feature = "kv_unstable_std")]
- assert!(v.to_str().is_none());
- }
- }
-
- #[test]
- fn test_to_bool() {
- for v in bool() {
- assert!(v.to_bool().is_some());
- }
-
- for v in unsigned()
- .chain(signed())
- .chain(float())
- .chain(str())
- .chain(char())
- {
- assert!(v.to_bool().is_none());
- }
- }
-
- #[test]
- fn test_to_char() {
- for v in char() {
- assert!(v.to_char().is_some());
- }
-
- for v in unsigned()
- .chain(signed())
- .chain(float())
- .chain(str())
- .chain(bool())
- {
- assert!(v.to_char().is_none());
- }
- }
-
- #[test]
- fn test_downcast_ref() {
- #[derive(Debug)]
- struct Foo(u64);
-
- let v = Value::capture_debug(&Foo(42));
-
- assert!(v.is::<Foo>());
- assert_eq!(42u64, v.downcast_ref::<Foo>().expect("invalid downcast").0);
- }
-
- #[test]
- fn test_visit_integer() {
- struct Extract(Option<u64>);
-
- impl<'v> Visit<'v> for Extract {
- fn visit_any(&mut self, value: Value) -> Result<(), Error> {
- unimplemented!("unexpected value: {:?}", value)
- }
-
- fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
- self.0 = Some(value);
-
- Ok(())
- }
- }
-
- let mut extract = Extract(None);
- Value::from(42u64).visit(&mut extract).unwrap();
-
- assert_eq!(Some(42), extract.0);
- }
-
- #[test]
- fn test_visit_borrowed_str() {
- struct Extract<'v>(Option<&'v str>);
-
- impl<'v> Visit<'v> for Extract<'v> {
- fn visit_any(&mut self, value: Value) -> Result<(), Error> {
- unimplemented!("unexpected value: {:?}", value)
- }
-
- fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
- self.0 = Some(value);
-
- Ok(())
- }
- }
-
- let mut extract = Extract(None);
-
- let short_lived = String::from("A short-lived string");
- Value::from(&*short_lived).visit(&mut extract).unwrap();
-
- assert_eq!(Some("A short-lived string"), extract.0);
- }
-}
+//! Structured values.
+//!
+//! This module defines the [`Value`] type and supporting APIs for
+//! capturing and serializing them.
+
+use std::fmt;
+
+pub use crate::kv::Error;
+
+/// A type that can be converted into a [`Value`](struct.Value.html).
+pub trait ToValue {
+ /// Perform the conversion.
+ fn to_value(&self) -> Value;
+}
+
+impl<'a, T> ToValue for &'a T
+where
+ T: ToValue + ?Sized,
+{
+ fn to_value(&self) -> Value {
+ (**self).to_value()
+ }
+}
+
+impl<'v> ToValue for Value<'v> {
+ fn to_value(&self) -> Value {
+ Value {
+ inner: self.inner.clone(),
+ }
+ }
+}
+
+/// A value in a key-value.
+///
+/// Values are an anonymous bag containing some structured datum.
+///
+/// # Capturing values
+///
+/// There are a few ways to capture a value:
+///
+/// - Using the `Value::from_*` methods.
+/// - Using the `ToValue` trait.
+/// - Using the standard `From` trait.
+///
+/// ## Using the `Value::from_*` methods
+///
+/// `Value` offers a few constructor methods that capture values of different kinds.
+///
+/// ```
+/// use log::kv::Value;
+///
+/// let value = Value::from_debug(&42i32);
+///
+/// assert_eq!(None, value.to_i64());
+/// ```
+///
+/// ## Using the `ToValue` trait
+///
+/// The `ToValue` trait can be used to capture values generically.
+/// It's the bound used by `Source`.
+///
+/// ```
+/// # use log::kv::ToValue;
+/// let value = 42i32.to_value();
+///
+/// assert_eq!(Some(42), value.to_i64());
+/// ```
+///
+/// ## Using the standard `From` trait
+///
+/// Standard types that implement `ToValue` also implement `From`.
+///
+/// ```
+/// use log::kv::Value;
+///
+/// let value = Value::from(42i32);
+///
+/// assert_eq!(Some(42), value.to_i64());
+/// ```
+///
+/// # Data model
+///
+/// Values can hold one of a number of types:
+///
+/// - **Null:** The absence of any other meaningful value. Note that
+/// `Some(Value::null())` is not the same as `None`. The former is
+/// `null` while the latter is `undefined`. This is important to be
+/// able to tell the difference between a key-value that was logged,
+/// but its value was empty (`Some(Value::null())`) and a key-value
+/// that was never logged at all (`None`).
+/// - **Strings:** `str`, `char`.
+/// - **Booleans:** `bool`.
+/// - **Integers:** `u8`-`u128`, `i8`-`i128`, `NonZero*`.
+/// - **Floating point numbers:** `f32`-`f64`.
+/// - **Errors:** `dyn (Error + 'static)`.
+/// - **`serde`:** Any type in `serde`'s data model.
+/// - **`sval`:** Any type in `sval`'s data model.
+///
+/// # Serialization
+///
+/// Values provide a number of ways to be serialized.
+///
+/// For basic types the [`Value::visit`] method can be used to extract the
+/// underlying typed value. However this is limited in the amount of types
+/// supported (see the [`VisitValue`] trait methods).
+///
+/// For more complex types one of the following traits can be used:
+/// * `sval::Value`, requires the `kv_sval` feature.
+/// * `serde::Serialize`, requires the `kv_serde` feature.
+///
+/// You don't need a visitor to serialize values through `serde` or `sval`.
+///
+/// A value can always be serialized using any supported framework, regardless
+/// of how it was captured. If, for example, a value was captured using its
+/// `Display` implementation, it will serialize through `serde` as a string. If it was
+/// captured as a struct using `serde`, it will also serialize as a struct
+/// through `sval`, or can be formatted using a `Debug`-compatible representation.
+pub struct Value<'v> {
+ inner: inner::Inner<'v>,
+}
+
+impl<'v> Value<'v> {
+ /// Get a value from a type implementing `ToValue`.
+ pub fn from_any<T>(value: &'v T) -> Self
+ where
+ T: ToValue,
+ {
+ value.to_value()
+ }
+
+ /// Get a value from a type implementing `std::fmt::Debug`.
+ pub fn from_debug<T>(value: &'v T) -> Self
+ where
+ T: fmt::Debug,
+ {
+ Value {
+ inner: inner::Inner::from_debug(value),
+ }
+ }
+
+ /// Get a value from a type implementing `std::fmt::Display`.
+ pub fn from_display<T>(value: &'v T) -> Self
+ where
+ T: fmt::Display,
+ {
+ Value {
+ inner: inner::Inner::from_display(value),
+ }
+ }
+
+ /// Get a value from a type implementing `serde::Serialize`.
+ #[cfg(feature = "kv_serde")]
+ pub fn from_serde<T>(value: &'v T) -> Self
+ where
+ T: serde::Serialize,
+ {
+ Value {
+ inner: inner::Inner::from_serde1(value),
+ }
+ }
+
+ /// Get a value from a type implementing `sval::Value`.
+ #[cfg(feature = "kv_sval")]
+ pub fn from_sval<T>(value: &'v T) -> Self
+ where
+ T: sval::Value,
+ {
+ Value {
+ inner: inner::Inner::from_sval2(value),
+ }
+ }
+
+ /// Get a value from a dynamic `std::fmt::Debug`.
+ pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
+ Value {
+ inner: inner::Inner::from_dyn_debug(value),
+ }
+ }
+
+ /// Get a value from a dynamic `std::fmt::Display`.
+ pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
+ Value {
+ inner: inner::Inner::from_dyn_display(value),
+ }
+ }
+
+ /// Get a value from a dynamic error.
+ #[cfg(feature = "kv_std")]
+ pub fn from_dyn_error(err: &'v (dyn std::error::Error + 'static)) -> Self {
+ Value {
+ inner: inner::Inner::from_dyn_error(err),
+ }
+ }
+
+ /// Get a `null` value.
+ pub fn null() -> Self {
+ Value {
+ inner: inner::Inner::empty(),
+ }
+ }
+
+ /// Get a value from an internal primitive.
+ fn from_inner<T>(value: T) -> Self
+ where
+ T: Into<inner::Inner<'v>>,
+ {
+ Value {
+ inner: value.into(),
+ }
+ }
+
+ /// Inspect this value using a simple visitor.
+ ///
+ /// When the `kv_serde` or `kv_sval` features are enabled, you can also
+ /// serialize a value using its `Serialize` or `Value` implementation.
+ pub fn visit(&self, visitor: impl VisitValue<'v>) -> Result<(), Error> {
+ inner::visit(&self.inner, visitor)
+ }
+}
+
+impl<'v> fmt::Debug for Value<'v> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(&self.inner, f)
+ }
+}
+
+impl<'v> fmt::Display for Value<'v> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(&self.inner, f)
+ }
+}
+
+#[cfg(feature = "kv_serde")]
+impl<'v> serde::Serialize for Value<'v> {
+ fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ self.inner.serialize(s)
+ }
+}
+
+#[cfg(feature = "kv_sval")]
+impl<'v> sval::Value for Value<'v> {
+ fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> sval::Result {
+ sval::Value::stream(&self.inner, stream)
+ }
+}
+
+#[cfg(feature = "kv_sval")]
+impl<'v> sval_ref::ValueRef<'v> for Value<'v> {
+ fn stream_ref<S: sval::Stream<'v> + ?Sized>(&self, stream: &mut S) -> sval::Result {
+ sval_ref::ValueRef::stream_ref(&self.inner, stream)
+ }
+}
+
+impl ToValue for str {
+ fn to_value(&self) -> Value {
+ Value::from(self)
+ }
+}
+
+impl<'v> From<&'v str> for Value<'v> {
+ fn from(value: &'v str) -> Self {
+ Value::from_inner(value)
+ }
+}
+
+impl ToValue for () {
+ fn to_value(&self) -> Value {
+ Value::from_inner(())
+ }
+}
+
+impl<T> ToValue for Option<T>
+where
+ T: ToValue,
+{
+ fn to_value(&self) -> Value {
+ match *self {
+ Some(ref value) => value.to_value(),
+ None => Value::from_inner(()),
+ }
+ }
+}
+
+macro_rules! impl_to_value_primitive {
+ ($($into_ty:ty,)*) => {
+ $(
+ impl ToValue for $into_ty {
+ fn to_value(&self) -> Value {
+ Value::from(*self)
+ }
+ }
+
+ impl<'v> From<$into_ty> for Value<'v> {
+ fn from(value: $into_ty) -> Self {
+ Value::from_inner(value)
+ }
+ }
+
+ impl<'v> From<&'v $into_ty> for Value<'v> {
+ fn from(value: &'v $into_ty) -> Self {
+ Value::from_inner(*value)
+ }
+ }
+ )*
+ };
+}
+
+macro_rules! impl_to_value_nonzero_primitive {
+ ($($into_ty:ident,)*) => {
+ $(
+ impl ToValue for std::num::$into_ty {
+ fn to_value(&self) -> Value {
+ Value::from(self.get())
+ }
+ }
+
+ impl<'v> From<std::num::$into_ty> for Value<'v> {
+ fn from(value: std::num::$into_ty) -> Self {
+ Value::from(value.get())
+ }
+ }
+
+ impl<'v> From<&'v std::num::$into_ty> for Value<'v> {
+ fn from(value: &'v std::num::$into_ty) -> Self {
+ Value::from(value.get())
+ }
+ }
+ )*
+ };
+}
+
+macro_rules! impl_value_to_primitive {
+ ($(#[doc = $doc:tt] $into_name:ident -> $into_ty:ty,)*) => {
+ impl<'v> Value<'v> {
+ $(
+ #[doc = $doc]
+ pub fn $into_name(&self) -> Option<$into_ty> {
+ self.inner.$into_name()
+ }
+ )*
+ }
+ }
+}
+
+impl_to_value_primitive![
+ usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
+];
+
+#[rustfmt::skip]
+impl_to_value_nonzero_primitive![
+ NonZeroUsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
+ NonZeroIsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
+];
+
+impl_value_to_primitive![
+ #[doc = "Try convert this value into a `u64`."]
+ to_u64 -> u64,
+ #[doc = "Try convert this value into a `i64`."]
+ to_i64 -> i64,
+ #[doc = "Try convert this value into a `u128`."]
+ to_u128 -> u128,
+ #[doc = "Try convert this value into a `i128`."]
+ to_i128 -> i128,
+ #[doc = "Try convert this value into a `f64`."]
+ to_f64 -> f64,
+ #[doc = "Try convert this value into a `char`."]
+ to_char -> char,
+ #[doc = "Try convert this value into a `bool`."]
+ to_bool -> bool,
+];
+
+impl<'v> Value<'v> {
+ /// Try convert this value into an error.
+ #[cfg(feature = "kv_std")]
+ pub fn to_borrowed_error(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ self.inner.to_borrowed_error()
+ }
+
+ /// Try convert this value into a borrowed string.
+ pub fn to_borrowed_str(&self) -> Option<&str> {
+ self.inner.to_borrowed_str()
+ }
+}
+
+#[cfg(feature = "kv_std")]
+mod std_support {
+ use std::borrow::Cow;
+ use std::rc::Rc;
+ use std::sync::Arc;
+
+ use super::*;
+
+ impl<T> ToValue for Box<T>
+ where
+ T: ToValue + ?Sized,
+ {
+ fn to_value(&self) -> Value {
+ (**self).to_value()
+ }
+ }
+
+ impl<T> ToValue for Arc<T>
+ where
+ T: ToValue + ?Sized,
+ {
+ fn to_value(&self) -> Value {
+ (**self).to_value()
+ }
+ }
+
+ impl<T> ToValue for Rc<T>
+ where
+ T: ToValue + ?Sized,
+ {
+ fn to_value(&self) -> Value {
+ (**self).to_value()
+ }
+ }
+
+ impl ToValue for String {
+ fn to_value(&self) -> Value {
+ Value::from(&**self)
+ }
+ }
+
+ impl<'v> ToValue for Cow<'v, str> {
+ fn to_value(&self) -> Value {
+ Value::from(&**self)
+ }
+ }
+
+ impl<'v> Value<'v> {
+ /// Try convert this value into a string.
+ pub fn to_cow_str(&self) -> Option<Cow<'v, str>> {
+ self.inner.to_str()
+ }
+ }
+
+ impl<'v> From<&'v String> for Value<'v> {
+ fn from(v: &'v String) -> Self {
+ Value::from(&**v)
+ }
+ }
+}
+
+/// A visitor for a [`Value`].
+///
+/// Also see [`Value`'s documentation on seralization]. Value visitors are a simple alternative
+/// to a more fully-featured serialization framework like `serde` or `sval`. A value visitor
+/// can differentiate primitive types through methods like [`VisitValue::visit_bool`] and
+/// [`VisitValue::visit_str`], but more complex types like maps and sequences
+/// will fallthrough to [`VisitValue::visit_any`].
+///
+/// If you're trying to serialize a value to a format like JSON, you can use either `serde`
+/// or `sval` directly with the value. You don't need a visitor.
+///
+/// [`Value`'s documentation on seralization]: Value#serialization
+pub trait VisitValue<'v> {
+ /// Visit a `Value`.
+ ///
+ /// This is the only required method on `VisitValue` and acts as a fallback for any
+ /// more specific methods that aren't overridden.
+ /// The `Value` may be formatted using its `fmt::Debug` or `fmt::Display` implementation,
+ /// or serialized using its `sval::Value` or `serde::Serialize` implementation.
+ fn visit_any(&mut self, value: Value) -> Result<(), Error>;
+
+ /// Visit an empty value.
+ fn visit_null(&mut self) -> Result<(), Error> {
+ self.visit_any(Value::null())
+ }
+
+ /// Visit an unsigned integer.
+ fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
+ self.visit_any(value.into())
+ }
+
+ /// Visit a signed integer.
+ fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
+ self.visit_any(value.into())
+ }
+
+ /// Visit a big unsigned integer.
+ fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
+ self.visit_any((value).into())
+ }
+
+ /// Visit a big signed integer.
+ fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
+ self.visit_any((value).into())
+ }
+
+ /// Visit a floating point.
+ fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
+ self.visit_any(value.into())
+ }
+
+ /// Visit a boolean.
+ fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
+ self.visit_any(value.into())
+ }
+
+ /// Visit a string.
+ fn visit_str(&mut self, value: &str) -> Result<(), Error> {
+ self.visit_any(value.into())
+ }
+
+ /// Visit a string.
+ fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
+ self.visit_str(value)
+ }
+
+ /// Visit a Unicode character.
+ fn visit_char(&mut self, value: char) -> Result<(), Error> {
+ let mut b = [0; 4];
+ self.visit_str(&*value.encode_utf8(&mut b))
+ }
+
+ /// Visit an error.
+ #[cfg(feature = "kv_std")]
+ fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
+ self.visit_any(Value::from_dyn_error(err))
+ }
+
+ /// Visit an error.
+ #[cfg(feature = "kv_std")]
+ fn visit_borrowed_error(
+ &mut self,
+ err: &'v (dyn std::error::Error + 'static),
+ ) -> Result<(), Error> {
+ self.visit_any(Value::from_dyn_error(err))
+ }
+}
+
+impl<'a, 'v, T: ?Sized> VisitValue<'v> for &'a mut T
+where
+ T: VisitValue<'v>,
+{
+ fn visit_any(&mut self, value: Value) -> Result<(), Error> {
+ (**self).visit_any(value)
+ }
+
+ fn visit_null(&mut self) -> Result<(), Error> {
+ (**self).visit_null()
+ }
+
+ fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
+ (**self).visit_u64(value)
+ }
+
+ fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
+ (**self).visit_i64(value)
+ }
+
+ fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
+ (**self).visit_u128(value)
+ }
+
+ fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
+ (**self).visit_i128(value)
+ }
+
+ fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
+ (**self).visit_f64(value)
+ }
+
+ fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
+ (**self).visit_bool(value)
+ }
+
+ fn visit_str(&mut self, value: &str) -> Result<(), Error> {
+ (**self).visit_str(value)
+ }
+
+ fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
+ (**self).visit_borrowed_str(value)
+ }
+
+ fn visit_char(&mut self, value: char) -> Result<(), Error> {
+ (**self).visit_char(value)
+ }
+
+ #[cfg(feature = "kv_std")]
+ fn visit_error(&mut self, err: &(dyn std::error::Error + 'static)) -> Result<(), Error> {
+ (**self).visit_error(err)
+ }
+
+ #[cfg(feature = "kv_std")]
+ fn visit_borrowed_error(
+ &mut self,
+ err: &'v (dyn std::error::Error + 'static),
+ ) -> Result<(), Error> {
+ (**self).visit_borrowed_error(err)
+ }
+}
+
+#[cfg(feature = "value-bag")]
+pub(in crate::kv) mod inner {
+ /**
+ An implementation of `Value` based on a library called `value_bag`.
+
+ `value_bag` was written specifically for use in `log`'s value, but was split out when it outgrew
+ the codebase here. It's a general-purpose type-erasure library that handles mapping between
+ more fully-featured serialization frameworks.
+ */
+ use super::*;
+
+ pub use value_bag::ValueBag as Inner;
+
+ pub use value_bag::Error;
+
+ #[cfg(test)]
+ pub use value_bag::test::TestToken as Token;
+
+ pub fn visit<'v>(
+ inner: &Inner<'v>,
+ visitor: impl VisitValue<'v>,
+ ) -> Result<(), crate::kv::Error> {
+ struct InnerVisitValue<V>(V);
+
+ impl<'v, V> value_bag::visit::Visit<'v> for InnerVisitValue<V>
+ where
+ V: VisitValue<'v>,
+ {
+ fn visit_any(&mut self, value: value_bag::ValueBag) -> Result<(), Error> {
+ self.0
+ .visit_any(Value { inner: value })
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_empty(&mut self) -> Result<(), Error> {
+ self.0.visit_null().map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
+ self.0
+ .visit_u64(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_i64(&mut self, value: i64) -> Result<(), Error> {
+ self.0
+ .visit_i64(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_u128(&mut self, value: u128) -> Result<(), Error> {
+ self.0
+ .visit_u128(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_i128(&mut self, value: i128) -> Result<(), Error> {
+ self.0
+ .visit_i128(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_f64(&mut self, value: f64) -> Result<(), Error> {
+ self.0
+ .visit_f64(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_bool(&mut self, value: bool) -> Result<(), Error> {
+ self.0
+ .visit_bool(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_str(&mut self, value: &str) -> Result<(), Error> {
+ self.0
+ .visit_str(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
+ self.0
+ .visit_borrowed_str(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ fn visit_char(&mut self, value: char) -> Result<(), Error> {
+ self.0
+ .visit_char(value)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ #[cfg(feature = "kv_std")]
+ fn visit_error(
+ &mut self,
+ err: &(dyn std::error::Error + 'static),
+ ) -> Result<(), Error> {
+ self.0
+ .visit_error(err)
+ .map_err(crate::kv::Error::into_value)
+ }
+
+ #[cfg(feature = "kv_std")]
+ fn visit_borrowed_error(
+ &mut self,
+ err: &'v (dyn std::error::Error + 'static),
+ ) -> Result<(), Error> {
+ self.0
+ .visit_borrowed_error(err)
+ .map_err(crate::kv::Error::into_value)
+ }
+ }
+
+ inner
+ .visit(&mut InnerVisitValue(visitor))
+ .map_err(crate::kv::Error::from_value)
+ }
+}
+
+#[cfg(not(feature = "value-bag"))]
+pub(in crate::kv) mod inner {
+ /**
+ This is a dependency-free implementation of `Value` when there's no serialization frameworks involved.
+ In these simple cases a more fully featured solution like `value_bag` isn't needed, so we avoid pulling it in.
+
+ There are a few things here that need to remain consistent with the `value_bag`-based implementation:
+
+ 1. Conversions should always produce the same results. If a conversion here returns `Some`, then
+ the same `value_bag`-based conversion must also. Of particular note here are floats to ints; they're
+ based on the standard library's `TryInto` conversions, which need to be convert to `i32` or `u32`,
+ and then to `f64`.
+ 2. VisitValues should always be called in the same way. If a particular type of value calls `visit_i64`,
+ then the same `value_bag`-based visitor must also.
+ */
+ use super::*;
+
+ #[derive(Clone)]
+ pub enum Inner<'v> {
+ None,
+ Bool(bool),
+ Str(&'v str),
+ Char(char),
+ I64(i64),
+ U64(u64),
+ F64(f64),
+ I128(i128),
+ U128(u128),
+ Debug(&'v dyn fmt::Debug),
+ Display(&'v dyn fmt::Display),
+ }
+
+ impl<'v> From<()> for Inner<'v> {
+ fn from(_: ()) -> Self {
+ Inner::None
+ }
+ }
+
+ impl<'v> From<bool> for Inner<'v> {
+ fn from(v: bool) -> Self {
+ Inner::Bool(v)
+ }
+ }
+
+ impl<'v> From<char> for Inner<'v> {
+ fn from(v: char) -> Self {
+ Inner::Char(v)
+ }
+ }
+
+ impl<'v> From<f32> for Inner<'v> {
+ fn from(v: f32) -> Self {
+ Inner::F64(v as f64)
+ }
+ }
+
+ impl<'v> From<f64> for Inner<'v> {
+ fn from(v: f64) -> Self {
+ Inner::F64(v)
+ }
+ }
+
+ impl<'v> From<i8> for Inner<'v> {
+ fn from(v: i8) -> Self {
+ Inner::I64(v as i64)
+ }
+ }
+
+ impl<'v> From<i16> for Inner<'v> {
+ fn from(v: i16) -> Self {
+ Inner::I64(v as i64)
+ }
+ }
+
+ impl<'v> From<i32> for Inner<'v> {
+ fn from(v: i32) -> Self {
+ Inner::I64(v as i64)
+ }
+ }
+
+ impl<'v> From<i64> for Inner<'v> {
+ fn from(v: i64) -> Self {
+ Inner::I64(v as i64)
+ }
+ }
+
+ impl<'v> From<isize> for Inner<'v> {
+ fn from(v: isize) -> Self {
+ Inner::I64(v as i64)
+ }
+ }
+
+ impl<'v> From<u8> for Inner<'v> {
+ fn from(v: u8) -> Self {
+ Inner::U64(v as u64)
+ }
+ }
+
+ impl<'v> From<u16> for Inner<'v> {
+ fn from(v: u16) -> Self {
+ Inner::U64(v as u64)
+ }
+ }
+
+ impl<'v> From<u32> for Inner<'v> {
+ fn from(v: u32) -> Self {
+ Inner::U64(v as u64)
+ }
+ }
+
+ impl<'v> From<u64> for Inner<'v> {
+ fn from(v: u64) -> Self {
+ Inner::U64(v as u64)
+ }
+ }
+
+ impl<'v> From<usize> for Inner<'v> {
+ fn from(v: usize) -> Self {
+ Inner::U64(v as u64)
+ }
+ }
+
+ impl<'v> From<i128> for Inner<'v> {
+ fn from(v: i128) -> Self {
+ Inner::I128(v)
+ }
+ }
+
+ impl<'v> From<u128> for Inner<'v> {
+ fn from(v: u128) -> Self {
+ Inner::U128(v)
+ }
+ }
+
+ impl<'v> From<&'v str> for Inner<'v> {
+ fn from(v: &'v str) -> Self {
+ Inner::Str(v)
+ }
+ }
+
+ impl<'v> fmt::Debug for Inner<'v> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Inner::None => fmt::Debug::fmt(&None::<()>, f),
+ Inner::Bool(v) => fmt::Debug::fmt(v, f),
+ Inner::Str(v) => fmt::Debug::fmt(v, f),
+ Inner::Char(v) => fmt::Debug::fmt(v, f),
+ Inner::I64(v) => fmt::Debug::fmt(v, f),
+ Inner::U64(v) => fmt::Debug::fmt(v, f),
+ Inner::F64(v) => fmt::Debug::fmt(v, f),
+ Inner::I128(v) => fmt::Debug::fmt(v, f),
+ Inner::U128(v) => fmt::Debug::fmt(v, f),
+ Inner::Debug(v) => fmt::Debug::fmt(v, f),
+ Inner::Display(v) => fmt::Display::fmt(v, f),
+ }
+ }
+ }
+
+ impl<'v> fmt::Display for Inner<'v> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Inner::None => fmt::Debug::fmt(&None::<()>, f),
+ Inner::Bool(v) => fmt::Display::fmt(v, f),
+ Inner::Str(v) => fmt::Display::fmt(v, f),
+ Inner::Char(v) => fmt::Display::fmt(v, f),
+ Inner::I64(v) => fmt::Display::fmt(v, f),
+ Inner::U64(v) => fmt::Display::fmt(v, f),
+ Inner::F64(v) => fmt::Display::fmt(v, f),
+ Inner::I128(v) => fmt::Display::fmt(v, f),
+ Inner::U128(v) => fmt::Display::fmt(v, f),
+ Inner::Debug(v) => fmt::Debug::fmt(v, f),
+ Inner::Display(v) => fmt::Display::fmt(v, f),
+ }
+ }
+ }
+
+ impl<'v> Inner<'v> {
+ pub fn from_debug<T: fmt::Debug>(value: &'v T) -> Self {
+ Inner::Debug(value)
+ }
+
+ pub fn from_display<T: fmt::Display>(value: &'v T) -> Self {
+ Inner::Display(value)
+ }
+
+ pub fn from_dyn_debug(value: &'v dyn fmt::Debug) -> Self {
+ Inner::Debug(value)
+ }
+
+ pub fn from_dyn_display(value: &'v dyn fmt::Display) -> Self {
+ Inner::Display(value)
+ }
+
+ pub fn empty() -> Self {
+ Inner::None
+ }
+
+ pub fn to_bool(&self) -> Option<bool> {
+ match self {
+ Inner::Bool(v) => Some(*v),
+ _ => None,
+ }
+ }
+
+ pub fn to_char(&self) -> Option<char> {
+ match self {
+ Inner::Char(v) => Some(*v),
+ _ => None,
+ }
+ }
+
+ pub fn to_f64(&self) -> Option<f64> {
+ match self {
+ Inner::F64(v) => Some(*v),
+ Inner::I64(v) => {
+ let v: i32 = (*v).try_into().ok()?;
+ v.try_into().ok()
+ }
+ Inner::U64(v) => {
+ let v: u32 = (*v).try_into().ok()?;
+ v.try_into().ok()
+ }
+ Inner::I128(v) => {
+ let v: i32 = (*v).try_into().ok()?;
+ v.try_into().ok()
+ }
+ Inner::U128(v) => {
+ let v: u32 = (*v).try_into().ok()?;
+ v.try_into().ok()
+ }
+ _ => None,
+ }
+ }
+
+ pub fn to_i64(&self) -> Option<i64> {
+ match self {
+ Inner::I64(v) => Some(*v),
+ Inner::U64(v) => (*v).try_into().ok(),
+ Inner::I128(v) => (*v).try_into().ok(),
+ Inner::U128(v) => (*v).try_into().ok(),
+ _ => None,
+ }
+ }
+
+ pub fn to_u64(&self) -> Option<u64> {
+ match self {
+ Inner::U64(v) => Some(*v),
+ Inner::I64(v) => (*v).try_into().ok(),
+ Inner::I128(v) => (*v).try_into().ok(),
+ Inner::U128(v) => (*v).try_into().ok(),
+ _ => None,
+ }
+ }
+
+ pub fn to_u128(&self) -> Option<u128> {
+ match self {
+ Inner::U128(v) => Some(*v),
+ Inner::I64(v) => (*v).try_into().ok(),
+ Inner::U64(v) => (*v).try_into().ok(),
+ Inner::I128(v) => (*v).try_into().ok(),
+ _ => None,
+ }
+ }
+
+ pub fn to_i128(&self) -> Option<i128> {
+ match self {
+ Inner::I128(v) => Some(*v),
+ Inner::I64(v) => (*v).try_into().ok(),
+ Inner::U64(v) => (*v).try_into().ok(),
+ Inner::U128(v) => (*v).try_into().ok(),
+ _ => None,
+ }
+ }
+
+ pub fn to_borrowed_str(&self) -> Option<&'v str> {
+ match self {
+ Inner::Str(v) => Some(v),
+ _ => None,
+ }
+ }
+
+ #[cfg(test)]
+ pub fn to_test_token(&self) -> Token {
+ match self {
+ Inner::None => Token::None,
+ Inner::Bool(v) => Token::Bool(*v),
+ Inner::Str(v) => Token::Str(*v),
+ Inner::Char(v) => Token::Char(*v),
+ Inner::I64(v) => Token::I64(*v),
+ Inner::U64(v) => Token::U64(*v),
+ Inner::F64(v) => Token::F64(*v),
+ Inner::I128(_) => unimplemented!(),
+ Inner::U128(_) => unimplemented!(),
+ Inner::Debug(_) => unimplemented!(),
+ Inner::Display(_) => unimplemented!(),
+ }
+ }
+ }
+
+ #[cfg(test)]
+ #[derive(Debug, PartialEq)]
+ pub enum Token<'v> {
+ None,
+ Bool(bool),
+ Char(char),
+ Str(&'v str),
+ F64(f64),
+ I64(i64),
+ U64(u64),
+ }
+
+ pub fn visit<'v>(
+ inner: &Inner<'v>,
+ mut visitor: impl VisitValue<'v>,
+ ) -> Result<(), crate::kv::Error> {
+ match inner {
+ Inner::None => visitor.visit_null(),
+ Inner::Bool(v) => visitor.visit_bool(*v),
+ Inner::Str(v) => visitor.visit_borrowed_str(*v),
+ Inner::Char(v) => visitor.visit_char(*v),
+ Inner::I64(v) => visitor.visit_i64(*v),
+ Inner::U64(v) => visitor.visit_u64(*v),
+ Inner::F64(v) => visitor.visit_f64(*v),
+ Inner::I128(v) => visitor.visit_i128(*v),
+ Inner::U128(v) => visitor.visit_u128(*v),
+ Inner::Debug(v) => visitor.visit_any(Value::from_dyn_debug(*v)),
+ Inner::Display(v) => visitor.visit_any(Value::from_dyn_display(*v)),
+ }
+ }
+}
+
+impl<'v> Value<'v> {
+ /// Get a value from a type implementing `std::fmt::Debug`.
+ #[cfg(feature = "kv_unstable")]
+ #[deprecated(note = "use `from_debug` instead")]
+ pub fn capture_debug<T>(value: &'v T) -> Self
+ where
+ T: fmt::Debug + 'static,
+ {
+ Value::from_debug(value)
+ }
+
+ /// Get a value from a type implementing `std::fmt::Display`.
+ #[cfg(feature = "kv_unstable")]
+ #[deprecated(note = "use `from_display` instead")]
+ pub fn capture_display<T>(value: &'v T) -> Self
+ where
+ T: fmt::Display + 'static,
+ {
+ Value::from_display(value)
+ }
+
+ /// Get a value from an error.
+ #[cfg(feature = "kv_unstable_std")]
+ #[deprecated(note = "use `from_dyn_error` instead")]
+ pub fn capture_error<T>(err: &'v T) -> Self
+ where
+ T: std::error::Error + 'static,
+ {
+ Value::from_dyn_error(err)
+ }
+
+ /// Get a value from a type implementing `serde::Serialize`.
+ #[cfg(feature = "kv_unstable_serde")]
+ #[deprecated(note = "use `from_serde` instead")]
+ pub fn capture_serde<T>(value: &'v T) -> Self
+ where
+ T: serde::Serialize + 'static,
+ {
+ Value::from_serde(value)
+ }
+
+ /// Get a value from a type implementing `sval::Value`.
+ #[cfg(feature = "kv_unstable_sval")]
+ #[deprecated(note = "use `from_sval` instead")]
+ pub fn capture_sval<T>(value: &'v T) -> Self
+ where
+ T: sval::Value + 'static,
+ {
+ Value::from_sval(value)
+ }
+
+ /// Check whether this value can be downcast to `T`.
+ #[cfg(feature = "kv_unstable")]
+ #[deprecated(
+ note = "downcasting has been removed; log an issue at https://github.com/rust-lang/log/issues if this is something you rely on"
+ )]
+ pub fn is<T: 'static>(&self) -> bool {
+ false
+ }
+
+ /// Try downcast this value to `T`.
+ #[cfg(feature = "kv_unstable")]
+ #[deprecated(
+ note = "downcasting has been removed; log an issue at https://github.com/rust-lang/log/issues if this is something you rely on"
+ )]
+ pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
+ None
+ }
+}
+
+// NOTE: Deprecated; but aliases can't carry this attribute
+#[cfg(feature = "kv_unstable")]
+pub use VisitValue as Visit;
+
+/// Get a value from a type implementing `std::fmt::Debug`.
+#[cfg(feature = "kv_unstable")]
+#[deprecated(note = "use the `key:? = value` macro syntax instead")]
+#[macro_export]
+macro_rules! as_debug {
+ ($capture:expr) => {
+ $crate::kv::Value::from_debug(&$capture)
+ };
+}
+
+/// Get a value from a type implementing `std::fmt::Display`.
+#[cfg(feature = "kv_unstable")]
+#[deprecated(note = "use the `key:% = value` macro syntax instead")]
+#[macro_export]
+macro_rules! as_display {
+ ($capture:expr) => {
+ $crate::kv::Value::from_display(&$capture)
+ };
+}
+
+/// Get a value from an error.
+#[cfg(feature = "kv_unstable_std")]
+#[deprecated(note = "use the `key:err = value` macro syntax instead")]
+#[macro_export]
+macro_rules! as_error {
+ ($capture:expr) => {
+ $crate::kv::Value::from_dyn_error(&$capture)
+ };
+}
+
+#[cfg(feature = "kv_unstable_serde")]
+#[deprecated(note = "use the `key:serde = value` macro syntax instead")]
+/// Get a value from a type implementing `serde::Serialize`.
+#[macro_export]
+macro_rules! as_serde {
+ ($capture:expr) => {
+ $crate::kv::Value::from_serde(&$capture)
+ };
+}
+
+/// Get a value from a type implementing `sval::Value`.
+#[cfg(feature = "kv_unstable_sval")]
+#[deprecated(note = "use the `key:sval = value` macro syntax instead")]
+#[macro_export]
+macro_rules! as_sval {
+ ($capture:expr) => {
+ $crate::kv::Value::from_sval(&$capture)
+ };
+}
+
+#[cfg(test)]
+pub(crate) mod tests {
+ use super::*;
+
+ impl<'v> Value<'v> {
+ pub(crate) fn to_token(&self) -> inner::Token {
+ self.inner.to_test_token()
+ }
+ }
+
+ fn unsigned() -> impl Iterator<Item = Value<'static>> {
+ vec![
+ Value::from(8u8),
+ Value::from(16u16),
+ Value::from(32u32),
+ Value::from(64u64),
+ Value::from(1usize),
+ Value::from(std::num::NonZeroU8::new(8).unwrap()),
+ Value::from(std::num::NonZeroU16::new(16).unwrap()),
+ Value::from(std::num::NonZeroU32::new(32).unwrap()),
+ Value::from(std::num::NonZeroU64::new(64).unwrap()),
+ Value::from(std::num::NonZeroUsize::new(1).unwrap()),
+ ]
+ .into_iter()
+ }
+
+ fn signed() -> impl Iterator<Item = Value<'static>> {
+ vec![
+ Value::from(-8i8),
+ Value::from(-16i16),
+ Value::from(-32i32),
+ Value::from(-64i64),
+ Value::from(-1isize),
+ Value::from(std::num::NonZeroI8::new(-8).unwrap()),
+ Value::from(std::num::NonZeroI16::new(-16).unwrap()),
+ Value::from(std::num::NonZeroI32::new(-32).unwrap()),
+ Value::from(std::num::NonZeroI64::new(-64).unwrap()),
+ Value::from(std::num::NonZeroIsize::new(-1).unwrap()),
+ ]
+ .into_iter()
+ }
+
+ fn float() -> impl Iterator<Item = Value<'static>> {
+ vec![Value::from(32.32f32), Value::from(64.64f64)].into_iter()
+ }
+
+ fn bool() -> impl Iterator<Item = Value<'static>> {
+ vec![Value::from(true), Value::from(false)].into_iter()
+ }
+
+ fn str() -> impl Iterator<Item = Value<'static>> {
+ vec![Value::from("a string"), Value::from("a loong string")].into_iter()
+ }
+
+ fn char() -> impl Iterator<Item = Value<'static>> {
+ vec![Value::from('a'), Value::from('â›°')].into_iter()
+ }
+
+ #[test]
+ fn test_to_value_display() {
+ assert_eq!(42u64.to_value().to_string(), "42");
+ assert_eq!(42i64.to_value().to_string(), "42");
+ assert_eq!(42.01f64.to_value().to_string(), "42.01");
+ assert_eq!(true.to_value().to_string(), "true");
+ assert_eq!('a'.to_value().to_string(), "a");
+ assert_eq!("a loong string".to_value().to_string(), "a loong string");
+ assert_eq!(Some(true).to_value().to_string(), "true");
+ assert_eq!(().to_value().to_string(), "None");
+ assert_eq!(None::<bool>.to_value().to_string(), "None");
+ }
+
+ #[test]
+ fn test_to_value_structured() {
+ assert_eq!(42u64.to_value().to_token(), inner::Token::U64(42));
+ assert_eq!(42i64.to_value().to_token(), inner::Token::I64(42));
+ assert_eq!(42.01f64.to_value().to_token(), inner::Token::F64(42.01));
+ assert_eq!(true.to_value().to_token(), inner::Token::Bool(true));
+ assert_eq!('a'.to_value().to_token(), inner::Token::Char('a'));
+ assert_eq!(
+ "a loong string".to_value().to_token(),
+ inner::Token::Str("a loong string".into())
+ );
+ assert_eq!(Some(true).to_value().to_token(), inner::Token::Bool(true));
+ assert_eq!(().to_value().to_token(), inner::Token::None);
+ assert_eq!(None::<bool>.to_value().to_token(), inner::Token::None);
+ }
+
+ #[test]
+ fn test_to_number() {
+ for v in unsigned() {
+ assert!(v.to_u64().is_some());
+ assert!(v.to_i64().is_some());
+ }
+
+ for v in signed() {
+ assert!(v.to_i64().is_some());
+ }
+
+ for v in unsigned().chain(signed()).chain(float()) {
+ assert!(v.to_f64().is_some());
+ }
+
+ for v in bool().chain(str()).chain(char()) {
+ assert!(v.to_u64().is_none());
+ assert!(v.to_i64().is_none());
+ assert!(v.to_f64().is_none());
+ }
+ }
+
+ #[test]
+ fn test_to_float() {
+ // Only integers from i32::MIN..=u32::MAX can be converted into floats
+ assert!(Value::from(i32::MIN).to_f64().is_some());
+ assert!(Value::from(u32::MAX).to_f64().is_some());
+
+ assert!(Value::from((i32::MIN as i64) - 1).to_f64().is_none());
+ assert!(Value::from((u32::MAX as u64) + 1).to_f64().is_none());
+ }
+
+ #[test]
+ fn test_to_cow_str() {
+ for v in str() {
+ assert!(v.to_borrowed_str().is_some());
+
+ #[cfg(feature = "kv_std")]
+ assert!(v.to_cow_str().is_some());
+ }
+
+ let short_lived = String::from("short lived");
+ let v = Value::from(&*short_lived);
+
+ assert!(v.to_borrowed_str().is_some());
+
+ #[cfg(feature = "kv_std")]
+ assert!(v.to_cow_str().is_some());
+
+ for v in unsigned().chain(signed()).chain(float()).chain(bool()) {
+ assert!(v.to_borrowed_str().is_none());
+
+ #[cfg(feature = "kv_std")]
+ assert!(v.to_cow_str().is_none());
+ }
+ }
+
+ #[test]
+ fn test_to_bool() {
+ for v in bool() {
+ assert!(v.to_bool().is_some());
+ }
+
+ for v in unsigned()
+ .chain(signed())
+ .chain(float())
+ .chain(str())
+ .chain(char())
+ {
+ assert!(v.to_bool().is_none());
+ }
+ }
+
+ #[test]
+ fn test_to_char() {
+ for v in char() {
+ assert!(v.to_char().is_some());
+ }
+
+ for v in unsigned()
+ .chain(signed())
+ .chain(float())
+ .chain(str())
+ .chain(bool())
+ {
+ assert!(v.to_char().is_none());
+ }
+ }
+
+ #[test]
+ fn test_visit_integer() {
+ struct Extract(Option<u64>);
+
+ impl<'v> VisitValue<'v> for Extract {
+ fn visit_any(&mut self, value: Value) -> Result<(), Error> {
+ unimplemented!("unexpected value: {value:?}")
+ }
+
+ fn visit_u64(&mut self, value: u64) -> Result<(), Error> {
+ self.0 = Some(value);
+
+ Ok(())
+ }
+ }
+
+ let mut extract = Extract(None);
+ Value::from(42u64).visit(&mut extract).unwrap();
+
+ assert_eq!(Some(42), extract.0);
+ }
+
+ #[test]
+ fn test_visit_borrowed_str() {
+ struct Extract<'v>(Option<&'v str>);
+
+ impl<'v> VisitValue<'v> for Extract<'v> {
+ fn visit_any(&mut self, value: Value) -> Result<(), Error> {
+ unimplemented!("unexpected value: {value:?}")
+ }
+
+ fn visit_borrowed_str(&mut self, value: &'v str) -> Result<(), Error> {
+ self.0 = Some(value);
+
+ Ok(())
+ }
+ }
+
+ let mut extract = Extract(None);
+
+ let short_lived = String::from("A short-lived string");
+ Value::from(&*short_lived).visit(&mut extract).unwrap();
+
+ assert_eq!(Some("A short-lived string"), extract.0);
+ }
+}
diff --git a/crates/log/src/lib.rs b/crates/log/src/lib.rs
index 8eb1c50..799b88e 100644
--- a/crates/log/src/lib.rs
+++ b/crates/log/src/lib.rs
@@ -1,1994 +1,1901 @@
-// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! A lightweight logging facade.
-//!
-//! The `log` crate provides a single logging API that abstracts over the
-//! actual logging implementation. Libraries can use the logging API provided
-//! by this crate, and the consumer of those libraries can choose the logging
-//! implementation that is most suitable for its use case.
-//!
-//! If no logging implementation is selected, the facade falls back to a "noop"
-//! implementation that ignores all log messages. The overhead in this case
-//! is very small - just an integer load, comparison and jump.
-//!
-//! A log request consists of a _target_, a _level_, and a _body_. A target is a
-//! string which defaults to the module path of the location of the log request,
-//! though that default may be overridden. Logger implementations typically use
-//! the target to filter requests based on some user configuration.
-//!
-//! # Usage
-//!
-//! The basic use of the log crate is through the five logging macros: [`error!`],
-//! [`warn!`], [`info!`], [`debug!`] and [`trace!`]
-//! where `error!` represents the highest-priority log messages
-//! and `trace!` the lowest. The log messages are filtered by configuring
-//! the log level to exclude messages with a lower priority.
-//! Each of these macros accept format strings similarly to [`println!`].
-//!
-//!
-//! [`error!`]: ./macro.error.html
-//! [`warn!`]: ./macro.warn.html
-//! [`info!`]: ./macro.info.html
-//! [`debug!`]: ./macro.debug.html
-//! [`trace!`]: ./macro.trace.html
-//! [`println!`]: https://doc.rust-lang.org/stable/std/macro.println.html
-//!
-//! ## In libraries
-//!
-//! Libraries should link only to the `log` crate, and use the provided
-//! macros to log whatever information will be useful to downstream consumers.
-//!
-//! ### Examples
-//!
-//! ```edition2018
-//! # #[derive(Debug)] pub struct Yak(String);
-//! # impl Yak { fn shave(&mut self, _: u32) {} }
-//! # fn find_a_razor() -> Result<u32, u32> { Ok(1) }
-//! use log::{info, warn};
-//!
-//! pub fn shave_the_yak(yak: &mut Yak) {
-//! info!(target: "yak_events", "Commencing yak shaving for {:?}", yak);
-//!
-//! loop {
-//! match find_a_razor() {
-//! Ok(razor) => {
-//! info!("Razor located: {}", razor);
-//! yak.shave(razor);
-//! break;
-//! }
-//! Err(err) => {
-//! warn!("Unable to locate a razor: {}, retrying", err);
-//! }
-//! }
-//! }
-//! }
-//! # fn main() {}
-//! ```
-//!
-//! ## In executables
-//!
-//! Executables should choose a logging implementation and initialize it early in the
-//! runtime of the program. Logging implementations will typically include a
-//! function to do this. Any log messages generated before
-//! the implementation is initialized will be ignored.
-//!
-//! The executable itself may use the `log` crate to log as well.
-//!
-//! ### Warning
-//!
-//! The logging system may only be initialized once.
-//!
-//! ## Structured logging
-//!
-//! If you enable the `kv_unstable` feature you can associate structured values
-//! with your log records. If we take the example from before, we can include
-//! some additional context besides what's in the formatted message:
-//!
-//! ```edition2018
-//! # #[macro_use] extern crate serde;
-//! # #[derive(Debug, Serialize)] pub struct Yak(String);
-//! # impl Yak { fn shave(&mut self, _: u32) {} }
-//! # fn find_a_razor() -> Result<u32, std::io::Error> { Ok(1) }
-//! # #[cfg(feature = "kv_unstable_serde")]
-//! # fn main() {
-//! use log::{info, warn, as_serde, as_error};
-//!
-//! pub fn shave_the_yak(yak: &mut Yak) {
-//! info!(target: "yak_events", yak = as_serde!(yak); "Commencing yak shaving");
-//!
-//! loop {
-//! match find_a_razor() {
-//! Ok(razor) => {
-//! info!(razor = razor; "Razor located");
-//! yak.shave(razor);
-//! break;
-//! }
-//! Err(err) => {
-//! warn!(err = as_error!(err); "Unable to locate a razor, retrying");
-//! }
-//! }
-//! }
-//! }
-//! # }
-//! # #[cfg(not(feature = "kv_unstable_serde"))]
-//! # fn main() {}
-//! ```
-//!
-//! # Available logging implementations
-//!
-//! In order to produce log output executables have to use
-//! a logger implementation compatible with the facade.
-//! There are many available implementations to choose from,
-//! here are some of the most popular ones:
-//!
-//! * Simple minimal loggers:
-//! * [env_logger]
-//! * [simple_logger]
-//! * [simplelog]
-//! * [pretty_env_logger]
-//! * [stderrlog]
-//! * [flexi_logger]
-//! * Complex configurable frameworks:
-//! * [log4rs]
-//! * [fern]
-//! * Adaptors for other facilities:
-//! * [syslog]
-//! * [slog-stdlog]
-//! * [systemd-journal-logger]
-//! * [android_log]
-//! * [win_dbg_logger]
-//! * [db_logger]
-//! * For WebAssembly binaries:
-//! * [console_log]
-//! * For dynamic libraries:
-//! * You may need to construct an FFI-safe wrapper over `log` to initialize in your libraries
-//!
-//! # Implementing a Logger
-//!
-//! Loggers implement the [`Log`] trait. Here's a very basic example that simply
-//! logs all messages at the [`Error`][level_link], [`Warn`][level_link] or
-//! [`Info`][level_link] levels to stdout:
-//!
-//! ```edition2018
-//! use log::{Record, Level, Metadata};
-//!
-//! struct SimpleLogger;
-//!
-//! impl log::Log for SimpleLogger {
-//! fn enabled(&self, metadata: &Metadata) -> bool {
-//! metadata.level() <= Level::Info
-//! }
-//!
-//! fn log(&self, record: &Record) {
-//! if self.enabled(record.metadata()) {
-//! println!("{} - {}", record.level(), record.args());
-//! }
-//! }
-//!
-//! fn flush(&self) {}
-//! }
-//!
-//! # fn main() {}
-//! ```
-//!
-//! Loggers are installed by calling the [`set_logger`] function. The maximum
-//! log level also needs to be adjusted via the [`set_max_level`] function. The
-//! logging facade uses this as an optimization to improve performance of log
-//! messages at levels that are disabled. It's important to set it, as it
-//! defaults to [`Off`][filter_link], so no log messages will ever be captured!
-//! In the case of our example logger, we'll want to set the maximum log level
-//! to [`Info`][filter_link], since we ignore any [`Debug`][level_link] or
-//! [`Trace`][level_link] level log messages. A logging implementation should
-//! provide a function that wraps a call to [`set_logger`] and
-//! [`set_max_level`], handling initialization of the logger:
-//!
-//! ```edition2018
-//! # use log::{Level, Metadata};
-//! # struct SimpleLogger;
-//! # impl log::Log for SimpleLogger {
-//! # fn enabled(&self, _: &Metadata) -> bool { false }
-//! # fn log(&self, _: &log::Record) {}
-//! # fn flush(&self) {}
-//! # }
-//! # fn main() {}
-//! use log::{SetLoggerError, LevelFilter};
-//!
-//! static LOGGER: SimpleLogger = SimpleLogger;
-//!
-//! pub fn init() -> Result<(), SetLoggerError> {
-//! log::set_logger(&LOGGER)
-//! .map(|()| log::set_max_level(LevelFilter::Info))
-//! }
-//! ```
-//!
-//! Implementations that adjust their configurations at runtime should take care
-//! to adjust the maximum log level as well.
-//!
-//! # Use with `std`
-//!
-//! `set_logger` requires you to provide a `&'static Log`, which can be hard to
-//! obtain if your logger depends on some runtime configuration. The
-//! `set_boxed_logger` function is available with the `std` Cargo feature. It is
-//! identical to `set_logger` except that it takes a `Box<Log>` rather than a
-//! `&'static Log`:
-//!
-//! ```edition2018
-//! # use log::{Level, LevelFilter, Log, SetLoggerError, Metadata};
-//! # struct SimpleLogger;
-//! # impl log::Log for SimpleLogger {
-//! # fn enabled(&self, _: &Metadata) -> bool { false }
-//! # fn log(&self, _: &log::Record) {}
-//! # fn flush(&self) {}
-//! # }
-//! # fn main() {}
-//! # #[cfg(feature = "std")]
-//! pub fn init() -> Result<(), SetLoggerError> {
-//! log::set_boxed_logger(Box::new(SimpleLogger))
-//! .map(|()| log::set_max_level(LevelFilter::Info))
-//! }
-//! ```
-//!
-//! # Compile time filters
-//!
-//! Log levels can be statically disabled at compile time via Cargo features. Log invocations at
-//! disabled levels will be skipped and will not even be present in the resulting binary.
-//! This level is configured separately for release and debug builds. The features are:
-//!
-//! * `max_level_off`
-//! * `max_level_error`
-//! * `max_level_warn`
-//! * `max_level_info`
-//! * `max_level_debug`
-//! * `max_level_trace`
-//! * `release_max_level_off`
-//! * `release_max_level_error`
-//! * `release_max_level_warn`
-//! * `release_max_level_info`
-//! * `release_max_level_debug`
-//! * `release_max_level_trace`
-//!
-//! These features control the value of the `STATIC_MAX_LEVEL` constant. The logging macros check
-//! this value before logging a message. By default, no levels are disabled.
-//!
-//! Libraries should avoid using the max level features because they're global and can't be changed
-//! once they're set.
-//!
-//! For example, a crate can disable trace level logs in debug builds and trace, debug, and info
-//! level logs in release builds with the following configuration:
-//!
-//! ```toml
-//! [dependencies]
-//! log = { version = "0.4", features = ["max_level_debug", "release_max_level_warn"] }
-//! ```
-//! # Crate Feature Flags
-//!
-//! The following crate feature flags are available in addition to the filters. They are
-//! configured in your `Cargo.toml`.
-//!
-//! * `std` allows use of `std` crate instead of the default `core`. Enables using `std::error` and
-//! `set_boxed_logger` functionality.
-//! * `serde` enables support for serialization and deserialization of `Level` and `LevelFilter`.
-//!
-//! ```toml
-//! [dependencies]
-//! log = { version = "0.4", features = ["std", "serde"] }
-//! ```
-//!
-//! # Version compatibility
-//!
-//! The 0.3 and 0.4 versions of the `log` crate are almost entirely compatible. Log messages
-//! made using `log` 0.3 will forward transparently to a logger implementation using `log` 0.4. Log
-//! messages made using `log` 0.4 will forward to a logger implementation using `log` 0.3, but the
-//! module path and file name information associated with the message will unfortunately be lost.
-//!
-//! [`Log`]: trait.Log.html
-//! [level_link]: enum.Level.html
-//! [filter_link]: enum.LevelFilter.html
-//! [`set_logger`]: fn.set_logger.html
-//! [`set_max_level`]: fn.set_max_level.html
-//! [`try_set_logger_raw`]: fn.try_set_logger_raw.html
-//! [`shutdown_logger_raw`]: fn.shutdown_logger_raw.html
-//! [env_logger]: https://docs.rs/env_logger/*/env_logger/
-//! [simple_logger]: https://github.com/borntyping/rust-simple_logger
-//! [simplelog]: https://github.com/drakulix/simplelog.rs
-//! [pretty_env_logger]: https://docs.rs/pretty_env_logger/*/pretty_env_logger/
-//! [stderrlog]: https://docs.rs/stderrlog/*/stderrlog/
-//! [flexi_logger]: https://docs.rs/flexi_logger/*/flexi_logger/
-//! [syslog]: https://docs.rs/syslog/*/syslog/
-//! [slog-stdlog]: https://docs.rs/slog-stdlog/*/slog_stdlog/
-//! [log4rs]: https://docs.rs/log4rs/*/log4rs/
-//! [fern]: https://docs.rs/fern/*/fern/
-//! [systemd-journal-logger]: https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/
-//! [android_log]: https://docs.rs/android_log/*/android_log/
-//! [win_dbg_logger]: https://docs.rs/win_dbg_logger/*/win_dbg_logger/
-//! [console_log]: https://docs.rs/console_log/*/console_log/
-
-#![doc(
- html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
- html_favicon_url = "https://www.rust-lang.org/favicon.ico",
- html_root_url = "https://docs.rs/log/0.4.17"
-)]
-#![warn(missing_docs)]
-#![deny(missing_debug_implementations, unconditional_recursion)]
-#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
-// When compiled for the rustc compiler itself we want to make sure that this is
-// an unstable crate
-#![cfg_attr(rustbuild, feature(staged_api, rustc_private))]
-#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))]
-
-#[cfg(all(not(feature = "std"), not(test)))]
-extern crate core as std;
-
-#[macro_use]
-extern crate cfg_if;
-
-use std::cmp;
-#[cfg(feature = "std")]
-use std::error;
-use std::fmt;
-use std::mem;
-use std::str::FromStr;
-
-#[macro_use]
-mod macros;
-mod serde;
-
-#[cfg(feature = "kv_unstable")]
-pub mod kv;
-
-#[cfg(default_log_impl)]
-extern crate once_cell;
-#[cfg(default_log_impl)]
-mod android_logger;
-
-#[cfg(has_atomics)]
-use std::sync::atomic::{AtomicUsize, Ordering};
-
-#[cfg(not(has_atomics))]
-use std::cell::Cell;
-#[cfg(not(has_atomics))]
-use std::sync::atomic::Ordering;
-
-#[cfg(not(has_atomics))]
-struct AtomicUsize {
- v: Cell<usize>,
-}
-
-#[cfg(not(has_atomics))]
-impl AtomicUsize {
- const fn new(v: usize) -> AtomicUsize {
- AtomicUsize { v: Cell::new(v) }
- }
-
- fn load(&self, _order: Ordering) -> usize {
- self.v.get()
- }
-
- fn store(&self, val: usize, _order: Ordering) {
- self.v.set(val)
- }
-
- #[cfg(atomic_cas)]
- fn compare_exchange(
- &self,
- current: usize,
- new: usize,
- _success: Ordering,
- _failure: Ordering,
- ) -> Result<usize, usize> {
- let prev = self.v.get();
- if current == prev {
- self.v.set(new);
- }
- Ok(prev)
- }
-}
-
-// Any platform without atomics is unlikely to have multiple cores, so
-// writing via Cell will not be a race condition.
-#[cfg(not(has_atomics))]
-unsafe impl Sync for AtomicUsize {}
-
-// The LOGGER static holds a pointer to the global logger. It is protected by
-// the STATE static which determines whether LOGGER has been initialized yet.
-static mut LOGGER: &dyn Log = &NopLogger;
-
-static STATE: AtomicUsize = AtomicUsize::new(0);
-
-// There are three different states that we care about: the logger's
-// uninitialized, the logger's initializing (set_logger's been called but
-// LOGGER hasn't actually been set yet), or the logger's active.
-const UNINITIALIZED: usize = 0;
-const INITIALIZING: usize = 1;
-const INITIALIZED: usize = 2;
-
-#[cfg(not(default_log_impl))]
-static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
-#[cfg(default_log_impl)]
-static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
-
-static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
-
-static SET_LOGGER_ERROR: &str = "attempted to set a logger after the logging system \
- was already initialized";
-static LEVEL_PARSE_ERROR: &str =
- "attempted to convert a string that doesn't match an existing log level";
-
-/// An enum representing the available verbosity levels of the logger.
-///
-/// Typical usage includes: checking if a certain `Level` is enabled with
-/// [`log_enabled!`](macro.log_enabled.html), specifying the `Level` of
-/// [`log!`](macro.log.html), and comparing a `Level` directly to a
-/// [`LevelFilter`](enum.LevelFilter.html).
-#[repr(usize)]
-#[derive(Copy, Eq, Debug, Hash)]
-pub enum Level {
- /// The "error" level.
- ///
- /// Designates very serious errors.
- // This way these line up with the discriminants for LevelFilter below
- // This works because Rust treats field-less enums the same way as C does:
- // https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
- Error = 1,
- /// The "warn" level.
- ///
- /// Designates hazardous situations.
- Warn,
- /// The "info" level.
- ///
- /// Designates useful information.
- Info,
- /// The "debug" level.
- ///
- /// Designates lower priority information.
- Debug,
- /// The "trace" level.
- ///
- /// Designates very low priority, often extremely verbose, information.
- Trace,
-}
-
-impl Clone for Level {
- #[inline]
- fn clone(&self) -> Level {
- *self
- }
-}
-
-impl PartialEq for Level {
- #[inline]
- fn eq(&self, other: &Level) -> bool {
- *self as usize == *other as usize
- }
-}
-
-impl PartialEq<LevelFilter> for Level {
- #[inline]
- fn eq(&self, other: &LevelFilter) -> bool {
- *self as usize == *other as usize
- }
-}
-
-impl PartialOrd for Level {
- #[inline]
- fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
- Some(self.cmp(other))
- }
-
- #[inline]
- fn lt(&self, other: &Level) -> bool {
- (*self as usize) < *other as usize
- }
-
- #[inline]
- fn le(&self, other: &Level) -> bool {
- *self as usize <= *other as usize
- }
-
- #[inline]
- fn gt(&self, other: &Level) -> bool {
- *self as usize > *other as usize
- }
-
- #[inline]
- fn ge(&self, other: &Level) -> bool {
- *self as usize >= *other as usize
- }
-}
-
-impl PartialOrd<LevelFilter> for Level {
- #[inline]
- fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
- Some((*self as usize).cmp(&(*other as usize)))
- }
-
- #[inline]
- fn lt(&self, other: &LevelFilter) -> bool {
- (*self as usize) < *other as usize
- }
-
- #[inline]
- fn le(&self, other: &LevelFilter) -> bool {
- *self as usize <= *other as usize
- }
-
- #[inline]
- fn gt(&self, other: &LevelFilter) -> bool {
- *self as usize > *other as usize
- }
-
- #[inline]
- fn ge(&self, other: &LevelFilter) -> bool {
- *self as usize >= *other as usize
- }
-}
-
-impl Ord for Level {
- #[inline]
- fn cmp(&self, other: &Level) -> cmp::Ordering {
- (*self as usize).cmp(&(*other as usize))
- }
-}
-
-fn ok_or<T, E>(t: Option<T>, e: E) -> Result<T, E> {
- match t {
- Some(t) => Ok(t),
- None => Err(e),
- }
-}
-
-// Reimplemented here because std::ascii is not available in libcore
-fn eq_ignore_ascii_case(a: &str, b: &str) -> bool {
- fn to_ascii_uppercase(c: u8) -> u8 {
- if c >= b'a' && c <= b'z' {
- c - b'a' + b'A'
- } else {
- c
- }
- }
-
- if a.len() == b.len() {
- a.bytes()
- .zip(b.bytes())
- .all(|(a, b)| to_ascii_uppercase(a) == to_ascii_uppercase(b))
- } else {
- false
- }
-}
-
-impl FromStr for Level {
- type Err = ParseLevelError;
- fn from_str(level: &str) -> Result<Level, Self::Err> {
- ok_or(
- LOG_LEVEL_NAMES
- .iter()
- .position(|&name| eq_ignore_ascii_case(name, level))
- .into_iter()
- .filter(|&idx| idx != 0)
- .map(|idx| Level::from_usize(idx).unwrap())
- .next(),
- ParseLevelError(()),
- )
- }
-}
-
-impl fmt::Display for Level {
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- fmt.pad(self.as_str())
- }
-}
-
-impl Level {
- fn from_usize(u: usize) -> Option<Level> {
- match u {
- 1 => Some(Level::Error),
- 2 => Some(Level::Warn),
- 3 => Some(Level::Info),
- 4 => Some(Level::Debug),
- 5 => Some(Level::Trace),
- _ => None,
- }
- }
-
- /// Returns the most verbose logging level.
- #[inline]
- pub fn max() -> Level {
- Level::Trace
- }
-
- /// Converts the `Level` to the equivalent `LevelFilter`.
- #[inline]
- pub fn to_level_filter(&self) -> LevelFilter {
- LevelFilter::from_usize(*self as usize).unwrap()
- }
-
- /// Returns the string representation of the `Level`.
- ///
- /// This returns the same string as the `fmt::Display` implementation.
- pub fn as_str(&self) -> &'static str {
- LOG_LEVEL_NAMES[*self as usize]
- }
-
- /// Iterate through all supported logging levels.
- ///
- /// The order of iteration is from more severe to less severe log messages.
- ///
- /// # Examples
- ///
- /// ```
- /// use log::Level;
- ///
- /// let mut levels = Level::iter();
- ///
- /// assert_eq!(Some(Level::Error), levels.next());
- /// assert_eq!(Some(Level::Trace), levels.last());
- /// ```
- pub fn iter() -> impl Iterator<Item = Self> {
- (1..6).map(|i| Self::from_usize(i).unwrap())
- }
-}
-
-/// An enum representing the available verbosity level filters of the logger.
-///
-/// A `LevelFilter` may be compared directly to a [`Level`]. Use this type
-/// to get and set the maximum log level with [`max_level()`] and [`set_max_level`].
-///
-/// [`Level`]: enum.Level.html
-/// [`max_level()`]: fn.max_level.html
-/// [`set_max_level`]: fn.set_max_level.html
-#[repr(usize)]
-#[derive(Copy, Eq, Debug, Hash)]
-pub enum LevelFilter {
- /// A level lower than all log levels.
- Off,
- /// Corresponds to the `Error` log level.
- Error,
- /// Corresponds to the `Warn` log level.
- Warn,
- /// Corresponds to the `Info` log level.
- Info,
- /// Corresponds to the `Debug` log level.
- Debug,
- /// Corresponds to the `Trace` log level.
- Trace,
-}
-
-// Deriving generates terrible impls of these traits
-
-impl Clone for LevelFilter {
- #[inline]
- fn clone(&self) -> LevelFilter {
- *self
- }
-}
-
-impl PartialEq for LevelFilter {
- #[inline]
- fn eq(&self, other: &LevelFilter) -> bool {
- *self as usize == *other as usize
- }
-}
-
-impl PartialEq<Level> for LevelFilter {
- #[inline]
- fn eq(&self, other: &Level) -> bool {
- other.eq(self)
- }
-}
-
-impl PartialOrd for LevelFilter {
- #[inline]
- fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
- Some(self.cmp(other))
- }
-
- #[inline]
- fn lt(&self, other: &LevelFilter) -> bool {
- (*self as usize) < *other as usize
- }
-
- #[inline]
- fn le(&self, other: &LevelFilter) -> bool {
- *self as usize <= *other as usize
- }
-
- #[inline]
- fn gt(&self, other: &LevelFilter) -> bool {
- *self as usize > *other as usize
- }
-
- #[inline]
- fn ge(&self, other: &LevelFilter) -> bool {
- *self as usize >= *other as usize
- }
-}
-
-impl PartialOrd<Level> for LevelFilter {
- #[inline]
- fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
- Some((*self as usize).cmp(&(*other as usize)))
- }
-
- #[inline]
- fn lt(&self, other: &Level) -> bool {
- (*self as usize) < *other as usize
- }
-
- #[inline]
- fn le(&self, other: &Level) -> bool {
- *self as usize <= *other as usize
- }
-
- #[inline]
- fn gt(&self, other: &Level) -> bool {
- *self as usize > *other as usize
- }
-
- #[inline]
- fn ge(&self, other: &Level) -> bool {
- *self as usize >= *other as usize
- }
-}
-
-impl Ord for LevelFilter {
- #[inline]
- fn cmp(&self, other: &LevelFilter) -> cmp::Ordering {
- (*self as usize).cmp(&(*other as usize))
- }
-}
-
-impl FromStr for LevelFilter {
- type Err = ParseLevelError;
- fn from_str(level: &str) -> Result<LevelFilter, Self::Err> {
- ok_or(
- LOG_LEVEL_NAMES
- .iter()
- .position(|&name| eq_ignore_ascii_case(name, level))
- .map(|p| LevelFilter::from_usize(p).unwrap()),
- ParseLevelError(()),
- )
- }
-}
-
-impl fmt::Display for LevelFilter {
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- fmt.pad(self.as_str())
- }
-}
-
-impl LevelFilter {
- fn from_usize(u: usize) -> Option<LevelFilter> {
- match u {
- 0 => Some(LevelFilter::Off),
- 1 => Some(LevelFilter::Error),
- 2 => Some(LevelFilter::Warn),
- 3 => Some(LevelFilter::Info),
- 4 => Some(LevelFilter::Debug),
- 5 => Some(LevelFilter::Trace),
- _ => None,
- }
- }
-
- /// Returns the most verbose logging level filter.
- #[inline]
- pub fn max() -> LevelFilter {
- LevelFilter::Trace
- }
-
- /// Converts `self` to the equivalent `Level`.
- ///
- /// Returns `None` if `self` is `LevelFilter::Off`.
- #[inline]
- pub fn to_level(&self) -> Option<Level> {
- Level::from_usize(*self as usize)
- }
-
- /// Returns the string representation of the `LevelFilter`.
- ///
- /// This returns the same string as the `fmt::Display` implementation.
- pub fn as_str(&self) -> &'static str {
- LOG_LEVEL_NAMES[*self as usize]
- }
-
- /// Iterate through all supported filtering levels.
- ///
- /// The order of iteration is from less to more verbose filtering.
- ///
- /// # Examples
- ///
- /// ```
- /// use log::LevelFilter;
- ///
- /// let mut levels = LevelFilter::iter();
- ///
- /// assert_eq!(Some(LevelFilter::Off), levels.next());
- /// assert_eq!(Some(LevelFilter::Trace), levels.last());
- /// ```
- pub fn iter() -> impl Iterator<Item = Self> {
- (0..6).map(|i| Self::from_usize(i).unwrap())
- }
-}
-
-#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
-enum MaybeStaticStr<'a> {
- Static(&'static str),
- Borrowed(&'a str),
-}
-
-impl<'a> MaybeStaticStr<'a> {
- #[inline]
- fn get(&self) -> &'a str {
- match *self {
- MaybeStaticStr::Static(s) => s,
- MaybeStaticStr::Borrowed(s) => s,
- }
- }
-}
-
-/// The "payload" of a log message.
-///
-/// # Use
-///
-/// `Record` structures are passed as parameters to the [`log`][method.log]
-/// method of the [`Log`] trait. Logger implementors manipulate these
-/// structures in order to display log messages. `Record`s are automatically
-/// created by the [`log!`] macro and so are not seen by log users.
-///
-/// Note that the [`level()`] and [`target()`] accessors are equivalent to
-/// `self.metadata().level()` and `self.metadata().target()` respectively.
-/// These methods are provided as a convenience for users of this structure.
-///
-/// # Example
-///
-/// The following example shows a simple logger that displays the level,
-/// module path, and message of any `Record` that is passed to it.
-///
-/// ```edition2018
-/// struct SimpleLogger;
-///
-/// impl log::Log for SimpleLogger {
-/// fn enabled(&self, metadata: &log::Metadata) -> bool {
-/// true
-/// }
-///
-/// fn log(&self, record: &log::Record) {
-/// if !self.enabled(record.metadata()) {
-/// return;
-/// }
-///
-/// println!("{}:{} -- {}",
-/// record.level(),
-/// record.target(),
-/// record.args());
-/// }
-/// fn flush(&self) {}
-/// }
-/// ```
-///
-/// [method.log]: trait.Log.html#tymethod.log
-/// [`Log`]: trait.Log.html
-/// [`log!`]: macro.log.html
-/// [`level()`]: struct.Record.html#method.level
-/// [`target()`]: struct.Record.html#method.target
-#[derive(Clone, Debug)]
-pub struct Record<'a> {
- metadata: Metadata<'a>,
- args: fmt::Arguments<'a>,
- module_path: Option<MaybeStaticStr<'a>>,
- file: Option<MaybeStaticStr<'a>>,
- line: Option<u32>,
- #[cfg(feature = "kv_unstable")]
- key_values: KeyValues<'a>,
-}
-
-// This wrapper type is only needed so we can
-// `#[derive(Debug)]` on `Record`. It also
-// provides a useful `Debug` implementation for
-// the underlying `Source`.
-#[cfg(feature = "kv_unstable")]
-#[derive(Clone)]
-struct KeyValues<'a>(&'a dyn kv::Source);
-
-#[cfg(feature = "kv_unstable")]
-impl<'a> fmt::Debug for KeyValues<'a> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let mut visitor = f.debug_map();
- self.0.visit(&mut visitor).map_err(|_| fmt::Error)?;
- visitor.finish()
- }
-}
-
-impl<'a> Record<'a> {
- /// Returns a new builder.
- #[inline]
- pub fn builder() -> RecordBuilder<'a> {
- RecordBuilder::new()
- }
-
- /// The message body.
- #[inline]
- pub fn args(&self) -> &fmt::Arguments<'a> {
- &self.args
- }
-
- /// Metadata about the log directive.
- #[inline]
- pub fn metadata(&self) -> &Metadata<'a> {
- &self.metadata
- }
-
- /// The verbosity level of the message.
- #[inline]
- pub fn level(&self) -> Level {
- self.metadata.level()
- }
-
- /// The name of the target of the directive.
- #[inline]
- pub fn target(&self) -> &'a str {
- self.metadata.target()
- }
-
- /// The module path of the message.
- #[inline]
- pub fn module_path(&self) -> Option<&'a str> {
- self.module_path.map(|s| s.get())
- }
-
- /// The module path of the message, if it is a `'static` string.
- #[inline]
- pub fn module_path_static(&self) -> Option<&'static str> {
- match self.module_path {
- Some(MaybeStaticStr::Static(s)) => Some(s),
- _ => None,
- }
- }
-
- /// The source file containing the message.
- #[inline]
- pub fn file(&self) -> Option<&'a str> {
- self.file.map(|s| s.get())
- }
-
- /// The module path of the message, if it is a `'static` string.
- #[inline]
- pub fn file_static(&self) -> Option<&'static str> {
- match self.file {
- Some(MaybeStaticStr::Static(s)) => Some(s),
- _ => None,
- }
- }
-
- /// The line containing the message.
- #[inline]
- pub fn line(&self) -> Option<u32> {
- self.line
- }
-
- /// The structured key-value pairs associated with the message.
- #[cfg(feature = "kv_unstable")]
- #[inline]
- pub fn key_values(&self) -> &dyn kv::Source {
- self.key_values.0
- }
-
- /// Create a new [`RecordBuilder`](struct.RecordBuilder.html) based on this record.
- #[cfg(feature = "kv_unstable")]
- #[inline]
- pub fn to_builder(&self) -> RecordBuilder {
- RecordBuilder {
- record: Record {
- metadata: Metadata {
- level: self.metadata.level,
- target: self.metadata.target,
- },
- args: self.args,
- module_path: self.module_path,
- file: self.file,
- line: self.line,
- key_values: self.key_values.clone(),
- },
- }
- }
-}
-
-/// Builder for [`Record`](struct.Record.html).
-///
-/// Typically should only be used by log library creators or for testing and "shim loggers".
-/// The `RecordBuilder` can set the different parameters of `Record` object, and returns
-/// the created object when `build` is called.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::{Level, Record};
-///
-/// let record = Record::builder()
-/// .args(format_args!("Error!"))
-/// .level(Level::Error)
-/// .target("myApp")
-/// .file(Some("server.rs"))
-/// .line(Some(144))
-/// .module_path(Some("server"))
-/// .build();
-/// ```
-///
-/// Alternatively, use [`MetadataBuilder`](struct.MetadataBuilder.html):
-///
-/// ```edition2018
-/// use log::{Record, Level, MetadataBuilder};
-///
-/// let error_metadata = MetadataBuilder::new()
-/// .target("myApp")
-/// .level(Level::Error)
-/// .build();
-///
-/// let record = Record::builder()
-/// .metadata(error_metadata)
-/// .args(format_args!("Error!"))
-/// .line(Some(433))
-/// .file(Some("app.rs"))
-/// .module_path(Some("server"))
-/// .build();
-/// ```
-#[derive(Debug)]
-pub struct RecordBuilder<'a> {
- record: Record<'a>,
-}
-
-impl<'a> RecordBuilder<'a> {
- /// Construct new `RecordBuilder`.
- ///
- /// The default options are:
- ///
- /// - `args`: [`format_args!("")`]
- /// - `metadata`: [`Metadata::builder().build()`]
- /// - `module_path`: `None`
- /// - `file`: `None`
- /// - `line`: `None`
- ///
- /// [`format_args!("")`]: https://doc.rust-lang.org/std/macro.format_args.html
- /// [`Metadata::builder().build()`]: struct.MetadataBuilder.html#method.build
- #[inline]
- pub fn new() -> RecordBuilder<'a> {
- RecordBuilder {
- record: Record {
- args: format_args!(""),
- metadata: Metadata::builder().build(),
- module_path: None,
- file: None,
- line: None,
- #[cfg(feature = "kv_unstable")]
- key_values: KeyValues(&Option::None::<(kv::Key, kv::Value)>),
- },
- }
- }
-
- /// Set [`args`](struct.Record.html#method.args).
- #[inline]
- pub fn args(&mut self, args: fmt::Arguments<'a>) -> &mut RecordBuilder<'a> {
- self.record.args = args;
- self
- }
-
- /// Set [`metadata`](struct.Record.html#method.metadata). Construct a `Metadata` object with [`MetadataBuilder`](struct.MetadataBuilder.html).
- #[inline]
- pub fn metadata(&mut self, metadata: Metadata<'a>) -> &mut RecordBuilder<'a> {
- self.record.metadata = metadata;
- self
- }
-
- /// Set [`Metadata::level`](struct.Metadata.html#method.level).
- #[inline]
- pub fn level(&mut self, level: Level) -> &mut RecordBuilder<'a> {
- self.record.metadata.level = level;
- self
- }
-
- /// Set [`Metadata::target`](struct.Metadata.html#method.target)
- #[inline]
- pub fn target(&mut self, target: &'a str) -> &mut RecordBuilder<'a> {
- self.record.metadata.target = target;
- self
- }
-
- /// Set [`module_path`](struct.Record.html#method.module_path)
- #[inline]
- pub fn module_path(&mut self, path: Option<&'a str>) -> &mut RecordBuilder<'a> {
- self.record.module_path = path.map(MaybeStaticStr::Borrowed);
- self
- }
-
- /// Set [`module_path`](struct.Record.html#method.module_path) to a `'static` string
- #[inline]
- pub fn module_path_static(&mut self, path: Option<&'static str>) -> &mut RecordBuilder<'a> {
- self.record.module_path = path.map(MaybeStaticStr::Static);
- self
- }
-
- /// Set [`file`](struct.Record.html#method.file)
- #[inline]
- pub fn file(&mut self, file: Option<&'a str>) -> &mut RecordBuilder<'a> {
- self.record.file = file.map(MaybeStaticStr::Borrowed);
- self
- }
-
- /// Set [`file`](struct.Record.html#method.file) to a `'static` string.
- #[inline]
- pub fn file_static(&mut self, file: Option<&'static str>) -> &mut RecordBuilder<'a> {
- self.record.file = file.map(MaybeStaticStr::Static);
- self
- }
-
- /// Set [`line`](struct.Record.html#method.line)
- #[inline]
- pub fn line(&mut self, line: Option<u32>) -> &mut RecordBuilder<'a> {
- self.record.line = line;
- self
- }
-
- /// Set [`key_values`](struct.Record.html#method.key_values)
- #[cfg(feature = "kv_unstable")]
- #[inline]
- pub fn key_values(&mut self, kvs: &'a dyn kv::Source) -> &mut RecordBuilder<'a> {
- self.record.key_values = KeyValues(kvs);
- self
- }
-
- /// Invoke the builder and return a `Record`
- #[inline]
- pub fn build(&self) -> Record<'a> {
- self.record.clone()
- }
-}
-
-/// Metadata about a log message.
-///
-/// # Use
-///
-/// `Metadata` structs are created when users of the library use
-/// logging macros.
-///
-/// They are consumed by implementations of the `Log` trait in the
-/// `enabled` method.
-///
-/// `Record`s use `Metadata` to determine the log message's severity
-/// and target.
-///
-/// Users should use the `log_enabled!` macro in their code to avoid
-/// constructing expensive log messages.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::{Record, Level, Metadata};
-///
-/// struct MyLogger;
-///
-/// impl log::Log for MyLogger {
-/// fn enabled(&self, metadata: &Metadata) -> bool {
-/// metadata.level() <= Level::Info
-/// }
-///
-/// fn log(&self, record: &Record) {
-/// if self.enabled(record.metadata()) {
-/// println!("{} - {}", record.level(), record.args());
-/// }
-/// }
-/// fn flush(&self) {}
-/// }
-///
-/// # fn main(){}
-/// ```
-#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
-pub struct Metadata<'a> {
- level: Level,
- target: &'a str,
-}
-
-impl<'a> Metadata<'a> {
- /// Returns a new builder.
- #[inline]
- pub fn builder() -> MetadataBuilder<'a> {
- MetadataBuilder::new()
- }
-
- /// The verbosity level of the message.
- #[inline]
- pub fn level(&self) -> Level {
- self.level
- }
-
- /// The name of the target of the directive.
- #[inline]
- pub fn target(&self) -> &'a str {
- self.target
- }
-}
-
-/// Builder for [`Metadata`](struct.Metadata.html).
-///
-/// Typically should only be used by log library creators or for testing and "shim loggers".
-/// The `MetadataBuilder` can set the different parameters of a `Metadata` object, and returns
-/// the created object when `build` is called.
-///
-/// # Example
-///
-/// ```edition2018
-/// let target = "myApp";
-/// use log::{Level, MetadataBuilder};
-/// let metadata = MetadataBuilder::new()
-/// .level(Level::Debug)
-/// .target(target)
-/// .build();
-/// ```
-#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
-pub struct MetadataBuilder<'a> {
- metadata: Metadata<'a>,
-}
-
-impl<'a> MetadataBuilder<'a> {
- /// Construct a new `MetadataBuilder`.
- ///
- /// The default options are:
- ///
- /// - `level`: `Level::Info`
- /// - `target`: `""`
- #[inline]
- pub fn new() -> MetadataBuilder<'a> {
- MetadataBuilder {
- metadata: Metadata {
- level: Level::Info,
- target: "",
- },
- }
- }
-
- /// Setter for [`level`](struct.Metadata.html#method.level).
- #[inline]
- pub fn level(&mut self, arg: Level) -> &mut MetadataBuilder<'a> {
- self.metadata.level = arg;
- self
- }
-
- /// Setter for [`target`](struct.Metadata.html#method.target).
- #[inline]
- pub fn target(&mut self, target: &'a str) -> &mut MetadataBuilder<'a> {
- self.metadata.target = target;
- self
- }
-
- /// Returns a `Metadata` object.
- #[inline]
- pub fn build(&self) -> Metadata<'a> {
- self.metadata.clone()
- }
-}
-
-/// A trait encapsulating the operations required of a logger.
-pub trait Log: Sync + Send {
- /// Determines if a log message with the specified metadata would be
- /// logged.
- ///
- /// This is used by the `log_enabled!` macro to allow callers to avoid
- /// expensive computation of log message arguments if the message would be
- /// discarded anyway.
- ///
- /// # For implementors
- ///
- /// This method isn't called automatically by the `log!` macros.
- /// It's up to an implementation of the `Log` trait to call `enabled` in its own
- /// `log` method implementation to guarantee that filtering is applied.
- fn enabled(&self, metadata: &Metadata) -> bool;
-
- /// Logs the `Record`.
- ///
- /// # For implementors
- ///
- /// Note that `enabled` is *not* necessarily called before this method.
- /// Implementations of `log` should perform all necessary filtering
- /// internally.
- fn log(&self, record: &Record);
-
- /// Flushes any buffered records.
- fn flush(&self);
-}
-
-// Just used as a dummy initial value for LOGGER
-struct NopLogger;
-
-impl Log for NopLogger {
- fn enabled(&self, _: &Metadata) -> bool {
- false
- }
-
- fn log(&self, _: &Record) {}
- fn flush(&self) {}
-}
-
-impl<T> Log for &'_ T
-where
- T: ?Sized + Log,
-{
- fn enabled(&self, metadata: &Metadata) -> bool {
- (**self).enabled(metadata)
- }
-
- fn log(&self, record: &Record) {
- (**self).log(record)
- }
- fn flush(&self) {
- (**self).flush()
- }
-}
-
-#[cfg(feature = "std")]
-impl<T> Log for std::boxed::Box<T>
-where
- T: ?Sized + Log,
-{
- fn enabled(&self, metadata: &Metadata) -> bool {
- self.as_ref().enabled(metadata)
- }
-
- fn log(&self, record: &Record) {
- self.as_ref().log(record)
- }
- fn flush(&self) {
- self.as_ref().flush()
- }
-}
-
-#[cfg(feature = "std")]
-impl<T> Log for std::sync::Arc<T>
-where
- T: ?Sized + Log,
-{
- fn enabled(&self, metadata: &Metadata) -> bool {
- self.as_ref().enabled(metadata)
- }
-
- fn log(&self, record: &Record) {
- self.as_ref().log(record)
- }
- fn flush(&self) {
- self.as_ref().flush()
- }
-}
-
-/// Sets the global maximum log level.
-///
-/// Generally, this should only be called by the active logging implementation.
-///
-/// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs.
-#[inline]
-pub fn set_max_level(level: LevelFilter) {
- MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed)
-}
-
-/// Returns the current maximum log level.
-///
-/// The [`log!`], [`error!`], [`warn!`], [`info!`], [`debug!`], and [`trace!`] macros check
-/// this value and discard any message logged at a higher level. The maximum
-/// log level is set by the [`set_max_level`] function.
-///
-/// [`log!`]: macro.log.html
-/// [`error!`]: macro.error.html
-/// [`warn!`]: macro.warn.html
-/// [`info!`]: macro.info.html
-/// [`debug!`]: macro.debug.html
-/// [`trace!`]: macro.trace.html
-/// [`set_max_level`]: fn.set_max_level.html
-#[inline(always)]
-pub fn max_level() -> LevelFilter {
- // Since `LevelFilter` is `repr(usize)`,
- // this transmute is sound if and only if `MAX_LOG_LEVEL_FILTER`
- // is set to a usize that is a valid discriminant for `LevelFilter`.
- // Since `MAX_LOG_LEVEL_FILTER` is private, the only time it's set
- // is by `set_max_level` above, i.e. by casting a `LevelFilter` to `usize`.
- // So any usize stored in `MAX_LOG_LEVEL_FILTER` is a valid discriminant.
- unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) }
-}
-
-/// Sets the global logger to a `Box<Log>`.
-///
-/// This is a simple convenience wrapper over `set_logger`, which takes a
-/// `Box<Log>` rather than a `&'static Log`. See the documentation for
-/// [`set_logger`] for more details.
-///
-/// Requires the `std` feature.
-///
-/// # Errors
-///
-/// An error is returned if a logger has already been set.
-///
-/// [`set_logger`]: fn.set_logger.html
-#[cfg(all(feature = "std", atomic_cas))]
-pub fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), SetLoggerError> {
- set_logger_inner(|| Box::leak(logger))
-}
-
-/// Sets the global logger to a `&'static Log`.
-///
-/// This function may only be called once in the lifetime of a program. Any log
-/// events that occur before the call to `set_logger` completes will be ignored.
-///
-/// This function does not typically need to be called manually. Logger
-/// implementations should provide an initialization method that installs the
-/// logger internally.
-///
-/// # Availability
-///
-/// This method is available even when the `std` feature is disabled. However,
-/// it is currently unavailable on `thumbv6` targets, which lack support for
-/// some atomic operations which are used by this function. Even on those
-/// targets, [`set_logger_racy`] will be available.
-///
-/// # Errors
-///
-/// An error is returned if a logger has already been set.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::{error, info, warn, Record, Level, Metadata, LevelFilter};
-///
-/// static MY_LOGGER: MyLogger = MyLogger;
-///
-/// struct MyLogger;
-///
-/// impl log::Log for MyLogger {
-/// fn enabled(&self, metadata: &Metadata) -> bool {
-/// metadata.level() <= Level::Info
-/// }
-///
-/// fn log(&self, record: &Record) {
-/// if self.enabled(record.metadata()) {
-/// println!("{} - {}", record.level(), record.args());
-/// }
-/// }
-/// fn flush(&self) {}
-/// }
-///
-/// # fn main(){
-/// log::set_logger(&MY_LOGGER).unwrap();
-/// log::set_max_level(LevelFilter::Info);
-///
-/// info!("hello log");
-/// warn!("warning");
-/// error!("oops");
-/// # }
-/// ```
-///
-/// [`set_logger_racy`]: fn.set_logger_racy.html
-#[cfg(atomic_cas)]
-pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
- set_logger_inner(|| logger)
-}
-
-#[cfg(atomic_cas)]
-fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError>
-where
- F: FnOnce() -> &'static dyn Log,
-{
- let old_state = match STATE.compare_exchange(
- UNINITIALIZED,
- INITIALIZING,
- Ordering::SeqCst,
- Ordering::SeqCst,
- ) {
- Ok(s) | Err(s) => s,
- };
- match old_state {
- UNINITIALIZED => {
- unsafe {
- LOGGER = make_logger();
- }
- STATE.store(INITIALIZED, Ordering::SeqCst);
- Ok(())
- }
- INITIALIZING => {
- while STATE.load(Ordering::SeqCst) == INITIALIZING {
- // TODO: replace with `hint::spin_loop` once MSRV is 1.49.0.
- #[allow(deprecated)]
- std::sync::atomic::spin_loop_hint();
- }
- Err(SetLoggerError(()))
- }
- _ => Err(SetLoggerError(())),
- }
-}
-
-/// A thread-unsafe version of [`set_logger`].
-///
-/// This function is available on all platforms, even those that do not have
-/// support for atomics that is needed by [`set_logger`].
-///
-/// In almost all cases, [`set_logger`] should be preferred.
-///
-/// # Safety
-///
-/// This function is only safe to call when no other logger initialization
-/// function is called while this function still executes.
-///
-/// This can be upheld by (for example) making sure that **there are no other
-/// threads**, and (on embedded) that **interrupts are disabled**.
-///
-/// It is safe to use other logging functions while this function runs
-/// (including all logging macros).
-///
-/// [`set_logger`]: fn.set_logger.html
-pub unsafe fn set_logger_racy(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
- match STATE.load(Ordering::SeqCst) {
- UNINITIALIZED => {
- LOGGER = logger;
- STATE.store(INITIALIZED, Ordering::SeqCst);
- Ok(())
- }
- INITIALIZING => {
- // This is just plain UB, since we were racing another initialization function
- unreachable!("set_logger_racy must not be used with other initialization functions")
- }
- _ => Err(SetLoggerError(())),
- }
-}
-
-/// The type returned by [`set_logger`] if [`set_logger`] has already been called.
-///
-/// [`set_logger`]: fn.set_logger.html
-#[allow(missing_copy_implementations)]
-#[derive(Debug)]
-pub struct SetLoggerError(());
-
-impl fmt::Display for SetLoggerError {
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- fmt.write_str(SET_LOGGER_ERROR)
- }
-}
-
-// The Error trait is not available in libcore
-#[cfg(feature = "std")]
-impl error::Error for SetLoggerError {}
-
-/// The type returned by [`from_str`] when the string doesn't match any of the log levels.
-///
-/// [`from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
-#[allow(missing_copy_implementations)]
-#[derive(Debug, PartialEq)]
-pub struct ParseLevelError(());
-
-impl fmt::Display for ParseLevelError {
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- fmt.write_str(LEVEL_PARSE_ERROR)
- }
-}
-
-// The Error trait is not available in libcore
-#[cfg(feature = "std")]
-impl error::Error for ParseLevelError {}
-
-/// Returns a reference to the logger.
-///
-/// If a logger has not been set, a no-op implementation is returned.
-pub fn logger() -> &'static dyn Log {
- if STATE.load(Ordering::SeqCst) != INITIALIZED {
- #[cfg(default_log_impl)]
- {
- // On Android, default to logging to logcat if not explicitly initialized. This
- // prevents logs from being dropped by default, which may happen unexpectedly in case
- // of using libraries from multiple linker namespaces and failing to initialize the
- // logger in each namespace. See b/294216366#comment7.
- use android_logger::{AndroidLogger, Config};
- use std::sync::OnceLock;
- static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
- return
- ANDROID_LOGGER.get_or_init(|| {
- // Pass all logs down to liblog - it does its own filtering.
- AndroidLogger::new(Config::default().with_max_level(LevelFilter::Trace))
- });
- }
- static NOP: NopLogger = NopLogger;
- &NOP
- } else {
- unsafe { LOGGER }
- }
-}
-
-// WARNING: this is not part of the crate's public API and is subject to change at any time
-#[doc(hidden)]
-#[cfg(not(feature = "kv_unstable"))]
-pub fn __private_api_log(
- args: fmt::Arguments,
- level: Level,
- &(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
- kvs: Option<&[(&str, &str)]>,
-) {
- if kvs.is_some() {
- panic!(
- "key-value support is experimental and must be enabled using the `kv_unstable` feature"
- )
- }
-
- logger().log(
- &Record::builder()
- .args(args)
- .level(level)
- .target(target)
- .module_path_static(Some(module_path))
- .file_static(Some(file))
- .line(Some(line))
- .build(),
- );
-}
-
-// WARNING: this is not part of the crate's public API and is subject to change at any time
-#[doc(hidden)]
-#[cfg(feature = "kv_unstable")]
-pub fn __private_api_log(
- args: fmt::Arguments,
- level: Level,
- &(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
- kvs: Option<&[(&str, &dyn kv::ToValue)]>,
-) {
- logger().log(
- &Record::builder()
- .args(args)
- .level(level)
- .target(target)
- .module_path_static(Some(module_path))
- .file_static(Some(file))
- .line(Some(line))
- .key_values(&kvs)
- .build(),
- );
-}
-
-// WARNING: this is not part of the crate's public API and is subject to change at any time
-#[doc(hidden)]
-pub fn __private_api_enabled(level: Level, target: &str) -> bool {
- logger().enabled(&Metadata::builder().level(level).target(target).build())
-}
-
-// WARNING: this is not part of the crate's public API and is subject to change at any time
-#[doc(hidden)]
-pub mod __private_api {
- pub use std::option::Option;
-}
-
-/// The statically resolved maximum log level.
-///
-/// See the crate level documentation for information on how to configure this.
-///
-/// This value is checked by the log macros, but not by the `Log`ger returned by
-/// the [`logger`] function. Code that manually calls functions on that value
-/// should compare the level against this value.
-///
-/// [`logger`]: fn.logger.html
-pub const STATIC_MAX_LEVEL: LevelFilter = MAX_LEVEL_INNER;
-
-cfg_if! {
- if #[cfg(all(not(debug_assertions), feature = "release_max_level_off"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Off;
- } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_error"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Error;
- } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_warn"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Warn;
- } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_info"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Info;
- } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_debug"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Debug;
- } else if #[cfg(all(not(debug_assertions), feature = "release_max_level_trace"))] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Trace;
- } else if #[cfg(feature = "max_level_off")] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Off;
- } else if #[cfg(feature = "max_level_error")] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Error;
- } else if #[cfg(feature = "max_level_warn")] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Warn;
- } else if #[cfg(feature = "max_level_info")] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Info;
- } else if #[cfg(feature = "max_level_debug")] {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Debug;
- } else {
- const MAX_LEVEL_INNER: LevelFilter = LevelFilter::Trace;
- }
-}
-
-#[cfg(test)]
-mod tests {
- extern crate std;
- use super::{Level, LevelFilter, ParseLevelError};
- use tests::std::string::ToString;
-
- #[test]
- fn test_levelfilter_from_str() {
- let tests = [
- ("off", Ok(LevelFilter::Off)),
- ("error", Ok(LevelFilter::Error)),
- ("warn", Ok(LevelFilter::Warn)),
- ("info", Ok(LevelFilter::Info)),
- ("debug", Ok(LevelFilter::Debug)),
- ("trace", Ok(LevelFilter::Trace)),
- ("OFF", Ok(LevelFilter::Off)),
- ("ERROR", Ok(LevelFilter::Error)),
- ("WARN", Ok(LevelFilter::Warn)),
- ("INFO", Ok(LevelFilter::Info)),
- ("DEBUG", Ok(LevelFilter::Debug)),
- ("TRACE", Ok(LevelFilter::Trace)),
- ("asdf", Err(ParseLevelError(()))),
- ];
- for &(s, ref expected) in &tests {
- assert_eq!(expected, &s.parse());
- }
- }
-
- #[test]
- fn test_level_from_str() {
- let tests = [
- ("OFF", Err(ParseLevelError(()))),
- ("error", Ok(Level::Error)),
- ("warn", Ok(Level::Warn)),
- ("info", Ok(Level::Info)),
- ("debug", Ok(Level::Debug)),
- ("trace", Ok(Level::Trace)),
- ("ERROR", Ok(Level::Error)),
- ("WARN", Ok(Level::Warn)),
- ("INFO", Ok(Level::Info)),
- ("DEBUG", Ok(Level::Debug)),
- ("TRACE", Ok(Level::Trace)),
- ("asdf", Err(ParseLevelError(()))),
- ];
- for &(s, ref expected) in &tests {
- assert_eq!(expected, &s.parse());
- }
- }
-
- #[test]
- fn test_level_as_str() {
- let tests = &[
- (Level::Error, "ERROR"),
- (Level::Warn, "WARN"),
- (Level::Info, "INFO"),
- (Level::Debug, "DEBUG"),
- (Level::Trace, "TRACE"),
- ];
- for (input, expected) in tests {
- assert_eq!(*expected, input.as_str());
- }
- }
-
- #[test]
- fn test_level_show() {
- assert_eq!("INFO", Level::Info.to_string());
- assert_eq!("ERROR", Level::Error.to_string());
- }
-
- #[test]
- fn test_levelfilter_show() {
- assert_eq!("OFF", LevelFilter::Off.to_string());
- assert_eq!("ERROR", LevelFilter::Error.to_string());
- }
-
- #[test]
- fn test_cross_cmp() {
- assert!(Level::Debug > LevelFilter::Error);
- assert!(LevelFilter::Warn < Level::Trace);
- assert!(LevelFilter::Off < Level::Error);
- }
-
- #[test]
- fn test_cross_eq() {
- assert!(Level::Error == LevelFilter::Error);
- assert!(LevelFilter::Off != Level::Error);
- assert!(Level::Trace == LevelFilter::Trace);
- }
-
- #[test]
- fn test_to_level() {
- assert_eq!(Some(Level::Error), LevelFilter::Error.to_level());
- assert_eq!(None, LevelFilter::Off.to_level());
- assert_eq!(Some(Level::Debug), LevelFilter::Debug.to_level());
- }
-
- #[test]
- fn test_to_level_filter() {
- assert_eq!(LevelFilter::Error, Level::Error.to_level_filter());
- assert_eq!(LevelFilter::Trace, Level::Trace.to_level_filter());
- }
-
- #[test]
- fn test_level_filter_as_str() {
- let tests = &[
- (LevelFilter::Off, "OFF"),
- (LevelFilter::Error, "ERROR"),
- (LevelFilter::Warn, "WARN"),
- (LevelFilter::Info, "INFO"),
- (LevelFilter::Debug, "DEBUG"),
- (LevelFilter::Trace, "TRACE"),
- ];
- for (input, expected) in tests {
- assert_eq!(*expected, input.as_str());
- }
- }
-
- #[test]
- #[cfg(feature = "std")]
- fn test_error_trait() {
- use super::SetLoggerError;
- let e = SetLoggerError(());
- assert_eq!(
- &e.to_string(),
- "attempted to set a logger after the logging system \
- was already initialized"
- );
- }
-
- #[test]
- fn test_metadata_builder() {
- use super::MetadataBuilder;
- let target = "myApp";
- let metadata_test = MetadataBuilder::new()
- .level(Level::Debug)
- .target(target)
- .build();
- assert_eq!(metadata_test.level(), Level::Debug);
- assert_eq!(metadata_test.target(), "myApp");
- }
-
- #[test]
- fn test_metadata_convenience_builder() {
- use super::Metadata;
- let target = "myApp";
- let metadata_test = Metadata::builder()
- .level(Level::Debug)
- .target(target)
- .build();
- assert_eq!(metadata_test.level(), Level::Debug);
- assert_eq!(metadata_test.target(), "myApp");
- }
-
- #[test]
- fn test_record_builder() {
- use super::{MetadataBuilder, RecordBuilder};
- let target = "myApp";
- let metadata = MetadataBuilder::new().target(target).build();
- let fmt_args = format_args!("hello");
- let record_test = RecordBuilder::new()
- .args(fmt_args)
- .metadata(metadata)
- .module_path(Some("foo"))
- .file(Some("bar"))
- .line(Some(30))
- .build();
- assert_eq!(record_test.metadata().target(), "myApp");
- assert_eq!(record_test.module_path(), Some("foo"));
- assert_eq!(record_test.file(), Some("bar"));
- assert_eq!(record_test.line(), Some(30));
- }
-
- #[test]
- fn test_record_convenience_builder() {
- use super::{Metadata, Record};
- let target = "myApp";
- let metadata = Metadata::builder().target(target).build();
- let fmt_args = format_args!("hello");
- let record_test = Record::builder()
- .args(fmt_args)
- .metadata(metadata)
- .module_path(Some("foo"))
- .file(Some("bar"))
- .line(Some(30))
- .build();
- assert_eq!(record_test.target(), "myApp");
- assert_eq!(record_test.module_path(), Some("foo"));
- assert_eq!(record_test.file(), Some("bar"));
- assert_eq!(record_test.line(), Some(30));
- }
-
- #[test]
- fn test_record_complete_builder() {
- use super::{Level, Record};
- let target = "myApp";
- let record_test = Record::builder()
- .module_path(Some("foo"))
- .file(Some("bar"))
- .line(Some(30))
- .target(target)
- .level(Level::Error)
- .build();
- assert_eq!(record_test.target(), "myApp");
- assert_eq!(record_test.level(), Level::Error);
- assert_eq!(record_test.module_path(), Some("foo"));
- assert_eq!(record_test.file(), Some("bar"));
- assert_eq!(record_test.line(), Some(30));
- }
-
- #[test]
- #[cfg(feature = "kv_unstable")]
- fn test_record_key_values_builder() {
- use super::Record;
- use kv::{self, Visitor};
-
- struct TestVisitor {
- seen_pairs: usize,
- }
-
- impl<'kvs> Visitor<'kvs> for TestVisitor {
- fn visit_pair(
- &mut self,
- _: kv::Key<'kvs>,
- _: kv::Value<'kvs>,
- ) -> Result<(), kv::Error> {
- self.seen_pairs += 1;
- Ok(())
- }
- }
-
- let kvs: &[(&str, i32)] = &[("a", 1), ("b", 2)];
- let record_test = Record::builder().key_values(&kvs).build();
-
- let mut visitor = TestVisitor { seen_pairs: 0 };
-
- record_test.key_values().visit(&mut visitor).unwrap();
-
- assert_eq!(2, visitor.seen_pairs);
- }
-
- #[test]
- #[cfg(feature = "kv_unstable")]
- fn test_record_key_values_get_coerce() {
- use super::Record;
-
- let kvs: &[(&str, &str)] = &[("a", "1"), ("b", "2")];
- let record = Record::builder().key_values(&kvs).build();
-
- assert_eq!(
- "2",
- record
- .key_values()
- .get("b".into())
- .expect("missing key")
- .to_borrowed_str()
- .expect("invalid value")
- );
- }
-
- // Test that the `impl Log for Foo` blocks work
- // This test mostly operates on a type level, so failures will be compile errors
- #[test]
- fn test_foreign_impl() {
- use super::Log;
- #[cfg(feature = "std")]
- use std::sync::Arc;
-
- fn assert_is_log<T: Log + ?Sized>() {}
-
- assert_is_log::<&dyn Log>();
-
- #[cfg(feature = "std")]
- assert_is_log::<Box<dyn Log>>();
-
- #[cfg(feature = "std")]
- assert_is_log::<Arc<dyn Log>>();
-
- // Assert these statements for all T: Log + ?Sized
- #[allow(unused)]
- fn forall<T: Log + ?Sized>() {
- #[cfg(feature = "std")]
- assert_is_log::<Box<T>>();
-
- assert_is_log::<&T>();
-
- #[cfg(feature = "std")]
- assert_is_log::<Arc<T>>();
- }
- }
-}
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! A lightweight logging facade.
+//!
+//! The `log` crate provides a single logging API that abstracts over the
+//! actual logging implementation. Libraries can use the logging API provided
+//! by this crate, and the consumer of those libraries can choose the logging
+//! implementation that is most suitable for its use case.
+//!
+//! If no logging implementation is selected, the facade falls back to a "noop"
+//! implementation that ignores all log messages. The overhead in this case
+//! is very small - just an integer load, comparison and jump.
+//!
+//! A log request consists of a _target_, a _level_, and a _body_. A target is a
+//! string which defaults to the module path of the location of the log request,
+//! though that default may be overridden. Logger implementations typically use
+//! the target to filter requests based on some user configuration.
+//!
+//! # Usage
+//!
+//! The basic use of the log crate is through the five logging macros: [`error!`],
+//! [`warn!`], [`info!`], [`debug!`] and [`trace!`]
+//! where `error!` represents the highest-priority log messages
+//! and `trace!` the lowest. The log messages are filtered by configuring
+//! the log level to exclude messages with a lower priority.
+//! Each of these macros accept format strings similarly to [`println!`].
+//!
+//!
+//! [`error!`]: ./macro.error.html
+//! [`warn!`]: ./macro.warn.html
+//! [`info!`]: ./macro.info.html
+//! [`debug!`]: ./macro.debug.html
+//! [`trace!`]: ./macro.trace.html
+//! [`println!`]: https://doc.rust-lang.org/stable/std/macro.println.html
+//!
+//! Avoid writing expressions with side-effects in log statements. They may not be evaluated.
+//!
+//! ## In libraries
+//!
+//! Libraries should link only to the `log` crate, and use the provided
+//! macros to log whatever information will be useful to downstream consumers.
+//!
+//! ### Examples
+//!
+//! ```
+//! # #[derive(Debug)] pub struct Yak(String);
+//! # impl Yak { fn shave(&mut self, _: u32) {} }
+//! # fn find_a_razor() -> Result<u32, u32> { Ok(1) }
+//! use log::{info, warn};
+//!
+//! pub fn shave_the_yak(yak: &mut Yak) {
+//! info!(target: "yak_events", "Commencing yak shaving for {yak:?}");
+//!
+//! loop {
+//! match find_a_razor() {
+//! Ok(razor) => {
+//! info!("Razor located: {razor}");
+//! yak.shave(razor);
+//! break;
+//! }
+//! Err(err) => {
+//! warn!("Unable to locate a razor: {err}, retrying");
+//! }
+//! }
+//! }
+//! }
+//! # fn main() {}
+//! ```
+//!
+//! ## In executables
+//!
+//! Executables should choose a logging implementation and initialize it early in the
+//! runtime of the program. Logging implementations will typically include a
+//! function to do this. Any log messages generated before
+//! the implementation is initialized will be ignored.
+//!
+//! The executable itself may use the `log` crate to log as well.
+//!
+//! ### Warning
+//!
+//! The logging system may only be initialized once.
+//!
+//! ## Structured logging
+//!
+//! If you enable the `kv` feature you can associate structured values
+//! with your log records. If we take the example from before, we can include
+//! some additional context besides what's in the formatted message:
+//!
+//! ```
+//! # use serde::Serialize;
+//! # #[derive(Debug, Serialize)] pub struct Yak(String);
+//! # impl Yak { fn shave(&mut self, _: u32) {} }
+//! # fn find_a_razor() -> Result<u32, std::io::Error> { Ok(1) }
+//! # #[cfg(feature = "kv_serde")]
+//! # fn main() {
+//! use log::{info, warn};
+//!
+//! pub fn shave_the_yak(yak: &mut Yak) {
+//! info!(target: "yak_events", yak:serde; "Commencing yak shaving");
+//!
+//! loop {
+//! match find_a_razor() {
+//! Ok(razor) => {
+//! info!(razor; "Razor located");
+//! yak.shave(razor);
+//! break;
+//! }
+//! Err(e) => {
+//! warn!(e:err; "Unable to locate a razor, retrying");
+//! }
+//! }
+//! }
+//! }
+//! # }
+//! # #[cfg(not(feature = "kv_serde"))]
+//! # fn main() {}
+//! ```
+//!
+//! See the [`kv`] module documentation for more details.
+//!
+//! # Available logging implementations
+//!
+//! In order to produce log output executables have to use
+//! a logger implementation compatible with the facade.
+//! There are many available implementations to choose from,
+//! here are some of the most popular ones:
+//!
+//! * Simple minimal loggers:
+//! * [env_logger]
+//! * [colog]
+//! * [simple_logger]
+//! * [simplelog]
+//! * [pretty_env_logger]
+//! * [stderrlog]
+//! * [flexi_logger]
+//! * [call_logger]
+//! * [structured-logger]
+//! * Complex configurable frameworks:
+//! * [log4rs]
+//! * [fern]
+//! * Adaptors for other facilities:
+//! * [syslog]
+//! * [slog-stdlog]
+//! * [systemd-journal-logger]
+//! * [android_log]
+//! * [win_dbg_logger]
+//! * [db_logger]
+//! * [log-to-defmt]
+//! * [logcontrol-log]
+//! * For WebAssembly binaries:
+//! * [console_log]
+//! * For dynamic libraries:
+//! * You may need to construct an FFI-safe wrapper over `log` to initialize in your libraries
+//! * Utilities:
+//! * [log_err]
+//! * [log-reload]
+//!
+//! # Implementing a Logger
+//!
+//! Loggers implement the [`Log`] trait. Here's a very basic example that simply
+//! logs all messages at the [`Error`][level_link], [`Warn`][level_link] or
+//! [`Info`][level_link] levels to stdout:
+//!
+//! ```
+//! use log::{Record, Level, Metadata};
+//!
+//! struct SimpleLogger;
+//!
+//! impl log::Log for SimpleLogger {
+//! fn enabled(&self, metadata: &Metadata) -> bool {
+//! metadata.level() <= Level::Info
+//! }
+//!
+//! fn log(&self, record: &Record) {
+//! if self.enabled(record.metadata()) {
+//! println!("{} - {}", record.level(), record.args());
+//! }
+//! }
+//!
+//! fn flush(&self) {}
+//! }
+//!
+//! # fn main() {}
+//! ```
+//!
+//! Loggers are installed by calling the [`set_logger`] function. The maximum
+//! log level also needs to be adjusted via the [`set_max_level`] function. The
+//! logging facade uses this as an optimization to improve performance of log
+//! messages at levels that are disabled. It's important to set it, as it
+//! defaults to [`Off`][filter_link], so no log messages will ever be captured!
+//! In the case of our example logger, we'll want to set the maximum log level
+//! to [`Info`][filter_link], since we ignore any [`Debug`][level_link] or
+//! [`Trace`][level_link] level log messages. A logging implementation should
+//! provide a function that wraps a call to [`set_logger`] and
+//! [`set_max_level`], handling initialization of the logger:
+//!
+//! ```
+//! # use log::{Level, Metadata};
+//! # struct SimpleLogger;
+//! # impl log::Log for SimpleLogger {
+//! # fn enabled(&self, _: &Metadata) -> bool { false }
+//! # fn log(&self, _: &log::Record) {}
+//! # fn flush(&self) {}
+//! # }
+//! # fn main() {}
+//! use log::{SetLoggerError, LevelFilter};
+//!
+//! static LOGGER: SimpleLogger = SimpleLogger;
+//!
+//! pub fn init() -> Result<(), SetLoggerError> {
+//! log::set_logger(&LOGGER)
+//! .map(|()| log::set_max_level(LevelFilter::Info))
+//! }
+//! ```
+//!
+//! Implementations that adjust their configurations at runtime should take care
+//! to adjust the maximum log level as well.
+//!
+//! # Use with `std`
+//!
+//! `set_logger` requires you to provide a `&'static Log`, which can be hard to
+//! obtain if your logger depends on some runtime configuration. The
+//! `set_boxed_logger` function is available with the `std` Cargo feature. It is
+//! identical to `set_logger` except that it takes a `Box<Log>` rather than a
+//! `&'static Log`:
+//!
+//! ```
+//! # use log::{Level, LevelFilter, Log, SetLoggerError, Metadata};
+//! # struct SimpleLogger;
+//! # impl log::Log for SimpleLogger {
+//! # fn enabled(&self, _: &Metadata) -> bool { false }
+//! # fn log(&self, _: &log::Record) {}
+//! # fn flush(&self) {}
+//! # }
+//! # fn main() {}
+//! # #[cfg(feature = "std")]
+//! pub fn init() -> Result<(), SetLoggerError> {
+//! log::set_boxed_logger(Box::new(SimpleLogger))
+//! .map(|()| log::set_max_level(LevelFilter::Info))
+//! }
+//! ```
+//!
+//! # Compile time filters
+//!
+//! Log levels can be statically disabled at compile time by enabling one of these Cargo features:
+//!
+//! * `max_level_off`
+//! * `max_level_error`
+//! * `max_level_warn`
+//! * `max_level_info`
+//! * `max_level_debug`
+//! * `max_level_trace`
+//!
+//! Log invocations at disabled levels will be skipped and will not even be present in the
+//! resulting binary. These features control the value of the `STATIC_MAX_LEVEL` constant. The
+//! logging macros check this value before logging a message. By default, no levels are disabled.
+//!
+//! It is possible to override this level for release builds only with the following features:
+//!
+//! * `release_max_level_off`
+//! * `release_max_level_error`
+//! * `release_max_level_warn`
+//! * `release_max_level_info`
+//! * `release_max_level_debug`
+//! * `release_max_level_trace`
+//!
+//! Libraries should avoid using the max level features because they're global and can't be changed
+//! once they're set.
+//!
+//! For example, a crate can disable trace level logs in debug builds and trace, debug, and info
+//! level logs in release builds with the following configuration:
+//!
+//! ```toml
+//! [dependencies]
+//! log = { version = "0.4", features = ["max_level_debug", "release_max_level_warn"] }
+//! ```
+//! # Crate Feature Flags
+//!
+//! The following crate feature flags are available in addition to the filters. They are
+//! configured in your `Cargo.toml`.
+//!
+//! * `std` allows use of `std` crate instead of the default `core`. Enables using `std::error` and
+//! `set_boxed_logger` functionality.
+//! * `serde` enables support for serialization and deserialization of `Level` and `LevelFilter`.
+//!
+//! ```toml
+//! [dependencies]
+//! log = { version = "0.4", features = ["std", "serde"] }
+//! ```
+//!
+//! # Version compatibility
+//!
+//! The 0.3 and 0.4 versions of the `log` crate are almost entirely compatible. Log messages
+//! made using `log` 0.3 will forward transparently to a logger implementation using `log` 0.4. Log
+//! messages made using `log` 0.4 will forward to a logger implementation using `log` 0.3, but the
+//! module path and file name information associated with the message will unfortunately be lost.
+//!
+//! [`Log`]: trait.Log.html
+//! [level_link]: enum.Level.html
+//! [filter_link]: enum.LevelFilter.html
+//! [`set_logger`]: fn.set_logger.html
+//! [`set_max_level`]: fn.set_max_level.html
+//! [`try_set_logger_raw`]: fn.try_set_logger_raw.html
+//! [`shutdown_logger_raw`]: fn.shutdown_logger_raw.html
+//! [env_logger]: https://docs.rs/env_logger/*/env_logger/
+//! [colog]: https://docs.rs/colog/*/colog/
+//! [simple_logger]: https://github.com/borntyping/rust-simple_logger
+//! [simplelog]: https://github.com/drakulix/simplelog.rs
+//! [pretty_env_logger]: https://docs.rs/pretty_env_logger/*/pretty_env_logger/
+//! [stderrlog]: https://docs.rs/stderrlog/*/stderrlog/
+//! [flexi_logger]: https://docs.rs/flexi_logger/*/flexi_logger/
+//! [call_logger]: https://docs.rs/call_logger/*/call_logger/
+//! [syslog]: https://docs.rs/syslog/*/syslog/
+//! [slog-stdlog]: https://docs.rs/slog-stdlog/*/slog_stdlog/
+//! [log4rs]: https://docs.rs/log4rs/*/log4rs/
+//! [fern]: https://docs.rs/fern/*/fern/
+//! [systemd-journal-logger]: https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/
+//! [android_log]: https://docs.rs/android_log/*/android_log/
+//! [win_dbg_logger]: https://docs.rs/win_dbg_logger/*/win_dbg_logger/
+//! [db_logger]: https://docs.rs/db_logger/*/db_logger/
+//! [log-to-defmt]: https://docs.rs/log-to-defmt/*/log_to_defmt/
+//! [console_log]: https://docs.rs/console_log/*/console_log/
+//! [structured-logger]: https://docs.rs/structured-logger/latest/structured_logger/
+//! [logcontrol-log]: https://docs.rs/logcontrol-log/*/logcontrol_log/
+//! [log_err]: https://docs.rs/log_err/*/log_err/
+//! [log-reload]: https://docs.rs/log-reload/*/log_reload/
+
+#![doc(
+ html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
+ html_favicon_url = "https://www.rust-lang.org/favicon.ico",
+ html_root_url = "https://docs.rs/log/0.4.22"
+)]
+#![warn(missing_docs)]
+#![deny(missing_debug_implementations, unconditional_recursion)]
+#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
+
+#[cfg(any(
+ all(feature = "max_level_off", feature = "max_level_error"),
+ all(feature = "max_level_off", feature = "max_level_warn"),
+ all(feature = "max_level_off", feature = "max_level_info"),
+ all(feature = "max_level_off", feature = "max_level_debug"),
+ all(feature = "max_level_off", feature = "max_level_trace"),
+ all(feature = "max_level_error", feature = "max_level_warn"),
+ all(feature = "max_level_error", feature = "max_level_info"),
+ all(feature = "max_level_error", feature = "max_level_debug"),
+ all(feature = "max_level_error", feature = "max_level_trace"),
+ all(feature = "max_level_warn", feature = "max_level_info"),
+ all(feature = "max_level_warn", feature = "max_level_debug"),
+ all(feature = "max_level_warn", feature = "max_level_trace"),
+ all(feature = "max_level_info", feature = "max_level_debug"),
+ all(feature = "max_level_info", feature = "max_level_trace"),
+ all(feature = "max_level_debug", feature = "max_level_trace"),
+))]
+compile_error!("multiple max_level_* features set");
+
+#[rustfmt::skip]
+#[cfg(any(
+ all(feature = "release_max_level_off", feature = "release_max_level_error"),
+ all(feature = "release_max_level_off", feature = "release_max_level_warn"),
+ all(feature = "release_max_level_off", feature = "release_max_level_info"),
+ all(feature = "release_max_level_off", feature = "release_max_level_debug"),
+ all(feature = "release_max_level_off", feature = "release_max_level_trace"),
+ all(feature = "release_max_level_error", feature = "release_max_level_warn"),
+ all(feature = "release_max_level_error", feature = "release_max_level_info"),
+ all(feature = "release_max_level_error", feature = "release_max_level_debug"),
+ all(feature = "release_max_level_error", feature = "release_max_level_trace"),
+ all(feature = "release_max_level_warn", feature = "release_max_level_info"),
+ all(feature = "release_max_level_warn", feature = "release_max_level_debug"),
+ all(feature = "release_max_level_warn", feature = "release_max_level_trace"),
+ all(feature = "release_max_level_info", feature = "release_max_level_debug"),
+ all(feature = "release_max_level_info", feature = "release_max_level_trace"),
+ all(feature = "release_max_level_debug", feature = "release_max_level_trace"),
+))]
+compile_error!("multiple release_max_level_* features set");
+
+#[cfg(all(not(feature = "std"), not(test)))]
+extern crate core as std;
+
+use std::cfg;
+#[cfg(feature = "std")]
+use std::error;
+use std::str::FromStr;
+use std::{cmp, fmt, mem};
+
+#[macro_use]
+mod macros;
+mod serde;
+
+#[cfg(feature = "kv")]
+pub mod kv;
+
+#[cfg(default_log_impl)]
+extern crate once_cell;
+#[cfg(default_log_impl)]
+mod android_logger;
+
+#[cfg(target_has_atomic = "ptr")]
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+#[cfg(not(target_has_atomic = "ptr"))]
+use std::cell::Cell;
+#[cfg(not(target_has_atomic = "ptr"))]
+use std::sync::atomic::Ordering;
+
+#[cfg(not(target_has_atomic = "ptr"))]
+struct AtomicUsize {
+ v: Cell<usize>,
+}
+
+#[cfg(not(target_has_atomic = "ptr"))]
+impl AtomicUsize {
+ const fn new(v: usize) -> AtomicUsize {
+ AtomicUsize { v: Cell::new(v) }
+ }
+
+ fn load(&self, _order: Ordering) -> usize {
+ self.v.get()
+ }
+
+ fn store(&self, val: usize, _order: Ordering) {
+ self.v.set(val)
+ }
+
+ #[cfg(target_has_atomic = "ptr")]
+ fn compare_exchange(
+ &self,
+ current: usize,
+ new: usize,
+ _success: Ordering,
+ _failure: Ordering,
+ ) -> Result<usize, usize> {
+ let prev = self.v.get();
+ if current == prev {
+ self.v.set(new);
+ }
+ Ok(prev)
+ }
+}
+
+// Any platform without atomics is unlikely to have multiple cores, so
+// writing via Cell will not be a race condition.
+#[cfg(not(target_has_atomic = "ptr"))]
+unsafe impl Sync for AtomicUsize {}
+
+// The LOGGER static holds a pointer to the global logger. It is protected by
+// the STATE static which determines whether LOGGER has been initialized yet.
+static mut LOGGER: &dyn Log = &NopLogger;
+
+static STATE: AtomicUsize = AtomicUsize::new(0);
+
+// There are three different states that we care about: the logger's
+// uninitialized, the logger's initializing (set_logger's been called but
+// LOGGER hasn't actually been set yet), or the logger's active.
+const UNINITIALIZED: usize = 0;
+const INITIALIZING: usize = 1;
+const INITIALIZED: usize = 2;
+
+#[cfg(not(default_log_impl))]
+static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
+#[cfg(default_log_impl)]
+static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(5);
+
+static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
+
+static SET_LOGGER_ERROR: &str = "attempted to set a logger after the logging system \
+ was already initialized";
+static LEVEL_PARSE_ERROR: &str =
+ "attempted to convert a string that doesn't match an existing log level";
+
+/// An enum representing the available verbosity levels of the logger.
+///
+/// Typical usage includes: checking if a certain `Level` is enabled with
+/// [`log_enabled!`](macro.log_enabled.html), specifying the `Level` of
+/// [`log!`](macro.log.html), and comparing a `Level` directly to a
+/// [`LevelFilter`](enum.LevelFilter.html).
+#[repr(usize)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
+pub enum Level {
+ /// The "error" level.
+ ///
+ /// Designates very serious errors.
+ // This way these line up with the discriminants for LevelFilter below
+ // This works because Rust treats field-less enums the same way as C does:
+ // https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
+ Error = 1,
+ /// The "warn" level.
+ ///
+ /// Designates hazardous situations.
+ Warn,
+ /// The "info" level.
+ ///
+ /// Designates useful information.
+ Info,
+ /// The "debug" level.
+ ///
+ /// Designates lower priority information.
+ Debug,
+ /// The "trace" level.
+ ///
+ /// Designates very low priority, often extremely verbose, information.
+ Trace,
+}
+
+impl PartialEq<LevelFilter> for Level {
+ #[inline]
+ fn eq(&self, other: &LevelFilter) -> bool {
+ *self as usize == *other as usize
+ }
+}
+
+impl PartialOrd<LevelFilter> for Level {
+ #[inline]
+ fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
+ Some((*self as usize).cmp(&(*other as usize)))
+ }
+}
+
+impl FromStr for Level {
+ type Err = ParseLevelError;
+ fn from_str(level: &str) -> Result<Level, Self::Err> {
+ LOG_LEVEL_NAMES
+ .iter()
+ .position(|&name| name.eq_ignore_ascii_case(level))
+ .into_iter()
+ .filter(|&idx| idx != 0)
+ .map(|idx| Level::from_usize(idx).unwrap())
+ .next()
+ .ok_or(ParseLevelError(()))
+ }
+}
+
+impl fmt::Display for Level {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.pad(self.as_str())
+ }
+}
+
+impl Level {
+ fn from_usize(u: usize) -> Option<Level> {
+ match u {
+ 1 => Some(Level::Error),
+ 2 => Some(Level::Warn),
+ 3 => Some(Level::Info),
+ 4 => Some(Level::Debug),
+ 5 => Some(Level::Trace),
+ _ => None,
+ }
+ }
+
+ /// Returns the most verbose logging level.
+ #[inline]
+ pub fn max() -> Level {
+ Level::Trace
+ }
+
+ /// Converts the `Level` to the equivalent `LevelFilter`.
+ #[inline]
+ pub fn to_level_filter(&self) -> LevelFilter {
+ LevelFilter::from_usize(*self as usize).unwrap()
+ }
+
+ /// Returns the string representation of the `Level`.
+ ///
+ /// This returns the same string as the `fmt::Display` implementation.
+ pub fn as_str(&self) -> &'static str {
+ LOG_LEVEL_NAMES[*self as usize]
+ }
+
+ /// Iterate through all supported logging levels.
+ ///
+ /// The order of iteration is from more severe to less severe log messages.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use log::Level;
+ ///
+ /// let mut levels = Level::iter();
+ ///
+ /// assert_eq!(Some(Level::Error), levels.next());
+ /// assert_eq!(Some(Level::Trace), levels.last());
+ /// ```
+ pub fn iter() -> impl Iterator<Item = Self> {
+ (1..6).map(|i| Self::from_usize(i).unwrap())
+ }
+}
+
+/// An enum representing the available verbosity level filters of the logger.
+///
+/// A `LevelFilter` may be compared directly to a [`Level`]. Use this type
+/// to get and set the maximum log level with [`max_level()`] and [`set_max_level`].
+///
+/// [`Level`]: enum.Level.html
+/// [`max_level()`]: fn.max_level.html
+/// [`set_max_level`]: fn.set_max_level.html
+#[repr(usize)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
+pub enum LevelFilter {
+ /// A level lower than all log levels.
+ Off,
+ /// Corresponds to the `Error` log level.
+ Error,
+ /// Corresponds to the `Warn` log level.
+ Warn,
+ /// Corresponds to the `Info` log level.
+ Info,
+ /// Corresponds to the `Debug` log level.
+ Debug,
+ /// Corresponds to the `Trace` log level.
+ Trace,
+}
+
+impl PartialEq<Level> for LevelFilter {
+ #[inline]
+ fn eq(&self, other: &Level) -> bool {
+ other.eq(self)
+ }
+}
+
+impl PartialOrd<Level> for LevelFilter {
+ #[inline]
+ fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
+ Some((*self as usize).cmp(&(*other as usize)))
+ }
+}
+
+impl FromStr for LevelFilter {
+ type Err = ParseLevelError;
+ fn from_str(level: &str) -> Result<LevelFilter, Self::Err> {
+ LOG_LEVEL_NAMES
+ .iter()
+ .position(|&name| name.eq_ignore_ascii_case(level))
+ .map(|p| LevelFilter::from_usize(p).unwrap())
+ .ok_or(ParseLevelError(()))
+ }
+}
+
+impl fmt::Display for LevelFilter {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.pad(self.as_str())
+ }
+}
+
+impl LevelFilter {
+ fn from_usize(u: usize) -> Option<LevelFilter> {
+ match u {
+ 0 => Some(LevelFilter::Off),
+ 1 => Some(LevelFilter::Error),
+ 2 => Some(LevelFilter::Warn),
+ 3 => Some(LevelFilter::Info),
+ 4 => Some(LevelFilter::Debug),
+ 5 => Some(LevelFilter::Trace),
+ _ => None,
+ }
+ }
+
+ /// Returns the most verbose logging level filter.
+ #[inline]
+ pub fn max() -> LevelFilter {
+ LevelFilter::Trace
+ }
+
+ /// Converts `self` to the equivalent `Level`.
+ ///
+ /// Returns `None` if `self` is `LevelFilter::Off`.
+ #[inline]
+ pub fn to_level(&self) -> Option<Level> {
+ Level::from_usize(*self as usize)
+ }
+
+ /// Returns the string representation of the `LevelFilter`.
+ ///
+ /// This returns the same string as the `fmt::Display` implementation.
+ pub fn as_str(&self) -> &'static str {
+ LOG_LEVEL_NAMES[*self as usize]
+ }
+
+ /// Iterate through all supported filtering levels.
+ ///
+ /// The order of iteration is from less to more verbose filtering.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use log::LevelFilter;
+ ///
+ /// let mut levels = LevelFilter::iter();
+ ///
+ /// assert_eq!(Some(LevelFilter::Off), levels.next());
+ /// assert_eq!(Some(LevelFilter::Trace), levels.last());
+ /// ```
+ pub fn iter() -> impl Iterator<Item = Self> {
+ (0..6).map(|i| Self::from_usize(i).unwrap())
+ }
+}
+
+#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
+enum MaybeStaticStr<'a> {
+ Static(&'static str),
+ Borrowed(&'a str),
+}
+
+impl<'a> MaybeStaticStr<'a> {
+ #[inline]
+ fn get(&self) -> &'a str {
+ match *self {
+ MaybeStaticStr::Static(s) => s,
+ MaybeStaticStr::Borrowed(s) => s,
+ }
+ }
+}
+
+/// The "payload" of a log message.
+///
+/// # Use
+///
+/// `Record` structures are passed as parameters to the [`log`][method.log]
+/// method of the [`Log`] trait. Logger implementors manipulate these
+/// structures in order to display log messages. `Record`s are automatically
+/// created by the [`log!`] macro and so are not seen by log users.
+///
+/// Note that the [`level()`] and [`target()`] accessors are equivalent to
+/// `self.metadata().level()` and `self.metadata().target()` respectively.
+/// These methods are provided as a convenience for users of this structure.
+///
+/// # Example
+///
+/// The following example shows a simple logger that displays the level,
+/// module path, and message of any `Record` that is passed to it.
+///
+/// ```
+/// struct SimpleLogger;
+///
+/// impl log::Log for SimpleLogger {
+/// fn enabled(&self, _metadata: &log::Metadata) -> bool {
+/// true
+/// }
+///
+/// fn log(&self, record: &log::Record) {
+/// if !self.enabled(record.metadata()) {
+/// return;
+/// }
+///
+/// println!("{}:{} -- {}",
+/// record.level(),
+/// record.target(),
+/// record.args());
+/// }
+/// fn flush(&self) {}
+/// }
+/// ```
+///
+/// [method.log]: trait.Log.html#tymethod.log
+/// [`Log`]: trait.Log.html
+/// [`log!`]: macro.log.html
+/// [`level()`]: struct.Record.html#method.level
+/// [`target()`]: struct.Record.html#method.target
+#[derive(Clone, Debug)]
+pub struct Record<'a> {
+ metadata: Metadata<'a>,
+ args: fmt::Arguments<'a>,
+ module_path: Option<MaybeStaticStr<'a>>,
+ file: Option<MaybeStaticStr<'a>>,
+ line: Option<u32>,
+ #[cfg(feature = "kv")]
+ key_values: KeyValues<'a>,
+}
+
+// This wrapper type is only needed so we can
+// `#[derive(Debug)]` on `Record`. It also
+// provides a useful `Debug` implementation for
+// the underlying `Source`.
+#[cfg(feature = "kv")]
+#[derive(Clone)]
+struct KeyValues<'a>(&'a dyn kv::Source);
+
+#[cfg(feature = "kv")]
+impl<'a> fmt::Debug for KeyValues<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let mut visitor = f.debug_map();
+ self.0.visit(&mut visitor).map_err(|_| fmt::Error)?;
+ visitor.finish()
+ }
+}
+
+impl<'a> Record<'a> {
+ /// Returns a new builder.
+ #[inline]
+ pub fn builder() -> RecordBuilder<'a> {
+ RecordBuilder::new()
+ }
+
+ /// The message body.
+ #[inline]
+ pub fn args(&self) -> &fmt::Arguments<'a> {
+ &self.args
+ }
+
+ /// Metadata about the log directive.
+ #[inline]
+ pub fn metadata(&self) -> &Metadata<'a> {
+ &self.metadata
+ }
+
+ /// The verbosity level of the message.
+ #[inline]
+ pub fn level(&self) -> Level {
+ self.metadata.level()
+ }
+
+ /// The name of the target of the directive.
+ #[inline]
+ pub fn target(&self) -> &'a str {
+ self.metadata.target()
+ }
+
+ /// The module path of the message.
+ #[inline]
+ pub fn module_path(&self) -> Option<&'a str> {
+ self.module_path.map(|s| s.get())
+ }
+
+ /// The module path of the message, if it is a `'static` string.
+ #[inline]
+ pub fn module_path_static(&self) -> Option<&'static str> {
+ match self.module_path {
+ Some(MaybeStaticStr::Static(s)) => Some(s),
+ _ => None,
+ }
+ }
+
+ /// The source file containing the message.
+ #[inline]
+ pub fn file(&self) -> Option<&'a str> {
+ self.file.map(|s| s.get())
+ }
+
+ /// The source file containing the message, if it is a `'static` string.
+ #[inline]
+ pub fn file_static(&self) -> Option<&'static str> {
+ match self.file {
+ Some(MaybeStaticStr::Static(s)) => Some(s),
+ _ => None,
+ }
+ }
+
+ /// The line containing the message.
+ #[inline]
+ pub fn line(&self) -> Option<u32> {
+ self.line
+ }
+
+ /// The structured key-value pairs associated with the message.
+ #[cfg(feature = "kv")]
+ #[inline]
+ pub fn key_values(&self) -> &dyn kv::Source {
+ self.key_values.0
+ }
+
+ /// Create a new [`RecordBuilder`](struct.RecordBuilder.html) based on this record.
+ #[cfg(feature = "kv")]
+ #[inline]
+ pub fn to_builder(&self) -> RecordBuilder {
+ RecordBuilder {
+ record: Record {
+ metadata: Metadata {
+ level: self.metadata.level,
+ target: self.metadata.target,
+ },
+ args: self.args,
+ module_path: self.module_path,
+ file: self.file,
+ line: self.line,
+ key_values: self.key_values.clone(),
+ },
+ }
+ }
+}
+
+/// Builder for [`Record`](struct.Record.html).
+///
+/// Typically should only be used by log library creators or for testing and "shim loggers".
+/// The `RecordBuilder` can set the different parameters of `Record` object, and returns
+/// the created object when `build` is called.
+///
+/// # Examples
+///
+/// ```
+/// use log::{Level, Record};
+///
+/// let record = Record::builder()
+/// .args(format_args!("Error!"))
+/// .level(Level::Error)
+/// .target("myApp")
+/// .file(Some("server.rs"))
+/// .line(Some(144))
+/// .module_path(Some("server"))
+/// .build();
+/// ```
+///
+/// Alternatively, use [`MetadataBuilder`](struct.MetadataBuilder.html):
+///
+/// ```
+/// use log::{Record, Level, MetadataBuilder};
+///
+/// let error_metadata = MetadataBuilder::new()
+/// .target("myApp")
+/// .level(Level::Error)
+/// .build();
+///
+/// let record = Record::builder()
+/// .metadata(error_metadata)
+/// .args(format_args!("Error!"))
+/// .line(Some(433))
+/// .file(Some("app.rs"))
+/// .module_path(Some("server"))
+/// .build();
+/// ```
+#[derive(Debug)]
+pub struct RecordBuilder<'a> {
+ record: Record<'a>,
+}
+
+impl<'a> RecordBuilder<'a> {
+ /// Construct new `RecordBuilder`.
+ ///
+ /// The default options are:
+ ///
+ /// - `args`: [`format_args!("")`]
+ /// - `metadata`: [`Metadata::builder().build()`]
+ /// - `module_path`: `None`
+ /// - `file`: `None`
+ /// - `line`: `None`
+ ///
+ /// [`format_args!("")`]: https://doc.rust-lang.org/std/macro.format_args.html
+ /// [`Metadata::builder().build()`]: struct.MetadataBuilder.html#method.build
+ #[inline]
+ pub fn new() -> RecordBuilder<'a> {
+ RecordBuilder {
+ record: Record {
+ args: format_args!(""),
+ metadata: Metadata::builder().build(),
+ module_path: None,
+ file: None,
+ line: None,
+ #[cfg(feature = "kv")]
+ key_values: KeyValues(&None::<(kv::Key, kv::Value)>),
+ },
+ }
+ }
+
+ /// Set [`args`](struct.Record.html#method.args).
+ #[inline]
+ pub fn args(&mut self, args: fmt::Arguments<'a>) -> &mut RecordBuilder<'a> {
+ self.record.args = args;
+ self
+ }
+
+ /// Set [`metadata`](struct.Record.html#method.metadata). Construct a `Metadata` object with [`MetadataBuilder`](struct.MetadataBuilder.html).
+ #[inline]
+ pub fn metadata(&mut self, metadata: Metadata<'a>) -> &mut RecordBuilder<'a> {
+ self.record.metadata = metadata;
+ self
+ }
+
+ /// Set [`Metadata::level`](struct.Metadata.html#method.level).
+ #[inline]
+ pub fn level(&mut self, level: Level) -> &mut RecordBuilder<'a> {
+ self.record.metadata.level = level;
+ self
+ }
+
+ /// Set [`Metadata::target`](struct.Metadata.html#method.target)
+ #[inline]
+ pub fn target(&mut self, target: &'a str) -> &mut RecordBuilder<'a> {
+ self.record.metadata.target = target;
+ self
+ }
+
+ /// Set [`module_path`](struct.Record.html#method.module_path)
+ #[inline]
+ pub fn module_path(&mut self, path: Option<&'a str>) -> &mut RecordBuilder<'a> {
+ self.record.module_path = path.map(MaybeStaticStr::Borrowed);
+ self
+ }
+
+ /// Set [`module_path`](struct.Record.html#method.module_path) to a `'static` string
+ #[inline]
+ pub fn module_path_static(&mut self, path: Option<&'static str>) -> &mut RecordBuilder<'a> {
+ self.record.module_path = path.map(MaybeStaticStr::Static);
+ self
+ }
+
+ /// Set [`file`](struct.Record.html#method.file)
+ #[inline]
+ pub fn file(&mut self, file: Option<&'a str>) -> &mut RecordBuilder<'a> {
+ self.record.file = file.map(MaybeStaticStr::Borrowed);
+ self
+ }
+
+ /// Set [`file`](struct.Record.html#method.file) to a `'static` string.
+ #[inline]
+ pub fn file_static(&mut self, file: Option<&'static str>) -> &mut RecordBuilder<'a> {
+ self.record.file = file.map(MaybeStaticStr::Static);
+ self
+ }
+
+ /// Set [`line`](struct.Record.html#method.line)
+ #[inline]
+ pub fn line(&mut self, line: Option<u32>) -> &mut RecordBuilder<'a> {
+ self.record.line = line;
+ self
+ }
+
+ /// Set [`key_values`](struct.Record.html#method.key_values)
+ #[cfg(feature = "kv")]
+ #[inline]
+ pub fn key_values(&mut self, kvs: &'a dyn kv::Source) -> &mut RecordBuilder<'a> {
+ self.record.key_values = KeyValues(kvs);
+ self
+ }
+
+ /// Invoke the builder and return a `Record`
+ #[inline]
+ pub fn build(&self) -> Record<'a> {
+ self.record.clone()
+ }
+}
+
+impl<'a> Default for RecordBuilder<'a> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+/// Metadata about a log message.
+///
+/// # Use
+///
+/// `Metadata` structs are created when users of the library use
+/// logging macros.
+///
+/// They are consumed by implementations of the `Log` trait in the
+/// `enabled` method.
+///
+/// `Record`s use `Metadata` to determine the log message's severity
+/// and target.
+///
+/// Users should use the `log_enabled!` macro in their code to avoid
+/// constructing expensive log messages.
+///
+/// # Examples
+///
+/// ```
+/// use log::{Record, Level, Metadata};
+///
+/// struct MyLogger;
+///
+/// impl log::Log for MyLogger {
+/// fn enabled(&self, metadata: &Metadata) -> bool {
+/// metadata.level() <= Level::Info
+/// }
+///
+/// fn log(&self, record: &Record) {
+/// if self.enabled(record.metadata()) {
+/// println!("{} - {}", record.level(), record.args());
+/// }
+/// }
+/// fn flush(&self) {}
+/// }
+///
+/// # fn main(){}
+/// ```
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
+pub struct Metadata<'a> {
+ level: Level,
+ target: &'a str,
+}
+
+impl<'a> Metadata<'a> {
+ /// Returns a new builder.
+ #[inline]
+ pub fn builder() -> MetadataBuilder<'a> {
+ MetadataBuilder::new()
+ }
+
+ /// The verbosity level of the message.
+ #[inline]
+ pub fn level(&self) -> Level {
+ self.level
+ }
+
+ /// The name of the target of the directive.
+ #[inline]
+ pub fn target(&self) -> &'a str {
+ self.target
+ }
+}
+
+/// Builder for [`Metadata`](struct.Metadata.html).
+///
+/// Typically should only be used by log library creators or for testing and "shim loggers".
+/// The `MetadataBuilder` can set the different parameters of a `Metadata` object, and returns
+/// the created object when `build` is called.
+///
+/// # Example
+///
+/// ```
+/// let target = "myApp";
+/// use log::{Level, MetadataBuilder};
+/// let metadata = MetadataBuilder::new()
+/// .level(Level::Debug)
+/// .target(target)
+/// .build();
+/// ```
+#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
+pub struct MetadataBuilder<'a> {
+ metadata: Metadata<'a>,
+}
+
+impl<'a> MetadataBuilder<'a> {
+ /// Construct a new `MetadataBuilder`.
+ ///
+ /// The default options are:
+ ///
+ /// - `level`: `Level::Info`
+ /// - `target`: `""`
+ #[inline]
+ pub fn new() -> MetadataBuilder<'a> {
+ MetadataBuilder {
+ metadata: Metadata {
+ level: Level::Info,
+ target: "",
+ },
+ }
+ }
+
+ /// Setter for [`level`](struct.Metadata.html#method.level).
+ #[inline]
+ pub fn level(&mut self, arg: Level) -> &mut MetadataBuilder<'a> {
+ self.metadata.level = arg;
+ self
+ }
+
+ /// Setter for [`target`](struct.Metadata.html#method.target).
+ #[inline]
+ pub fn target(&mut self, target: &'a str) -> &mut MetadataBuilder<'a> {
+ self.metadata.target = target;
+ self
+ }
+
+ /// Returns a `Metadata` object.
+ #[inline]
+ pub fn build(&self) -> Metadata<'a> {
+ self.metadata.clone()
+ }
+}
+
+impl<'a> Default for MetadataBuilder<'a> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+/// A trait encapsulating the operations required of a logger.
+pub trait Log: Sync + Send {
+ /// Determines if a log message with the specified metadata would be
+ /// logged.
+ ///
+ /// This is used by the `log_enabled!` macro to allow callers to avoid
+ /// expensive computation of log message arguments if the message would be
+ /// discarded anyway.
+ ///
+ /// # For implementors
+ ///
+ /// This method isn't called automatically by the `log!` macros.
+ /// It's up to an implementation of the `Log` trait to call `enabled` in its own
+ /// `log` method implementation to guarantee that filtering is applied.
+ fn enabled(&self, metadata: &Metadata) -> bool;
+
+ /// Logs the `Record`.
+ ///
+ /// # For implementors
+ ///
+ /// Note that `enabled` is *not* necessarily called before this method.
+ /// Implementations of `log` should perform all necessary filtering
+ /// internally.
+ fn log(&self, record: &Record);
+
+ /// Flushes any buffered records.
+ ///
+ /// # For implementors
+ ///
+ /// This method isn't called automatically by the `log!` macros.
+ /// It can be called manually on shut-down to ensure any in-flight records are flushed.
+ fn flush(&self);
+}
+
+// Just used as a dummy initial value for LOGGER
+struct NopLogger;
+
+impl Log for NopLogger {
+ fn enabled(&self, _: &Metadata) -> bool {
+ false
+ }
+
+ fn log(&self, _: &Record) {}
+ fn flush(&self) {}
+}
+
+impl<T> Log for &'_ T
+where
+ T: ?Sized + Log,
+{
+ fn enabled(&self, metadata: &Metadata) -> bool {
+ (**self).enabled(metadata)
+ }
+
+ fn log(&self, record: &Record) {
+ (**self).log(record);
+ }
+ fn flush(&self) {
+ (**self).flush();
+ }
+}
+
+#[cfg(feature = "std")]
+impl<T> Log for std::boxed::Box<T>
+where
+ T: ?Sized + Log,
+{
+ fn enabled(&self, metadata: &Metadata) -> bool {
+ self.as_ref().enabled(metadata)
+ }
+
+ fn log(&self, record: &Record) {
+ self.as_ref().log(record);
+ }
+ fn flush(&self) {
+ self.as_ref().flush();
+ }
+}
+
+#[cfg(feature = "std")]
+impl<T> Log for std::sync::Arc<T>
+where
+ T: ?Sized + Log,
+{
+ fn enabled(&self, metadata: &Metadata) -> bool {
+ self.as_ref().enabled(metadata)
+ }
+
+ fn log(&self, record: &Record) {
+ self.as_ref().log(record);
+ }
+ fn flush(&self) {
+ self.as_ref().flush();
+ }
+}
+
+/// Sets the global maximum log level.
+///
+/// Generally, this should only be called by the active logging implementation.
+///
+/// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs.
+#[inline]
+#[cfg(target_has_atomic = "ptr")]
+pub fn set_max_level(level: LevelFilter) {
+ MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
+}
+
+/// A thread-unsafe version of [`set_max_level`].
+///
+/// This function is available on all platforms, even those that do not have
+/// support for atomics that is needed by [`set_max_level`].
+///
+/// In almost all cases, [`set_max_level`] should be preferred.
+///
+/// # Safety
+///
+/// This function is only safe to call when it cannot race with any other
+/// calls to `set_max_level` or `set_max_level_racy`.
+///
+/// This can be upheld by (for example) making sure that **there are no other
+/// threads**, and (on embedded) that **interrupts are disabled**.
+///
+/// It is safe to use all other logging functions while this function runs
+/// (including all logging macros).
+///
+/// [`set_max_level`]: fn.set_max_level.html
+#[inline]
+pub unsafe fn set_max_level_racy(level: LevelFilter) {
+ // `MAX_LOG_LEVEL_FILTER` uses a `Cell` as the underlying primitive when a
+ // platform doesn't support `target_has_atomic = "ptr"`, so even though this looks the same
+ // as `set_max_level` it may have different safety properties.
+ MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
+}
+
+/// Returns the current maximum log level.
+///
+/// The [`log!`], [`error!`], [`warn!`], [`info!`], [`debug!`], and [`trace!`] macros check
+/// this value and discard any message logged at a higher level. The maximum
+/// log level is set by the [`set_max_level`] function.
+///
+/// [`log!`]: macro.log.html
+/// [`error!`]: macro.error.html
+/// [`warn!`]: macro.warn.html
+/// [`info!`]: macro.info.html
+/// [`debug!`]: macro.debug.html
+/// [`trace!`]: macro.trace.html
+/// [`set_max_level`]: fn.set_max_level.html
+#[inline(always)]
+pub fn max_level() -> LevelFilter {
+ // Since `LevelFilter` is `repr(usize)`,
+ // this transmute is sound if and only if `MAX_LOG_LEVEL_FILTER`
+ // is set to a usize that is a valid discriminant for `LevelFilter`.
+ // Since `MAX_LOG_LEVEL_FILTER` is private, the only time it's set
+ // is by `set_max_level` above, i.e. by casting a `LevelFilter` to `usize`.
+ // So any usize stored in `MAX_LOG_LEVEL_FILTER` is a valid discriminant.
+ unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) }
+}
+
+/// Sets the global logger to a `Box<Log>`.
+///
+/// This is a simple convenience wrapper over `set_logger`, which takes a
+/// `Box<Log>` rather than a `&'static Log`. See the documentation for
+/// [`set_logger`] for more details.
+///
+/// Requires the `std` feature.
+///
+/// # Errors
+///
+/// An error is returned if a logger has already been set.
+///
+/// [`set_logger`]: fn.set_logger.html
+#[cfg(all(feature = "std", target_has_atomic = "ptr"))]
+pub fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), SetLoggerError> {
+ set_logger_inner(|| Box::leak(logger))
+}
+
+/// Sets the global logger to a `&'static Log`.
+///
+/// This function may only be called once in the lifetime of a program. Any log
+/// events that occur before the call to `set_logger` completes will be ignored.
+///
+/// This function does not typically need to be called manually. Logger
+/// implementations should provide an initialization method that installs the
+/// logger internally.
+///
+/// # Availability
+///
+/// This method is available even when the `std` feature is disabled. However,
+/// it is currently unavailable on `thumbv6` targets, which lack support for
+/// some atomic operations which are used by this function. Even on those
+/// targets, [`set_logger_racy`] will be available.
+///
+/// # Errors
+///
+/// An error is returned if a logger has already been set.
+///
+/// # Examples
+///
+/// ```
+/// use log::{error, info, warn, Record, Level, Metadata, LevelFilter};
+///
+/// static MY_LOGGER: MyLogger = MyLogger;
+///
+/// struct MyLogger;
+///
+/// impl log::Log for MyLogger {
+/// fn enabled(&self, metadata: &Metadata) -> bool {
+/// metadata.level() <= Level::Info
+/// }
+///
+/// fn log(&self, record: &Record) {
+/// if self.enabled(record.metadata()) {
+/// println!("{} - {}", record.level(), record.args());
+/// }
+/// }
+/// fn flush(&self) {}
+/// }
+///
+/// # fn main(){
+/// log::set_logger(&MY_LOGGER).unwrap();
+/// log::set_max_level(LevelFilter::Info);
+///
+/// info!("hello log");
+/// warn!("warning");
+/// error!("oops");
+/// # }
+/// ```
+///
+/// [`set_logger_racy`]: fn.set_logger_racy.html
+#[cfg(target_has_atomic = "ptr")]
+pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
+ set_logger_inner(|| logger)
+}
+
+#[cfg(target_has_atomic = "ptr")]
+fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError>
+where
+ F: FnOnce() -> &'static dyn Log,
+{
+ match STATE.compare_exchange(
+ UNINITIALIZED,
+ INITIALIZING,
+ Ordering::Acquire,
+ Ordering::Relaxed,
+ ) {
+ Ok(UNINITIALIZED) => {
+ unsafe {
+ LOGGER = make_logger();
+ }
+ STATE.store(INITIALIZED, Ordering::Release);
+ Ok(())
+ }
+ Err(INITIALIZING) => {
+ while STATE.load(Ordering::Relaxed) == INITIALIZING {
+ std::hint::spin_loop();
+ }
+ Err(SetLoggerError(()))
+ }
+ _ => Err(SetLoggerError(())),
+ }
+}
+
+/// A thread-unsafe version of [`set_logger`].
+///
+/// This function is available on all platforms, even those that do not have
+/// support for atomics that is needed by [`set_logger`].
+///
+/// In almost all cases, [`set_logger`] should be preferred.
+///
+/// # Safety
+///
+/// This function is only safe to call when it cannot race with any other
+/// calls to `set_logger` or `set_logger_racy`.
+///
+/// This can be upheld by (for example) making sure that **there are no other
+/// threads**, and (on embedded) that **interrupts are disabled**.
+///
+/// It is safe to use other logging functions while this function runs
+/// (including all logging macros).
+///
+/// [`set_logger`]: fn.set_logger.html
+pub unsafe fn set_logger_racy(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
+ match STATE.load(Ordering::Acquire) {
+ UNINITIALIZED => {
+ LOGGER = logger;
+ STATE.store(INITIALIZED, Ordering::Release);
+ Ok(())
+ }
+ INITIALIZING => {
+ // This is just plain UB, since we were racing another initialization function
+ unreachable!("set_logger_racy must not be used with other initialization functions")
+ }
+ _ => Err(SetLoggerError(())),
+ }
+}
+
+/// The type returned by [`set_logger`] if [`set_logger`] has already been called.
+///
+/// [`set_logger`]: fn.set_logger.html
+#[allow(missing_copy_implementations)]
+#[derive(Debug)]
+pub struct SetLoggerError(());
+
+impl fmt::Display for SetLoggerError {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.write_str(SET_LOGGER_ERROR)
+ }
+}
+
+// The Error trait is not available in libcore
+#[cfg(feature = "std")]
+impl error::Error for SetLoggerError {}
+
+/// The type returned by [`from_str`] when the string doesn't match any of the log levels.
+///
+/// [`from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
+#[allow(missing_copy_implementations)]
+#[derive(Debug, PartialEq, Eq)]
+pub struct ParseLevelError(());
+
+impl fmt::Display for ParseLevelError {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ fmt.write_str(LEVEL_PARSE_ERROR)
+ }
+}
+
+// The Error trait is not available in libcore
+#[cfg(feature = "std")]
+impl error::Error for ParseLevelError {}
+
+/// Returns a reference to the logger.
+///
+/// If a logger has not been set, a no-op implementation is returned.
+pub fn logger() -> &'static dyn Log {
+ // Acquire memory ordering guarantees that current thread would see any
+ // memory writes that happened before store of the value
+ // into `STATE` with memory ordering `Release` or stronger.
+ //
+ // Since the value `INITIALIZED` is written only after `LOGGER` was
+ // initialized, observing it after `Acquire` load here makes both
+ // write to the `LOGGER` static and initialization of the logger
+ // internal state synchronized with current thread.
+ if STATE.load(Ordering::Acquire) != INITIALIZED {
+ #[cfg(default_log_impl)]
+ {
+ // On Android, default to logging to logcat if not explicitly initialized. This
+ // prevents logs from being dropped by default, which may happen unexpectedly in case
+ // of using libraries from multiple linker namespaces and failing to initialize the
+ // logger in each namespace. See b/294216366#comment7.
+ use android_logger::{AndroidLogger, Config};
+ use std::sync::OnceLock;
+ static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
+ return
+ ANDROID_LOGGER.get_or_init(|| {
+ // Pass all logs down to liblog - it does its own filtering.
+ AndroidLogger::new(Config::default().with_max_level(LevelFilter::Trace))
+ });
+ }
+ static NOP: NopLogger = NopLogger;
+ &NOP
+ } else {
+ unsafe { LOGGER }
+ }
+}
+
+// WARNING: this is not part of the crate's public API and is subject to change at any time
+#[doc(hidden)]
+pub mod __private_api;
+
+/// The statically resolved maximum log level.
+///
+/// See the crate level documentation for information on how to configure this.
+///
+/// This value is checked by the log macros, but not by the `Log`ger returned by
+/// the [`logger`] function. Code that manually calls functions on that value
+/// should compare the level against this value.
+///
+/// [`logger`]: fn.logger.html
+pub const STATIC_MAX_LEVEL: LevelFilter = match cfg!(debug_assertions) {
+ false if cfg!(feature = "release_max_level_off") => LevelFilter::Off,
+ false if cfg!(feature = "release_max_level_error") => LevelFilter::Error,
+ false if cfg!(feature = "release_max_level_warn") => LevelFilter::Warn,
+ false if cfg!(feature = "release_max_level_info") => LevelFilter::Info,
+ false if cfg!(feature = "release_max_level_debug") => LevelFilter::Debug,
+ false if cfg!(feature = "release_max_level_trace") => LevelFilter::Trace,
+ _ if cfg!(feature = "max_level_off") => LevelFilter::Off,
+ _ if cfg!(feature = "max_level_error") => LevelFilter::Error,
+ _ if cfg!(feature = "max_level_warn") => LevelFilter::Warn,
+ _ if cfg!(feature = "max_level_info") => LevelFilter::Info,
+ _ if cfg!(feature = "max_level_debug") => LevelFilter::Debug,
+ _ => LevelFilter::Trace,
+};
+
+#[cfg(test)]
+mod tests {
+ use super::{Level, LevelFilter, ParseLevelError, STATIC_MAX_LEVEL};
+
+ #[test]
+ fn test_levelfilter_from_str() {
+ let tests = [
+ ("off", Ok(LevelFilter::Off)),
+ ("error", Ok(LevelFilter::Error)),
+ ("warn", Ok(LevelFilter::Warn)),
+ ("info", Ok(LevelFilter::Info)),
+ ("debug", Ok(LevelFilter::Debug)),
+ ("trace", Ok(LevelFilter::Trace)),
+ ("OFF", Ok(LevelFilter::Off)),
+ ("ERROR", Ok(LevelFilter::Error)),
+ ("WARN", Ok(LevelFilter::Warn)),
+ ("INFO", Ok(LevelFilter::Info)),
+ ("DEBUG", Ok(LevelFilter::Debug)),
+ ("TRACE", Ok(LevelFilter::Trace)),
+ ("asdf", Err(ParseLevelError(()))),
+ ];
+ for &(s, ref expected) in &tests {
+ assert_eq!(expected, &s.parse());
+ }
+ }
+
+ #[test]
+ fn test_level_from_str() {
+ let tests = [
+ ("OFF", Err(ParseLevelError(()))),
+ ("error", Ok(Level::Error)),
+ ("warn", Ok(Level::Warn)),
+ ("info", Ok(Level::Info)),
+ ("debug", Ok(Level::Debug)),
+ ("trace", Ok(Level::Trace)),
+ ("ERROR", Ok(Level::Error)),
+ ("WARN", Ok(Level::Warn)),
+ ("INFO", Ok(Level::Info)),
+ ("DEBUG", Ok(Level::Debug)),
+ ("TRACE", Ok(Level::Trace)),
+ ("asdf", Err(ParseLevelError(()))),
+ ];
+ for &(s, ref expected) in &tests {
+ assert_eq!(expected, &s.parse());
+ }
+ }
+
+ #[test]
+ fn test_level_as_str() {
+ let tests = &[
+ (Level::Error, "ERROR"),
+ (Level::Warn, "WARN"),
+ (Level::Info, "INFO"),
+ (Level::Debug, "DEBUG"),
+ (Level::Trace, "TRACE"),
+ ];
+ for (input, expected) in tests {
+ assert_eq!(*expected, input.as_str());
+ }
+ }
+
+ #[test]
+ fn test_level_show() {
+ assert_eq!("INFO", Level::Info.to_string());
+ assert_eq!("ERROR", Level::Error.to_string());
+ }
+
+ #[test]
+ fn test_levelfilter_show() {
+ assert_eq!("OFF", LevelFilter::Off.to_string());
+ assert_eq!("ERROR", LevelFilter::Error.to_string());
+ }
+
+ #[test]
+ fn test_cross_cmp() {
+ assert!(Level::Debug > LevelFilter::Error);
+ assert!(LevelFilter::Warn < Level::Trace);
+ assert!(LevelFilter::Off < Level::Error);
+ }
+
+ #[test]
+ fn test_cross_eq() {
+ assert!(Level::Error == LevelFilter::Error);
+ assert!(LevelFilter::Off != Level::Error);
+ assert!(Level::Trace == LevelFilter::Trace);
+ }
+
+ #[test]
+ fn test_to_level() {
+ assert_eq!(Some(Level::Error), LevelFilter::Error.to_level());
+ assert_eq!(None, LevelFilter::Off.to_level());
+ assert_eq!(Some(Level::Debug), LevelFilter::Debug.to_level());
+ }
+
+ #[test]
+ fn test_to_level_filter() {
+ assert_eq!(LevelFilter::Error, Level::Error.to_level_filter());
+ assert_eq!(LevelFilter::Trace, Level::Trace.to_level_filter());
+ }
+
+ #[test]
+ fn test_level_filter_as_str() {
+ let tests = &[
+ (LevelFilter::Off, "OFF"),
+ (LevelFilter::Error, "ERROR"),
+ (LevelFilter::Warn, "WARN"),
+ (LevelFilter::Info, "INFO"),
+ (LevelFilter::Debug, "DEBUG"),
+ (LevelFilter::Trace, "TRACE"),
+ ];
+ for (input, expected) in tests {
+ assert_eq!(*expected, input.as_str());
+ }
+ }
+
+ #[test]
+ #[cfg_attr(not(debug_assertions), ignore)]
+ fn test_static_max_level_debug() {
+ if cfg!(feature = "max_level_off") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
+ } else if cfg!(feature = "max_level_error") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
+ } else if cfg!(feature = "max_level_warn") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
+ } else if cfg!(feature = "max_level_info") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
+ } else if cfg!(feature = "max_level_debug") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
+ } else {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
+ }
+ }
+
+ #[test]
+ #[cfg_attr(debug_assertions, ignore)]
+ fn test_static_max_level_release() {
+ if cfg!(feature = "release_max_level_off") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
+ } else if cfg!(feature = "release_max_level_error") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
+ } else if cfg!(feature = "release_max_level_warn") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
+ } else if cfg!(feature = "release_max_level_info") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
+ } else if cfg!(feature = "release_max_level_debug") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
+ } else if cfg!(feature = "release_max_level_trace") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
+ } else if cfg!(feature = "max_level_off") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
+ } else if cfg!(feature = "max_level_error") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
+ } else if cfg!(feature = "max_level_warn") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
+ } else if cfg!(feature = "max_level_info") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
+ } else if cfg!(feature = "max_level_debug") {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
+ } else {
+ assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
+ }
+ }
+
+ #[test]
+ #[cfg(feature = "std")]
+ fn test_error_trait() {
+ use super::SetLoggerError;
+ let e = SetLoggerError(());
+ assert_eq!(
+ &e.to_string(),
+ "attempted to set a logger after the logging system \
+ was already initialized"
+ );
+ }
+
+ #[test]
+ fn test_metadata_builder() {
+ use super::MetadataBuilder;
+ let target = "myApp";
+ let metadata_test = MetadataBuilder::new()
+ .level(Level::Debug)
+ .target(target)
+ .build();
+ assert_eq!(metadata_test.level(), Level::Debug);
+ assert_eq!(metadata_test.target(), "myApp");
+ }
+
+ #[test]
+ fn test_metadata_convenience_builder() {
+ use super::Metadata;
+ let target = "myApp";
+ let metadata_test = Metadata::builder()
+ .level(Level::Debug)
+ .target(target)
+ .build();
+ assert_eq!(metadata_test.level(), Level::Debug);
+ assert_eq!(metadata_test.target(), "myApp");
+ }
+
+ #[test]
+ fn test_record_builder() {
+ use super::{MetadataBuilder, RecordBuilder};
+ let target = "myApp";
+ let metadata = MetadataBuilder::new().target(target).build();
+ let fmt_args = format_args!("hello");
+ let record_test = RecordBuilder::new()
+ .args(fmt_args)
+ .metadata(metadata)
+ .module_path(Some("foo"))
+ .file(Some("bar"))
+ .line(Some(30))
+ .build();
+ assert_eq!(record_test.metadata().target(), "myApp");
+ assert_eq!(record_test.module_path(), Some("foo"));
+ assert_eq!(record_test.file(), Some("bar"));
+ assert_eq!(record_test.line(), Some(30));
+ }
+
+ #[test]
+ fn test_record_convenience_builder() {
+ use super::{Metadata, Record};
+ let target = "myApp";
+ let metadata = Metadata::builder().target(target).build();
+ let fmt_args = format_args!("hello");
+ let record_test = Record::builder()
+ .args(fmt_args)
+ .metadata(metadata)
+ .module_path(Some("foo"))
+ .file(Some("bar"))
+ .line(Some(30))
+ .build();
+ assert_eq!(record_test.target(), "myApp");
+ assert_eq!(record_test.module_path(), Some("foo"));
+ assert_eq!(record_test.file(), Some("bar"));
+ assert_eq!(record_test.line(), Some(30));
+ }
+
+ #[test]
+ fn test_record_complete_builder() {
+ use super::{Level, Record};
+ let target = "myApp";
+ let record_test = Record::builder()
+ .module_path(Some("foo"))
+ .file(Some("bar"))
+ .line(Some(30))
+ .target(target)
+ .level(Level::Error)
+ .build();
+ assert_eq!(record_test.target(), "myApp");
+ assert_eq!(record_test.level(), Level::Error);
+ assert_eq!(record_test.module_path(), Some("foo"));
+ assert_eq!(record_test.file(), Some("bar"));
+ assert_eq!(record_test.line(), Some(30));
+ }
+
+ #[test]
+ #[cfg(feature = "kv")]
+ fn test_record_key_values_builder() {
+ use super::Record;
+ use crate::kv::{self, VisitSource};
+
+ struct TestVisitSource {
+ seen_pairs: usize,
+ }
+
+ impl<'kvs> VisitSource<'kvs> for TestVisitSource {
+ fn visit_pair(
+ &mut self,
+ _: kv::Key<'kvs>,
+ _: kv::Value<'kvs>,
+ ) -> Result<(), kv::Error> {
+ self.seen_pairs += 1;
+ Ok(())
+ }
+ }
+
+ let kvs: &[(&str, i32)] = &[("a", 1), ("b", 2)];
+ let record_test = Record::builder().key_values(&kvs).build();
+
+ let mut visitor = TestVisitSource { seen_pairs: 0 };
+
+ record_test.key_values().visit(&mut visitor).unwrap();
+
+ assert_eq!(2, visitor.seen_pairs);
+ }
+
+ #[test]
+ #[cfg(feature = "kv")]
+ fn test_record_key_values_get_coerce() {
+ use super::Record;
+
+ let kvs: &[(&str, &str)] = &[("a", "1"), ("b", "2")];
+ let record = Record::builder().key_values(&kvs).build();
+
+ assert_eq!(
+ "2",
+ record
+ .key_values()
+ .get("b".into())
+ .expect("missing key")
+ .to_borrowed_str()
+ .expect("invalid value")
+ );
+ }
+
+ // Test that the `impl Log for Foo` blocks work
+ // This test mostly operates on a type level, so failures will be compile errors
+ #[test]
+ fn test_foreign_impl() {
+ use super::Log;
+ #[cfg(feature = "std")]
+ use std::sync::Arc;
+
+ fn assert_is_log<T: Log + ?Sized>() {}
+
+ assert_is_log::<&dyn Log>();
+
+ #[cfg(feature = "std")]
+ assert_is_log::<Box<dyn Log>>();
+
+ #[cfg(feature = "std")]
+ assert_is_log::<Arc<dyn Log>>();
+
+ // Assert these statements for all T: Log + ?Sized
+ #[allow(unused)]
+ fn forall<T: Log + ?Sized>() {
+ #[cfg(feature = "std")]
+ assert_is_log::<Box<T>>();
+
+ assert_is_log::<&T>();
+
+ #[cfg(feature = "std")]
+ assert_is_log::<Arc<T>>();
+ }
+ }
+}
diff --git a/crates/log/src/macros.rs b/crates/log/src/macros.rs
index f214d0d..87693f2 100644
--- a/crates/log/src/macros.rs
+++ b/crates/log/src/macros.rs
@@ -1,280 +1,367 @@
-// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-/// The standard logging macro.
-///
-/// This macro will generically log with the specified `Level` and `format!`
-/// based argument list.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::{log, Level};
-///
-/// # fn main() {
-/// let data = (42, "Forty-two");
-/// let private_data = "private";
-///
-/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
-/// log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
-/// data.0, data.1, private_data);
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! log {
- // log!(target: "my_target", Level::Info; key1 = 42, key2 = true; "a {} event", "log");
- (target: $target:expr, $lvl:expr, $($key:tt = $value:expr),+; $($arg:tt)+) => ({
- let lvl = $lvl;
- if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
- $crate::__private_api_log(
- __log_format_args!($($arg)+),
- lvl,
- &($target, __log_module_path!(), __log_file!(), __log_line!()),
- $crate::__private_api::Option::Some(&[$((__log_key!($key), &$value)),+])
- );
- }
- });
-
- // log!(target: "my_target", Level::Info; "a {} event", "log");
- (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
- let lvl = $lvl;
- if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
- $crate::__private_api_log(
- __log_format_args!($($arg)+),
- lvl,
- &($target, __log_module_path!(), __log_file!(), __log_line!()),
- $crate::__private_api::Option::None,
- );
- }
- });
-
- // log!(Level::Info, "a log event")
- ($lvl:expr, $($arg:tt)+) => (log!(target: __log_module_path!(), $lvl, $($arg)+));
-}
-
-/// Logs a message at the error level.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::error;
-///
-/// # fn main() {
-/// let (err_info, port) = ("No connection", 22);
-///
-/// error!("Error: {} on port {}", err_info, port);
-/// error!(target: "app_events", "App Error: {}, Port: {}", err_info, 22);
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! error {
- // error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
- // error!(target: "my_target", "a {} event", "log")
- (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Error, $($arg)+));
-
- // error!("a {} event", "log")
- ($($arg:tt)+) => (log!($crate::Level::Error, $($arg)+))
-}
-
-/// Logs a message at the warn level.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::warn;
-///
-/// # fn main() {
-/// let warn_description = "Invalid Input";
-///
-/// warn!("Warning! {}!", warn_description);
-/// warn!(target: "input_events", "App received warning: {}", warn_description);
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! warn {
- // warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
- // warn!(target: "my_target", "a {} event", "log")
- (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Warn, $($arg)+));
-
- // warn!("a {} event", "log")
- ($($arg:tt)+) => (log!($crate::Level::Warn, $($arg)+))
-}
-
-/// Logs a message at the info level.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::info;
-///
-/// # fn main() {
-/// # struct Connection { port: u32, speed: f32 }
-/// let conn_info = Connection { port: 40, speed: 3.20 };
-///
-/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
-/// info!(target: "connection_events", "Successfull connection, port: {}, speed: {}",
-/// conn_info.port, conn_info.speed);
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! info {
- // info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
- // info!(target: "my_target", "a {} event", "log")
- (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Info, $($arg)+));
-
- // info!("a {} event", "log")
- ($($arg:tt)+) => (log!($crate::Level::Info, $($arg)+))
-}
-
-/// Logs a message at the debug level.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::debug;
-///
-/// # fn main() {
-/// # struct Position { x: f32, y: f32 }
-/// let pos = Position { x: 3.234, y: -1.223 };
-///
-/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
-/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! debug {
- // debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
- // debug!(target: "my_target", "a {} event", "log")
- (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Debug, $($arg)+));
-
- // debug!("a {} event", "log")
- ($($arg:tt)+) => (log!($crate::Level::Debug, $($arg)+))
-}
-
-/// Logs a message at the trace level.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::trace;
-///
-/// # fn main() {
-/// # struct Position { x: f32, y: f32 }
-/// let pos = Position { x: 3.234, y: -1.223 };
-///
-/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
-/// trace!(target: "app_events", "x is {} and y is {}",
-/// if pos.x >= 0.0 { "positive" } else { "negative" },
-/// if pos.y >= 0.0 { "positive" } else { "negative" });
-/// # }
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! trace {
- // trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
- // trace!(target: "my_target", "a {} event", "log")
- (target: $target:expr, $($arg:tt)+) => (log!(target: $target, $crate::Level::Trace, $($arg)+));
-
- // trace!("a {} event", "log")
- ($($arg:tt)+) => (log!($crate::Level::Trace, $($arg)+))
-}
-
-/// Determines if a message logged at the specified level in that module will
-/// be logged.
-///
-/// This can be used to avoid expensive computation of log message arguments if
-/// the message would be ignored anyway.
-///
-/// # Examples
-///
-/// ```edition2018
-/// use log::Level::Debug;
-/// use log::{debug, log_enabled};
-///
-/// # fn foo() {
-/// if log_enabled!(Debug) {
-/// let data = expensive_call();
-/// debug!("expensive debug data: {} {}", data.x, data.y);
-/// }
-/// if log_enabled!(target: "Global", Debug) {
-/// let data = expensive_call();
-/// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
-/// }
-/// # }
-/// # struct Data { x: u32, y: u32 }
-/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
-/// # fn main() {}
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! log_enabled {
- (target: $target:expr, $lvl:expr) => {{
- let lvl = $lvl;
- lvl <= $crate::STATIC_MAX_LEVEL
- && lvl <= $crate::max_level()
- && $crate::__private_api_enabled(lvl, $target)
- }};
- ($lvl:expr) => {
- log_enabled!(target: __log_module_path!(), $lvl)
- };
-}
-
-// The log macro above cannot invoke format_args directly because it uses
-// local_inner_macros. A format_args invocation there would resolve to
-// $crate::format_args which does not exist. Instead invoke format_args here
-// outside of local_inner_macros so that it resolves (probably) to
-// core::format_args or std::format_args. Same for the several macros that
-// follow.
-//
-// This is a workaround until we drop support for pre-1.30 compilers. At that
-// point we can remove use of local_inner_macros, use $crate:: when invoking
-// local macros, and invoke format_args directly.
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __log_format_args {
- ($($args:tt)*) => {
- format_args!($($args)*)
- };
-}
-
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __log_module_path {
- () => {
- module_path!()
- };
-}
-
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __log_file {
- () => {
- file!()
- };
-}
-
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __log_line {
- () => {
- line!()
- };
-}
-
-#[doc(hidden)]
-#[macro_export]
-macro_rules! __log_key {
- // key1 = 42
- ($($args:ident)*) => {
- stringify!($($args)*)
- };
- // "key1" = 42
- ($($args:expr)*) => {
- $($args)*
- };
-}
+// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/// The standard logging macro.
+///
+/// This macro will generically log with the specified `Level` and `format!`
+/// based argument list.
+///
+/// # Examples
+///
+/// ```
+/// use log::{log, Level};
+///
+/// # fn main() {
+/// let data = (42, "Forty-two");
+/// let private_data = "private";
+///
+/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
+/// log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
+/// data.0, data.1, private_data);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! log {
+ // log!(target: "my_target", Level::Info, key1:? = 42, key2 = true; "a {} event", "log");
+ (target: $target:expr, $lvl:expr, $($key:tt $(:$capture:tt)? $(= $value:expr)?),+; $($arg:tt)+) => ({
+ let lvl = $lvl;
+ if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
+ $crate::__private_api::log::<&_>(
+ $crate::__private_api::format_args!($($arg)+),
+ lvl,
+ &($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()),
+ &[$(($crate::__log_key!($key), $crate::__log_value!($key $(:$capture)* = $($value)*))),+]
+ );
+ }
+ });
+
+ // log!(target: "my_target", Level::Info, "a {} event", "log");
+ (target: $target:expr, $lvl:expr, $($arg:tt)+) => ({
+ let lvl = $lvl;
+ if lvl <= $crate::STATIC_MAX_LEVEL && lvl <= $crate::max_level() {
+ $crate::__private_api::log(
+ $crate::__private_api::format_args!($($arg)+),
+ lvl,
+ &($target, $crate::__private_api::module_path!(), $crate::__private_api::loc()),
+ (),
+ );
+ }
+ });
+
+ // log!(Level::Info, "a log event")
+ ($lvl:expr, $($arg:tt)+) => ($crate::log!(target: $crate::__private_api::module_path!(), $lvl, $($arg)+));
+}
+
+/// Logs a message at the error level.
+///
+/// # Examples
+///
+/// ```
+/// use log::error;
+///
+/// # fn main() {
+/// let (err_info, port) = ("No connection", 22);
+///
+/// error!("Error: {err_info} on port {port}");
+/// error!(target: "app_events", "App Error: {err_info}, Port: {port}");
+/// # }
+/// ```
+#[macro_export]
+macro_rules! error {
+ // error!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
+ // error!(target: "my_target", "a {} event", "log")
+ (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Error, $($arg)+));
+
+ // error!("a {} event", "log")
+ ($($arg:tt)+) => ($crate::log!($crate::Level::Error, $($arg)+))
+}
+
+/// Logs a message at the warn level.
+///
+/// # Examples
+///
+/// ```
+/// use log::warn;
+///
+/// # fn main() {
+/// let warn_description = "Invalid Input";
+///
+/// warn!("Warning! {warn_description}!");
+/// warn!(target: "input_events", "App received warning: {warn_description}");
+/// # }
+/// ```
+#[macro_export]
+macro_rules! warn {
+ // warn!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
+ // warn!(target: "my_target", "a {} event", "log")
+ (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Warn, $($arg)+));
+
+ // warn!("a {} event", "log")
+ ($($arg:tt)+) => ($crate::log!($crate::Level::Warn, $($arg)+))
+}
+
+/// Logs a message at the info level.
+///
+/// # Examples
+///
+/// ```
+/// use log::info;
+///
+/// # fn main() {
+/// # struct Connection { port: u32, speed: f32 }
+/// let conn_info = Connection { port: 40, speed: 3.20 };
+///
+/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
+/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
+/// conn_info.port, conn_info.speed);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! info {
+ // info!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
+ // info!(target: "my_target", "a {} event", "log")
+ (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Info, $($arg)+));
+
+ // info!("a {} event", "log")
+ ($($arg:tt)+) => ($crate::log!($crate::Level::Info, $($arg)+))
+}
+
+/// Logs a message at the debug level.
+///
+/// # Examples
+///
+/// ```
+/// use log::debug;
+///
+/// # fn main() {
+/// # struct Position { x: f32, y: f32 }
+/// let pos = Position { x: 3.234, y: -1.223 };
+///
+/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
+/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
+/// # }
+/// ```
+#[macro_export]
+macro_rules! debug {
+ // debug!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
+ // debug!(target: "my_target", "a {} event", "log")
+ (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Debug, $($arg)+));
+
+ // debug!("a {} event", "log")
+ ($($arg:tt)+) => ($crate::log!($crate::Level::Debug, $($arg)+))
+}
+
+/// Logs a message at the trace level.
+///
+/// # Examples
+///
+/// ```
+/// use log::trace;
+///
+/// # fn main() {
+/// # struct Position { x: f32, y: f32 }
+/// let pos = Position { x: 3.234, y: -1.223 };
+///
+/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
+/// trace!(target: "app_events", "x is {} and y is {}",
+/// if pos.x >= 0.0 { "positive" } else { "negative" },
+/// if pos.y >= 0.0 { "positive" } else { "negative" });
+/// # }
+/// ```
+#[macro_export]
+macro_rules! trace {
+ // trace!(target: "my_target", key1 = 42, key2 = true; "a {} event", "log")
+ // trace!(target: "my_target", "a {} event", "log")
+ (target: $target:expr, $($arg:tt)+) => ($crate::log!(target: $target, $crate::Level::Trace, $($arg)+));
+
+ // trace!("a {} event", "log")
+ ($($arg:tt)+) => ($crate::log!($crate::Level::Trace, $($arg)+))
+}
+
+/// Determines if a message logged at the specified level in that module will
+/// be logged.
+///
+/// This can be used to avoid expensive computation of log message arguments if
+/// the message would be ignored anyway.
+///
+/// # Examples
+///
+/// ```
+/// use log::Level::Debug;
+/// use log::{debug, log_enabled};
+///
+/// # fn foo() {
+/// if log_enabled!(Debug) {
+/// let data = expensive_call();
+/// debug!("expensive debug data: {} {}", data.x, data.y);
+/// }
+/// if log_enabled!(target: "Global", Debug) {
+/// let data = expensive_call();
+/// debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
+/// }
+/// # }
+/// # struct Data { x: u32, y: u32 }
+/// # fn expensive_call() -> Data { Data { x: 0, y: 0 } }
+/// # fn main() {}
+/// ```
+#[macro_export]
+macro_rules! log_enabled {
+ (target: $target:expr, $lvl:expr) => {{
+ let lvl = $lvl;
+ lvl <= $crate::STATIC_MAX_LEVEL
+ && lvl <= $crate::max_level()
+ && $crate::__private_api::enabled(lvl, $target)
+ }};
+ ($lvl:expr) => {
+ $crate::log_enabled!(target: $crate::__private_api::module_path!(), $lvl)
+ };
+}
+
+// These macros use a pattern of #[cfg]s to produce nicer error
+// messages when log features aren't available
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(feature = "kv")]
+macro_rules! __log_key {
+ // key1 = 42
+ ($($args:ident)*) => {
+ $crate::__private_api::stringify!($($args)*)
+ };
+ // "key1" = 42
+ ($($args:expr)*) => {
+ $($args)*
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(not(feature = "kv"))]
+macro_rules! __log_key {
+ ($($args:tt)*) => {
+ compile_error!("key value support requires the `kv` feature of `log`")
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(feature = "kv")]
+macro_rules! __log_value {
+ // Entrypoint
+ ($key:tt = $args:expr) => {
+ $crate::__log_value!(($args):value)
+ };
+ ($key:tt :$capture:tt = $args:expr) => {
+ $crate::__log_value!(($args):$capture)
+ };
+ ($key:ident =) => {
+ $crate::__log_value!(($key):value)
+ };
+ ($key:ident :$capture:tt =) => {
+ $crate::__log_value!(($key):$capture)
+ };
+ // ToValue
+ (($args:expr):value) => {
+ $crate::__private_api::capture_to_value(&&$args)
+ };
+ // Debug
+ (($args:expr):?) => {
+ $crate::__private_api::capture_debug(&&$args)
+ };
+ (($args:expr):debug) => {
+ $crate::__private_api::capture_debug(&&$args)
+ };
+ // Display
+ (($args:expr):%) => {
+ $crate::__private_api::capture_display(&&$args)
+ };
+ (($args:expr):display) => {
+ $crate::__private_api::capture_display(&&$args)
+ };
+ //Error
+ (($args:expr):err) => {
+ $crate::__log_value_error!($args)
+ };
+ // sval::Value
+ (($args:expr):sval) => {
+ $crate::__log_value_sval!($args)
+ };
+ // serde::Serialize
+ (($args:expr):serde) => {
+ $crate::__log_value_serde!($args)
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(not(feature = "kv"))]
+macro_rules! __log_value {
+ ($($args:tt)*) => {
+ compile_error!("key value support requires the `kv` feature of `log`")
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(feature = "kv_sval")]
+macro_rules! __log_value_sval {
+ ($args:expr) => {
+ $crate::__private_api::capture_sval(&&$args)
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(not(feature = "kv_sval"))]
+macro_rules! __log_value_sval {
+ ($args:expr) => {
+ compile_error!("capturing values as `sval::Value` requites the `kv_sval` feature of `log`")
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(feature = "kv_serde")]
+macro_rules! __log_value_serde {
+ ($args:expr) => {
+ $crate::__private_api::capture_serde(&&$args)
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(not(feature = "kv_serde"))]
+macro_rules! __log_value_serde {
+ ($args:expr) => {
+ compile_error!(
+ "capturing values as `serde::Serialize` requites the `kv_serde` feature of `log`"
+ )
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(feature = "kv_std")]
+macro_rules! __log_value_error {
+ ($args:expr) => {
+ $crate::__private_api::capture_error(&$args)
+ };
+}
+
+#[doc(hidden)]
+#[macro_export]
+#[cfg(not(feature = "kv_std"))]
+macro_rules! __log_value_error {
+ ($args:expr) => {
+ compile_error!(
+ "capturing values as `std::error::Error` requites the `kv_std` feature of `log`"
+ )
+ };
+}
diff --git a/crates/log/src/serde.rs b/crates/log/src/serde.rs
index e691321..63bef7f 100644
--- a/crates/log/src/serde.rs
+++ b/crates/log/src/serde.rs
@@ -1,400 +1,397 @@
-#![cfg(feature = "serde")]
-
-extern crate serde;
-use self::serde::de::{
- Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, Unexpected, VariantAccess,
- Visitor,
-};
-use self::serde::ser::{Serialize, Serializer};
-
-use {Level, LevelFilter, LOG_LEVEL_NAMES};
-
-use std::fmt;
-use std::str::{self, FromStr};
-
-// The Deserialize impls are handwritten to be case insensitive using FromStr.
-
-impl Serialize for Level {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- match *self {
- Level::Error => serializer.serialize_unit_variant("Level", 0, "ERROR"),
- Level::Warn => serializer.serialize_unit_variant("Level", 1, "WARN"),
- Level::Info => serializer.serialize_unit_variant("Level", 2, "INFO"),
- Level::Debug => serializer.serialize_unit_variant("Level", 3, "DEBUG"),
- Level::Trace => serializer.serialize_unit_variant("Level", 4, "TRACE"),
- }
- }
-}
-
-impl<'de> Deserialize<'de> for Level {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- struct LevelIdentifier;
-
- impl<'de> Visitor<'de> for LevelIdentifier {
- type Value = Level;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("log level")
- }
-
- fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
- where
- E: Error,
- {
- // Case insensitive.
- FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES[1..]))
- }
-
- fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
- where
- E: Error,
- {
- let variant = str::from_utf8(value)
- .map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
-
- self.visit_str(variant)
- }
-
- fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
- where
- E: Error,
- {
- let variant = LOG_LEVEL_NAMES[1..]
- .get(v as usize)
- .ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
-
- self.visit_str(variant)
- }
- }
-
- impl<'de> DeserializeSeed<'de> for LevelIdentifier {
- type Value = Level;
-
- fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
- where
- D: Deserializer<'de>,
- {
- deserializer.deserialize_identifier(LevelIdentifier)
- }
- }
-
- struct LevelEnum;
-
- impl<'de> Visitor<'de> for LevelEnum {
- type Value = Level;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("log level")
- }
-
- fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
- where
- A: EnumAccess<'de>,
- {
- let (level, variant) = value.variant_seed(LevelIdentifier)?;
- // Every variant is a unit variant.
- variant.unit_variant()?;
- Ok(level)
- }
- }
-
- deserializer.deserialize_enum("Level", &LOG_LEVEL_NAMES[1..], LevelEnum)
- }
-}
-
-impl Serialize for LevelFilter {
- fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- where
- S: Serializer,
- {
- match *self {
- LevelFilter::Off => serializer.serialize_unit_variant("LevelFilter", 0, "OFF"),
- LevelFilter::Error => serializer.serialize_unit_variant("LevelFilter", 1, "ERROR"),
- LevelFilter::Warn => serializer.serialize_unit_variant("LevelFilter", 2, "WARN"),
- LevelFilter::Info => serializer.serialize_unit_variant("LevelFilter", 3, "INFO"),
- LevelFilter::Debug => serializer.serialize_unit_variant("LevelFilter", 4, "DEBUG"),
- LevelFilter::Trace => serializer.serialize_unit_variant("LevelFilter", 5, "TRACE"),
- }
- }
-}
-
-impl<'de> Deserialize<'de> for LevelFilter {
- fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
- where
- D: Deserializer<'de>,
- {
- struct LevelFilterIdentifier;
-
- impl<'de> Visitor<'de> for LevelFilterIdentifier {
- type Value = LevelFilter;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("log level filter")
- }
-
- fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
- where
- E: Error,
- {
- // Case insensitive.
- FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES))
- }
-
- fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
- where
- E: Error,
- {
- let variant = str::from_utf8(value)
- .map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
-
- self.visit_str(variant)
- }
-
- fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
- where
- E: Error,
- {
- let variant = LOG_LEVEL_NAMES
- .get(v as usize)
- .ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
-
- self.visit_str(variant)
- }
- }
-
- impl<'de> DeserializeSeed<'de> for LevelFilterIdentifier {
- type Value = LevelFilter;
-
- fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
- where
- D: Deserializer<'de>,
- {
- deserializer.deserialize_identifier(LevelFilterIdentifier)
- }
- }
-
- struct LevelFilterEnum;
-
- impl<'de> Visitor<'de> for LevelFilterEnum {
- type Value = LevelFilter;
-
- fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
- formatter.write_str("log level filter")
- }
-
- fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
- where
- A: EnumAccess<'de>,
- {
- let (level_filter, variant) = value.variant_seed(LevelFilterIdentifier)?;
- // Every variant is a unit variant.
- variant.unit_variant()?;
- Ok(level_filter)
- }
- }
-
- deserializer.deserialize_enum("LevelFilter", &LOG_LEVEL_NAMES, LevelFilterEnum)
- }
-}
-
-#[cfg(test)]
-mod tests {
- extern crate serde_test;
- use self::serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};
-
- use {Level, LevelFilter};
-
- fn level_token(variant: &'static str) -> Token {
- Token::UnitVariant {
- name: "Level",
- variant: variant,
- }
- }
-
- fn level_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
- [
- Token::Enum { name: "Level" },
- Token::Bytes(variant),
- Token::Unit,
- ]
- }
-
- fn level_variant_tokens(variant: u32) -> [Token; 3] {
- [
- Token::Enum { name: "Level" },
- Token::U32(variant),
- Token::Unit,
- ]
- }
-
- fn level_filter_token(variant: &'static str) -> Token {
- Token::UnitVariant {
- name: "LevelFilter",
- variant: variant,
- }
- }
-
- fn level_filter_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
- [
- Token::Enum {
- name: "LevelFilter",
- },
- Token::Bytes(variant),
- Token::Unit,
- ]
- }
-
- fn level_filter_variant_tokens(variant: u32) -> [Token; 3] {
- [
- Token::Enum {
- name: "LevelFilter",
- },
- Token::U32(variant),
- Token::Unit,
- ]
- }
-
- #[test]
- fn test_level_ser_de() {
- let cases = [
- (Level::Error, [level_token("ERROR")]),
- (Level::Warn, [level_token("WARN")]),
- (Level::Info, [level_token("INFO")]),
- (Level::Debug, [level_token("DEBUG")]),
- (Level::Trace, [level_token("TRACE")]),
- ];
-
- for &(s, expected) in &cases {
- assert_tokens(&s, &expected);
- }
- }
-
- #[test]
- fn test_level_case_insensitive() {
- let cases = [
- (Level::Error, [level_token("error")]),
- (Level::Warn, [level_token("warn")]),
- (Level::Info, [level_token("info")]),
- (Level::Debug, [level_token("debug")]),
- (Level::Trace, [level_token("trace")]),
- ];
-
- for &(s, expected) in &cases {
- assert_de_tokens(&s, &expected);
- }
- }
-
- #[test]
- fn test_level_de_bytes() {
- let cases = [
- (Level::Error, level_bytes_tokens(b"ERROR")),
- (Level::Warn, level_bytes_tokens(b"WARN")),
- (Level::Info, level_bytes_tokens(b"INFO")),
- (Level::Debug, level_bytes_tokens(b"DEBUG")),
- (Level::Trace, level_bytes_tokens(b"TRACE")),
- ];
-
- for &(value, tokens) in &cases {
- assert_de_tokens(&value, &tokens);
- }
- }
-
- #[test]
- fn test_level_de_variant_index() {
- let cases = [
- (Level::Error, level_variant_tokens(0)),
- (Level::Warn, level_variant_tokens(1)),
- (Level::Info, level_variant_tokens(2)),
- (Level::Debug, level_variant_tokens(3)),
- (Level::Trace, level_variant_tokens(4)),
- ];
-
- for &(value, tokens) in &cases {
- assert_de_tokens(&value, &tokens);
- }
- }
-
- #[test]
- fn test_level_de_error() {
- let msg = "unknown variant `errorx`, expected one of \
- `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
- assert_de_tokens_error::<Level>(&[level_token("errorx")], msg);
- }
-
- #[test]
- fn test_level_filter_ser_de() {
- let cases = [
- (LevelFilter::Off, [level_filter_token("OFF")]),
- (LevelFilter::Error, [level_filter_token("ERROR")]),
- (LevelFilter::Warn, [level_filter_token("WARN")]),
- (LevelFilter::Info, [level_filter_token("INFO")]),
- (LevelFilter::Debug, [level_filter_token("DEBUG")]),
- (LevelFilter::Trace, [level_filter_token("TRACE")]),
- ];
-
- for &(s, expected) in &cases {
- assert_tokens(&s, &expected);
- }
- }
-
- #[test]
- fn test_level_filter_case_insensitive() {
- let cases = [
- (LevelFilter::Off, [level_filter_token("off")]),
- (LevelFilter::Error, [level_filter_token("error")]),
- (LevelFilter::Warn, [level_filter_token("warn")]),
- (LevelFilter::Info, [level_filter_token("info")]),
- (LevelFilter::Debug, [level_filter_token("debug")]),
- (LevelFilter::Trace, [level_filter_token("trace")]),
- ];
-
- for &(s, expected) in &cases {
- assert_de_tokens(&s, &expected);
- }
- }
-
- #[test]
- fn test_level_filter_de_bytes() {
- let cases = [
- (LevelFilter::Off, level_filter_bytes_tokens(b"OFF")),
- (LevelFilter::Error, level_filter_bytes_tokens(b"ERROR")),
- (LevelFilter::Warn, level_filter_bytes_tokens(b"WARN")),
- (LevelFilter::Info, level_filter_bytes_tokens(b"INFO")),
- (LevelFilter::Debug, level_filter_bytes_tokens(b"DEBUG")),
- (LevelFilter::Trace, level_filter_bytes_tokens(b"TRACE")),
- ];
-
- for &(value, tokens) in &cases {
- assert_de_tokens(&value, &tokens);
- }
- }
-
- #[test]
- fn test_level_filter_de_variant_index() {
- let cases = [
- (LevelFilter::Off, level_filter_variant_tokens(0)),
- (LevelFilter::Error, level_filter_variant_tokens(1)),
- (LevelFilter::Warn, level_filter_variant_tokens(2)),
- (LevelFilter::Info, level_filter_variant_tokens(3)),
- (LevelFilter::Debug, level_filter_variant_tokens(4)),
- (LevelFilter::Trace, level_filter_variant_tokens(5)),
- ];
-
- for &(value, tokens) in &cases {
- assert_de_tokens(&value, &tokens);
- }
- }
-
- #[test]
- fn test_level_filter_de_error() {
- let msg = "unknown variant `errorx`, expected one of \
- `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
- assert_de_tokens_error::<LevelFilter>(&[level_filter_token("errorx")], msg);
- }
-}
+#![cfg(feature = "serde")]
+
+use serde::de::{
+ Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, Unexpected, VariantAccess,
+ Visitor,
+};
+use serde::ser::{Serialize, Serializer};
+
+use crate::{Level, LevelFilter, LOG_LEVEL_NAMES};
+
+use std::fmt;
+use std::str::{self, FromStr};
+
+// The Deserialize impls are handwritten to be case insensitive using FromStr.
+
+impl Serialize for Level {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match *self {
+ Level::Error => serializer.serialize_unit_variant("Level", 0, "ERROR"),
+ Level::Warn => serializer.serialize_unit_variant("Level", 1, "WARN"),
+ Level::Info => serializer.serialize_unit_variant("Level", 2, "INFO"),
+ Level::Debug => serializer.serialize_unit_variant("Level", 3, "DEBUG"),
+ Level::Trace => serializer.serialize_unit_variant("Level", 4, "TRACE"),
+ }
+ }
+}
+
+impl<'de> Deserialize<'de> for Level {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct LevelIdentifier;
+
+ impl<'de> Visitor<'de> for LevelIdentifier {
+ type Value = Level;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("log level")
+ }
+
+ fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let variant = LOG_LEVEL_NAMES[1..]
+ .get(v as usize)
+ .ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
+
+ self.visit_str(variant)
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ // Case insensitive.
+ FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES[1..]))
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let variant = str::from_utf8(value)
+ .map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
+
+ self.visit_str(variant)
+ }
+ }
+
+ impl<'de> DeserializeSeed<'de> for LevelIdentifier {
+ type Value = Level;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_identifier(LevelIdentifier)
+ }
+ }
+
+ struct LevelEnum;
+
+ impl<'de> Visitor<'de> for LevelEnum {
+ type Value = Level;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("log level")
+ }
+
+ fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ let (level, variant) = value.variant_seed(LevelIdentifier)?;
+ // Every variant is a unit variant.
+ variant.unit_variant()?;
+ Ok(level)
+ }
+ }
+
+ deserializer.deserialize_enum("Level", &LOG_LEVEL_NAMES[1..], LevelEnum)
+ }
+}
+
+impl Serialize for LevelFilter {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ match *self {
+ LevelFilter::Off => serializer.serialize_unit_variant("LevelFilter", 0, "OFF"),
+ LevelFilter::Error => serializer.serialize_unit_variant("LevelFilter", 1, "ERROR"),
+ LevelFilter::Warn => serializer.serialize_unit_variant("LevelFilter", 2, "WARN"),
+ LevelFilter::Info => serializer.serialize_unit_variant("LevelFilter", 3, "INFO"),
+ LevelFilter::Debug => serializer.serialize_unit_variant("LevelFilter", 4, "DEBUG"),
+ LevelFilter::Trace => serializer.serialize_unit_variant("LevelFilter", 5, "TRACE"),
+ }
+ }
+}
+
+impl<'de> Deserialize<'de> for LevelFilter {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ struct LevelFilterIdentifier;
+
+ impl<'de> Visitor<'de> for LevelFilterIdentifier {
+ type Value = LevelFilter;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("log level filter")
+ }
+
+ fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let variant = LOG_LEVEL_NAMES
+ .get(v as usize)
+ .ok_or_else(|| Error::invalid_value(Unexpected::Unsigned(v), &self))?;
+
+ self.visit_str(variant)
+ }
+
+ fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ // Case insensitive.
+ FromStr::from_str(s).map_err(|_| Error::unknown_variant(s, &LOG_LEVEL_NAMES))
+ }
+
+ fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
+ where
+ E: Error,
+ {
+ let variant = str::from_utf8(value)
+ .map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
+
+ self.visit_str(variant)
+ }
+ }
+
+ impl<'de> DeserializeSeed<'de> for LevelFilterIdentifier {
+ type Value = LevelFilter;
+
+ fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ deserializer.deserialize_identifier(LevelFilterIdentifier)
+ }
+ }
+
+ struct LevelFilterEnum;
+
+ impl<'de> Visitor<'de> for LevelFilterEnum {
+ type Value = LevelFilter;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("log level filter")
+ }
+
+ fn visit_enum<A>(self, value: A) -> Result<Self::Value, A::Error>
+ where
+ A: EnumAccess<'de>,
+ {
+ let (level_filter, variant) = value.variant_seed(LevelFilterIdentifier)?;
+ // Every variant is a unit variant.
+ variant.unit_variant()?;
+ Ok(level_filter)
+ }
+ }
+
+ deserializer.deserialize_enum("LevelFilter", &LOG_LEVEL_NAMES, LevelFilterEnum)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::{Level, LevelFilter};
+ use serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};
+
+ fn level_token(variant: &'static str) -> Token {
+ Token::UnitVariant {
+ name: "Level",
+ variant,
+ }
+ }
+
+ fn level_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
+ [
+ Token::Enum { name: "Level" },
+ Token::Bytes(variant),
+ Token::Unit,
+ ]
+ }
+
+ fn level_variant_tokens(variant: u32) -> [Token; 3] {
+ [
+ Token::Enum { name: "Level" },
+ Token::U32(variant),
+ Token::Unit,
+ ]
+ }
+
+ fn level_filter_token(variant: &'static str) -> Token {
+ Token::UnitVariant {
+ name: "LevelFilter",
+ variant,
+ }
+ }
+
+ fn level_filter_bytes_tokens(variant: &'static [u8]) -> [Token; 3] {
+ [
+ Token::Enum {
+ name: "LevelFilter",
+ },
+ Token::Bytes(variant),
+ Token::Unit,
+ ]
+ }
+
+ fn level_filter_variant_tokens(variant: u32) -> [Token; 3] {
+ [
+ Token::Enum {
+ name: "LevelFilter",
+ },
+ Token::U32(variant),
+ Token::Unit,
+ ]
+ }
+
+ #[test]
+ fn test_level_ser_de() {
+ let cases = &[
+ (Level::Error, [level_token("ERROR")]),
+ (Level::Warn, [level_token("WARN")]),
+ (Level::Info, [level_token("INFO")]),
+ (Level::Debug, [level_token("DEBUG")]),
+ (Level::Trace, [level_token("TRACE")]),
+ ];
+
+ for (s, expected) in cases {
+ assert_tokens(s, expected);
+ }
+ }
+
+ #[test]
+ fn test_level_case_insensitive() {
+ let cases = &[
+ (Level::Error, [level_token("error")]),
+ (Level::Warn, [level_token("warn")]),
+ (Level::Info, [level_token("info")]),
+ (Level::Debug, [level_token("debug")]),
+ (Level::Trace, [level_token("trace")]),
+ ];
+
+ for (s, expected) in cases {
+ assert_de_tokens(s, expected);
+ }
+ }
+
+ #[test]
+ fn test_level_de_bytes() {
+ let cases = &[
+ (Level::Error, level_bytes_tokens(b"ERROR")),
+ (Level::Warn, level_bytes_tokens(b"WARN")),
+ (Level::Info, level_bytes_tokens(b"INFO")),
+ (Level::Debug, level_bytes_tokens(b"DEBUG")),
+ (Level::Trace, level_bytes_tokens(b"TRACE")),
+ ];
+
+ for (value, tokens) in cases {
+ assert_de_tokens(value, tokens);
+ }
+ }
+
+ #[test]
+ fn test_level_de_variant_index() {
+ let cases = &[
+ (Level::Error, level_variant_tokens(0)),
+ (Level::Warn, level_variant_tokens(1)),
+ (Level::Info, level_variant_tokens(2)),
+ (Level::Debug, level_variant_tokens(3)),
+ (Level::Trace, level_variant_tokens(4)),
+ ];
+
+ for (value, tokens) in cases {
+ assert_de_tokens(value, tokens);
+ }
+ }
+
+ #[test]
+ fn test_level_de_error() {
+ let msg = "unknown variant `errorx`, expected one of \
+ `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
+ assert_de_tokens_error::<Level>(&[level_token("errorx")], msg);
+ }
+
+ #[test]
+ fn test_level_filter_ser_de() {
+ let cases = &[
+ (LevelFilter::Off, [level_filter_token("OFF")]),
+ (LevelFilter::Error, [level_filter_token("ERROR")]),
+ (LevelFilter::Warn, [level_filter_token("WARN")]),
+ (LevelFilter::Info, [level_filter_token("INFO")]),
+ (LevelFilter::Debug, [level_filter_token("DEBUG")]),
+ (LevelFilter::Trace, [level_filter_token("TRACE")]),
+ ];
+
+ for (s, expected) in cases {
+ assert_tokens(s, expected);
+ }
+ }
+
+ #[test]
+ fn test_level_filter_case_insensitive() {
+ let cases = &[
+ (LevelFilter::Off, [level_filter_token("off")]),
+ (LevelFilter::Error, [level_filter_token("error")]),
+ (LevelFilter::Warn, [level_filter_token("warn")]),
+ (LevelFilter::Info, [level_filter_token("info")]),
+ (LevelFilter::Debug, [level_filter_token("debug")]),
+ (LevelFilter::Trace, [level_filter_token("trace")]),
+ ];
+
+ for (s, expected) in cases {
+ assert_de_tokens(s, expected);
+ }
+ }
+
+ #[test]
+ fn test_level_filter_de_bytes() {
+ let cases = &[
+ (LevelFilter::Off, level_filter_bytes_tokens(b"OFF")),
+ (LevelFilter::Error, level_filter_bytes_tokens(b"ERROR")),
+ (LevelFilter::Warn, level_filter_bytes_tokens(b"WARN")),
+ (LevelFilter::Info, level_filter_bytes_tokens(b"INFO")),
+ (LevelFilter::Debug, level_filter_bytes_tokens(b"DEBUG")),
+ (LevelFilter::Trace, level_filter_bytes_tokens(b"TRACE")),
+ ];
+
+ for (value, tokens) in cases {
+ assert_de_tokens(value, tokens);
+ }
+ }
+
+ #[test]
+ fn test_level_filter_de_variant_index() {
+ let cases = &[
+ (LevelFilter::Off, level_filter_variant_tokens(0)),
+ (LevelFilter::Error, level_filter_variant_tokens(1)),
+ (LevelFilter::Warn, level_filter_variant_tokens(2)),
+ (LevelFilter::Info, level_filter_variant_tokens(3)),
+ (LevelFilter::Debug, level_filter_variant_tokens(4)),
+ (LevelFilter::Trace, level_filter_variant_tokens(5)),
+ ];
+
+ for (value, tokens) in cases {
+ assert_de_tokens(value, tokens);
+ }
+ }
+
+ #[test]
+ fn test_level_filter_de_error() {
+ let msg = "unknown variant `errorx`, expected one of \
+ `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`";
+ assert_de_tokens_error::<LevelFilter>(&[level_filter_token("errorx")], msg);
+ }
+}
diff --git a/crates/log/triagebot.toml b/crates/log/triagebot.toml
index 84659a1..fa0824a 100644
--- a/crates/log/triagebot.toml
+++ b/crates/log/triagebot.toml
@@ -1 +1 @@
-[assign]
+[assign]
diff --git a/pseudo_crate/Cargo.lock b/pseudo_crate/Cargo.lock
index bead7bd..70e98a0 100644
--- a/pseudo_crate/Cargo.lock
+++ b/pseudo_crate/Cargo.lock
@@ -2917,12 +2917,9 @@
[[package]]
name = "log"
-version = "0.4.17"
+version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
-dependencies = [
- "cfg-if",
-]
+checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "lru-cache"
diff --git a/pseudo_crate/Cargo.toml b/pseudo_crate/Cargo.toml
index dd9957b..c91febb 100644
--- a/pseudo_crate/Cargo.toml
+++ b/pseudo_crate/Cargo.toml
@@ -164,7 +164,7 @@
linkme-impl = "=0.3.10"
litrs = "=0.3.0"
lock_api = "=0.4.11"
-log = "=0.4.17"
+log = "=0.4.22"
lru-cache = "=0.1.2"
lz4_flex = "=0.11.2"
macaddr = "=1.0.1"