iceoryx2_loggers/lib.rs
1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13//! Concrete logger backend implementations for iceoryx2.
14//!
15//! This crate provides logger implementations that can be registered with
16//! `iceoryx2_log` using `iceoryx2_log::set_logger()`. Each logger is
17//! feature-gated, allowing users to include only what they need.
18//!
19//! # Architecture
20//!
21//! The iceoryx2 logging system is split into two crates:
22//! - **`iceoryx2_log`**: The frontend providing the logging API and macros
23//! - **`iceoryx2_loggers`**: This crate, providing selectable logger backends
24//!
25//! This separation keeps the logging API lightweight and platform-agnostic
26//! while allowing flexible backend selection at runtime.
27//!
28//! See `iceoryx2_log` for usage examples and the complete logging API.
29//!
30//! # Feature Flags
31//!
32//! Exactly one of these three features must be selected according to your
33//! platform:
34//!
35//! * `std` - Build for platforms that have `std` support
36//! * `posix` - Build for platforms that have a POSIX abastraction, but no `std` support
37//! * `bare_metal` - Build for bare metal platforms
38//!
39//! Optionally, the default logger can also be configured. If none are
40//! configured, the null logger is used and all logs are discarded:
41//!
42//! * `buffer` - output log messages to a buffer
43//! * `console` - output log messages to the console
44//! * `file` - output log messages to the file
45//! * `log` - utilize the `log` crate to output log messages
46//! * `tracing` - utilize the `tracing` crate to output log messages
47
48#![cfg_attr(not(feature = "std"), no_std)]
49#![warn(clippy::alloc_instead_of_core)]
50#![warn(clippy::std_instead_of_alloc)]
51#![warn(clippy::std_instead_of_core)]
52
53use core::fmt::Write;
54
55use iceoryx2_log_types::Log;
56
57#[cfg(feature = "buffer")]
58mod buffer;
59#[cfg(feature = "console")]
60mod console;
61#[cfg(feature = "file")]
62mod file;
63#[cfg(feature = "log")]
64mod log;
65#[cfg(feature = "tracing")]
66mod tracing;
67
68mod null;
69mod writer;
70
71extern crate alloc;
72
73#[no_mangle]
74pub extern "Rust" fn __internal_stdout() -> &'static mut dyn Write {
75 writer::stdout()
76}
77
78#[no_mangle]
79pub extern "Rust" fn __internal_stderr() -> &'static mut dyn Write {
80 writer::stderr()
81}
82
83#[cfg(feature = "console")]
84#[no_mangle]
85pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
86 {
87 static CONSOLE_LOGGER: console::Logger = console::Logger::new();
88 &CONSOLE_LOGGER
89 }
90}
91
92#[cfg(feature = "buffer")]
93#[no_mangle]
94pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
95 {
96 static BUFFER_LOGGER: buffer::Logger = buffer::Logger::new();
97 &BUFFER_LOGGER
98 }
99}
100
101#[cfg(feature = "file")]
102#[no_mangle]
103pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
104 {
105 static FILE_NAME: &str = "iceoryx2.log";
106 static FILE_LOGGER: std::sync::LazyLock<file::Logger> =
107 std::sync::LazyLock::new(|| file::Logger::new(FILE_NAME));
108 &*FILE_LOGGER
109 }
110}
111
112#[cfg(feature = "log")]
113#[no_mangle]
114pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
115 {
116 static LOG_LOGGER: log::Logger = log::Logger::new();
117 &LOG_LOGGER
118 }
119}
120
121#[cfg(feature = "tracing")]
122#[no_mangle]
123pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
124 {
125 static TRACING_LOGGER: tracing::Logger = tracing::Logger::new();
126 &TRACING_LOGGER
127 }
128}
129
130#[cfg(not(any(
131 feature = "console",
132 feature = "buffer",
133 feature = "file",
134 feature = "log",
135 feature = "tracing"
136)))]
137#[no_mangle]
138pub extern "Rust" fn __internal_default_logger() -> &'static dyn Log {
139 {
140 static NULL_LOGGER: null::Logger = null::Logger;
141 &NULL_LOGGER
142 }
143}