|
| 1 | +use crate::rcl_bindings::RCUTILS_LOG_SEVERITY; |
| 2 | +use std::{borrow::Borrow, ffi::CString, time::Duration}; |
| 3 | + |
| 4 | +/// These parameters determine the behavior of an instance of logging. |
| 5 | +#[derive(Debug, Clone, Copy)] |
| 6 | +pub struct LogParams<'a> { |
| 7 | + /// The name of the logger |
| 8 | + logger_name: LoggerName<'a>, |
| 9 | + /// The severity of the logging instance. |
| 10 | + severity: LogSeverity, |
| 11 | + /// Specify when a log message should be published (See[`LoggingOccurrence`] above) |
| 12 | + occurs: LogOccurrence, |
| 13 | + /// Specify the publication interval of the message. A value of ZERO (0) indicates that the |
| 14 | + /// message should be published every time, otherwise, the message will only be published once |
| 15 | + /// the specified interval has elapsed. |
| 16 | + /// This field is typically used to limit the output from high-frequency messages, e.g. instead |
| 17 | + /// of publishing a log message every 10 milliseconds, the `publish_interval` can be configured |
| 18 | + /// such that the message is published every 10 seconds. |
| 19 | + interval: Duration, |
| 20 | + /// The log message will only published if the specified expression evaluates to true |
| 21 | + only_if: bool, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'a> LogParams<'a> { |
| 25 | + /// Create a set of default log parameters, given the name of a logger |
| 26 | + pub fn new(logger_name: LoggerName<'a>) -> Self { |
| 27 | + Self { |
| 28 | + logger_name, |
| 29 | + severity: Default::default(), |
| 30 | + occurs: Default::default(), |
| 31 | + interval: Duration::new(0, 0), |
| 32 | + only_if: true, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Get the logger name |
| 37 | + pub fn get_logger_name(&self) -> &LoggerName { |
| 38 | + &self.logger_name |
| 39 | + } |
| 40 | + |
| 41 | + /// Get the severity of the log |
| 42 | + pub fn get_severity(&self) -> LogSeverity { |
| 43 | + self.severity |
| 44 | + } |
| 45 | + |
| 46 | + /// Get the occurrence |
| 47 | + pub fn get_occurence(&self) -> LogOccurrence { |
| 48 | + self.occurs |
| 49 | + } |
| 50 | + |
| 51 | + /// Get the interval |
| 52 | + pub fn get_interval(&self) -> Duration { |
| 53 | + self.interval |
| 54 | + } |
| 55 | + |
| 56 | + /// Get the arbitrary filter set by the user |
| 57 | + pub fn get_user_filter(&self) -> bool { |
| 58 | + self.only_if |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Methods for defining the behavior of a logging instance. |
| 63 | +/// |
| 64 | +/// This trait is implemented by Logger, Node, and anything that a `&str` can be |
| 65 | +/// [borrowed][1] from, such as string literals (`"my_str"`), [`String`], or |
| 66 | +/// [`Cow<str>`][2]. |
| 67 | +/// |
| 68 | +/// [1]: Borrow |
| 69 | +/// [2]: std::borrow::Cow |
| 70 | +pub trait ToLogParams<'a>: Sized { |
| 71 | + /// Convert the object into LogParams with default settings |
| 72 | + fn to_log_params(self) -> LogParams<'a>; |
| 73 | + |
| 74 | + /// The logging should only happen once |
| 75 | + fn once(self) -> LogParams<'a> { |
| 76 | + self.occurs(LogOccurrence::Once) |
| 77 | + } |
| 78 | + |
| 79 | + /// The first time the logging happens, we should skip it |
| 80 | + fn skip_first(self) -> LogParams<'a> { |
| 81 | + self.occurs(LogOccurrence::SkipFirst) |
| 82 | + } |
| 83 | + |
| 84 | + /// Set the occurrence behavior of the log instance |
| 85 | + fn occurs(self, occurs: LogOccurrence) -> LogParams<'a> { |
| 86 | + let mut params = self.to_log_params(); |
| 87 | + params.occurs = occurs; |
| 88 | + params |
| 89 | + } |
| 90 | + |
| 91 | + /// Set an interval during which this log will not publish. A value of zero |
| 92 | + /// will never block the message from being published, and this is the |
| 93 | + /// default behavior. |
| 94 | + /// |
| 95 | + /// A negative duration is not valid, but will be treated as a zero duration. |
| 96 | + fn interval(self, interval: Duration) -> LogParams<'a> { |
| 97 | + let mut params = self.to_log_params(); |
| 98 | + params.interval = interval; |
| 99 | + params |
| 100 | + } |
| 101 | + |
| 102 | + /// The log will not be published if a `false` expression is passed into |
| 103 | + /// this function. |
| 104 | + /// |
| 105 | + /// Other factors may prevent the log from being published if a `true` is |
| 106 | + /// passed in, such as `ToLogParams::interval` or `ToLogParams::once` |
| 107 | + /// filtering the log. |
| 108 | + fn only_if(self, only_if: bool) -> LogParams<'a> { |
| 109 | + let mut params = self.to_log_params(); |
| 110 | + params.only_if = only_if; |
| 111 | + params |
| 112 | + } |
| 113 | + |
| 114 | + /// Log as a debug message. |
| 115 | + fn debug(self) -> LogParams<'a> { |
| 116 | + self.severity(LogSeverity::Debug) |
| 117 | + } |
| 118 | + |
| 119 | + /// Log as an informative message. This is the default, so you don't |
| 120 | + /// generally need to use this. |
| 121 | + fn info(self) -> LogParams<'a> { |
| 122 | + self.severity(LogSeverity::Info) |
| 123 | + } |
| 124 | + |
| 125 | + /// Log as a warning message. |
| 126 | + fn warn(self) -> LogParams<'a> { |
| 127 | + self.severity(LogSeverity::Warn) |
| 128 | + } |
| 129 | + |
| 130 | + /// Log as an error message. |
| 131 | + fn error(self) -> LogParams<'a> { |
| 132 | + self.severity(LogSeverity::Error) |
| 133 | + } |
| 134 | + |
| 135 | + /// Log as a fatal message. |
| 136 | + fn fatal(self) -> LogParams<'a> { |
| 137 | + self.severity(LogSeverity::Fatal) |
| 138 | + } |
| 139 | + |
| 140 | + /// Set the severity for this instance of logging. The default value will be |
| 141 | + /// [`LogSeverity::Info`]. |
| 142 | + fn severity(self, severity: LogSeverity) -> LogParams<'a> { |
| 143 | + let mut params = self.to_log_params(); |
| 144 | + params.severity = severity; |
| 145 | + params |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/// This is used to borrow a logger name which might be validated (e.g. came |
| 150 | +/// from a [`Logger`][1] struct) or not (e.g. a user-defined `&str`). If an |
| 151 | +/// unvalidated logger name is used with one of the logging macros, we will log |
| 152 | +/// an error about it, and the original log message will be logged with the |
| 153 | +/// default logger. |
| 154 | +/// |
| 155 | +/// [1]: crate::Logger |
| 156 | +#[derive(Debug, Clone, Copy)] |
| 157 | +pub enum LoggerName<'a> { |
| 158 | + /// The logger name is already available as a valid CString |
| 159 | + Validated(&'a CString), |
| 160 | + /// The logger name has not been validated yet |
| 161 | + Unvalidated(&'a str), |
| 162 | +} |
| 163 | + |
| 164 | +/// Logging severity. |
| 165 | +#[doc(hidden)] |
| 166 | +#[derive(Debug, Clone, Copy)] |
| 167 | +pub enum LogSeverity { |
| 168 | + /// Use the severity level of the parent logger (or the root logger if the |
| 169 | + /// current logger has no parent) |
| 170 | + Unset, |
| 171 | + /// For messages that are not needed outside of debugging. |
| 172 | + Debug, |
| 173 | + /// For messages that provide useful information about the state of the |
| 174 | + /// application. |
| 175 | + Info, |
| 176 | + /// For messages that indicate something unusual or unintended might have happened. |
| 177 | + Warn, |
| 178 | + /// For messages that indicate an error has occurred which may cause the application |
| 179 | + /// to misbehave. |
| 180 | + Error, |
| 181 | + /// For messages that indicate an error has occurred which is so severe that the |
| 182 | + /// application should terminate because it cannot recover. |
| 183 | + /// |
| 184 | + /// Using this severity level will not automatically cause the application to |
| 185 | + /// terminate, the application developer must decide how to do that on a |
| 186 | + /// case-by-case basis. |
| 187 | + Fatal, |
| 188 | +} |
| 189 | + |
| 190 | +impl LogSeverity { |
| 191 | + pub(super) fn as_native(&self) -> RCUTILS_LOG_SEVERITY { |
| 192 | + use crate::rcl_bindings::rcl_log_severity_t::*; |
| 193 | + match self { |
| 194 | + LogSeverity::Unset => RCUTILS_LOG_SEVERITY_UNSET, |
| 195 | + LogSeverity::Debug => RCUTILS_LOG_SEVERITY_DEBUG, |
| 196 | + LogSeverity::Info => RCUTILS_LOG_SEVERITY_INFO, |
| 197 | + LogSeverity::Warn => RCUTILS_LOG_SEVERITY_WARN, |
| 198 | + LogSeverity::Error => RCUTILS_LOG_SEVERITY_ERROR, |
| 199 | + LogSeverity::Fatal => RCUTILS_LOG_SEVERITY_FATAL, |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +impl Default for LogSeverity { |
| 205 | + fn default() -> Self { |
| 206 | + Self::Info |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +/// Specify when a log message should be published |
| 211 | +#[derive(Debug, Clone, Copy)] |
| 212 | +pub enum LogOccurrence { |
| 213 | + /// Every message will be published if all other conditions are met |
| 214 | + All, |
| 215 | + /// The message will only be published on the first occurrence (Note: no other conditions apply) |
| 216 | + Once, |
| 217 | + /// The log message will not be published on the first occurrence, but will be published on |
| 218 | + /// each subsequent occurrence (assuming all other conditions are met) |
| 219 | + SkipFirst, |
| 220 | +} |
| 221 | + |
| 222 | +impl Default for LogOccurrence { |
| 223 | + fn default() -> Self { |
| 224 | + Self::All |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +// Anything that we can borrow a string from can be used as if it's a logger and |
| 229 | +// turned into LogParams |
| 230 | +impl<'a, T: Borrow<str>> ToLogParams<'a> for &'a T { |
| 231 | + fn to_log_params(self) -> LogParams<'a> { |
| 232 | + LogParams::new(LoggerName::Unvalidated(self.borrow())) |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +impl<'a> ToLogParams<'a> for &'a str { |
| 237 | + fn to_log_params(self) -> LogParams<'a> { |
| 238 | + LogParams::new(LoggerName::Unvalidated(self)) |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +impl<'a> ToLogParams<'a> for LogParams<'a> { |
| 243 | + fn to_log_params(self) -> LogParams<'a> { |
| 244 | + self |
| 245 | + } |
| 246 | +} |
0 commit comments