Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <[email protected]>
  • Loading branch information
caguero committed Aug 16, 2024
1 parent 6f20cd2 commit 4eefd71
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 388 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ if(NOT GZ_UTILS_VENDOR_CLI11)
gz_find_package(CLI11 REQUIRED_BY cli PKGCONFIG_IGNORE)
endif()

gz_find_package(spdlog REQUIRED_BY logger)
gz_find_package(spdlog REQUIRED_BY log)

#============================================================================
# Configure the build
#============================================================================
gz_configure_build(QUIT_IF_BUILD_ERRORS
COMPONENTS cli logger)
COMPONENTS cli log)

#============================================================================
# Create package information
Expand Down
1 change: 0 additions & 1 deletion cli/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ gz_add_component(
cli
INTERFACE
INDEPENDENT_FROM_PROJECT_LIB
LIB_DEPS spdlog::spdlog
GET_TARGET_NAME gz_utils_cli_target_name)

# Make sure that the name is visible also in cli/include/CMakeLists.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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)
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}::logger
gz-utils${GZ_UTILS_VER}::log
)
32 changes: 13 additions & 19 deletions examples/logger/main.cc → examples/log/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@
*
*/

#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.
*/
#include <filesystem>
#include <gz/utils/log/Logger.hh>

//////////////////////////////////////////////////
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>();
gz::utils::log::Logger logger("my_logger");
logger.RawLogger().set_level(spdlog::level::trace);

spdlog::logger logger("split_sink", {splitSink, splitSinkConsole});
logger.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.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.critical("critical");
logger.SetLogDestination(logPath);
logger.RawLogger().trace("trace");
logger.RawLogger().info("info");
logger.RawLogger().warn("warn");
logger.RawLogger().error("error");
logger.RawLogger().critical("critical");
}
File renamed without changes.
1 change: 1 addition & 0 deletions log/include/gz/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gz_install_all_headers(COMPONENT log)
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,24 @@
* limitations under the License.
*
*/
#ifndef GZ_UTILS_CONSOLE_HH_
#define GZ_UTILS_CONSOLE_HH_
#ifndef GZ_UTILS_LOG_LOGGER_HH_
#define GZ_UTILS_LOG_LOGGER_HH_

