nautilus_common/python/
logging.rsuse std::collections::HashMap;
use log::LevelFilter;
use nautilus_core::uuid::UUID4;
use nautilus_model::identifiers::TraderId;
use pyo3::prelude::*;
use ustr::Ustr;
use crate::{
enums::{LogColor, LogLevel},
logging::{
self, headers,
logger::{self, LogGuard, LoggerConfig},
logging_clock_set_realtime_mode, logging_clock_set_static_mode,
logging_clock_set_static_time, logging_set_bypass, map_log_level_to_filter,
parse_level_filter_str,
writer::FileWriterConfig,
},
};
#[pymethods]
impl LoggerConfig {
#[staticmethod]
#[pyo3(name = "from_spec")]
#[must_use]
pub fn py_from_spec(spec: String) -> Self {
Self::from_spec(&spec)
}
}
#[pymethods]
impl FileWriterConfig {
#[new]
#[pyo3(signature = (directory=None, file_name=None, file_format=None))]
#[must_use]
pub const fn py_new(
directory: Option<String>,
file_name: Option<String>,
file_format: Option<String>,
) -> Self {
Self::new(directory, file_name, file_format)
}
}
#[pyfunction()]
#[pyo3(name = "init_tracing")]
pub fn py_init_tracing() {
logging::init_tracing();
}
#[pyfunction]
#[pyo3(name = "init_logging")]
#[allow(clippy::too_many_arguments)]
#[pyo3(signature = (trader_id, instance_id, level_stdout, level_file=None, component_levels=None, directory=None, file_name=None, file_format=None, is_colored=None, is_bypassed=None, print_config=None))]
pub fn py_init_logging(
trader_id: TraderId,
instance_id: UUID4,
level_stdout: LogLevel,
level_file: Option<LogLevel>,
component_levels: Option<HashMap<String, String>>,
directory: Option<String>,
file_name: Option<String>,
file_format: Option<String>,
is_colored: Option<bool>,
is_bypassed: Option<bool>,
print_config: Option<bool>,
) -> LogGuard {
let level_file = level_file.map_or(LevelFilter::Off, map_log_level_to_filter);
let config = LoggerConfig::new(
map_log_level_to_filter(level_stdout),
level_file,
parse_component_levels(component_levels),
is_colored.unwrap_or(true),
print_config.unwrap_or(false),
);
let file_config = FileWriterConfig::new(directory, file_name, file_format);
if is_bypassed.unwrap_or(false) {
logging_set_bypass();
}
logging::init_logging(trader_id, instance_id, config, file_config)
}
fn parse_component_levels(
original_map: Option<HashMap<String, String>>,
) -> HashMap<Ustr, LevelFilter> {
match original_map {
Some(map) => {
let mut new_map = HashMap::new();
for (key, value) in map {
let ustr_key = Ustr::from(&key);
let value = parse_level_filter_str(&value);
new_map.insert(ustr_key, value);
}
new_map
}
None => HashMap::new(),
}
}
#[pyfunction]
#[pyo3(name = "logger_log")]
pub fn py_logger_log(level: LogLevel, color: LogColor, component: &str, message: &str) {
logger::log(level, color, Ustr::from(component), message);
}
#[pyfunction]
#[pyo3(name = "log_header")]
pub fn py_log_header(trader_id: TraderId, machine_id: &str, instance_id: UUID4, component: &str) {
headers::log_header(trader_id, machine_id, instance_id, Ustr::from(component));
}
#[pyfunction]
#[pyo3(name = "log_sysinfo")]
pub fn py_log_sysinfo(component: &str) {
headers::log_sysinfo(Ustr::from(component));
}
#[pyfunction]
#[pyo3(name = "logging_clock_set_static_mode")]
pub fn py_logging_clock_set_static_mode() {
logging_clock_set_static_mode();
}
#[pyfunction]
#[pyo3(name = "logging_clock_set_realtime_mode")]
pub fn py_logging_clock_set_realtime_mode() {
logging_clock_set_realtime_mode();
}
#[pyfunction]
#[pyo3(name = "logging_clock_set_static_time")]
pub fn py_logging_clock_set_static_time(time_ns: u64) {
logging_clock_set_static_time(time_ns);
}