AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
logging.cpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "auto_apms_util/logging.hpp"
16
17#include "auto_apms_util/exceptions.hpp"
18#include "rclcpp/logging.hpp"
19#include "rcutils/error_handling.h"
20#include "rcutils/logging.h"
21
22namespace auto_apms_util
23{
24
25void setLoggingSeverity(const rclcpp::Logger & logger, const std::string & severity_string)
26{
27 int severity;
28 rcutils_ret_t ret =
29 rcutils_logging_severity_level_from_string(severity_string.c_str(), rcl_get_default_allocator(), &severity);
30 if (RCUTILS_RET_LOGGING_SEVERITY_STRING_INVALID == ret) {
31 throw exceptions::SetLoggingSeverityError("Unknown severity string '" + severity_string + "'");
32 }
33 if (ret != RCUTILS_RET_OK) {
34 const std::string msg = rcutils_get_error_string().str;
35 rcutils_reset_error();
36 throw exceptions::SetLoggingSeverityError(msg);
37 }
38
39 ret = rcutils_logging_set_logger_level(logger.get_name(), severity);
40 if (ret != RCUTILS_RET_OK) {
41 const std::string msg = rcutils_get_error_string().str;
42 rcutils_reset_error();
43 throw exceptions::SetLoggingSeverityError(msg);
44 }
45}
46
47void setLoggingSeverity(const rclcpp::Logger & logger, rclcpp::Logger::Level severity_level)
48{
49 switch (severity_level) {
50 case rclcpp::Logger::Level::Debug:
51 setLoggingSeverity(logger, "DEBUG");
52 break;
53 case rclcpp::Logger::Level::Info:
54 setLoggingSeverity(logger, "INFO");
55 break;
56 case rclcpp::Logger::Level::Warn:
57 setLoggingSeverity(logger, "WARN");
58 break;
59 case rclcpp::Logger::Level::Error:
60 setLoggingSeverity(logger, "ERROR");
61 break;
62 case rclcpp::Logger::Level::Fatal:
63 setLoggingSeverity(logger, "FATAL");
64 break;
65 case rclcpp::Logger::Level::Unset:
66 setLoggingSeverity(logger, "UNSET");
67 break;
68 }
69}
70
71} // namespace auto_apms_util
void setLoggingSeverity(const rclcpp::Logger &logger, const std::string &severity)
Set the logging severity of a ROS 2 logger.
Definition logging.cpp:25
Fundamental helper classes and utility functions.