#include <filesystem>
#include <fstream>
#include <iostream>
#include <spdlog/spdlog.h>
#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
namespace gz::utils::log
{
/// \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
class GZ_UTILS_VISIBLE Logger
{
/// \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);
public: explicit Logger(const std::string &_loggerName);

/// \brief Set the log destination filename.
/// \param[in] _filename Log file name.
Expand All @@ -55,19 +43,15 @@ namespace gz::utils::logger

/// \brief Access the underlying spdlog logger.
/// \return The spdlog logger.
public: [[nodiscard]] spdlog::logger &Logger() const;
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> LoggerPtr() const;

/// \brief Access the global gz console logger.
/// \return The gz consoler logger.
public: static Console &Root();
public: [[nodiscard]] std::shared_ptr<spdlog::logger> RawLoggerPtr() const;

/// \brief Implementation Pointer.
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
}

#endif // GZ_UTILS_CONSOLE_HH_
#endif // GZ_UTILS_LOG_LOGGER_HH_
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@
*
*/

#ifndef GZ_UTILS_LOGGER_SPLITSINK_HH_
#define GZ_UTILS_LOGGER_SPLITSINK_HH_
#ifndef GZ_UTILS_LOG_SPLITSINK_HH_
#define GZ_UTILS_LOG_SPLITSINK_HH_

#include <mutex>
#include <string>
#include <vector>

#include <spdlog/details/log_msg.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/console_globals.h>
#include <spdlog/pattern_formatter.h>

#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/ringbuffer_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

namespace gz::utils::logger
namespace gz::utils::log
{
/// \brief Logging sink for spdlog that logs in Gazebo-conventions
/// \brief Logging sink for spdlog that logs in Gazebo-conventions.
///
/// This will route messages with severity (warn, err, critical) to stderr,
/// and all other levels (info, debug, trace) to stdout
/// and all other levels (info, debug, trace) to stdout.
template<typename Mutex>
class SplitConsoleSink : public spdlog::sinks::base_sink<Mutex>
{
/// \brief Class constructor.
public: SplitConsoleSink() = default;

/// \brief No copy constructor.
public: SplitConsoleSink(const SplitConsoleSink &) = delete;

/// \brief No asignment operator.
public: SplitConsoleSink &operator=(const SplitConsoleSink &) = delete;

/// \brief Try to log a message.
/// \param[in] _msg The message to log.
protected: void sink_it_(const spdlog::details::log_msg &_msg) override
{
if (!this->should_log(_msg.level))
{
return;
}

if (_msg.level == spdlog::level::warn ||
_msg.level == spdlog::level::err ||
if (_msg.level == spdlog::level::warn ||
_msg.level == spdlog::level::err ||
_msg.level == spdlog::level::critical)
{
this->stderr.log(_msg);
Expand All @@ -59,22 +64,31 @@ class SplitConsoleSink : public spdlog::sinks::base_sink<Mutex>
this->stdout.log(_msg);
}

/// \brief Flush messages.
protected: void flush_() override
{
this->stdout.flush();
this->stderr.flush();
}

void set_pattern_(const std::string &pattern) override
/// \brief Set the logging pattern.
/// \param[in] _pattern The logging pattern.
protected: void set_pattern_(const std::string &_pattern) override
{
this->set_formatter_(spdlog::details::make_unique<spdlog::pattern_formatter>(pattern));
this->set_formatter(
spdlog::details::make_unique<spdlog::pattern_formatter>(_pattern));
}

void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
/// \brief Set the new formatter.
/// \param[in] _sinkFormatter The formatter.
protected: void set_formatter_(
std::unique_ptr<spdlog::formatter> _sinkFormatter) override
{
spdlog::sinks::base_sink<Mutex>::formatter_ = std::move(sink_formatter);
this->stdout.set_formatter(spdlog::sinks::base_sink<Mutex>::formatter_->clone());
this->stderr.set_formatter(spdlog::sinks::base_sink<Mutex>::formatter_->clone());
spdlog::sinks::base_sink<Mutex>::formatter_ = std::move(_sinkFormatter);
this->stdout.set_formatter(
spdlog::sinks::base_sink<Mutex>::formatter_->clone());
this->stderr.set_formatter(
spdlog::sinks::base_sink<Mutex>::formatter_->clone());
}

/// \brief Standard output.
Expand All @@ -91,42 +105,62 @@ using SplitConsoleSinkSt = SplitConsoleSink<spdlog::details::null_mutex>;
///
/// This will route messages with severity (warn, err, critical) to stderr,
/// and all other levels (info, debug, trace) to stdout
template<typename Mutex, size_t n_items>
template<typename Mutex, size_t numItems>
class SplitRingBufferSink: public spdlog::sinks::base_sink<Mutex>
{
/// \brief Class constructor.
public: SplitRingBufferSink() = default;

/// \brief No copy constructor.
public: SplitRingBufferSink(const SplitRingBufferSink &) = delete;

/// \brief No asignment operator.
public: SplitRingBufferSink &operator=(const SplitRingBufferSink &) = delete;

public: std::vector<spdlog::details::log_msg_buffer> last_raw_stdout(size_t lim = 0)
/// \brief ToDo.
/// \param[in] _lim
/// \return
public: std::vector<spdlog::details::log_msg_buffer> last_raw_stdout(
size_t _lim = 0)
{
return this->stdout.last_raw(lim);
return this->stdout.last_raw(_lim);
}

public: std::vector<spdlog::details::log_msg_buffer> last_raw_stderr(size_t lim = 0)
/// \brief ToDo.
/// \param[in] _lim
/// \return
public: std::vector<spdlog::details::log_msg_buffer> last_raw_stderr(
size_t _lim = 0)
{
return this->stderr.last_raw(lim);
return this->stderr.last_raw(_lim);
}

public: std::vector<std::string> last_formatted_stdout(size_t lim = 0)
/// \brief ToDo.
/// \param[in] _lim
/// \return
public: std::vector<std::string> last_formatted_stdout(size_t _lim = 0)
{
return this->stdout.last_formatted(lim);
return this->stdout.last_formatted(_lim);
}

public: std::vector<std::string> last_formatted_stderr(size_t lim = 0)
/// \brief ToDo.
/// \param[in] _lim
/// \return
public: std::vector<std::string> last_formatted_stderr(size_t _lim = 0)
{
return this->stderr.last_formatted(lim);
return this->stderr.last_formatted(_lim);
}

/// \brief ToDo.
/// \param[in] _lim
/// \return
protected: void sink_it_(const spdlog::details::log_msg &_msg) override
{
if (!this->should_log(_msg.level))
{
return;
}

if (_msg.level == spdlog::level::warn ||
_msg.level == spdlog::level::err ||
if (_msg.level == spdlog::level::warn ||
_msg.level == spdlog::level::err ||
_msg.level == spdlog::level::critical)
{
this->stderr.log(_msg);
Expand All @@ -135,34 +169,43 @@ class SplitRingBufferSink: public spdlog::sinks::base_sink<Mutex>
this->stdout.log(_msg);
}

/// \brief Flush messages.
protected: void flush_() override
{
this->stdout.flush();
this->stderr.flush();
}

protected: void set_pattern_(const std::string &pattern) override
/// \brief Set the logging pattern.
/// \param[in] _pattern The logging pattern.
protected: void set_pattern_(const std::string &_pattern) override
{
this->set_formatter_(spdlog::details::make_unique<spdlog::pattern_formatter>(pattern));
this->set_formatter_(
spdlog::details::make_unique<spdlog::pattern_formatter>(_pattern));
}

protected: void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
/// \brief Set the new formatter.
/// \param[in] _sinkFormatter The formatter.
protected: void set_formatter_(
std::unique_ptr<spdlog::formatter> sinkFormatter) override
{
spdlog::sinks::base_sink<Mutex>::formatter_ = std::move(sink_formatter);
this->stdout.set_formatter(spdlog::sinks::base_sink<Mutex>::formatter_->clone());
this->stderr.set_formatter(spdlog::sinks::base_sink<Mutex>::formatter_->clone());
spdlog::sinks::base_sink<Mutex>::formatter_ = std::move(sinkFormatter);
this->stdout.set_formatter(
spdlog::sinks::base_sink<Mutex>::formatter_->clone());
this->stderr.set_formatter(
spdlog::sinks::base_sink<Mutex>::formatter_->clone());
}


/// \brief Standard output.
private: spdlog::sinks::ringbuffer_sink_st stdout {n_items};
private: spdlog::sinks::ringbuffer_sink_st stdout {numItems};

/// \brief Standard error.
private: spdlog::sinks::ringbuffer_sink_st stderr {n_items};
private: spdlog::sinks::ringbuffer_sink_st stderr {numItems};
};

template <size_t n_items>
using SplitRingBufferSinkMt = SplitRingBufferSink<std::mutex, n_items>;
template <size_t numItems>
using SplitRingBufferSinkMt = SplitRingBufferSink<std::mutex, numItems>;

} // namespace gz::utils::logger
#endif // GZ_UTILS_LOGGER_SPLITSINK_HH__
} // namespace gz::utils::log
#endif // GZ_UTILS_LOG_SPLITSINK_HH__
8 changes: 4 additions & 4 deletions logger/src/CMakeLists.txt → log/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
gz_get_libsources_and_unittests(sources gtest_sources)

gz_add_component(logger
gz_add_component(log
SOURCES ${sources}
INDEPENDENT_FROM_PROJECT_LIB
GET_TARGET_NAME gz_utils_logger_target_name)
GET_TARGET_NAME gz_utils_log_target_name)

target_link_libraries(${gz_utils_logger_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_logger_target_name}
LIB_DEPS ${gz_utils_log_target_name}
)
Loading

0 comments on commit 4eefd71

Please sign in to comment.