AutoAPMS
Resilient Robot Mission Management
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 exposeToGlobalDebugLogging(const rclcpp::Logger & logger)
26{
27 (void)logger;
28#ifdef _AUTO_APMS_DEBUG_LOGGING
29 auto ret = rcutils_logging_set_logger_level(logger.get_name(), RCUTILS_LOG_SEVERITY_DEBUG);
30 if (ret != RCUTILS_RET_OK) {
31 const std::string msg = rcutils_get_error_string().str;
32 rcutils_reset_error();
33 throw exceptions::SetLoggingSeverityError("Failed to expose the logger to global DEBUG logging: " + msg);
34 }
35#endif
36}
37
38void setLoggingSeverity(const rclcpp::Logger & logger, const std::string & severity_string)
39{
40 int severity;
41 rcutils_ret_t ret =
42 rcutils_logging_severity_level_from_string(severity_string.c_str(), rcl_get_default_allocator(), &severity);
43 if (RCUTILS_RET_LOGGING_SEVERITY_STRING_INVALID == ret) {
44 throw exceptions::SetLoggingSeverityError("Unknown severity string '" + severity_string + "'");
45 }
46 if (ret != RCUTILS_RET_OK) {
47 const std::string msg = rcutils_get_error_string().str;
48 rcutils_reset_error();
49 throw exceptions::SetLoggingSeverityError(msg);
50 }
51
52 ret = rcutils_logging_set_logger_level(logger.get_name(), severity);
53 if (ret != RCUTILS_RET_OK) {
54 const std::string msg = rcutils_get_error_string().str;
55 rcutils_reset_error();
56 throw exceptions::SetLoggingSeverityError(msg);
57 }
58}
59
60void setLoggingSeverity(const rclcpp::Logger & logger, rclcpp::Logger::Level severity_level)
61{
62 switch (severity_level) {
63 case rclcpp::Logger::Level::Debug:
64 setLoggingSeverity(logger, "DEBUG");
65 break;
66 case rclcpp::Logger::Level::Info:
67 setLoggingSeverity(logger, "INFO");
68 break;
69 case rclcpp::Logger::Level::Warn:
70 setLoggingSeverity(logger, "WARN");
71 break;
72 case rclcpp::Logger::Level::Error:
73 setLoggingSeverity(logger, "ERROR");
74 break;
75 case rclcpp::Logger::Level::Fatal:
76 setLoggingSeverity(logger, "FATAL");
77 break;
78 case rclcpp::Logger::Level::Unset:
79 setLoggingSeverity(logger, "UNSET");
80 break;
81 }
82}
83
84} // 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:38
void exposeToGlobalDebugLogging(const rclcpp::Logger &logger)
Enable ROS 2 debug logging, if the C preprocessor flag _AUTO_APMS_DEBUG_LOGGING is set.
Definition logging.cpp:25
Fundamental helper classes and utility functions.