From c97d35e16a758220303f7d169ef3246d830ca22c Mon Sep 17 00:00:00 2001 From: Max SCHMELLER Date: Fri, 22 Nov 2024 16:23:02 +0900 Subject: [PATCH 1/4] feat(loggers): add a new generic dependency-injectable logger class Signed-off-by: Max SCHMELLER --- .../include/nebula_common/loggers/logger.hpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 nebula_common/include/nebula_common/loggers/logger.hpp diff --git a/nebula_common/include/nebula_common/loggers/logger.hpp b/nebula_common/include/nebula_common/loggers/logger.hpp new file mode 100644 index 000000000..b416e2523 --- /dev/null +++ b/nebula_common/include/nebula_common/loggers/logger.hpp @@ -0,0 +1,49 @@ +// Copyright 2024 TIER IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include +#include + +#define NEBULA_LOG_STREAM(log_func, stream_args) \ + { \ + std::stringstream ss{}; \ + ss << stream_args; \ + log_func(ss.str()); \ + } + +namespace nebula::drivers::loggers +{ + +class Logger +{ +public: + Logger() = default; + Logger(const Logger &) = default; + Logger(Logger &&) = delete; + Logger & operator=(const Logger &) = default; + Logger & operator=(Logger &&) = delete; + virtual ~Logger() = default; + + virtual void debug(const std::string & message) = 0; + virtual void info(const std::string & message) = 0; + virtual void warn(const std::string & message) = 0; + virtual void error(const std::string & message) = 0; + + virtual std::shared_ptr child(const std::string & name) = 0; +}; + +} // namespace nebula::drivers::loggers From 4d21fe042dceb0e3f278bfa609f3f5982496d6d2 Mon Sep 17 00:00:00 2001 From: Max SCHMELLER Date: Fri, 22 Nov 2024 16:23:26 +0900 Subject: [PATCH 2/4] feat(loggers): add a console logger implementation Signed-off-by: Max SCHMELLER --- .../nebula_common/loggers/console_logger.hpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 nebula_common/include/nebula_common/loggers/console_logger.hpp diff --git a/nebula_common/include/nebula_common/loggers/console_logger.hpp b/nebula_common/include/nebula_common/loggers/console_logger.hpp new file mode 100644 index 000000000..fb2a61276 --- /dev/null +++ b/nebula_common/include/nebula_common/loggers/console_logger.hpp @@ -0,0 +1,55 @@ +// Copyright 2024 TIER IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "nebula_common/loggers/logger.hpp" + +#include +#include +#include +#include +#include +#include + +namespace nebula::drivers::loggers +{ + +class ConsoleLogger : public Logger +{ +public: + explicit ConsoleLogger(std::string name) : name_(std::move(name)) {} + + void debug(const std::string & message) override { print_tagged(std::cout, "DEBUG", message); } + void info(const std::string & message) override { print_tagged(std::cout, "INFO", message); } + void warn(const std::string & message) override { print_tagged(std::cerr, "WARN", message); } + void error(const std::string & message) override { print_tagged(std::cerr, "ERROR", message); } + + std::shared_ptr child(const std::string & name) override + { + return std::make_shared(name_ + "." + name); + } + +private: + std::string name_; + + void print_tagged(std::ostream & os, const std::string & severity, const std::string & message) + { + // In multithreaded logging, building the string first (... + ...) and then shifting to the + // stream will ensure that no other logger outputs between string fragments + os << ("[" + name_ + "][" + severity + "] " + message) << std::endl; + } +}; + +} // namespace nebula::drivers::loggers From c6fcf9b29318fc144b6c53810a543eac7fe171d3 Mon Sep 17 00:00:00 2001 From: Max SCHMELLER Date: Fri, 22 Nov 2024 16:23:46 +0900 Subject: [PATCH 3/4] feat(loggers): add an rclcpp logger implementation Signed-off-by: Max SCHMELLER --- .../nebula_ros/common/rclcpp_logger.hpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 nebula_ros/include/nebula_ros/common/rclcpp_logger.hpp diff --git a/nebula_ros/include/nebula_ros/common/rclcpp_logger.hpp b/nebula_ros/include/nebula_ros/common/rclcpp_logger.hpp new file mode 100644 index 000000000..333435288 --- /dev/null +++ b/nebula_ros/include/nebula_ros/common/rclcpp_logger.hpp @@ -0,0 +1,59 @@ +// Copyright 2024 TIER IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include +#include + +#include +#include + +namespace nebula::drivers::loggers +{ + +class RclcppLogger : public Logger +{ +public: + explicit RclcppLogger(const std::string & name) : underlying_logger_(rclcpp::get_logger(name)) {} + explicit RclcppLogger(const rclcpp::Logger & underlying) : underlying_logger_(underlying) {} + + void debug(const std::string & message) override + { + RCLCPP_DEBUG_STREAM(underlying_logger_, message); + } + void info(const std::string & message) override + { + RCLCPP_INFO_STREAM(underlying_logger_, message); + } + void warn(const std::string & message) override + { + RCLCPP_WARN_STREAM(underlying_logger_, message); + } + void error(const std::string & message) override + { + RCLCPP_ERROR_STREAM(underlying_logger_, message); + } + + std::shared_ptr child(const std::string & name) override + { + return std::make_shared(underlying_logger_.get_child(name)); + } + +private: + rclcpp::Logger underlying_logger_; +}; + +} // namespace nebula::drivers::loggers From 4136ccbff7c2179dc2b1e73d23ef09bd1afd4526 Mon Sep 17 00:00:00 2001 From: Max SCHMELLER Date: Mon, 25 Nov 2024 18:02:32 +0900 Subject: [PATCH 4/4] chore(logger): remove useless rule-of-five boilerplate Signed-off-by: Max SCHMELLER --- nebula_common/include/nebula_common/loggers/logger.hpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/nebula_common/include/nebula_common/loggers/logger.hpp b/nebula_common/include/nebula_common/loggers/logger.hpp index b416e2523..9bf407156 100644 --- a/nebula_common/include/nebula_common/loggers/logger.hpp +++ b/nebula_common/include/nebula_common/loggers/logger.hpp @@ -31,11 +31,6 @@ namespace nebula::drivers::loggers class Logger { public: - Logger() = default; - Logger(const Logger &) = default; - Logger(Logger &&) = delete; - Logger & operator=(const Logger &) = default; - Logger & operator=(Logger &&) = delete; virtual ~Logger() = default; virtual void debug(const std::string & message) = 0;