remove logging statement indirection

This commit is contained in:
Robin Appelman 2025-08-09 17:24:54 +02:00
commit ea695e8460
8 changed files with 120 additions and 126 deletions

View file

@ -24,15 +24,11 @@ impl StatementList {
StatementList { statements }
}
pub fn iter(
&self,
) -> impl Iterator<Item = (LogStatementIndex, &'static LoggingStatement)> + Send + '_ {
self.statements
.iter()
.copied()
.flat_map(|(_, list)| list.iter())
.enumerate()
.map(|(index, statement)| (LogStatementIndex(index), statement))
pub fn iter(&self) -> impl Iterator<Item = LoggingStatementWithPathPrefix> + Send + '_ {
self.statements.iter().copied().flat_map(|(prefix, list)| {
list.iter()
.map(|statement| statement.with_path_prefix(prefix))
})
}
pub fn get(&self, mut index: LogStatementIndex) -> Option<LoggingStatementWithPathPrefix> {

View file

@ -1,5 +1,7 @@
use serde::Deserialize;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
#[derive(Debug, Default, PartialEq, Clone, Copy, Deserialize, Hash, PartialOrd, Ord, Eq)]
#[serde(from = "i64")]
@ -57,7 +59,7 @@ impl LogLevel {
}
}
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq, Ord)]
pub struct LoggingStatement {
pub level: LogLevel,
pub path: &'static str,
@ -68,24 +70,31 @@ pub struct LoggingStatement {
pub has_meaningful_message: bool,
}
impl PartialOrd for LoggingStatement {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(self.path, self.line).partial_cmp(&(other.path, other.line))
}
}
impl Hash for LoggingStatement {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.path, self.line).hash(state)
}
}
impl LoggingStatement {
pub fn with_path_prefix(&self, path_prefix: &'static str) -> LoggingStatementWithPathPrefix {
pub fn with_path_prefix(
&'static self,
path_prefix: &'static str,
) -> LoggingStatementWithPathPrefix {
LoggingStatementWithPathPrefix {
level: self.level,
path_prefix,
path: self.path,
line: self.line,
placeholders: self.placeholders,
exception: self.exception,
pattern: self.pattern,
has_meaningful_message: self.has_meaningful_message,
statement: self,
}
}
pub fn message(&self) -> impl Display + '_ {
LoggingMessage {
message: self.clone(),
}
LoggingMessage { message: &self }
}
pub fn pattern_len(&self) -> usize {
@ -93,50 +102,34 @@ impl LoggingStatement {
}
}
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq, Ord, PartialOrd, Hash)]
pub struct LoggingStatementWithPathPrefix {
pub level: LogLevel,
pub statement: &'static LoggingStatement,
pub path_prefix: &'static str,
pub path: &'static str,
pub line: usize,
pub placeholders: &'static [&'static str],
pub exception: Option<&'static str>,
pub pattern: &'static str,
pub has_meaningful_message: bool,
}
impl From<&LoggingStatementWithPathPrefix> for LoggingStatement {
fn from(value: &LoggingStatementWithPathPrefix) -> Self {
LoggingStatement {
level: value.level,
path: value.path,
line: value.line,
placeholders: value.placeholders,
exception: value.exception,
pattern: value.pattern,
has_meaningful_message: value.has_meaningful_message,
}
}
}
impl LoggingStatementWithPathPrefix {
fn raw_message(&self) -> LoggingMessage {
fn raw_message(&self) -> LoggingMessage<'static> {
LoggingMessage {
message: self.into(),
message: self.statement,
}
}
pub fn path(&self) -> impl Display {
LoggingStatementPath {
path_prefix: self.path_prefix,
path: self.path,
path: self.statement.path,
}
}
pub fn line(&self) -> usize {
self.statement.line
}
pub fn message(&self) -> impl Display {
LoggingStatementMessage {
message: self.raw_message(),
exception: self.exception,
exception: self.statement.exception,
}
}
}
@ -153,7 +146,7 @@ impl Display for LoggingStatementPath {
}
struct LoggingStatementMessage {
pub message: LoggingMessage,
pub message: LoggingMessage<'static>,
pub exception: Option<&'static str>,
}
@ -174,16 +167,16 @@ impl Display for LoggingStatementWithPathPrefix {
"«{}» {} line {}",
self.raw_message(),
self.path(),
self.line
self.statement.line
)
}
}
struct LoggingMessage {
message: LoggingStatement,
struct LoggingMessage<'a> {
message: &'a LoggingStatement,
}
impl Display for LoggingMessage {
impl Display for LoggingMessage<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.message.pattern.is_empty() {
return Ok(());