|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +/// \file iceberg/logging/internal/spdlog_logger.h |
| 23 | +/// \brief spdlog-backed logging sink. |
| 24 | +/// |
| 25 | +/// INTERNAL, NOT INSTALLED. It is only included from .cc files (logger.cc and |
| 26 | +/// spdlog_logger.cc) after config.h, and only when the project is built with |
| 27 | +/// ICEBERG_SPDLOG=ON. SpdLogger is not a consumer-constructible public type -- |
| 28 | +/// applications obtain it via the default logger or the "logger-impl"="spdlog" |
| 29 | +/// registry factory. |
| 30 | + |
| 31 | +#include "iceberg/logging/config.h" |
| 32 | + |
| 33 | +#ifdef ICEBERG_HAS_SPDLOG |
| 34 | + |
| 35 | +# include <atomic> |
| 36 | +# include <memory> |
| 37 | + |
| 38 | +# include <spdlog/logger.h> |
| 39 | + |
| 40 | +# include "iceberg/logging/log_level.h" |
| 41 | +# include "iceberg/logging/logger.h" |
| 42 | + |
| 43 | +namespace iceberg::internal { |
| 44 | + |
| 45 | +/// \brief Logger backed by spdlog (synchronous only in v1). |
| 46 | +/// |
| 47 | +/// Synchronous because spdlog::source_loc holds non-owning const char* that are |
| 48 | +/// unsafe to forward into an async logger (spdlog #3227). |
| 49 | +/// ICEBERG_EXPORT so the symbol is linkable from in-tree tests (and any |
| 50 | +/// internal consumer) under -fvisibility=hidden / MSVC DLL builds. The header |
| 51 | +/// is still not installed -- this is a binary-visibility detail, not public API. |
| 52 | +class ICEBERG_EXPORT SpdLogger : public Logger { |
| 53 | + public: |
| 54 | + /// \brief Construct over a default stderr-backed spdlog logger. |
| 55 | + explicit SpdLogger(LogLevel level = LogLevel::kInfo); |
| 56 | + |
| 57 | + /// \brief Construct over a caller-provided spdlog logger. |
| 58 | + /// |
| 59 | + /// The logger MUST be synchronous. Log() forwards spdlog::source_loc, which |
| 60 | + /// borrows the std::source_location's const char* pointers; an async spdlog |
| 61 | + /// logger would queue them past their lifetime (spdlog #3227 -> UB). This is a |
| 62 | + /// caller contract -- spdlog exposes no reliable sync/async query to assert on. |
| 63 | + explicit SpdLogger(std::shared_ptr<spdlog::logger> logger, |
| 64 | + LogLevel level = LogLevel::kInfo); |
| 65 | + |
| 66 | + /// \brief Apply the "pattern" property (spdlog set_pattern), then "level". |
| 67 | + Status Initialize( |
| 68 | + const std::unordered_map<std::string, std::string>& properties) override; |
| 69 | + |
| 70 | + bool ShouldLog(LogLevel level) const noexcept override { |
| 71 | + return level >= level_.load(std::memory_order_relaxed); |
| 72 | + } |
| 73 | + void Log(LogMessage&& message) noexcept override; |
| 74 | + void SetLevel(LogLevel level) noexcept override { |
| 75 | + level_.store(level, std::memory_order_relaxed); |
| 76 | + } |
| 77 | + LogLevel level() const noexcept override { |
| 78 | + return level_.load(std::memory_order_relaxed); |
| 79 | + } |
| 80 | + void Flush() noexcept override; |
| 81 | + |
| 82 | + private: |
| 83 | + std::shared_ptr<spdlog::logger> logger_; |
| 84 | + std::atomic<LogLevel> level_; |
| 85 | +}; |
| 86 | + |
| 87 | +} // namespace iceberg::internal |
| 88 | + |
| 89 | +#endif // ICEBERG_HAS_SPDLOG |
0 commit comments