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/// Error types for maliput lib.
32#[derive(Debug, PartialEq, Eq, thiserror::Error)]
33pub enum MaliputError {
34 // TODO(francocipollone): Add more specific error types as we handle it from the C++ side.
35 #[error("Maliput assertion error: {0}")]
36 AssertionError(String),
37 #[error("Other: {0}")]
38 Other(String),
39}
40
41impl From<cxx::Exception> for MaliputError {
42 fn from(e: cxx::Exception) -> Self {
43 MaliputError::AssertionError(e.to_string())
44 }
45}
46
47/// Log levels for the under the hood maliput library's logging system.
48///
49/// See https://github.com/maliput/maliput/blob/main/include/maliput/common/logger.h
50/// for more details on the logging system and available log levels.
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum LogLevel {
53 Off,
54 Trace,
55 Debug,
56 Info,
57 Warn,
58 Error,
59 Critical,
60 Unchanged,
61}
62
63impl From<LogLevel> for &'static str {
64 fn from(level: LogLevel) -> Self {
65 match level {
66 LogLevel::Off => "off",
67 LogLevel::Trace => "trace",
68 LogLevel::Debug => "debug",
69 LogLevel::Info => "info",
70 LogLevel::Warn => "warn",
71 LogLevel::Error => "error",
72 LogLevel::Critical => "critical",
73 LogLevel::Unchanged => "unchanged",
74 }
75 }
76}
77impl From<String> for LogLevel {
78 fn from(level: String) -> Self {
79 match level.as_str() {
80 "off" => LogLevel::Off,
81 "trace" => LogLevel::Trace,
82 "debug" => LogLevel::Debug,
83 "info" => LogLevel::Info,
84 "warn" => LogLevel::Warn,
85 "error" => LogLevel::Error,
86 "critical" => LogLevel::Critical,
87 "unchanged" => LogLevel::Unchanged,
88 _ => panic!("Invalid log level: {}", level),
89 }
90 }
91}
92impl std::fmt::Display for LogLevel {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 match self {
95 LogLevel::Off => write!(f, "off"),
96 LogLevel::Trace => write!(f, "trace"),
97 LogLevel::Debug => write!(f, "debug"),
98 LogLevel::Info => write!(f, "info"),
99 LogLevel::Warn => write!(f, "warn"),
100 LogLevel::Error => write!(f, "error"),
101 LogLevel::Critical => write!(f, "critical"),
102 LogLevel::Unchanged => write!(f, "unchanged"),
103 }
104 }
105}
106
107/// Set the log level for the maliput library's logging system.
108///
109/// # Arguments
110///
111/// * `level` - The desired log level to set. This should be one of the variants of `LogLevel`.
112///
113/// # Returns
114///
115/// A string indicating the previous log level before the change.
116///
117pub fn set_log_level(level: LogLevel) -> LogLevel {
118 maliput_sys::common::ffi::LOG_set_log_level(level.into()).into()
119}
120
121#[cfg(test)]
122mod tests {
123 use super::set_log_level;
124 use super::LogLevel;
125 #[test]
126 fn test_log_level() {
127 set_log_level(LogLevel::Off);
128 let last_level = set_log_level(LogLevel::Trace);
129 assert_eq!(last_level, LogLevel::Off);
130 let last_level = set_log_level(LogLevel::Debug);
131 assert_eq!(last_level, LogLevel::Trace);
132 let last_level = set_log_level(LogLevel::Info);
133 assert_eq!(last_level, LogLevel::Debug);
134 let last_level = set_log_level(LogLevel::Warn);
135 assert_eq!(last_level, LogLevel::Info);
136 let last_level = set_log_level(LogLevel::Error);
137 assert_eq!(last_level, LogLevel::Warn);
138 let last_level = set_log_level(LogLevel::Critical);
139 assert_eq!(last_level, LogLevel::Error);
140 let last_level = set_log_level(LogLevel::Unchanged);
141 assert_eq!(last_level, LogLevel::Critical);
142 let last_level = set_log_level(LogLevel::Off);
143 assert_eq!(last_level, LogLevel::Critical);
144 // TODO(francocipollone): Test an invalid log level. It throws under the hood in c++.
145 }
146}