sysinfo/
debug.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{
4    Component, ComponentExt, Cpu, CpuExt, Disk, DiskExt, NetworkData, NetworkExt, Networks,
5    NetworksExt, Process, ProcessExt, System, SystemExt,
6};
7
8use std::fmt;
9
10impl fmt::Debug for Cpu {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        f.debug_struct("Cpu")
13            .field("name", &self.name())
14            .field("CPU usage", &self.cpu_usage())
15            .field("frequency", &self.frequency())
16            .field("vendor ID", &self.vendor_id())
17            .field("brand", &self.brand())
18            .finish()
19    }
20}
21
22impl fmt::Debug for System {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.debug_struct("System")
25            .field("global CPU usage", &self.global_cpu_info().cpu_usage())
26            .field("load average", &self.load_average())
27            .field("total memory", &self.total_memory())
28            .field("free memory", &self.free_memory())
29            .field("total swap", &self.total_swap())
30            .field("free swap", &self.free_swap())
31            .field("nb CPUs", &self.cpus().len())
32            .field("nb network interfaces", &self.networks().iter().count())
33            .field("nb processes", &self.processes().len())
34            .field("nb disks", &self.disks().len())
35            .field("nb components", &self.components().len())
36            .finish()
37    }
38}
39
40impl fmt::Debug for Disk {
41    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(
43            fmt,
44            "Disk({:?})[FS: {:?}][Type: {:?}][removable: {}] mounted on {:?}: {}/{} B",
45            self.name(),
46            self.file_system()
47                .iter()
48                .map(|c| *c as char)
49                .collect::<Vec<_>>(),
50            self.kind(),
51            if self.is_removable() { "yes" } else { "no" },
52            self.mount_point(),
53            self.available_space(),
54            self.total_space(),
55        )
56    }
57}
58
59impl fmt::Debug for Process {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.debug_struct("Process")
62            .field("pid", &self.pid())
63            .field("parent", &self.parent())
64            .field("name", &self.name())
65            .field("environ", &self.environ())
66            .field("command", &self.cmd())
67            .field("executable path", &self.exe())
68            .field("current working directory", &self.cwd())
69            .field("memory usage", &self.memory())
70            .field("virtual memory usage", &self.virtual_memory())
71            .field("CPU usage", &self.cpu_usage())
72            .field("status", &self.status())
73            .field("root", &self.root())
74            .field("disk_usage", &self.disk_usage())
75            .finish()
76    }
77}
78
79impl fmt::Debug for Component {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        if let Some(critical) = self.critical() {
82            write!(
83                f,
84                "{}: {}°C (max: {}°C / critical: {}°C)",
85                self.label(),
86                self.temperature(),
87                self.max(),
88                critical
89            )
90        } else {
91            write!(
92                f,
93                "{}: {}°C (max: {}°C)",
94                self.label(),
95                self.temperature(),
96                self.max()
97            )
98        }
99    }
100}
101
102impl fmt::Debug for Networks {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(
105            f,
106            "Networks {{ {} }}",
107            self.iter()
108                .map(|x| format!("{x:?}"))
109                .collect::<Vec<_>>()
110                .join(", ")
111        )
112    }
113}
114
115impl fmt::Debug for NetworkData {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        f.debug_struct("NetworkData")
118            .field("income", &self.received())
119            .field("total income", &self.total_received())
120            .field("outcome", &self.transmitted())
121            .field("total outcome", &self.total_transmitted())
122            .field("packets income", &self.packets_received())
123            .field("total packets income", &self.total_packets_received())
124            .field("packets outcome", &self.packets_transmitted())
125            .field("total packets outcome", &self.total_packets_transmitted())
126            .field("errors income", &self.errors_on_received())
127            .field("total errors income", &self.total_errors_on_received())
128            .field("errors outcome", &self.errors_on_transmitted())
129            .field("total errors outcome", &self.total_errors_on_transmitted())
130            .finish()
131    }
132}