diff --git a/CHANGELOG.md b/CHANGELOG.md index f4563a4..e9c3168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 4.2.0 - 2021-7-12 +### Changed + +* Add feature flag `record_location_unstable` to expand record location support. + Be warned that this is accomplished by leaking the record location strings. + +## 4.1.0 - 2020-10-21 +### Changed + +* Require `slog` 2.4 or greater +* Remove dependency `crossbeam` +* Require `log` 0.4.11 or greater + ## 4.0.0 - 2018-08-13 ### Changed diff --git a/Cargo.toml b/Cargo.toml index 4070f0c..0a84aef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "slog-stdlog" -version = "4.1.0" +version = "4.2.0" authors = ["Dawid Ciężarkiewicz "] description = "`log` crate adapter for slog-rs" keywords = ["slog", "logging", "json", "log"] @@ -17,11 +17,13 @@ path = "lib.rs" [features] default = [] kv_unstable = ["log/kv_unstable_std", "slog/dynamic-keys"] +record_location_unstable = ["cached"] [dependencies] slog = "2.4" slog-scope = "4" log = { version = "0.4.11", features = ["std"] } +cached = { version = "0.23.0", optional = true } [dev-dependencies] slog-term = "2" diff --git a/lib.rs b/lib.rs index 842b7ce..b0d10fe 100644 --- a/lib.rs +++ b/lib.rs @@ -53,8 +53,8 @@ extern crate log; #[cfg(feature = "kv_unstable")] mod kv; +use slog::{b, Level, KV}; use std::{fmt, io}; -use slog::{Level, KV, b}; struct Logger; @@ -68,9 +68,33 @@ fn log_to_slog_level(level: log::Level) -> Level { } } +/// Gets a static string for Record location information. +/// Uses a cache to avoid leaking each string more than once. +#[cfg(feature = "record_location_unstable")] +#[cached::proc_macro::cached] +fn get_static_info(non_static_info: Option) -> &'static str { + match non_static_info { + Some(s) => Box::leak(s.into_boxed_str()), + None => "unknown", + } +} + fn record_as_location(r: &log::Record) -> slog::RecordLocation { + #[cfg(not(feature = "record_location_unstable"))] let module = r.module_path_static().unwrap_or(""); + #[cfg(not(feature = "record_location_unstable"))] let file = r.file_static().unwrap_or(""); + + // Warning: expands Record module and file names for non-static strings by leaking strings + #[cfg(feature = "record_location_unstable")] + let module = r + .module_path_static() + .unwrap_or_else(|| get_static_info(r.module_path().map(|s| s.to_string()))); + #[cfg(feature = "record_location_unstable")] + let file = r + .file_static() + .unwrap_or_else(|| get_static_info(r.file().map(|s| s.to_string()))); + let line = r.line().unwrap_or_default(); slog::RecordLocation { @@ -100,10 +124,10 @@ impl log::Log for Logger { }; #[cfg(feature = "kv_unstable")] { - let key_values = r.key_values(); - let mut visitor = kv::Visitor::new(); - key_values.visit(&mut visitor).unwrap(); - slog_scope::with_logger(|logger| logger.log(&slog::Record::new(&s, args, b!(visitor)))) + let key_values = r.key_values(); + let mut visitor = kv::Visitor::new(); + key_values.visit(&mut visitor).unwrap(); + slog_scope::with_logger(|logger| logger.log(&slog::Record::new(&s, args, b!(visitor)))) } #[cfg(not(feature = "kv_unstable"))] slog_scope::with_logger(|logger| logger.log(&slog::Record::new(&s, args, b!()))) @@ -252,7 +276,11 @@ impl slog::Drain for StdLog { let lazy = LazyLogString::new(info, logger_values); // Please don't yell at me for this! :D // https://github.com/rust-lang-nursery/log/issues/95 - log::__private_api_log(format_args!("{}", lazy), level, &(target, info.module(), info.file(), info.line())); + log::__private_api_log( + format_args!("{}", lazy), + level, + &(target, info.module(), info.file(), info.line()), + ); Ok(()) }