|
| 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/log_macros.h |
| 23 | +/// \brief Iceberg-prefixed logging macros. |
| 24 | + |
| 25 | +#include <cstdlib> |
| 26 | +#include <format> |
| 27 | +#include <memory> |
| 28 | +#include <source_location> |
| 29 | +#include <string> |
| 30 | +#include <string_view> |
| 31 | +#include <utility> |
| 32 | + |
| 33 | +#include "iceberg/logging/logger.h" |
| 34 | + |
| 35 | +#define ICEBERG_LOG_LEVEL_TRACE 0 |
| 36 | +#define ICEBERG_LOG_LEVEL_DEBUG 1 |
| 37 | +#define ICEBERG_LOG_LEVEL_INFO 2 |
| 38 | +#define ICEBERG_LOG_LEVEL_WARN 3 |
| 39 | +#define ICEBERG_LOG_LEVEL_ERROR 4 |
| 40 | +#define ICEBERG_LOG_LEVEL_CRITICAL 5 |
| 41 | +#define ICEBERG_LOG_LEVEL_FATAL 6 |
| 42 | +#define ICEBERG_LOG_LEVEL_OFF 7 |
| 43 | + |
| 44 | +#ifndef ICEBERG_LOG_ACTIVE_LEVEL |
| 45 | +# define ICEBERG_LOG_ACTIVE_LEVEL ICEBERG_LOG_LEVEL_TRACE |
| 46 | +#endif |
| 47 | + |
| 48 | +static_assert(static_cast<int>(::iceberg::LogLevel::kTrace) == ICEBERG_LOG_LEVEL_TRACE); |
| 49 | +static_assert(static_cast<int>(::iceberg::LogLevel::kDebug) == ICEBERG_LOG_LEVEL_DEBUG); |
| 50 | +static_assert(static_cast<int>(::iceberg::LogLevel::kInfo) == ICEBERG_LOG_LEVEL_INFO); |
| 51 | +static_assert(static_cast<int>(::iceberg::LogLevel::kWarn) == ICEBERG_LOG_LEVEL_WARN); |
| 52 | +static_assert(static_cast<int>(::iceberg::LogLevel::kError) == ICEBERG_LOG_LEVEL_ERROR); |
| 53 | +static_assert(static_cast<int>(::iceberg::LogLevel::kCritical) == |
| 54 | + ICEBERG_LOG_LEVEL_CRITICAL); |
| 55 | +static_assert(static_cast<int>(::iceberg::LogLevel::kFatal) == ICEBERG_LOG_LEVEL_FATAL); |
| 56 | +static_assert(static_cast<int>(::iceberg::LogLevel::kOff) == ICEBERG_LOG_LEVEL_OFF); |
| 57 | + |
| 58 | +namespace iceberg::logging::detail { |
| 59 | + |
| 60 | +template <typename... Args> |
| 61 | +std::string VFormat(std::string_view fmt, Args&&... args) { |
| 62 | + auto store = std::make_format_args(args...); |
| 63 | + return std::vformat(fmt, store); |
| 64 | +} |
| 65 | + |
| 66 | +template <typename FormatMessage> |
| 67 | +void EmitIfEnabled(Logger& logger, LogLevel level, const std::source_location& location, |
| 68 | + FormatMessage&& format_message) noexcept { |
| 69 | + if (level == LogLevel::kOff || !logger.ShouldLog(level)) return; |
| 70 | + |
| 71 | + try { |
| 72 | + ::iceberg::internal::Emit(logger, level, location, |
| 73 | + std::forward<FormatMessage>(format_message)()); |
| 74 | + } catch (...) { |
| 75 | + ::iceberg::internal::EmitFormatError(logger, level, location); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +template <typename FormatMessage> |
| 80 | +void LogCurrentIfEnabled(LogLevel level, const std::source_location& location, |
| 81 | + FormatMessage&& format_message) noexcept { |
| 82 | + const std::shared_ptr<Logger>& logger = ::iceberg::internal::CurrentLogger(); |
| 83 | + if (logger) { |
| 84 | + EmitIfEnabled(*logger, level, location, std::forward<FormatMessage>(format_message)); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +template <typename FormatMessage> |
| 89 | +void LogCurrentRuntime(LogLevel level, const std::source_location& location, |
| 90 | + FormatMessage&& format_message) noexcept { |
| 91 | + const std::shared_ptr<Logger>& logger = ::iceberg::internal::CurrentLogger(); |
| 92 | + if (logger) { |
| 93 | + EmitIfEnabled(*logger, level, location, std::forward<FormatMessage>(format_message)); |
| 94 | + } |
| 95 | + if (level == LogLevel::kFatal) { |
| 96 | + if (logger) logger->Flush(); |
| 97 | + std::abort(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +template <typename FormatMessage> |
| 102 | +void LogToRuntime(Logger& logger, LogLevel level, const std::source_location& location, |
| 103 | + FormatMessage&& format_message) noexcept { |
| 104 | + EmitIfEnabled(logger, level, location, std::forward<FormatMessage>(format_message)); |
| 105 | + if (level == LogLevel::kFatal) { |
| 106 | + logger.Flush(); |
| 107 | + std::abort(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +template <typename FormatMessage> |
| 112 | +[[noreturn]] void LogCurrentFatal(const std::source_location& location, |
| 113 | + FormatMessage&& format_message) noexcept { |
| 114 | + auto logger = ::iceberg::GetCurrentLogger(); |
| 115 | + if (logger) { |
| 116 | + EmitIfEnabled(*logger, LogLevel::kFatal, location, |
| 117 | + std::forward<FormatMessage>(format_message)); |
| 118 | + logger->Flush(); |
| 119 | + } |
| 120 | + std::abort(); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace iceberg::logging::detail |
| 124 | + |
| 125 | +#define ICEBERG_INTERNAL_LOG_CURRENT(level_, FMT_, ...) \ |
| 126 | + do { \ |
| 127 | + ::iceberg::logging::detail::LogCurrentIfEnabled( \ |
| 128 | + (level_), ::std::source_location::current(), [&]() -> ::std::string { \ |
| 129 | + return ::std::format((FMT_)__VA_OPT__(, ) __VA_ARGS__); \ |
| 130 | + }); \ |
| 131 | + } while (false) |
| 132 | + |
| 133 | +#define ICEBERG_INTERNAL_LOG_RUNTIME(level_, FMT_, ...) \ |
| 134 | + do { \ |
| 135 | + ::iceberg::logging::detail::LogCurrentRuntime( \ |
| 136 | + (level_), ::std::source_location::current(), [&]() -> ::std::string { \ |
| 137 | + return ::std::format((FMT_)__VA_OPT__(, ) __VA_ARGS__); \ |
| 138 | + }); \ |
| 139 | + } while (false) |
| 140 | + |
| 141 | +#define ICEBERG_INTERNAL_LOG_TO(logger_, level_, FMT_, ...) \ |
| 142 | + do { \ |
| 143 | + ::iceberg::logging::detail::LogToRuntime( \ |
| 144 | + (logger_), (level_), ::std::source_location::current(), [&]() -> ::std::string { \ |
| 145 | + return ::std::format((FMT_)__VA_OPT__(, ) __VA_ARGS__); \ |
| 146 | + }); \ |
| 147 | + } while (false) |
| 148 | + |
| 149 | +#define ICEBERG_INTERNAL_LOG_RUNTIME_FMT(level_, FMT_, ...) \ |
| 150 | + do { \ |
| 151 | + ::iceberg::logging::detail::LogCurrentRuntime( \ |
| 152 | + (level_), ::std::source_location::current(), [&]() -> ::std::string { \ |
| 153 | + return ::iceberg::logging::detail::VFormat((FMT_)__VA_OPT__(, ) __VA_ARGS__); \ |
| 154 | + }); \ |
| 155 | + } while (false) |
| 156 | + |
| 157 | +#define ICEBERG_INTERNAL_LOG_FATAL(FMT_, ...) \ |
| 158 | + do { \ |
| 159 | + ::iceberg::logging::detail::LogCurrentFatal( \ |
| 160 | + ::std::source_location::current(), [&]() -> ::std::string { \ |
| 161 | + return ::std::format((FMT_)__VA_OPT__(, ) __VA_ARGS__); \ |
| 162 | + }); \ |
| 163 | + } while (false) |
| 164 | + |
| 165 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_TRACE |
| 166 | +# define ICEBERG_LOG_TRACE(...) \ |
| 167 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kTrace, __VA_ARGS__) |
| 168 | +#else |
| 169 | +# define ICEBERG_LOG_TRACE(...) ((void)0) |
| 170 | +#endif |
| 171 | + |
| 172 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_DEBUG |
| 173 | +# define ICEBERG_LOG_DEBUG(...) \ |
| 174 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kDebug, __VA_ARGS__) |
| 175 | +#else |
| 176 | +# define ICEBERG_LOG_DEBUG(...) ((void)0) |
| 177 | +#endif |
| 178 | + |
| 179 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_INFO |
| 180 | +# define ICEBERG_LOG_INFO(...) \ |
| 181 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kInfo, __VA_ARGS__) |
| 182 | +#else |
| 183 | +# define ICEBERG_LOG_INFO(...) ((void)0) |
| 184 | +#endif |
| 185 | + |
| 186 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_WARN |
| 187 | +# define ICEBERG_LOG_WARN(...) \ |
| 188 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kWarn, __VA_ARGS__) |
| 189 | +#else |
| 190 | +# define ICEBERG_LOG_WARN(...) ((void)0) |
| 191 | +#endif |
| 192 | + |
| 193 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_ERROR |
| 194 | +# define ICEBERG_LOG_ERROR(...) \ |
| 195 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kError, __VA_ARGS__) |
| 196 | +#else |
| 197 | +# define ICEBERG_LOG_ERROR(...) ((void)0) |
| 198 | +#endif |
| 199 | + |
| 200 | +#if ICEBERG_LOG_ACTIVE_LEVEL <= ICEBERG_LOG_LEVEL_CRITICAL |
| 201 | +# define ICEBERG_LOG_CRITICAL(...) \ |
| 202 | + ICEBERG_INTERNAL_LOG_CURRENT(::iceberg::LogLevel::kCritical, __VA_ARGS__) |
| 203 | +#else |
| 204 | +# define ICEBERG_LOG_CRITICAL(...) ((void)0) |
| 205 | +#endif |
| 206 | + |
| 207 | +#define ICEBERG_LOG_FATAL(...) ICEBERG_INTERNAL_LOG_FATAL(__VA_ARGS__) |
| 208 | + |
| 209 | +#define ICEBERG_LOG(...) ICEBERG_INTERNAL_LOG_RUNTIME(__VA_ARGS__) |
| 210 | + |
| 211 | +#define ICEBERG_LOG_TO(...) ICEBERG_INTERNAL_LOG_TO(__VA_ARGS__) |
| 212 | + |
| 213 | +#define ICEBERG_LOG_RUNTIME_FMT(...) ICEBERG_INTERNAL_LOG_RUNTIME_FMT(__VA_ARGS__) |
0 commit comments