use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::Path;
use tracing::{debug, trace, warn};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ConfigLoadFailureKind {
Missing,
LoadError,
}
pub(super) fn trace_config_path_variable(var_name: &str, path: Option<&Path>) {
trace!(
var_name,
found = path.is_some(),
path_hash = path.map(path_hash).as_deref(),
path_file_name = ?path.and_then(Path::file_name),
"read config path variable"
);
}
pub(super) fn warn_explicit_config_load_failed(path: &Path, failure_kind: ConfigLoadFailureKind) {
warn!(
path_hash = %path_hash(path),
path_file_name = ?path.file_name(),
failure_kind = ?failure_kind,
"explicit config load failed"
);
}
pub(super) fn debug_config_path(message: &'static str, path: &Path) {
debug!(
path_hash = %path_hash(path),
path_file_name = ?path.file_name(),
message
);
}
pub(super) fn debug_optional_config_path(message: &'static str, path: Option<&str>) {
debug!(
path_hash = path.map(|value| short_hash(value.as_bytes())).as_deref(),
path_file_name = ?path.and_then(|value| Path::new(value).file_name()),
path_present = path.is_some(),
message
);
}
pub(super) fn short_hash(value: &[u8]) -> String {
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
format!("{:016x}", hasher.finish())
}
pub(super) fn path_hash(path: &Path) -> String {
short_hash(path.to_string_lossy().as_bytes())
}