maliput/common/mod.rs
1// BSD 3-Clause License
2//
3// Copyright (c) 2025, Woven by Toyota.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// * Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// * Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// * Neither the name of the copyright holder nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31/// Log levels for the under the hood maliput library's logging system.
32///
33/// See https://github.com/maliput/maliput/blob/main/include/maliput/common/logger.h
34/// for more details on the logging system and available log levels.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum LogLevel {
37 Off,
38 Trace,
39 Debug,
40 Info,
41 Warn,
42 Error,
43 Critical,
44 Unchanged,
45}
46
47impl From<LogLevel> for &'static str {
48 fn from(level: LogLevel) -> Self {
49 match level {
50 LogLevel::Off => "off",
51 LogLevel::Trace => "trace",
52 LogLevel::Debug => "debug",
53 LogLevel::Info => "info",
54 LogLevel::Warn => "warn",
55 LogLevel::Error => "error",
56 LogLevel::Critical => "critical",
57 LogLevel::Unchanged => "unchanged",
58 }
59 }
60}
61impl From<String> for LogLevel {
62 fn from(level: String) -> Self {
63 match level.as_str() {
64 "off" => LogLevel::Off,
65 "trace" => LogLevel::Trace,
66 "debug" => LogLevel::Debug,
67 "info" => LogLevel::Info,
68 "warn" => LogLevel::Warn,
69 "error" => LogLevel::Error,
70 "critical" => LogLevel::Critical,
71 "unchanged" => LogLevel::Unchanged,
72 _ => panic!("Invalid log level: {}", level),
73 }
74 }
75}
76impl std::fmt::Display for LogLevel {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 match self {
79 LogLevel::Off => write!(f, "off"),
80 LogLevel::Trace => write!(f, "trace"),
81 LogLevel::Debug => write!(f, "debug"),
82 LogLevel::Info => write!(f, "info"),
83 LogLevel::Warn => write!(f, "warn"),
84 LogLevel::Error => write!(f, "error"),
85 LogLevel::Critical => write!(f, "critical"),
86 LogLevel::Unchanged => write!(f, "unchanged"),
87 }
88 }
89}
90
91/// Set the log level for the maliput library's logging system.
92///
93/// # Arguments
94///
95/// * `level` - The desired log level to set. This should be one of the variants of `LogLevel`.
96///
97/// # Returns
98///
99/// A string indicating the previous log level before the change.
100///
101pub fn set_log_level(level: LogLevel) -> LogLevel {
102 maliput_sys::common::ffi::LOG_set_log_level(level.into()).into()
103}
104
105#[cfg(test)]
106mod tests {
107 use super::set_log_level;
108 use super::LogLevel;
109 #[test]
110 fn test_log_level() {
111 set_log_level(LogLevel::Off);
112 let last_level = set_log_level(LogLevel::Trace);
113 assert_eq!(last_level, LogLevel::Off);
114 let last_level = set_log_level(LogLevel::Debug);
115 assert_eq!(last_level, LogLevel::Trace);
116 let last_level = set_log_level(LogLevel::Info);
117 assert_eq!(last_level, LogLevel::Debug);
118 let last_level = set_log_level(LogLevel::Warn);
119 assert_eq!(last_level, LogLevel::Info);
120 let last_level = set_log_level(LogLevel::Error);
121 assert_eq!(last_level, LogLevel::Warn);
122 let last_level = set_log_level(LogLevel::Critical);
123 assert_eq!(last_level, LogLevel::Error);
124 let last_level = set_log_level(LogLevel::Unchanged);
125 assert_eq!(last_level, LogLevel::Critical);
126 let last_level = set_log_level(LogLevel::Off);
127 assert_eq!(last_level, LogLevel::Critical);
128 // TODO(francocipollone): Test an invalid log level. It throws under the hood in c++.
129 }
130}