Skip to main content

cdefmt_decoder/
log.rs

1//! Log related logic.
2//!
3//! The logic contained within this file relates to using a log id to extract and parse the log's
4//! information from the elf.
5
6use anyhow::Context;
7use cdefmt_parser::metadata::{Level, Metadata};
8use rformat::{fmt::format::format_string, prelude::*};
9
10use crate::{var::Var, Result};
11
12#[derive(Clone, Debug)]
13pub struct Log<'elf> {
14    metadata: Metadata<'elf>,
15    args: Vec<Var>,
16}
17
18impl<'elf> Log<'elf> {
19    pub(crate) fn new(metadata: Metadata<'elf>, args: Vec<Var>) -> Self {
20        Self { metadata, args }
21    }
22
23    pub fn get_level(&self) -> Level {
24        self.metadata.level
25    }
26
27    pub fn get_file(&self) -> &str {
28        self.metadata.file
29    }
30
31    pub fn get_line(&self) -> usize {
32        self.metadata.line
33    }
34
35    pub fn get_args(&self) -> &[Var] {
36        &self.args
37    }
38}
39
40impl Log<'_> {
41    pub fn to_string(&self) -> Result<String> {
42        let params = self
43            .args
44            .iter()
45            .zip(self.metadata.names.iter())
46            .map(|(a, n)| rformat::fmt::format::Parameter {
47                identifier: n,
48                formattable: rformat::formattable::into_formattable!((*a)),
49            })
50            .collect::<Vec<_>>();
51
52        format_string(self.metadata.fmt, &params).with_context(|| {
53            format!(
54                "{}:{}: Could not format '{}'",
55                self.get_file(),
56                self.get_line(),
57                self.metadata.fmt
58            )
59        })
60    }
61}