Skip to main content

nautilus_common/logging/
headers.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_core::UUID4;
17use nautilus_model::identifiers::TraderId;
18use sysinfo::System;
19use ustr::Ustr;
20
21use crate::{enums::LogColor, logging::log_info};
22
23#[rustfmt::skip]
24pub fn log_header(trader_id: TraderId, machine_id: &str, instance_id: UUID4, component: Ustr) {
25    let mut sys = System::new();
26    sys.refresh_cpu_all();
27    sys.refresh_memory();
28
29    let c = component;
30
31    let kernel_version = System::kernel_version().map_or(String::new(), |v| format!("kernel-{v} "));
32    let os_version = System::long_os_version().unwrap_or_default();
33    let pid = std::process::id();
34
35    header_sepr(c, "=================================================================");
36    header_sepr(c, " NAUTILUS TRADER - Automated Algorithmic Trading Platform");
37    header_sepr(c, " by Nautech Systems Pty Ltd.");
38    header_sepr(c, " Copyright (C) 2015-2026. All rights reserved.");
39    header_sepr(c, "=================================================================");
40    header_line(c, "");
41    header_line(c, "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⣶⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀");
42    header_line(c, "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣾⣿⣿⣿⠀⢸⣿⣿⣿⣿⣶⣶⣤⣀⠀⠀⠀⠀⠀");
43    header_line(c, "⠀⠀⠀⠀⠀⠀⢀⣴⡇⢀⣾⣿⣿⣿⣿⣿⠀⣾⣿⣿⣿⣿⣿⣿⣿⠿⠓⠀⠀⠀⠀");
44    header_line(c, "⠀⠀⠀⠀⠀⣰⣿⣿⡀⢸⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⠟⠁⣠⣄⠀⠀⠀⠀");
45    header_line(c, "⠀⠀⠀⠀⢠⣿⣿⣿⣇⠀⢿⣿⣿⣿⣿⣿⠀⢻⣿⣿⣿⡿⢃⣠⣾⣿⣿⣧⡀⠀⠀");
46    header_line(c, "⠀⠀⠀⠠⣾⣿⣿⣿⣿⣿⣧⠈⠋⢀⣴⣧⠀⣿⡏⢠⡀⢸⣿⣿⣿⣿⣿⣿⣿⡇⠀");
47    header_line(c, "⠀⠀⠀⣀⠙⢿⣿⣿⣿⣿⣿⠇⢠⣿⣿⣿⡄⠹⠃⠼⠃⠈⠉⠛⠛⠛⠛⠛⠻⠇⠀");
48    header_line(c, "⠀⠀⢸⡟⢠⣤⠉⠛⠿⢿⣿⠀⢸⣿⡿⠋⣠⣤⣄⠀⣾⣿⣿⣶⣶⣶⣦⡄⠀⠀⠀");
49    header_line(c, "⠀⠀⠸⠀⣾⠏⣸⣷⠂⣠⣤⠀⠘⢁⣴⣾⣿⣿⣿⡆⠘⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀");
50    header_line(c, "⠀⠀⠀⠀⠛⠀⣿⡟⠀⢻⣿⡄⠸⣿⣿⣿⣿⣿⣿⣿⡀⠘⣿⣿⣿⣿⠟⠀⠀⠀⠀");
51    header_line(c, "⠀⠀⠀⠀⠀⠀⣿⠇⠀⠀⢻⡿⠀⠈⠻⣿⣿⣿⣿⣿⡇⠀⢹⣿⠿⠋⠀⠀⠀⠀⠀");
52    header_line(c, "⠀⠀⠀⠀⠀⠀⠋⠀⠀⠀⡘⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀");
53    header_line(c, "");
54    header_sepr(c, "=================================================================");
55    header_sepr(c, " SYSTEM SPECIFICATION");
56    header_sepr(c, "=================================================================");
57
58    if let Some(cpu) = sys.cpus().first() {
59        header_line(c, &format!("CPU architecture: {}", cpu.brand()));
60        header_line(c, &format!("CPU(s): {} @ {} MHz", sys.cpus().len(), cpu.frequency()));
61    } else {
62        header_line(c, "CPU: unknown");
63    }
64    header_line(c, &format!("OS: {kernel_version}{os_version}"));
65
66    log_sysinfo(component);
67
68    header_sepr(c, "=================================================================");
69    header_sepr(c, " IDENTIFIERS");
70    header_sepr(c, "=================================================================");
71    header_line(c, &format!("trader_id: {trader_id}"));
72    header_line(c, &format!("machine_id: {machine_id}"));
73    header_line(c, &format!("instance_id: {instance_id}"));
74    header_line(c, &format!("PID: {pid}"));
75
76    header_sepr(c, "=================================================================");
77    header_sepr(c, " VERSIONING");
78    header_sepr(c, "=================================================================");
79
80    #[cfg(not(feature = "python"))]
81    log_rust_versioning(c);
82
83    #[cfg(feature = "python")]
84    log_python_versioning(c);
85}
86
87#[cfg(not(feature = "python"))]
88#[rustfmt::skip]
89fn log_rust_versioning(c: Ustr) {
90    use nautilus_core::consts::NAUTILUS_VERSION;
91    header_line(c, &format!("nautilus_trader: {NAUTILUS_VERSION}"));
92}
93
94#[cfg(feature = "python")]
95#[rustfmt::skip]
96fn log_python_versioning(c: Ustr) {
97    if !python_available() {
98        return;
99    }
100
101    let package = "nautilus_trader";
102    header_line(c, &format!("{package}: {}", python_package_version(package)));
103    header_line(c, &format!("python: {}", python_version()));
104
105    for package in ["numpy", "pandas", "msgspec", "pyarrow", "pytz", "uvloop"] {
106        header_line(c, &format!("{package}: {}", python_package_version(package)));
107    }
108
109    header_sepr(c, "=================================================================");
110}
111
112#[cfg(feature = "python")]
113#[inline]
114#[allow(unsafe_code)]
115fn python_available() -> bool {
116    // SAFETY: `Py_IsInitialized` reads a flag and is safe to call at any
117    // time, even before the interpreter has been started.
118    unsafe { pyo3::ffi::Py_IsInitialized() != 0 }
119}
120
121#[rustfmt::skip]
122pub fn log_sysinfo(component: Ustr) {
123    let mut sys = System::new();
124    sys.refresh_memory();
125
126    let c = component;
127
128    let ram_total = sys.total_memory();
129    let ram_used = sys.used_memory();
130    let ram_used_p = (ram_used as f64 / ram_total as f64) * 100.0;
131    let ram_avail = ram_total - ram_used;
132    let ram_avail_p = (ram_avail as f64 / ram_total as f64) * 100.0;
133
134    header_sepr(c, "=================================================================");
135    header_sepr(c, " MEMORY USAGE");
136    header_sepr(c, "=================================================================");
137    header_line(c, &format!("RAM-Total: {:.2} GiB", bytes_to_gib(ram_total)));
138    header_line(c, &format!("RAM-Used: {:.2} GiB ({:.2}%)", bytes_to_gib(ram_used), ram_used_p));
139    header_line(c, &format!("RAM-Avail: {:.2} GiB ({:.2}%)", bytes_to_gib(ram_avail), ram_avail_p));
140
141    let swap_total = sys.total_swap();
142    if swap_total > 0 {
143        let swap_used = sys.used_swap();
144        let swap_used_p = (swap_used as f64 / swap_total as f64) * 100.0;
145        let swap_avail = swap_total.saturating_sub(swap_used);
146        let swap_avail_p = (swap_avail as f64 / swap_total as f64) * 100.0;
147        header_line(c, &format!("Swap-Total: {:.2} GiB", bytes_to_gib(swap_total)));
148        header_line(c, &format!("Swap-Used: {:.2} GiB ({:.2}%)", bytes_to_gib(swap_used), swap_used_p));
149        header_line(c, &format!("Swap-Avail: {:.2} GiB ({:.2}%)", bytes_to_gib(swap_avail), swap_avail_p));
150    } else {
151        header_line(c, "Swap: disabled");
152    }
153}
154
155fn header_sepr(c: Ustr, s: &str) {
156    log_info!("{}", s, color = LogColor::Cyan, component = c.as_str());
157}
158
159fn header_line(c: Ustr, s: &str) {
160    log_info!("{}", s, component = c.as_str());
161}
162
163fn bytes_to_gib(b: u64) -> f64 {
164    b as f64 / (2u64.pow(30) as f64)
165}
166
167#[cfg(feature = "python")]
168fn python_package_version(package: &str) -> String {
169    nautilus_core::python::version::get_python_package_version(package)
170}
171
172#[cfg(feature = "python")]
173fn python_version() -> String {
174    nautilus_core::python::version::get_python_version()
175}