refactor: remove once_cell (#1361)

* refactor: remove once_cell

* some missing fixes
This commit is contained in:
Clement Tsang 2023-12-23 04:35:42 -05:00 committed by GitHub
parent 854f3aed95
commit a1168cac67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 289 additions and 283 deletions

1
Cargo.lock generated
View File

@ -176,7 +176,6 @@ dependencies = [
"log",
"mach2",
"nvml-wrapper",
"once_cell",
"predicates",
"ratatui",
"regex",

View File

@ -93,10 +93,7 @@ indexmap = "2.1.0"
itertools = "0.12.0"
kstring = { version = "2.0.0", features = ["arc"] }
log = { version = "0.4.20", optional = true }
nvml-wrapper = { version = "0.9.0", optional = true, features = [
"legacy-functions",
] }
once_cell = "1.18.0"
nvml-wrapper = { version = "0.9.0", optional = true, features = ["legacy-functions"] }
regex = "1.10.2"
serde = { version = "=1.0.193", features = ["derive"] }
starship-battery = { version = "0.8.2", optional = true }
@ -136,9 +133,7 @@ filedescriptor = "0.8.2"
[dev-dependencies]
assert_cmd = "2.0.12"
cargo-husky = { version = "1.5.0", default-features = false, features = [
"user-hooks",
] }
cargo-husky = { version = "1.5.0", default-features = false, features = ["user-hooks"] }
predicates = "3.0.3"
[build-dependencies]

View File

@ -364,13 +364,12 @@ impl DataCollection {
let io_device = {
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
use once_cell::sync::Lazy;
use std::sync::OnceLock;
use regex::Regex;
// Must trim one level further for macOS!
static DISK_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"disk\d+").unwrap());
if let Some(new_name) = DISK_REGEX.find(checked_name) {
static DISK_REGEX: OnceLock<Regex> = OnceLock::new();
if let Some(new_name) = DISK_REGEX.get_or_init(|| Regex::new(r"disk\d+").unwrap()).find(checked_name) {
io.get(new_name.as_str())
} else {
None

View File

@ -1,8 +1,9 @@
use std::sync::OnceLock;
use hashbrown::HashMap;
use nvml_wrapper::enum_wrappers::device::TemperatureSensor;
use nvml_wrapper::enums::device::UsedGpuMemory;
use nvml_wrapper::{error::NvmlError, Nvml};
use once_cell::sync::Lazy;
use crate::app::Filter;
@ -10,7 +11,7 @@ use crate::app::layout_manager::UsedWidgets;
use crate::data_harvester::memory::MemHarvest;
use crate::data_harvester::temperature::{is_temp_filtered, TempHarvest, TemperatureType};
pub static NVML_DATA: Lazy<Result<Nvml, NvmlError>> = Lazy::new(Nvml::init);
pub static NVML_DATA: OnceLock<Result<Nvml, NvmlError>> = OnceLock::new();
pub struct GpusData {
pub memory: Option<Vec<(String, MemHarvest)>>,
@ -23,7 +24,7 @@ pub struct GpusData {
pub fn get_nvidia_vecs(
temp_type: &TemperatureType, filter: &Option<Filter>, widgets_to_harvest: &UsedWidgets,
) -> Option<GpusData> {
if let Ok(nvml) = &*NVML_DATA {
if let Ok(nvml) = NVML_DATA.get_or_init(Nvml::init) {
if let Ok(num_gpu) = nvml.device_count() {
let mut temp_vec = Vec::with_capacity(num_gpu as usize);
let mut mem_vec = Vec::with_capacity(num_gpu as usize);

View File

@ -5,11 +5,11 @@ use std::{
fs::File,
io::{self, BufRead, BufReader, Read},
path::PathBuf,
sync::OnceLock,
};
use anyhow::anyhow;
use libc::uid_t;
use once_cell::sync::Lazy;
use rustix::{
fd::OwnedFd,
fs::{Mode, OFlags},
@ -18,7 +18,7 @@ use rustix::{
use crate::Pid;
static PAGESIZE: Lazy<u64> = Lazy::new(|| rustix::param::page_size() as u64);
static PAGESIZE: OnceLock<u64> = OnceLock::new();
#[inline]
fn next_part<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Result<&'a str, io::Error> {
@ -103,7 +103,7 @@ impl Stat {
/// Returns the Resident Set Size in bytes.
#[inline]
pub fn rss_bytes(&self) -> u64 {
self.rss * *PAGESIZE
self.rss * PAGESIZE.get_or_init(|| rustix::param::page_size() as u64)
}
}

View File

@ -132,19 +132,19 @@ impl CanvasStyling {
match colour_scheme {
ColourScheme::Default => {}
ColourScheme::DefaultLight => {
canvas_colours.set_colours_from_palette(&DEFAULT_LIGHT_MODE_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&default_light_mode_colour_palette())?;
}
ColourScheme::Gruvbox => {
canvas_colours.set_colours_from_palette(&GRUVBOX_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&gruvbox_colour_palette())?;
}
ColourScheme::GruvboxLight => {
canvas_colours.set_colours_from_palette(&GRUVBOX_LIGHT_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&gruvbox_light_colour_palette())?;
}
ColourScheme::Nord => {
canvas_colours.set_colours_from_palette(&NORD_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&nord_colour_palette())?;
}
ColourScheme::NordLight => {
canvas_colours.set_colours_from_palette(&NORD_LIGHT_COLOUR_PALETTE)?;
canvas_colours.set_colours_from_palette(&nord_light_colour_palette())?;
}
ColourScheme::Custom => {
if let Some(colors) = &config.colors {

View File

@ -1,4 +1,3 @@
use once_cell::sync::Lazy;
use tui::widgets::Borders;
use crate::options::ConfigColours;
@ -23,253 +22,260 @@ pub const TIME_LABEL_HEIGHT_LIMIT: u16 = 7;
// Side borders
pub const SIDE_BORDERS: Borders = Borders::LEFT.union(Borders::RIGHT);
pub static DEFAULT_TEXT_STYLE: Lazy<tui::style::Style> =
Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::Gray));
pub static DEFAULT_HEADER_STYLE: Lazy<tui::style::Style> =
Lazy::new(|| tui::style::Style::default().fg(tui::style::Color::LightBlue));
// Colour profiles
pub static DEFAULT_LIGHT_MODE_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
text_color: Some("black".into()),
border_color: Some("black".into()),
table_header_color: Some("black".into()),
widget_title_color: Some("black".into()),
selected_text_color: Some("white".into()),
graph_color: Some("black".into()),
disabled_text_color: Some("gray".into()),
ram_color: Some("blue".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("LightRed".into()),
swap_color: Some("red".into()),
arc_color: Some("LightBlue".into()),
gpu_core_colors: Some(vec![
"LightGreen".into(),
"LightCyan".into(),
"LightRed".into(),
"Cyan".into(),
"Green".into(),
"Blue".into(),
"Red".into(),
]),
rx_color: Some("blue".into()),
tx_color: Some("red".into()),
rx_total_color: Some("LightBlue".into()),
tx_total_color: Some("LightRed".into()),
cpu_core_colors: Some(vec![
"LightMagenta".into(),
"LightBlue".into(),
"LightRed".into(),
"Cyan".into(),
"Green".into(),
"Blue".into(),
"Red".into(),
]),
..ConfigColours::default()
});
// TODO: Generate these with a macro or something...
pub fn default_light_mode_colour_palette() -> ConfigColours {
ConfigColours {
text_color: Some("black".into()),
border_color: Some("black".into()),
table_header_color: Some("black".into()),
widget_title_color: Some("black".into()),
selected_text_color: Some("white".into()),
graph_color: Some("black".into()),
disabled_text_color: Some("gray".into()),
ram_color: Some("blue".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("LightRed".into()),
swap_color: Some("red".into()),
arc_color: Some("LightBlue".into()),
gpu_core_colors: Some(vec![
"LightGreen".into(),
"LightCyan".into(),
"LightRed".into(),
"Cyan".into(),
"Green".into(),
"Blue".into(),
"Red".into(),
]),
rx_color: Some("blue".into()),
tx_color: Some("red".into()),
rx_total_color: Some("LightBlue".into()),
tx_total_color: Some("LightRed".into()),
cpu_core_colors: Some(vec![
"LightMagenta".into(),
"LightBlue".into(),
"LightRed".into(),
"Cyan".into(),
"Green".into(),
"Blue".into(),
"Red".into(),
]),
..ConfigColours::default()
}
}
pub static GRUVBOX_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#83a598".into()),
all_cpu_color: Some("#8ec07c".into()),
avg_cpu_color: Some("#fb4934".into()),
cpu_core_colors: Some(vec![
"#cc241d".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#689d6a".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#fabd2f".into(),
"#83a598".into(),
"#d3869b".into(),
"#d65d0e".into(),
"#9d0006".into(),
"#79740e".into(),
"#b57614".into(),
"#076678".into(),
"#8f3f71".into(),
"#427b58".into(),
"#d65d03".into(),
"#af3a03".into(),
]),
ram_color: Some("#8ec07c".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#b16286".into()),
swap_color: Some("#fabd2f".into()),
arc_color: Some("#689d6a".into()),
gpu_core_colors: Some(vec![
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#cc241d".into(),
"#98971a".into(),
]),
rx_color: Some("#8ec07c".into()),
tx_color: Some("#fabd2f".into()),
rx_total_color: Some("#689d6a".into()),
tx_total_color: Some("#d79921".into()),
border_color: Some("#ebdbb2".into()),
highlighted_border_color: Some("#fe8019".into()),
disabled_text_color: Some("#665c54".into()),
text_color: Some("#ebdbb2".into()),
selected_text_color: Some("#1d2021".into()),
selected_bg_color: Some("#ebdbb2".into()),
widget_title_color: Some("#ebdbb2".into()),
graph_color: Some("#ebdbb2".into()),
high_battery_color: Some("#98971a".into()),
medium_battery_color: Some("#fabd2f".into()),
low_battery_color: Some("#fb4934".into()),
});
pub fn gruvbox_colour_palette() -> ConfigColours {
ConfigColours {
table_header_color: Some("#83a598".into()),
all_cpu_color: Some("#8ec07c".into()),
avg_cpu_color: Some("#fb4934".into()),
cpu_core_colors: Some(vec![
"#cc241d".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#689d6a".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#fabd2f".into(),
"#83a598".into(),
"#d3869b".into(),
"#d65d0e".into(),
"#9d0006".into(),
"#79740e".into(),
"#b57614".into(),
"#076678".into(),
"#8f3f71".into(),
"#427b58".into(),
"#d65d03".into(),
"#af3a03".into(),
]),
ram_color: Some("#8ec07c".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#b16286".into()),
swap_color: Some("#fabd2f".into()),
arc_color: Some("#689d6a".into()),
gpu_core_colors: Some(vec![
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#cc241d".into(),
"#98971a".into(),
]),
rx_color: Some("#8ec07c".into()),
tx_color: Some("#fabd2f".into()),
rx_total_color: Some("#689d6a".into()),
tx_total_color: Some("#d79921".into()),
border_color: Some("#ebdbb2".into()),
highlighted_border_color: Some("#fe8019".into()),
disabled_text_color: Some("#665c54".into()),
text_color: Some("#ebdbb2".into()),
selected_text_color: Some("#1d2021".into()),
selected_bg_color: Some("#ebdbb2".into()),
widget_title_color: Some("#ebdbb2".into()),
graph_color: Some("#ebdbb2".into()),
high_battery_color: Some("#98971a".into()),
medium_battery_color: Some("#fabd2f".into()),
low_battery_color: Some("#fb4934".into()),
}
}
pub static GRUVBOX_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#076678".into()),
all_cpu_color: Some("#8ec07c".into()),
avg_cpu_color: Some("#fb4934".into()),
cpu_core_colors: Some(vec![
"#cc241d".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#689d6a".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#fabd2f".into(),
"#83a598".into(),
"#d3869b".into(),
"#d65d0e".into(),
"#9d0006".into(),
"#79740e".into(),
"#b57614".into(),
"#076678".into(),
"#8f3f71".into(),
"#427b58".into(),
"#d65d03".into(),
"#af3a03".into(),
]),
ram_color: Some("#427b58".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#d79921".into()),
swap_color: Some("#cc241d".into()),
arc_color: Some("#689d6a".into()),
gpu_core_colors: Some(vec![
"#9d0006".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#fe8019".into(),
"#b8bb26".into(),
]),
rx_color: Some("#427b58".into()),
tx_color: Some("#cc241d".into()),
rx_total_color: Some("#689d6a".into()),
tx_total_color: Some("#9d0006".into()),
border_color: Some("#3c3836".into()),
highlighted_border_color: Some("#af3a03".into()),
disabled_text_color: Some("#d5c4a1".into()),
text_color: Some("#3c3836".into()),
selected_text_color: Some("#ebdbb2".into()),
selected_bg_color: Some("#3c3836".into()),
widget_title_color: Some("#3c3836".into()),
graph_color: Some("#3c3836".into()),
high_battery_color: Some("#98971a".into()),
medium_battery_color: Some("#d79921".into()),
low_battery_color: Some("#cc241d".into()),
});
pub fn gruvbox_light_colour_palette() -> ConfigColours {
ConfigColours {
table_header_color: Some("#076678".into()),
all_cpu_color: Some("#8ec07c".into()),
avg_cpu_color: Some("#fb4934".into()),
cpu_core_colors: Some(vec![
"#cc241d".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#689d6a".into(),
"#fe8019".into(),
"#b8bb26".into(),
"#fabd2f".into(),
"#83a598".into(),
"#d3869b".into(),
"#d65d0e".into(),
"#9d0006".into(),
"#79740e".into(),
"#b57614".into(),
"#076678".into(),
"#8f3f71".into(),
"#427b58".into(),
"#d65d03".into(),
"#af3a03".into(),
]),
ram_color: Some("#427b58".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#d79921".into()),
swap_color: Some("#cc241d".into()),
arc_color: Some("#689d6a".into()),
gpu_core_colors: Some(vec![
"#9d0006".into(),
"#98971a".into(),
"#d79921".into(),
"#458588".into(),
"#b16286".into(),
"#fe8019".into(),
"#b8bb26".into(),
]),
rx_color: Some("#427b58".into()),
tx_color: Some("#cc241d".into()),
rx_total_color: Some("#689d6a".into()),
tx_total_color: Some("#9d0006".into()),
border_color: Some("#3c3836".into()),
highlighted_border_color: Some("#af3a03".into()),
disabled_text_color: Some("#d5c4a1".into()),
text_color: Some("#3c3836".into()),
selected_text_color: Some("#ebdbb2".into()),
selected_bg_color: Some("#3c3836".into()),
widget_title_color: Some("#3c3836".into()),
graph_color: Some("#3c3836".into()),
high_battery_color: Some("#98971a".into()),
medium_battery_color: Some("#d79921".into()),
low_battery_color: Some("#cc241d".into()),
}
}
pub static NORD_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#81a1c1".into()),
all_cpu_color: Some("#88c0d0".into()),
avg_cpu_color: Some("#8fbcbb".into()),
cpu_core_colors: Some(vec![
"#5e81ac".into(),
"#81a1c1".into(),
"#d8dee9".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#d08770".into(),
"#bf616a".into(),
]),
ram_color: Some("#88c0d0".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#d8dee9".into()),
swap_color: Some("#d08770".into()),
arc_color: Some("#5e81ac".into()),
gpu_core_colors: Some(vec![
"#8fbcbb".into(),
"#81a1c1".into(),
"#d8dee9".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#bf616a".into(),
]),
rx_color: Some("#88c0d0".into()),
tx_color: Some("#d08770".into()),
rx_total_color: Some("#5e81ac".into()),
tx_total_color: Some("#8fbcbb".into()),
border_color: Some("#88c0d0".into()),
highlighted_border_color: Some("#5e81ac".into()),
disabled_text_color: Some("#4c566a".into()),
text_color: Some("#e5e9f0".into()),
selected_text_color: Some("#2e3440".into()),
selected_bg_color: Some("#88c0d0".into()),
widget_title_color: Some("#e5e9f0".into()),
graph_color: Some("#e5e9f0".into()),
high_battery_color: Some("#a3be8c".into()),
medium_battery_color: Some("#ebcb8b".into()),
low_battery_color: Some("#bf616a".into()),
});
pub fn nord_colour_palette() -> ConfigColours {
ConfigColours {
table_header_color: Some("#81a1c1".into()),
all_cpu_color: Some("#88c0d0".into()),
avg_cpu_color: Some("#8fbcbb".into()),
cpu_core_colors: Some(vec![
"#5e81ac".into(),
"#81a1c1".into(),
"#d8dee9".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#d08770".into(),
"#bf616a".into(),
]),
ram_color: Some("#88c0d0".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#d8dee9".into()),
swap_color: Some("#d08770".into()),
arc_color: Some("#5e81ac".into()),
gpu_core_colors: Some(vec![
"#8fbcbb".into(),
"#81a1c1".into(),
"#d8dee9".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#bf616a".into(),
]),
rx_color: Some("#88c0d0".into()),
tx_color: Some("#d08770".into()),
rx_total_color: Some("#5e81ac".into()),
tx_total_color: Some("#8fbcbb".into()),
border_color: Some("#88c0d0".into()),
highlighted_border_color: Some("#5e81ac".into()),
disabled_text_color: Some("#4c566a".into()),
text_color: Some("#e5e9f0".into()),
selected_text_color: Some("#2e3440".into()),
selected_bg_color: Some("#88c0d0".into()),
widget_title_color: Some("#e5e9f0".into()),
graph_color: Some("#e5e9f0".into()),
high_battery_color: Some("#a3be8c".into()),
medium_battery_color: Some("#ebcb8b".into()),
low_battery_color: Some("#bf616a".into()),
}
}
pub static NORD_LIGHT_COLOUR_PALETTE: Lazy<ConfigColours> = Lazy::new(|| ConfigColours {
table_header_color: Some("#5e81ac".into()),
all_cpu_color: Some("#81a1c1".into()),
avg_cpu_color: Some("#8fbcbb".into()),
cpu_core_colors: Some(vec![
"#5e81ac".into(),
"#88c0d0".into(),
"#4c566a".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#d08770".into(),
"#bf616a".into(),
]),
ram_color: Some("#81a1c1".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#4c566a".into()),
swap_color: Some("#d08770".into()),
arc_color: Some("#5e81ac".into()),
gpu_core_colors: Some(vec![
"#8fbcbb".into(),
"#88c0d0".into(),
"#4c566a".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#bf616a".into(),
]),
rx_color: Some("#81a1c1".into()),
tx_color: Some("#d08770".into()),
rx_total_color: Some("#5e81ac".into()),
tx_total_color: Some("#8fbcbb".into()),
border_color: Some("#2e3440".into()),
highlighted_border_color: Some("#5e81ac".into()),
disabled_text_color: Some("#d8dee9".into()),
text_color: Some("#2e3440".into()),
selected_text_color: Some("#f5f5f5".into()),
selected_bg_color: Some("#5e81ac".into()),
widget_title_color: Some("#2e3440".into()),
graph_color: Some("#2e3440".into()),
high_battery_color: Some("#a3be8c".into()),
medium_battery_color: Some("#ebcb8b".into()),
low_battery_color: Some("#bf616a".into()),
});
pub fn nord_light_colour_palette() -> ConfigColours {
ConfigColours {
table_header_color: Some("#5e81ac".into()),
all_cpu_color: Some("#81a1c1".into()),
avg_cpu_color: Some("#8fbcbb".into()),
cpu_core_colors: Some(vec![
"#5e81ac".into(),
"#88c0d0".into(),
"#4c566a".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#d08770".into(),
"#bf616a".into(),
]),
ram_color: Some("#81a1c1".into()),
#[cfg(not(target_os = "windows"))]
cache_color: Some("#4c566a".into()),
swap_color: Some("#d08770".into()),
arc_color: Some("#5e81ac".into()),
gpu_core_colors: Some(vec![
"#8fbcbb".into(),
"#88c0d0".into(),
"#4c566a".into(),
"#b48ead".into(),
"#a3be8c".into(),
"#ebcb8b".into(),
"#bf616a".into(),
]),
rx_color: Some("#81a1c1".into()),
tx_color: Some("#d08770".into()),
rx_total_color: Some("#5e81ac".into()),
tx_total_color: Some("#8fbcbb".into()),
border_color: Some("#2e3440".into()),
highlighted_border_color: Some("#5e81ac".into()),
disabled_text_color: Some("#d8dee9".into()),
text_color: Some("#2e3440".into()),
selected_text_color: Some("#f5f5f5".into()),
selected_bg_color: Some("#5e81ac".into()),
widget_title_color: Some("#2e3440".into()),
graph_color: Some("#2e3440".into()),
high_battery_color: Some("#a3be8c".into()),
medium_battery_color: Some("#ebcb8b".into()),
low_battery_color: Some("#bf616a".into()),
}
}
// Help text
pub const HELP_CONTENTS_TEXT: [&str; 10] = [

View File

@ -1,22 +1,8 @@
#[cfg(feature = "logging")]
pub static OFFSET: once_cell::sync::Lazy<time::UtcOffset> = once_cell::sync::Lazy::new(|| {
use time::util::local_offset::Soundness;
use std::sync::OnceLock;
// SAFETY: We only invoke this once, quickly, and it should be invoked in a single-thread context.
// We also should only ever hit this logging at all in a debug context which is generally fine,
// release builds should have this logging disabled entirely for now.
unsafe {
// XXX: If we ever DO add general logging as a release feature, evaluate this again and whether this is
// something we want enabled in release builds! What might be safe is falling back to the non-set-soundness
// mode when specifically using certain feature flags (e.g. dev-logging feature enables this behaviour).
time::util::local_offset::set_soundness(Soundness::Unsound);
let res = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
time::util::local_offset::set_soundness(Soundness::Sound);
res
}
});
#[cfg(feature = "logging")]
pub static OFFSET: OnceLock<time::UtcOffset> = OnceLock::new();
#[cfg(feature = "logging")]
pub fn init_logger(
@ -24,9 +10,29 @@ pub fn init_logger(
) -> Result<(), fern::InitError> {
let dispatch = fern::Dispatch::new()
.format(|out, message, record| {
let offset = OFFSET.get_or_init(|| {
use time::util::local_offset::Soundness;
// SAFETY: We only invoke this once, quickly, and it should be invoked in a single-thread context.
// We also should only ever hit this logging at all in a debug context which is generally fine,
// release builds should have this logging disabled entirely for now.
unsafe {
// XXX: If we ever DO add general logging as a release feature, evaluate this again and whether this is
// something we want enabled in release builds! What might be safe is falling back to the non-set-soundness
// mode when specifically using certain feature flags (e.g. dev-logging feature enables this behaviour).
time::util::local_offset::set_soundness(Soundness::Unsound);
let res =
time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);
time::util::local_offset::set_soundness(Soundness::Sound);
res
}
});
let offset_time = {
let utc = time::OffsetDateTime::now_utc();
utc.checked_to_offset(*OFFSET).unwrap_or(utc)
utc.checked_to_offset(*offset).unwrap_or(utc)
};
out.finish(format_args!(