Skip to main content

nautilus_common/ffi/
logging.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 std::{
17    ffi::c_char,
18    ops::{Deref, DerefMut},
19};
20
21use ahash::AHashMap;
22use nautilus_core::{
23    UUID4,
24    ffi::{
25        parsing::{optional_bytes_to_json, u8_as_bool},
26        string::{cstr_as_str, cstr_to_ustr, optional_cstr_to_str},
27    },
28};
29use nautilus_model::identifiers::TraderId;
30
31use crate::{
32    enums::{LogColor, LogLevel},
33    logging::{
34        headers, init_logging,
35        logger::{self, LogGuard, LoggerConfig},
36        map_log_level_to_filter, parse_component_levels,
37        writer::FileWriterConfig,
38    },
39};
40
41/// C compatible Foreign Function Interface (FFI) for an underlying [`LogGuard`].
42///
43/// This struct wraps `LogGuard` in a way that makes it compatible with C function
44/// calls, enabling interaction with `LogGuard` in a C environment.
45///
46/// It implements the `Deref` trait, allowing instances of `LogGuard_API` to be
47/// dereferenced to `LogGuard`, providing access to `LogGuard`'s methods without
48/// having to manually access the underlying `LogGuard` instance.
49#[repr(C)]
50#[derive(Debug)]
51#[allow(non_camel_case_types)]
52pub struct LogGuard_API(Box<LogGuard>);
53
54impl Deref for LogGuard_API {
55    type Target = LogGuard;
56
57    fn deref(&self) -> &Self::Target {
58        &self.0
59    }
60}
61
62impl DerefMut for LogGuard_API {
63    fn deref_mut(&mut self) -> &mut Self::Target {
64        &mut self.0
65    }
66}
67
68/// Initializes logging.
69///
70/// Logging should be used for Python and sync Rust logic which is most of
71/// the components in the [nautilus_trader](https://pypi.org/project/nautilus_trader) package.
72/// Logging can be configured to filter components and write up to a specific level only
73/// by passing a configuration using the `NAUTILUS_LOG` environment variable.
74///
75/// # Safety
76///
77/// Should only be called once during an application's run, ideally at the
78/// beginning of the run.
79///
80/// This function assumes:
81/// - `directory_ptr` is either NULL or a valid C string pointer.
82/// - `file_name_ptr` is either NULL or a valid C string pointer.
83/// - `file_format_ptr` is either NULL or a valid C string pointer.
84/// - `component_level_ptr` is either NULL or a valid C string pointer.
85///
86/// # Panics
87///
88/// Panics if initializing the Rust logger fails.
89#[unsafe(no_mangle)]
90pub unsafe extern "C" fn logging_init(
91    trader_id: TraderId,
92    instance_id: UUID4,
93    level_stdout: LogLevel,
94    level_file: LogLevel,
95    directory_ptr: *const c_char,
96    file_name_ptr: *const c_char,
97    file_format_ptr: *const c_char,
98    component_levels_ptr: *const c_char,
99    is_colored: u8,
100    is_bypassed: u8,
101    print_config: u8,
102    log_components_only: u8,
103    max_file_size: u64,
104    max_backup_count: u32,
105) -> LogGuard_API {
106    let level_stdout = map_log_level_to_filter(level_stdout);
107    let level_file = map_log_level_to_filter(level_file);
108
109    let component_levels_json = unsafe { optional_bytes_to_json(component_levels_ptr) };
110    let component_levels = parse_component_levels(component_levels_json)
111        .expect("Failed to parse component log levels");
112
113    let config = LoggerConfig::new(
114        level_stdout,
115        level_file,
116        component_levels,
117        AHashMap::new(), // module_level - not exposed to FFI
118        u8_as_bool(log_components_only),
119        u8_as_bool(is_colored),
120        u8_as_bool(print_config),
121        false, // use_tracing - not exposed to FFI
122        u8_as_bool(is_bypassed),
123        None,  // file_config - passed separately to init_logging
124        false, // clear_log_file
125    );
126
127    // Configure file rotation if max_file_size > 0
128    let file_rotate = if max_file_size > 0 {
129        Some((max_file_size, max_backup_count))
130    } else {
131        None
132    };
133
134    let directory = unsafe { optional_cstr_to_str(directory_ptr).map(ToString::to_string) };
135    let file_name = unsafe { optional_cstr_to_str(file_name_ptr).map(ToString::to_string) };
136    let file_format = unsafe { optional_cstr_to_str(file_format_ptr).map(ToString::to_string) };
137
138    let file_config = FileWriterConfig::new(directory, file_name, file_format, file_rotate);
139
140    if u8_as_bool(is_bypassed) {
141        logging_set_bypass();
142    }
143
144    LogGuard_API(Box::new(
145        init_logging(trader_id, instance_id, config, file_config)
146            .expect("Failed to initialize logging"),
147    ))
148}
149
150/// Creates a new log event.
151///
152/// # Safety
153///
154/// This function assumes:
155/// - `component_ptr` is a valid C string pointer.
156/// - `message_ptr` is a valid C string pointer.
157#[unsafe(no_mangle)]
158pub unsafe extern "C" fn logger_log(
159    level: LogLevel,
160    color: LogColor,
161    component_ptr: *const c_char,
162    message_ptr: *const c_char,
163) {
164    let component = unsafe { cstr_to_ustr(component_ptr) };
165    let message = unsafe { cstr_as_str(message_ptr) };
166
167    logger::log(level, color, component, message);
168}
169
170/// Logs the Nautilus system header.
171///
172/// # Safety
173///
174/// This function assumes:
175/// - `machine_id_ptr` is a valid C string pointer.
176/// - `component_ptr` is a valid C string pointer.
177#[unsafe(no_mangle)]
178pub unsafe extern "C" fn logging_log_header(
179    trader_id: TraderId,
180    machine_id_ptr: *const c_char,
181    instance_id: UUID4,
182    component_ptr: *const c_char,
183) {
184    let component = unsafe { cstr_to_ustr(component_ptr) };
185    let machine_id = unsafe { cstr_as_str(machine_id_ptr) };
186    headers::log_header(trader_id, machine_id, instance_id, component);
187}
188
189/// Logs system information.
190///
191/// # Safety
192///
193/// Assumes `component_ptr` is a valid C string pointer.
194#[unsafe(no_mangle)]
195pub unsafe extern "C" fn logging_log_sysinfo(component_ptr: *const c_char) {
196    let component = unsafe { cstr_to_ustr(component_ptr) };
197    headers::log_sysinfo(component);
198}
199
200/// Flushes global logger buffers of any records.
201#[unsafe(no_mangle)]
202pub extern "C" fn logger_flush() {
203    log::logger().flush();
204}
205
206/// Flushes global logger buffers of any records and then drops the logger.
207#[unsafe(no_mangle)]
208pub extern "C" fn logger_drop(log_guard: LogGuard_API) {
209    drop(log_guard);
210}
211
212#[unsafe(no_mangle)]
213pub extern "C" fn logging_is_initialized() -> u8 {
214    u8::from(crate::logging::logging_is_initialized())
215}
216
217#[unsafe(no_mangle)]
218pub extern "C" fn logging_set_bypass() {
219    crate::logging::logging_set_bypass();
220}
221
222#[unsafe(no_mangle)]
223pub extern "C" fn logging_shutdown() {
224    crate::logging::logging_shutdown();
225}
226
227#[unsafe(no_mangle)]
228pub extern "C" fn logging_is_colored() -> u8 {
229    u8::from(crate::logging::logging_is_colored())
230}
231
232#[unsafe(no_mangle)]
233pub extern "C" fn logging_clock_set_realtime_mode() {
234    crate::logging::logging_clock_set_realtime_mode();
235}
236
237#[unsafe(no_mangle)]
238pub extern "C" fn logging_clock_set_static_mode() {
239    crate::logging::logging_clock_set_static_mode();
240}
241
242#[unsafe(no_mangle)]
243pub extern "C" fn logging_clock_set_static_time(time_ns: u64) {
244    crate::logging::logging_clock_set_static_time(time_ns);
245}