|
| 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/loggers.h" |
| 21 | + |
| 22 | +#include <exception> |
| 23 | +#include <memory> |
| 24 | +#include <mutex> |
| 25 | +#include <shared_mutex> |
| 26 | +#include <string> |
| 27 | +#include <unordered_map> |
| 28 | +#include <utility> |
| 29 | + |
| 30 | +// Build-generated, .cc-only. Defines ICEBERG_HAS_SPDLOG; tested with #ifdef. |
| 31 | +#include "iceberg/logging/cerr_logger.h" |
| 32 | +#include "iceberg/logging/config.h" |
| 33 | +#include "iceberg/util/macros.h" |
| 34 | +#ifdef ICEBERG_HAS_SPDLOG |
| 35 | +# include "iceberg/logging/spdlog_logger_internal.h" |
| 36 | +#endif |
| 37 | + |
| 38 | +namespace iceberg { |
| 39 | + |
| 40 | +namespace { |
| 41 | + |
| 42 | +/// \brief Extract the logger type, defaulting to the compiled-in backend. |
| 43 | +std::string InferLoggerType( |
| 44 | + const std::unordered_map<std::string, std::string>& properties) { |
| 45 | + auto it = properties.find(std::string(kLoggerImpl)); |
| 46 | + if (it != properties.end() && !it->second.empty()) { |
| 47 | + return it->second; |
| 48 | + } |
| 49 | +#ifdef ICEBERG_HAS_SPDLOG |
| 50 | + return std::string(kLoggerTypeSpdlog); |
| 51 | +#else |
| 52 | + return std::string(kLoggerTypeCerr); |
| 53 | +#endif |
| 54 | +} |
| 55 | + |
| 56 | +struct LoggerRegistryState { |
| 57 | + std::shared_mutex mtx; |
| 58 | + std::unordered_map<std::string, LoggerFactory> map; |
| 59 | +}; |
| 60 | + |
| 61 | +LoggerRegistryState& GetRegistry() { |
| 62 | + static auto* state = new LoggerRegistryState{ |
| 63 | + .map = { |
| 64 | + {std::string(kLoggerTypeNoop), |
| 65 | + [](const std::unordered_map<std::string, std::string>&) |
| 66 | + -> Result<std::unique_ptr<Logger>> { return internal::MakeNoopLogger(); }}, |
| 67 | + {std::string(kLoggerTypeCerr), |
| 68 | + [](const std::unordered_map<std::string, std::string>&) |
| 69 | + -> Result<std::unique_ptr<Logger>> { |
| 70 | + return std::make_unique<CerrLogger>(); |
| 71 | + }}, |
| 72 | +#ifdef ICEBERG_HAS_SPDLOG |
| 73 | + {std::string(kLoggerTypeSpdlog), |
| 74 | + [](const std::unordered_map<std::string, std::string>&) |
| 75 | + -> Result<std::unique_ptr<Logger>> { |
| 76 | + return std::make_unique<internal::SpdLogger>(); |
| 77 | + }}, |
| 78 | +#endif |
| 79 | + }}; |
| 80 | + return *state; |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace |
| 84 | + |
| 85 | +Status Loggers::Register(std::string_view logger_type, LoggerFactory factory) { |
| 86 | + if (!factory) { |
| 87 | + return InvalidArgument("Logger factory for '{}' must not be empty", logger_type); |
| 88 | + } |
| 89 | + auto& registry = GetRegistry(); |
| 90 | + std::unique_lock lock(registry.mtx); |
| 91 | + registry.map[std::string(logger_type)] = std::move(factory); |
| 92 | + return {}; |
| 93 | +} |
| 94 | + |
| 95 | +Result<std::unique_ptr<Logger>> Loggers::Load( |
| 96 | + const std::unordered_map<std::string, std::string>& properties) { |
| 97 | + std::string logger_type = InferLoggerType(properties); |
| 98 | + |
| 99 | + LoggerFactory factory; |
| 100 | + { |
| 101 | + auto& registry = GetRegistry(); |
| 102 | + std::shared_lock lock(registry.mtx); |
| 103 | + auto it = registry.map.find(logger_type); |
| 104 | + if (it == registry.map.end()) { |
| 105 | + return InvalidArgument( |
| 106 | + "Unknown logger type '{}'. Register a factory with Loggers::Register() " |
| 107 | + "before using this type.", |
| 108 | + logger_type); |
| 109 | + } |
| 110 | + factory = it->second; |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + // Run the (user-supplied) factory outside the registry lock so it cannot |
| 115 | + // deadlock or re-enter the registry; the try/catch turns a throwing factory |
| 116 | + // into an error instead of propagating. |
| 117 | + ICEBERG_ASSIGN_OR_RAISE(auto logger, factory(properties)); |
| 118 | + if (!logger) { |
| 119 | + return InvalidArgument("Logger factory for '{}' returned null", logger_type); |
| 120 | + } |
| 121 | + ICEBERG_RETURN_UNEXPECTED(logger->Initialize(properties)); |
| 122 | + return logger; |
| 123 | + } catch (const std::exception& ex) { |
| 124 | + return InvalidArgument("Logger factory for '{}' failed: {}", logger_type, ex.what()); |
| 125 | + } catch (...) { |
| 126 | + return InvalidArgument("Logger factory for '{}' failed with unknown exception", |
| 127 | + logger_type); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace iceberg |
0 commit comments