-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move spdlog::logger to gz-utils #134
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
965eff9
Move spdlog::logger to gz-utils
mjcarroll ac15ba1
Code compiling and example.
caguero 6f20cd2
Merge branch 'main' into mjcarroll/logger_in_utils
caguero 4eefd71
Refactor
caguero 6144605
Adding spdlog dependency to CI
caguero 55eb7a5
Tweak
caguero a927dad
Tweak
caguero 4a893da
Tweak
caguero 4cf0bcb
Update examples/log/CMakeLists.txt
caguero be88966
Namespaces
caguero 0ac9ddd
Tweaks
caguero 49cd5af
Constructor
caguero d96ec02
SplitSink tweak
caguero d1402ec
Tweak
caguero 85c149e
Adding file
caguero b1ea607
Tweak
caguero 66451d1
Win
caguero 7638bbb
package.xml
caguero f33b379
Tweak
caguero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
libgz-cmake4-dev | ||
libspdlog-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cmake_minimum_required(VERSION 3.22.1 FATAL_ERROR) | ||
project(gz-utils-logger-demo) | ||
|
||
# Find the Gazebo Libraries used directly by the example | ||
find_package(gz-utils3 REQUIRED COMPONENTS log) | ||
set(GZ_UTILS_VER ${gz-utils3_VERSION_MAJOR}) | ||
|
||
add_executable(${PROJECT_NAME} main.cc) | ||
target_link_libraries( | ||
${PROJECT_NAME} | ||
gz-utils${GZ_UTILS_VER}::log | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2024 Open Source Robotics Foundation | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include <filesystem> | ||
#include <gz/utils/log/Logger.hh> | ||
|
||
////////////////////////////////////////////////// | ||
int main(int argc, char** argv) | ||
{ | ||
gz::utils::log::Logger logger("my_logger"); | ||
logger.RawLogger().set_level(spdlog::level::trace); | ||
|
||
std::filesystem::path logDir = std::filesystem::temp_directory_path(); | ||
std::filesystem::path logFile = "my_log.txt"; | ||
std::filesystem::path logPath = logDir / logFile; | ||
|
||
logger.SetLogDestination(logPath); | ||
logger.RawLogger().trace("trace"); | ||
logger.RawLogger().info("info"); | ||
logger.RawLogger().warn("warn"); | ||
logger.RawLogger().error("error"); | ||
logger.RawLogger().critical("critical"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(gz/utils) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gz_install_all_headers(COMPONENT log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2024 Open Source Robotics Foundation | ||
* | ||
* 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. | ||
* | ||
*/ | ||
#ifndef GZ_UTILS_LOG_LOGGER_HH_ | ||
#define GZ_UTILS_LOG_LOGGER_HH_ | ||
|
||
#include <spdlog/spdlog.h> | ||
#include <memory> | ||
#include <string> | ||
#include <gz/utils/config.hh> | ||
#include <gz/utils/ImplPtr.hh> | ||
#include <gz/utils/log/Export.hh> | ||
|
||
namespace gz | ||
{ | ||
namespace utils | ||
{ | ||
namespace log | ||
{ | ||
inline namespace GZ_UTILS_LOG_VERSION_NAMESPACE { | ||
|
||
/// \brief Gazebo console and file logging class. | ||
/// This will configure spdlog with a sane set of defaults for logging to the | ||
/// console as well as a file. | ||
class GZ_UTILS_LOG_VISIBLE Logger | ||
{ | ||
/// \brief Class constructor. | ||
/// \param[in] _loggerName Logger name. | ||
public: explicit Logger(const std::string &_loggerName); | ||
|
||
/// \brief Set the log destination filename. | ||
/// \param[in] _filename Log file name. | ||
public: void SetLogDestination(const std::string &_filename); | ||
|
||
/// \brief Get the log destination filename. | ||
/// \return Log file name. | ||
public: std::string LogDestination() const; | ||
|
||
/// \brief Access the underlying spdlog logger. | ||
/// \return The spdlog logger. | ||
public: [[nodiscard]] spdlog::logger &RawLogger() const; | ||
|
||
/// \brief Access the underlying spdlog logger, with ownership. | ||
/// \return The spdlog logger. | ||
public: [[nodiscard]] std::shared_ptr<spdlog::logger> RawLoggerPtr() const; | ||
|
||
/// \brief Implementation Pointer. | ||
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr) | ||
}; | ||
} // namespace GZ_UTILS_LOG_VERSION_NAMESPACE | ||
} // namespace log | ||
} // namespace utils | ||
} // namespace gz | ||
|
||
#endif // GZ_UTILS_LOG_LOGGER_HH_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2024 Open Source Robotics Foundation | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#ifndef GZ_UTILS_LOG_SPLITSINK_HH_ | ||
#define GZ_UTILS_LOG_SPLITSINK_HH_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include <gz/utils/config.hh> | ||
#include <gz/utils/ImplPtr.hh> | ||
#include <gz/utils/log/Export.hh> | ||
|
||
#include <spdlog/details/log_msg.h> | ||
#include <spdlog/pattern_formatter.h> | ||
#include <spdlog/sinks/base_sink.h> | ||
#include <spdlog/sinks/stdout_color_sinks.h> | ||
|
||
namespace gz | ||
{ | ||
namespace utils | ||
{ | ||
namespace log | ||
{ | ||
inline namespace GZ_UTILS_LOG_VERSION_NAMESPACE { | ||
|
||
/// \brief Logging sink for spdlog that logs in Gazebo-conventions. | ||
caguero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// This will route messages with severity (warn, err, critical) to stderr, | ||
/// and all other levels (info, debug, trace) to stdout. | ||
class GZ_UTILS_LOG_VISIBLE SplitConsoleSink : public spdlog::sinks::sink | ||
{ | ||
/// \brief Class constructor. | ||
public: SplitConsoleSink(); | ||
|
||
/// \brief Class destructor. | ||
public: ~SplitConsoleSink() override = default; | ||
|
||
/// \brief Log a message. | ||
/// \param[in] _msg The message to log. | ||
public: void log(const spdlog::details::log_msg &_msg) override; | ||
|
||
/// \brief Flush messages. | ||
public: void flush() override; | ||
|
||
/// \brief Set the logging pattern. | ||
/// \param[in] _pattern The logging pattern. | ||
public: void set_pattern(const std::string &_pattern) override; | ||
|
||
/// \brief Set the new formatter. | ||
/// \param[in] _sinkFormatter The formatter. | ||
public: void set_formatter(std::unique_ptr<spdlog::formatter> _sinkFormatter) | ||
override; | ||
|
||
/// \brief Set the color mode. | ||
/// \param[in] _mode Color mode. | ||
public: void set_color_mode(spdlog::color_mode _mode); | ||
|
||
/// \brief Implementation Pointer. | ||
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr) | ||
}; | ||
} // namespace GZ_UTILS_LOG_VERSION_NAMESPACE | ||
} // namespace log | ||
} // namespace utils | ||
} // namespace gz | ||
|
||
#endif // GZ_UTILS_LOG_SPLITSINK_HH__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
gz_get_libsources_and_unittests(sources gtest_sources) | ||
|
||
gz_add_component(log | ||
SOURCES ${sources} | ||
INDEPENDENT_FROM_PROJECT_LIB | ||
GET_TARGET_NAME gz_utils_log_target_name) | ||
|
||
target_link_libraries(${gz_utils_log_target_name} | ||
PUBLIC | ||
spdlog::spdlog) | ||
|
||
gz_build_tests(TYPE UNIT | ||
SOURCES ${gtest_sources} | ||
LIB_DEPS ${gz_utils_log_target_name} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (C) 2024 Open Source Robotics Foundation | ||
* | ||
* 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. | ||
* | ||
*/ | ||
#include <memory> | ||
#include <string> | ||
|
||
#include <gz/utils/log/Logger.hh> | ||
#include <gz/utils/log/SplitSink.hh> | ||
#include <spdlog/sinks/basic_file_sink.h> | ||
#include <spdlog/sinks/dist_sink.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
namespace gz::utils::log | ||
{ | ||
/// \brief Private data for the Logger class. | ||
class Logger::Implementation | ||
{ | ||
/// \brief Constructor. | ||
/// \param[in] _loggerName Logger name. | ||
public: explicit Implementation(const std::string &_loggerName) | ||
: consoleSink(std::make_shared<SplitConsoleSink>()), | ||
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()), | ||
logger(std::make_shared<spdlog::logger>(_loggerName, sinks)) | ||
{ | ||
} | ||
|
||
/// \brief The console sink with stdout and stderr. | ||
std::shared_ptr<SplitConsoleSink> consoleSink; | ||
|
||
/// \brief The file sink for logging into a file. | ||
std::shared_ptr<spdlog::sinks::basic_file_sink_mt> fileSink {nullptr}; | ||
|
||
/// \brief A sink distribution storing multiple sinks. | ||
std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr}; | ||
|
||
/// \brief The underlying spdlog logger. | ||
std::shared_ptr<spdlog::logger> logger {nullptr}; | ||
}; | ||
|
||
///////////////////////////////////////////////// | ||
Logger::Logger(const std::string &_loggerName) | ||
: dataPtr(gz::utils::MakeUniqueImpl<Implementation>(_loggerName)) | ||
{ | ||
// Add the console sink by default. | ||
this->dataPtr->sinks->add_sink(this->dataPtr->consoleSink); | ||
|
||
// Configure the logger. | ||
this->dataPtr->logger->set_level(spdlog::level::err); | ||
this->dataPtr->logger->flush_on(spdlog::level::err); | ||
|
||
spdlog::flush_every(std::chrono::seconds(5)); | ||
spdlog::register_logger(this->dataPtr->logger); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
void Logger::SetLogDestination(const std::string &_filename) | ||
{ | ||
if (this->dataPtr->fileSink) | ||
this->dataPtr->sinks->remove_sink(this->dataPtr->fileSink); | ||
|
||
if (!_filename.empty()) | ||
{ | ||
this->dataPtr->fileSink = | ||
std::make_shared<spdlog::sinks::basic_file_sink_mt>(_filename, true); | ||
this->dataPtr->sinks->add_sink(this->dataPtr->fileSink); | ||
} | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
std::string Logger::LogDestination() const | ||
{ | ||
std::string logPath = ""; | ||
if (this->dataPtr->fileSink) | ||
logPath = this->dataPtr->fileSink->filename(); | ||
|
||
return logPath; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
spdlog::logger &Logger::RawLogger() const | ||
{ | ||
return *this->dataPtr->logger; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
std::shared_ptr<spdlog::logger> Logger::RawLoggerPtr() const | ||
{ | ||
return this->dataPtr->logger; | ||
} | ||
|
||
} // namespace gz::utils::log |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think should just be
GZ_UTILS_VERSION_NAMESPACE
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in f33b379.