iceberg-cpp
Loading...
Searching...
No Matches
Classes | Functions | Variables
logger.h File Reference

Pluggable logging interface and the process-global default logger. More...

#include <concepts>
#include <cstdlib>
#include <format>
#include <memory>
#include <source_location>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "iceberg/iceberg_export.h"
#include "iceberg/logging/log_level.h"
#include "iceberg/result.h"

Go to the source code of this file.

Classes

struct  iceberg::LogAttribute
 A structured key/value attribute attached to a log record. More...
 
class  iceberg::LogMessage
 A single log record handed to a Logger. More...
 
class  iceberg::Logger
 Pluggable logging sink. More...
 
class  iceberg::ScopedLogger
 Bind a logger for the current thread until this object leaves scope. More...
 

Functions

std::shared_ptr< Loggericeberg::GetDefaultLogger ()
 Return the process-global default logger (never null).
 
std::shared_ptr< Loggericeberg::GetCurrentLogger ()
 Return the effective logger for this thread (never null): the active ScopedLogger binding if any, else the global default.
 
void iceberg::SetDefaultLogger (std::shared_ptr< Logger > logger)
 Install a new process-global default logger.
 
void iceberg::SetDefaultLevel (LogLevel level)
 Set the minimum level of the current default logger.
 
const std::shared_ptr< Logger > & iceberg::internal::CurrentLogger () noexcept
 Hot-path accessor for the default logger.
 
void iceberg::internal::Emit (Logger &logger, LogLevel level, const std::source_location &location, std::string &&message)
 Build a LogMessage from the already-formatted text and dispatch it.
 
void iceberg::internal::EmitFormatError (Logger &logger, LogLevel level, const std::source_location &location) noexcept
 Emit a fixed fallback record when formatting threw.
 
template<typename... Args>
void iceberg::Log (LogLevel level, internal::FmtWithLoc< std::type_identity_t< Args >... > fmt, Args &&... args) noexcept
 Log to the process-default logger, std::format style. Formats only if the level is enabled; never throws.
 
template<typename... Args>
void iceberg::Log (Logger &logger, LogLevel level, internal::FmtWithLoc< std::type_identity_t< Args >... > fmt, Args &&... args) noexcept
 Log to an explicit logger, std::format style. Formats only if enabled.
 

Variables

constexpr std::string_view iceberg::kLevelProperty = "level"
 Well-known Logger::Initialize() property keys.
 
constexpr std::string_view iceberg::kPatternProperty = "pattern"
 

Detailed Description

Pluggable logging interface and the process-global default logger.

This header is backend-agnostic: it never includes the build-generated backend configuration header and never references the spdlog feature macro, so consumers see one stable API regardless of how the backend was configured.

Function Documentation

◆ CurrentLogger()

ICEBERG_EXPORT const std::shared_ptr< Logger > & iceberg::internal::CurrentLogger ( )
noexcept

Hot-path accessor for the default logger.

Returns a reference to a thread-local cached shared_ptr that is refreshed only when the default logger has changed (no lock / no refcount churn in steady state). The reference is valid for the duration of the calling statement.

◆ Emit()

ICEBERG_EXPORT void iceberg::internal::Emit ( Logger logger,
LogLevel  level,
const std::source_location &  location,
std::string &&  message 
)

Build a LogMessage from the already-formatted text and dispatch it.

Declared ICEBERG_EXPORT because the logging macros expand into this call in consumer translation units.

◆ EmitFormatError()

ICEBERG_EXPORT void iceberg::internal::EmitFormatError ( Logger logger,
LogLevel  level,
const std::source_location &  location 
)
noexcept

Emit a fixed fallback record when formatting threw.

noexcept, allocation-light (small/SSO literal), performs no std::format, and does not recurse – so the macro's "logging never throws" guarantee holds even when a format argument throws.

◆ GetCurrentLogger()

ICEBERG_EXPORT std::shared_ptr< Logger > iceberg::GetCurrentLogger ( )

Return the effective logger for this thread (never null): the active ScopedLogger binding if any, else the global default.

Off the hot path – returns an owning copy, e.g. to capture the current logger and re-bind it on a worker thread (see ScopedLogger). During teardown, prefer the Log(...) overloads over emitting through this handle.

◆ GetDefaultLogger()

ICEBERG_EXPORT std::shared_ptr< Logger > iceberg::GetDefaultLogger ( )

Return the process-global default logger (never null).

Off the hot path – acquires the slot lock and returns an owning copy. The logging macros use the cheaper internal hot-path accessor instead.

◆ Log() [1/2]

template<typename... Args>
void iceberg::Log ( Logger logger,
LogLevel  level,
internal::FmtWithLoc< std::type_identity_t< Args >... >  fmt,
Args &&...  args 
)
noexcept

Log to an explicit logger, std::format style. Formats only if enabled.

Example: iceberg::Log(logger, LogLevel::kWarn, "retry {}", attempt);

◆ Log() [2/2]

template<typename... Args>
void iceberg::Log ( LogLevel  level,
internal::FmtWithLoc< std::type_identity_t< Args >... >  fmt,
Args &&...  args 
)
noexcept

Log to the process-default logger, std::format style. Formats only if the level is enabled; never throws.

Example: iceberg::Log(LogLevel::kInfo, "loaded {} files", n);

◆ SetDefaultLevel()

ICEBERG_EXPORT void iceberg::SetDefaultLevel ( LogLevel  level)

Set the minimum level of the current default logger.

Convenience for GetDefaultLogger()->SetLevel(level). Filtering is always decided by the logger's own ShouldLog(), so changing a logger's level by any means (this, SetLevel on a held handle, or Initialize) takes effect immediately.

◆ SetDefaultLogger()

ICEBERG_EXPORT void iceberg::SetDefaultLogger ( std::shared_ptr< Logger logger)

Install a new process-global default logger.

A null argument installs the no-op logger. Thread-safe; intended for occasional (configuration-time) use rather than the hot path.

Variable Documentation

◆ kLevelProperty

constexpr std::string_view iceberg::kLevelProperty = "level"
inlineconstexpr

Well-known Logger::Initialize() property keys.

level is honored by the base Logger::Initialize (parsed via LogLevelFromString) on every backend. pattern is honored only by the spdlog backend; CerrLogger uses a fixed layout and ignores it.