Skip to main content

intel_crashlog/
metadata.rs

1// Copyright (C) 2025 Intel Corporation
2// SPDX-License-Identifier: MIT
3
4//! Information extracted alongside the Crash Log records.
5
6#[cfg(not(feature = "std"))]
7use alloc::vec::Vec;
8#[cfg(not(feature = "std"))]
9use alloc::{fmt, string::String};
10#[cfg(feature = "std")]
11use std::fmt;
12
13use crate::cper::CperSectionBody;
14use crate::source::CrashLogSource;
15
16/// Crash Log Metadata
17#[derive(Default)]
18pub struct Metadata {
19    /// Name of the computer where the Crash Log has been extracted from.
20    pub computer: Option<String>,
21    /// Name of the source where the Crash Log has been extracted from.
22    pub source: Option<CrashLogSource>,
23    /// Time of the extraction
24    pub time: Option<Time>,
25    /// When the Crash Log is extracted from a CPER, this field stores the extra CPER sections that
26    /// could be read from the CPER structure.
27    pub extra_cper_sections: Vec<CperSectionBody>,
28}
29
30/// Crash Log Extraction Time
31pub struct Time {
32    pub year: u16,
33    pub month: u8,
34    pub day: u8,
35    pub hour: u8,
36    pub minute: u8,
37}
38
39impl fmt::Display for Metadata {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        match (
42            self.computer.as_ref(),
43            self.source.as_ref(),
44            self.time.as_ref(),
45        ) {
46            (Some(computer), Some(source), Some(time)) => write!(f, "{computer}-{source}-{time}"),
47            (Some(computer), None, Some(time)) => write!(f, "{computer}-{time}"),
48            (None, None, Some(time)) => write!(f, "{time}"),
49            (None, Some(source), Some(time)) => write!(f, "{source}-{time}"),
50            (Some(computer), None, None) => write!(f, "{computer}"),
51            (Some(computer), Some(source), None) => write!(f, "{computer}-{source}"),
52            (None, Some(source), None) => write!(f, "{source}"),
53            (None, None, None) => write!(f, "unnamed"),
54        }
55    }
56}
57
58impl fmt::Display for Time {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        write!(
61            f,
62            "{:04}-{:02}-{:02}-{:02}-{:02}",
63            self.year, self.month, self.day, self.hour, self.minute
64        )
65    }
66}