-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Agüero <[email protected]>
- Loading branch information
Showing
6 changed files
with
312 additions
and
3 deletions.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR) | ||
project(gz-utils-logger-demo) | ||
|
||
# Find the Gazebo Libraries used directly by the example | ||
find_package(gz-utils3 REQUIRED COMPONENTS logger) | ||
set(GZ_UTILS_VER ${gz-utils3_VERSION_MAJOR}) | ||
|
||
add_executable(${PROJECT_NAME} main.cc) | ||
target_link_libraries( | ||
${PROJECT_NAME} | ||
gz-utils${GZ_UTILS_VER}::logger | ||
) |
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,43 @@ | ||
/* | ||
* 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 <gz/utils/logger/Console.hh> | ||
#include <gz/utils/logger/SplitSink.hh> | ||
#include <gz/utils/config.hh> | ||
|
||
/* | ||
* At this point, all logger functionality will be available. | ||
*/ | ||
|
||
////////////////////////////////////////////////// | ||
int main(int argc, char** argv) | ||
{ | ||
auto splitSink = | ||
std::make_shared<gz::utils::logger::SplitRingBufferSinkMt<100>>(); | ||
auto splitSinkConsole = | ||
std::make_shared<gz::utils::logger::SplitConsoleSinkMt>(); | ||
|
||
spdlog::logger logger("split_sink", {splitSink, splitSinkConsole}); | ||
logger.set_level(spdlog::level::trace); | ||
|
||
logger.trace("trace"); | ||
logger.debug("debug"); | ||
logger.info("info"); | ||
logger.warn("warn"); | ||
logger.error("error"); | ||
logger.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,73 @@ | ||
/* | ||
* 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_CONSOLE_HH_ | ||
#define GZ_UTILS_CONSOLE_HH_ | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <memory> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include <gz/utils/ImplPtr.hh> | ||
#include <gz/utils/SuppressWarning.hh> | ||
|
||
#include <spdlog/logger.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
namespace gz::utils::logger | ||
{ | ||
/// \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_VISIBLE Console | ||
{ | ||
/// \brief Class constructor. | ||
/// \param[in] _loggerName Logger name. | ||
public: explicit Console(const std::string &_loggerName); | ||
|
||
/// \brief Set the console output color mode. | ||
///\param[in] _mode Color mode. | ||
public: void SetColorMode(spdlog::color_mode _mode); | ||
|
||
/// \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 &Logger() const; | ||
|
||
/// \brief Access the underlying spdlog logger, with ownership. | ||
/// \return The spdlog logger. | ||
public: [[nodiscard]] std::shared_ptr<spdlog::logger> LoggerPtr() const; | ||
|
||
/// \brief Access the global gz console logger. | ||
/// \return The gz consoler logger. | ||
public: static Console &Root(); | ||
|
||
/// \brief Implementation Pointer. | ||
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr) | ||
}; | ||
} | ||
|
||
#endif // GZ_UTILS_CONSOLE_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
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,179 @@ | ||
/* | ||
* 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 <sstream> | ||
#include <string> | ||
|
||
#include <gz/utils/logger/Console.hh> | ||
#include <gz/utils/NeverDestroyed.hh> | ||
|
||
#include <spdlog/sinks/basic_file_sink.h> | ||
#include <spdlog/sinks/dist_sink.h> | ||
#include <spdlog/sinks/stdout_color_sinks.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
namespace { | ||
/// \brief Custom log sink that routes to stdout/stderr in Gazebo conventions. | ||
class GzSplitSink : public spdlog::sinks::sink | ||
{ | ||
/// \brief Class destructor. | ||
public: ~GzSplitSink() override = default; | ||
|
||
/// \brief Log a message. | ||
/// \param[in] _msg The message to log. | ||
public: void log(const spdlog::details::log_msg &_msg) override | ||
{ | ||
if (_msg.level == spdlog::level::warn || | ||
_msg.level == spdlog::level::err || | ||
_msg.level == spdlog::level::critical) | ||
{ | ||
this->stderr.log(_msg); | ||
} | ||
else | ||
this->stdout.log(_msg); | ||
} | ||
|
||
/// \brief Flush messages. | ||
public: void flush() override | ||
{ | ||
this->stdout.flush(); | ||
this->stderr.flush(); | ||
} | ||
|
||
/// \brief Set the logging pattern. | ||
/// \param[in] _pattern The logging pattern. | ||
public: void set_pattern(const std::string &_pattern) override | ||
{ | ||
this->stdout.set_pattern(_pattern); | ||
this->stderr.set_pattern(_pattern); | ||
} | ||
|
||
/// \brief Set the new formatter. | ||
/// \param[in] _sinkFormatter The formatter. | ||
public: void set_formatter(std::unique_ptr<spdlog::formatter> _sinkFormatter) | ||
override | ||
{ | ||
this->stdout.set_formatter(_sinkFormatter->clone()); | ||
this->stderr.set_formatter(std::move(_sinkFormatter)); | ||
} | ||
|
||
/// \brief Set the color mode. | ||
/// \param[in] _mode Color mode. | ||
public: void set_color_mode(spdlog::color_mode _mode) | ||
{ | ||
this->stdout.set_color_mode(_mode); | ||
this->stderr.set_color_mode(_mode); | ||
} | ||
|
||
/// \brief Standard output. | ||
private: spdlog::sinks::stdout_color_sink_mt stdout; | ||
|
||
/// \brief Standard error. | ||
private: spdlog::sinks::stderr_color_sink_mt stderr; | ||
}; | ||
} // namespace | ||
|
||
namespace gz::utils::logger | ||
{ | ||
/// \brief Private data for the Console class. | ||
class Console::Implementation | ||
{ | ||
/// \brief Constructor. | ||
/// \param[in] _loggerName Logger name. | ||
public: explicit Implementation(const std::string &_loggerName) | ||
: consoleSink(std::make_shared<GzSplitSink>()), | ||
sinks(std::make_shared<spdlog::sinks::dist_sink_mt>()), | ||
logger(std::make_shared<spdlog::logger>(_loggerName, sinks)) | ||
{ | ||
} | ||
|
||
/// \brief . | ||
std::shared_ptr<GzSplitSink> consoleSink; | ||
|
||
/// \brief . | ||
std::shared_ptr<spdlog::sinks::basic_file_sink_mt> fileSink {nullptr}; | ||
|
||
/// \brief . | ||
std::shared_ptr<spdlog::sinks::dist_sink_mt> sinks {nullptr}; | ||
|
||
/// \brief . | ||
std::shared_ptr<spdlog::logger> logger {nullptr}; | ||
}; | ||
|
||
///////////////////////////////////////////////// | ||
Console::Console(const std::string &_loggerName) | ||
: dataPtr(gz::utils::MakeUniqueImpl<Implementation>(_loggerName)) | ||
{ | ||
// Add the console sink as a destination. | ||
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 Console::SetColorMode(spdlog::color_mode _mode) | ||
{ | ||
this->dataPtr->consoleSink->set_color_mode(_mode); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
void Console::SetLogDestination(const std::string &_filename) | ||
{ | ||
if (this->dataPtr->fileSink) | ||
this->dataPtr->sinks->remove_sink(this->dataPtr->fileSink); | ||
|
||
this->dataPtr->fileSink = | ||
std::make_shared<spdlog::sinks::basic_file_sink_mt>(_filename, true); | ||
this->dataPtr->sinks->add_sink(this->dataPtr->fileSink); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
std::string Console::LogDestination() const | ||
{ | ||
std::string logPath = ""; | ||
if (this->dataPtr->fileSink) | ||
{ | ||
logPath = this->dataPtr->fileSink->filename(); | ||
} | ||
|
||
return logPath; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
spdlog::logger &Console::Logger() const | ||
{ | ||
return *this->dataPtr->logger; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
std::shared_ptr<spdlog::logger> Console::LoggerPtr() const | ||
{ | ||
return this->dataPtr->logger; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
Console &Console::Root() | ||
{ | ||
static gz::utils::NeverDestroyed<Console> root{"gz"}; | ||
return root.Access(); | ||
} | ||
} // namespace gz::utils::logger |