-
Notifications
You must be signed in to change notification settings - Fork 61
feat: add a plugable logging system. #153
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
Open
yingcai-cy
wants to merge
6
commits into
apache:main
Choose a base branch
from
yingcai-cy:logger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14a0d79
feat: add a plugable logger interface.
yingcai-cy 2126cd6
try to fix msvc build.
yingcai-cy cc320fa
Introduce Logger concept and eliminate use of std::function for logge…
yingcai-cy 9f1e7e8
simplify logging and fix review comments.
yingcai-cy e292c79
Merge branch 'main' into logger
yingcai-cy 93c797a
try to fix lint issue.
yingcai-cy 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 hidden or 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 hidden or 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,131 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 "iceberg/util/logger.h" | ||
|
|
||
| #include <spdlog/sinks/stdout_color_sinks.h> | ||
| #include <spdlog/spdlog.h> | ||
|
|
||
| #include "iceberg/util/spdlog_logger.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| /// \brief Convert iceberg LogLevel to spdlog level | ||
| spdlog::level::level_enum ToSpdlogLevel(LogLevel level) { | ||
| switch (level) { | ||
| case LogLevel::kTrace: | ||
| return spdlog::level::trace; | ||
| case LogLevel::kDebug: | ||
| return spdlog::level::debug; | ||
| case LogLevel::kInfo: | ||
| return spdlog::level::info; | ||
| case LogLevel::kWarn: | ||
| return spdlog::level::warn; | ||
| case LogLevel::kError: | ||
| return spdlog::level::err; | ||
| case LogLevel::kCritical: | ||
| return spdlog::level::critical; | ||
| case LogLevel::kOff: | ||
| return spdlog::level::off; | ||
| default: | ||
| return spdlog::level::info; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * \brief Convert spdlog level to iceberg LogLevel | ||
| */ | ||
| LogLevel FromSpdlogLevel(spdlog::level::level_enum level) { | ||
| switch (level) { | ||
| case spdlog::level::trace: | ||
| return LogLevel::kTrace; | ||
| case spdlog::level::debug: | ||
| return LogLevel::kDebug; | ||
| case spdlog::level::info: | ||
| return LogLevel::kInfo; | ||
| case spdlog::level::warn: | ||
| return LogLevel::kWarn; | ||
| case spdlog::level::err: | ||
| return LogLevel::kError; | ||
| case spdlog::level::critical: | ||
| return LogLevel::kCritical; | ||
| case spdlog::level::off: | ||
| return LogLevel::kOff; | ||
| default: | ||
| return LogLevel::kInfo; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // SpdlogLogger implementation | ||
| SpdlogLogger::SpdlogLogger(std::string_view logger_name) { | ||
| auto spdlog_logger = spdlog::get(std::string(logger_name)); | ||
| if (!spdlog_logger) { | ||
| spdlog_logger = spdlog::stdout_color_mt(std::string(logger_name)); | ||
| } | ||
| logger_ = std::static_pointer_cast<void>(spdlog_logger); | ||
| current_level_ = FromSpdlogLevel(spdlog_logger->level()); | ||
| } | ||
|
|
||
| SpdlogLogger::SpdlogLogger(std::shared_ptr<void> spdlog_logger) | ||
| : logger_(std::move(spdlog_logger)) { | ||
| auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_); | ||
| current_level_ = FromSpdlogLevel(typed_logger->level()); | ||
| } | ||
|
|
||
| bool SpdlogLogger::ShouldLogImpl(LogLevel level) const noexcept { | ||
| return level >= current_level_; | ||
| } | ||
|
|
||
| void SpdlogLogger::LogRawImpl(LogLevel level, const std::source_location& location, | ||
| const std::string& message) const { | ||
| auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_); | ||
| auto spdlog_level = ToSpdlogLevel(level); | ||
|
|
||
| // Add source location information | ||
| std::string full_message = | ||
| std::format("[{}:{}:{}] {}", location.file_name(), location.line(), | ||
| location.function_name(), message); | ||
|
|
||
| typed_logger->log(spdlog_level, full_message); | ||
| } | ||
|
|
||
| void SpdlogLogger::SetLevelImpl(LogLevel level) { | ||
| current_level_ = level; | ||
| auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_); | ||
| typed_logger->set_level(ToSpdlogLevel(level)); | ||
| } | ||
|
|
||
| LogLevel SpdlogLogger::GetLevelImpl() const noexcept { return current_level_; } | ||
|
|
||
| // LoggerRegistry implementation | ||
| LoggerRegistry& LoggerRegistry::Instance() { | ||
| static LoggerRegistry instance; | ||
| return instance; | ||
| } | ||
|
|
||
| void LoggerRegistry::InitializeDefault(std::string_view logger_name) { | ||
| auto spdlog_logger = std::make_shared<SpdlogLogger>(logger_name); | ||
| SetDefaultLogger(spdlog_logger); | ||
| } | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or 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,211 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <format> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <source_location> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
|
|
||
| #include <iceberg/iceberg_export.h> | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief Log levels for the iceberg logger system | ||
| enum class LogLevel : int { | ||
| kTrace = 0, | ||
| kDebug = 1, | ||
| kInfo = 2, | ||
| kWarn = 3, | ||
| kError = 4, | ||
| kCritical = 5, | ||
| kOff = 6 | ||
| }; | ||
|
|
||
| /// \brief Convert log level to string representation | ||
| ICEBERG_EXPORT constexpr std::string_view LogLevelToString(LogLevel level) { | ||
| switch (level) { | ||
| case LogLevel::kTrace: | ||
| return "TRACE"; | ||
| case LogLevel::kDebug: | ||
| return "DEBUG"; | ||
| case LogLevel::kInfo: | ||
| return "INFO"; | ||
| case LogLevel::kWarn: | ||
| return "WARN"; | ||
| case LogLevel::kError: | ||
| return "ERROR"; | ||
| case LogLevel::kCritical: | ||
| return "CRITICAL"; | ||
| case LogLevel::kOff: | ||
| return "OFF"; | ||
| default: | ||
| return "UNKNOWN"; | ||
| } | ||
| } | ||
|
|
||
| /// \brief Logger interface that uses CRTP to avoid virtual function overhead | ||
| /// \tparam Derived The concrete logger implementation | ||
| template <typename Derived> | ||
| class ICEBERG_EXPORT LoggerInterface { | ||
| public: | ||
| /// \brief Check if a log level is enabled | ||
| bool ShouldLog(LogLevel level) const noexcept { | ||
| return derived()->ShouldLogImpl(level); | ||
| } | ||
|
|
||
| /// \brief Log a message with the specified level | ||
| template <typename... Args> | ||
| void Log(LogLevel level, const std::source_location& location, | ||
| std::string_view format_str, Args&&... args) const { | ||
| derived()->LogImpl(level, location, format_str, std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| /// \brief Set the minimum log level | ||
| void SetLevel(LogLevel level) { derived()->SetLevelImpl(level); } | ||
|
|
||
| /// \brief Get the current minimum log level | ||
| LogLevel GetLevel() const noexcept { return derived()->GetLevelImpl(); } | ||
|
|
||
| protected: | ||
| LoggerInterface() = default; | ||
| ~LoggerInterface() = default; | ||
|
|
||
| private: | ||
| /// \brief Get const pointer to the derived class instance | ||
| const Derived* derived() const noexcept { return static_cast<const Derived*>(this); } | ||
|
|
||
| /// \brief Get non-const pointer to the derived class instance | ||
| Derived* derived() noexcept { return static_cast<Derived*>(this); } | ||
| }; | ||
|
|
||
| /// \brief Concept to constrain types that implement the Logger interface | ||
| template <typename T> | ||
| concept Logger = requires(const T& t, T& nt, LogLevel level, | ||
| const std::source_location& location, | ||
| std::string_view format_str) { | ||
| { t.ShouldLogImpl(level) } -> std::convertible_to<bool>; | ||
| { t.LogImpl(level, location, format_str) } -> std::same_as<void>; | ||
| { nt.SetLevelImpl(level) } -> std::same_as<void>; | ||
| { t.GetLevelImpl() } -> std::convertible_to<LogLevel>; | ||
| } && std::is_base_of_v<LoggerInterface<T>, T>; | ||
|
|
||
| /// \brief Global logger registry for managing logger instances | ||
| class ICEBERG_EXPORT LoggerRegistry { | ||
| public: | ||
| /// \brief Get the singleton instance of the logger registry | ||
| static LoggerRegistry& Instance(); | ||
|
|
||
| /// \brief Set the default logger implementation | ||
| /// | ||
| /// \tparam LoggerImpl The logger implementation type | ||
| /// \param logger The logger instance to set as default | ||
| template <Logger LoggerImpl> | ||
| void SetDefaultLogger(std::shared_ptr<LoggerImpl> logger) { | ||
| default_logger_ = std::static_pointer_cast<void>(logger); | ||
| log_func_ = [](const void* logger_ptr, LogLevel level, | ||
| const std::source_location& location, std::string_view format_str, | ||
| std::format_args args) { | ||
| auto* typed_logger = static_cast<const LoggerImpl*>(logger_ptr); | ||
| std::string formatted_message = std::vformat(format_str, args); | ||
| typed_logger->Log(level, location, formatted_message); | ||
| }; | ||
| should_log_func_ = [](const void* logger_ptr, LogLevel level) -> bool { | ||
| auto* typed_logger = static_cast<const LoggerImpl*>(logger_ptr); | ||
| return typed_logger->ShouldLog(level); | ||
| }; | ||
| } | ||
|
|
||
| /// \brief Get the default logger | ||
| /// | ||
| /// \tparam LoggerImpl The expected logger implementation type | ||
| /// \return Shared pointer to the logger, or nullptr if type doesn't match | ||
| template <Logger LoggerImpl> | ||
| std::shared_ptr<LoggerImpl> GetDefaultLogger() const { | ||
| return std::static_pointer_cast<LoggerImpl>(default_logger_); | ||
| } | ||
|
|
||
| /// \brief Log using the default logger | ||
| template <typename... Args> | ||
| void Log(LogLevel level, const std::source_location& location, | ||
| std::string_view format_str, Args&&... args) const { | ||
| if (default_logger_ && should_log_func_ && log_func_) { | ||
| if (should_log_func_(default_logger_.get(), level)) { | ||
| try { | ||
| if constexpr (sizeof...(args) > 0) { | ||
| auto args_store = std::make_format_args(args...); | ||
| log_func_(default_logger_.get(), level, location, format_str, args_store); | ||
| } else { | ||
| log_func_(default_logger_.get(), level, location, format_str, | ||
| std::make_format_args()); | ||
| } | ||
| } catch (const std::exception& e) { | ||
| std::cerr << "Logging error: " << e.what() << std::endl; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// \brief Initialize with default spdlog logger | ||
| void InitializeDefault(std::string_view logger_name = "iceberg"); | ||
|
|
||
| private: | ||
| LoggerRegistry() = default; | ||
|
|
||
| std::shared_ptr<void> default_logger_; | ||
| void (*log_func_)(const void*, LogLevel, const std::source_location&, std::string_view, | ||
| std::format_args) = nullptr; | ||
| bool (*should_log_func_)(const void*, LogLevel) = nullptr; | ||
| }; | ||
|
|
||
| /// \brief Convenience macros for logging with automatic source location | ||
| #define ICEBERG_LOG_WITH_LOCATION(level, format_str, ...) \ | ||
| do { \ | ||
| ::iceberg::LoggerRegistry::Instance().Log(level, std::source_location::current(), \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__); \ | ||
| } while (false) | ||
|
|
||
| #define ICEBERG_LOG_TRACE(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kTrace, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
|
|
||
| #define ICEBERG_LOG_DEBUG(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kDebug, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
|
|
||
| #define ICEBERG_LOG_INFO(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kInfo, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
|
|
||
| #define ICEBERG_LOG_WARN(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kWarn, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
|
|
||
| #define ICEBERG_LOG_ERROR(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kError, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
|
|
||
| #define ICEBERG_LOG_CRITICAL(format_str, ...) \ | ||
| ICEBERG_LOG_WITH_LOCATION(::iceberg::LogLevel::kCritical, \ | ||
| format_str __VA_OPT__(, ) __VA_ARGS__) | ||
| } // namespace iceberg | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.