|
| 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 | +#include "iceberg/logging/internal/spdlog_logger.h" |
| 21 | + |
| 22 | +#ifdef ICEBERG_HAS_SPDLOG |
| 23 | + |
| 24 | +# include <memory> |
| 25 | +# include <string> |
| 26 | +# include <unordered_map> |
| 27 | +# include <utility> |
| 28 | + |
| 29 | +# include <spdlog/common.h> |
| 30 | +# include <spdlog/sinks/stdout_color_sinks.h> |
| 31 | + |
| 32 | +namespace iceberg::internal { |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +spdlog::level::level_enum ToSpdLevel(LogLevel level) noexcept { |
| 37 | + switch (level) { |
| 38 | + case LogLevel::kTrace: |
| 39 | + return spdlog::level::trace; |
| 40 | + case LogLevel::kDebug: |
| 41 | + return spdlog::level::debug; |
| 42 | + case LogLevel::kInfo: |
| 43 | + return spdlog::level::info; |
| 44 | + case LogLevel::kWarn: |
| 45 | + return spdlog::level::warn; |
| 46 | + case LogLevel::kError: |
| 47 | + return spdlog::level::err; |
| 48 | + case LogLevel::kCritical: |
| 49 | + case LogLevel::kFatal: |
| 50 | + // spdlog has no "fatal"; the process abort is owned by the macro layer. |
| 51 | + return spdlog::level::critical; |
| 52 | + case LogLevel::kOff: |
| 53 | + return spdlog::level::off; |
| 54 | + } |
| 55 | + return spdlog::level::off; |
| 56 | +} |
| 57 | + |
| 58 | +/// \brief The built-in sink: a color stderr spdlog logger. |
| 59 | +std::shared_ptr<spdlog::logger> MakeDefaultSpdLogger() { |
| 60 | + return std::make_shared<spdlog::logger>( |
| 61 | + "iceberg", std::make_shared<spdlog::sinks::stderr_color_sink_mt>()); |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace |
| 65 | + |
| 66 | +SpdLogger::SpdLogger(LogLevel level) : SpdLogger(MakeDefaultSpdLogger(), level) {} |
| 67 | + |
| 68 | +Status SpdLogger::Initialize( |
| 69 | + const std::unordered_map<std::string, std::string>& properties) { |
| 70 | + if (auto it = properties.find(std::string(kPatternProperty)); it != properties.end()) { |
| 71 | + logger_->set_pattern(it->second); |
| 72 | + } |
| 73 | + // Apply "level" via the base implementation. |
| 74 | + return Logger::Initialize(properties); |
| 75 | +} |
| 76 | + |
| 77 | +SpdLogger::SpdLogger(std::shared_ptr<spdlog::logger> logger, LogLevel level) |
| 78 | + : logger_(std::move(logger)), level_(level) { |
| 79 | + // logger_ is non-null for the rest of this object's life, so Initialize/Log/Flush |
| 80 | + // may dereference it unconditionally. Enforced by substitution rather than an |
| 81 | + // assertion, which would vanish under NDEBUG and leave a release-build crash: a |
| 82 | + // null argument falls back to the same stderr-backed logger the default |
| 83 | + // constructor builds, so a caller mistake degrades to the default sink. |
| 84 | + if (!logger_) { |
| 85 | + logger_ = MakeDefaultSpdLogger(); |
| 86 | + } |
| 87 | + logger_->set_level(spdlog::level::trace); // filtering is done by ShouldLog |
| 88 | +} |
| 89 | + |
| 90 | +void SpdLogger::Log(LogMessage&& message) noexcept { |
| 91 | + try { |
| 92 | + spdlog::source_loc loc{message.location.file_name(), |
| 93 | + static_cast<int>(message.location.line()), |
| 94 | + message.location.function_name()}; |
| 95 | + // Raw-message overload: the text is already formatted, so hand spdlog the bytes |
| 96 | + // directly instead of running them back through fmt (which would re-parse and |
| 97 | + // copy the whole message, allocating for long ones). It also means braces in the |
| 98 | + // message can never be interpreted as format placeholders. |
| 99 | + logger_->log(loc, ToSpdLevel(message.level), |
| 100 | + spdlog::string_view_t{message.message.data(), message.message.size()}); |
| 101 | + } catch (...) { |
| 102 | + // Logging must never throw. |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void SpdLogger::Flush() noexcept { |
| 107 | + try { |
| 108 | + logger_->flush(); |
| 109 | + } catch (...) { |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace iceberg::internal |
| 114 | + |
| 115 | +#endif // ICEBERG_HAS_SPDLOG |
0 commit comments