1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Simplistic logger. It has 6 [`LogLevel`]s which can be set via [`set_log_level()`] and read via
//! [`get_log_level()`].
//!
//! The logger provides convinience macros to combine error/panic handling directly with the
//! logger.
//! The [`fail!`] macro can return when the function which was called return an error containing
//! result.
//! The [`fatal_panic!`] macro calls [`panic!`].
//!
//! # Example
//!
//! ## Logging
//! ```
//! use iceoryx2_bb_log::{debug, error, info, trace, warn};
//!
//! #[derive(Debug)]
//! struct MyDataType {
//! value: u64
//! }
//!
//! impl MyDataType {
//! fn log_stuff(&self) {
//! trace!("trace message");
//! trace!(from self, "trace message");
//! trace!(from "Custom::Origin", "trace message");
//!
//! debug!("hello {} {}", 123, 456);
//! debug!(from self, "hello {}", 123);
//! debug!(from "Another::Origin", "hello {}", 123);
//!
//! info!("world");
//! info!(from self, "world");
//! info!(from "hello", "world");
//!
//! warn!("warn message");
//! warn!(from self, "warning");
//! warn!(from "Somewhere::Else", "warning!");
//!
//! error!("bla {}", 1);
//! error!(from self, "bla {}", 1);
//! error!(from "error origin", "bla {}", 1);
//! }
//!}
//! ```
//!
//! ## Error Handling
//! ```
//! use iceoryx2_bb_log::fail;
//!
//! #[derive(Debug)]
//! struct MyDataType {
//! value: u64
//! }
//!
//! impl MyDataType {
//! fn doStuff(&self, value: u64) -> Result<(), ()> {
//! if value == 0 { Err(()) } else { Ok(()) }
//! }
//!
//! fn doMoreStuff(&self) -> Result<(), u64> {
//! // fail when doStuff.is_err() and return the error 1234
//! fail!(from self, when self.doStuff(0),
//! with 1234, "Failed while calling doStuff");
//! Ok(())
//! }
//!
//! fn doMore(&self) -> Result<(), u64> {
//! if self.value == 0 {
//! // without condition, return error 4567
//! fail!(from self, with 4567, "Value is zero");
//! }
//!
//! Ok(())
//! }
//!
//! fn evenMore(&self) -> Result<(), u64> {
//! // forward error when it is compatible or convertable
//! fail!(from self, when self.doMore(), "doMore failed");
//! Ok(())
//! }
//! }
//! ```
//!
//! ## Panic Handling
//! ```
//! use iceoryx2_bb_log::fatal_panic;
//!
//! #[derive(Debug)]
//! struct MyDataType {
//! value: u64
//! }
//!
//! impl MyDataType {
//! fn doStuff(&self, value: u64) {
//! if value == 0 {
//! fatal_panic!(from self, "value is {}", value);
//! }
//! }
//!
//! fn moreStuff(&self) -> Result<(), ()> {
//! if self.value == 0 { Err(()) } else { Ok(()) }
//! }
//!
//! fn doIt(&self) {
//! fatal_panic!(from self, when self.moreStuff(), "moreStuff failed");
//! }
//! }
//! ```
//! ## Setting custom logger on application startup
//!
//! In this example we use the [`crate::logger::buffer::Logger`], that stores every log
//! message in an internal buffer, and use it as the default logger.
//!
//! ```
//! use iceoryx2_bb_log::{set_logger, info};
//!
//! static LOGGER: iceoryx2_bb_log::logger::buffer::Logger =
//! iceoryx2_bb_log::logger::buffer::Logger::new();
//!
//! assert!(set_logger(&LOGGER));
//! info!("hello world");
//! let log_content = LOGGER.content();
//!
//! for entry in log_content {
//! println!("{:?} {} {}", entry.log_level, entry.origin, entry.message);
//! }
//! ```
#[macro_use]
pub mod log;
#[macro_use]
pub mod fail;
pub mod logger;
use std::{
fmt::Arguments,
sync::{
atomic::{AtomicU8, Ordering},
Once,
},
};
use logger::Logger;
#[cfg(feature = "logger_tracing")]
static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new();
#[cfg(feature = "logger_log")]
static DEFAULT_LOGGER: logger::log::Logger = logger::log::Logger::new();
#[cfg(not(any(feature = "logger_log", feature = "logger_tracing")))]
static DEFAULT_LOGGER: logger::console::Logger = logger::console::Logger::new();
const DEFAULT_LOG_LEVEL: u8 = LogLevel::Trace as u8;
static mut LOGGER: Option<&'static dyn logger::Logger> = None;
static LOG_LEVEL: AtomicU8 = AtomicU8::new(DEFAULT_LOG_LEVEL);
static INIT: Once = Once::new();
/// Describes the log level.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub enum LogLevel {
Trace = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
Fatal = 5,
}
/// Sets the current log level. This is ignored for external frameworks like `log` or `tracing`.
/// Here you have to use the log-level settings of that framework.
pub fn set_log_level(v: LogLevel) {
LOG_LEVEL.store(v as u8, Ordering::Relaxed);
}
/// Returns the current log level
pub fn get_log_level() -> u8 {
LOG_LEVEL.load(Ordering::Relaxed)
}
/// Sets the [`Logger`]. Can be only called once at the beginning of the program. If the
/// [`Logger`] is already set it returns false and does not update it.
pub fn set_logger<T: logger::Logger + 'static>(value: &'static T) -> bool {
let mut set_logger_success = false;
INIT.call_once(|| {
unsafe { LOGGER = Some(value) };
set_logger_success = true;
});
set_logger_success
}
/// Returns a reference to the [`Logger`].
pub fn get_logger() -> &'static dyn Logger {
INIT.call_once(|| {
unsafe { LOGGER = Some(&DEFAULT_LOGGER) };
});
unsafe { *LOGGER.as_ref().unwrap() }
}
#[doc(hidden)]
pub fn __internal_print_log_msg(log_level: LogLevel, origin: Arguments, args: Arguments) {
get_logger().log(log_level, origin, args)
}