|
| 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/logger.h |
| 23 | +/// \brief Pluggable logging interface and the process-global default logger. |
| 24 | +/// |
| 25 | +/// This header is backend-agnostic: it never includes the build-generated |
| 26 | +/// backend configuration header and never references the spdlog feature macro, |
| 27 | +/// so consumers see one stable API regardless of how the backend was configured. |
| 28 | + |
| 29 | +#include <memory> |
| 30 | +#include <source_location> |
| 31 | +#include <string> |
| 32 | +#include <string_view> |
| 33 | +#include <unordered_map> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +#include "iceberg/iceberg_export.h" |
| 37 | +#include "iceberg/logging/log_level.h" |
| 38 | +#include "iceberg/result.h" |
| 39 | + |
| 40 | +namespace iceberg { |
| 41 | + |
| 42 | +/// \brief A structured key/value attribute attached to a log record. |
| 43 | +/// |
| 44 | +/// Both key and value are owned so a sink may retain the record safely. |
| 45 | +/// Unused in v1; reserved so structured logging can be added without an ABI |
| 46 | +/// break to LogMessage. |
| 47 | +struct LogAttribute { |
| 48 | + std::string key; |
| 49 | + std::string value; |
| 50 | +}; |
| 51 | + |
| 52 | +/// \brief A single log record handed to a Logger. |
| 53 | +/// |
| 54 | +/// The formatted message is owned (moved in by the logging macros), so a sink |
| 55 | +/// may safely retain the record beyond the Log() call. The member set must not |
| 56 | +/// depend on the build's logging backend (the spdlog backend never appears here). |
| 57 | +struct LogMessage { |
| 58 | + LogLevel level; |
| 59 | + std::string message; |
| 60 | + std::source_location location; |
| 61 | + std::vector<LogAttribute> attributes; |
| 62 | +}; |
| 63 | + |
| 64 | +/// \brief Pluggable logging sink. |
| 65 | +/// |
| 66 | +/// Implementations must be thread-safe and must not throw. They must also obey: |
| 67 | +/// - No reentrancy: Log()/Flush() must not call the logging macros or |
| 68 | +/// GetDefaultLogger() (UB -- deadlock with mutex-based sinks). |
| 69 | +/// - Lower-bound level: ShouldLog(l) must imply l >= level(), i.e. level() |
| 70 | +/// summarizes ShouldLog(), because the global fast-path gate reflects only |
| 71 | +/// level(). A logger needing finer logic must report its most permissive |
| 72 | +/// threshold from level(). |
| 73 | +class ICEBERG_EXPORT Logger { |
| 74 | + public: |
| 75 | + virtual ~Logger() = default; |
| 76 | + |
| 77 | + /// \brief Property-based setup, called by Loggers::Load() before first use. |
| 78 | + /// |
| 79 | + /// The default is a no-op. Recognized properties include "level" (parsed via |
| 80 | + /// LogLevelFromString) and, for formatting sinks, "pattern". |
| 81 | + virtual Status Initialize( |
| 82 | + [[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) { |
| 83 | + return {}; |
| 84 | + } |
| 85 | + |
| 86 | + /// \brief Cheap check whether a record at \p level would be emitted. |
| 87 | + virtual bool ShouldLog(LogLevel level) const = 0; |
| 88 | + |
| 89 | + /// \brief Emit one (already-formatted) record, taking ownership. Must not throw. |
| 90 | + virtual void Log(LogMessage&& message) noexcept = 0; |
| 91 | + |
| 92 | + /// \brief Set the minimum level this logger emits. |
| 93 | + virtual void SetLevel(LogLevel level) = 0; |
| 94 | + |
| 95 | + /// \brief Return the minimum level this logger emits. |
| 96 | + virtual LogLevel level() const = 0; |
| 97 | + |
| 98 | + /// \brief Flush any buffered output. Must not throw; best-effort on the fatal path. |
| 99 | + virtual void Flush() noexcept {} |
| 100 | + |
| 101 | + /// \brief Return true if this logger is a no-op. |
| 102 | + virtual bool IsNoop() const { return false; } |
| 103 | + |
| 104 | + /// \brief Return a shared, immortal no-op logger singleton. |
| 105 | + static std::shared_ptr<Logger> Noop(); |
| 106 | +}; |
| 107 | + |
| 108 | +/// \brief Return the process-global default logger (never null). |
| 109 | +/// |
| 110 | +/// Off the hot path -- acquires the slot lock and returns an owning copy. The |
| 111 | +/// logging macros use the cheaper internal hot-path accessor instead. |
| 112 | +ICEBERG_EXPORT std::shared_ptr<Logger> GetDefaultLogger(); |
| 113 | + |
| 114 | +/// \brief Install a new process-global default logger. |
| 115 | +/// |
| 116 | +/// A null argument installs the no-op logger. Thread-safe; intended for |
| 117 | +/// occasional (configuration-time) use rather than the hot path. |
| 118 | +ICEBERG_EXPORT void SetDefaultLogger(std::shared_ptr<Logger> logger); |
| 119 | + |
| 120 | +/// \brief Set the minimum level of the current default logger. |
| 121 | +/// |
| 122 | +/// Updates both the logger and the lock-free fast-path gate atomically. |
| 123 | +ICEBERG_EXPORT void SetDefaultLevel(LogLevel level); |
| 124 | + |
| 125 | +namespace detail { |
| 126 | + |
| 127 | +/// \brief Lock-free read of the cached effective minimum level. |
| 128 | +/// |
| 129 | +/// This is a non-authoritative lower-bound early-out: it may admit extra calls |
| 130 | +/// through to the authoritative Logger::ShouldLog, but never wrongly rejects. |
| 131 | +ICEBERG_EXPORT LogLevel EffectiveLevel() noexcept; |
| 132 | + |
| 133 | +/// \brief Hot-path accessor for the default logger. |
| 134 | +/// |
| 135 | +/// Returns a reference to a thread-local cached shared_ptr that is refreshed |
| 136 | +/// only when the default logger has changed (no lock / no refcount churn in |
| 137 | +/// steady state). The reference is valid for the duration of the calling |
| 138 | +/// statement. |
| 139 | +ICEBERG_EXPORT const std::shared_ptr<Logger>& CurrentLogger() noexcept; |
| 140 | + |
| 141 | +} // namespace detail |
| 142 | + |
| 143 | +} // namespace iceberg |
0 commit comments