Skip to content

Commit 14a0d79

Browse files
committed
feat: add a plugable logger interface.
1 parent 5bffdf6 commit 14a0d79

File tree

5 files changed

+557
-2
lines changed

5 files changed

+557
-2
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ set(ICEBERG_SOURCES
4949
arrow_c_data_guard_internal.cc
5050
util/murmurhash3_internal.cc
5151
util/timepoint.cc
52-
util/gzip_internal.cc)
52+
util/gzip_internal.cc
53+
util/logger.cc)
5354

5455
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
5556
set(ICEBERG_SHARED_BUILD_INTERFACE_LIBS)

src/iceberg/util/logger.cc

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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/util/logger.h"
21+
22+
#include <spdlog/sinks/stdout_color_sinks.h>
23+
#include <spdlog/spdlog.h>
24+
25+
namespace iceberg {
26+
27+
namespace {
28+
29+
/// \brief Convert iceberg LogLevel to spdlog level
30+
spdlog::level::level_enum ToSpdlogLevel(LogLevel level) {
31+
switch (level) {
32+
case LogLevel::kTrace:
33+
return spdlog::level::trace;
34+
case LogLevel::kDebug:
35+
return spdlog::level::debug;
36+
case LogLevel::kInfo:
37+
return spdlog::level::info;
38+
case LogLevel::kWarn:
39+
return spdlog::level::warn;
40+
case LogLevel::kError:
41+
return spdlog::level::err;
42+
case LogLevel::kCritical:
43+
return spdlog::level::critical;
44+
case LogLevel::kOff:
45+
return spdlog::level::off;
46+
default:
47+
return spdlog::level::info;
48+
}
49+
}
50+
51+
/**
52+
* \brief Convert spdlog level to iceberg LogLevel
53+
*/
54+
LogLevel FromSpdlogLevel(spdlog::level::level_enum level) {
55+
switch (level) {
56+
case spdlog::level::trace:
57+
return LogLevel::kTrace;
58+
case spdlog::level::debug:
59+
return LogLevel::kDebug;
60+
case spdlog::level::info:
61+
return LogLevel::kInfo;
62+
case spdlog::level::warn:
63+
return LogLevel::kWarn;
64+
case spdlog::level::err:
65+
return LogLevel::kError;
66+
case spdlog::level::critical:
67+
return LogLevel::kCritical;
68+
case spdlog::level::off:
69+
return LogLevel::kOff;
70+
default:
71+
return LogLevel::kInfo;
72+
}
73+
}
74+
75+
} // namespace
76+
77+
// SpdlogLogger implementation
78+
SpdlogLogger::SpdlogLogger(std::string_view logger_name) {
79+
auto spdlog_logger = spdlog::get(std::string(logger_name));
80+
if (!spdlog_logger) {
81+
spdlog_logger = spdlog::stdout_color_mt(std::string(logger_name));
82+
}
83+
logger_ = std::static_pointer_cast<void>(spdlog_logger);
84+
current_level_ = FromSpdlogLevel(spdlog_logger->level());
85+
}
86+
87+
SpdlogLogger::SpdlogLogger(std::shared_ptr<void> spdlog_logger)
88+
: logger_(std::move(spdlog_logger)) {
89+
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
90+
current_level_ = FromSpdlogLevel(typed_logger->level());
91+
}
92+
93+
bool SpdlogLogger::ShouldLogImpl(LogLevel level) const noexcept {
94+
return level >= current_level_;
95+
}
96+
97+
void SpdlogLogger::LogRawImpl(LogLevel level, const std::string& message) const {
98+
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
99+
auto spdlog_level = ToSpdlogLevel(level);
100+
typed_logger->log(spdlog_level, message);
101+
}
102+
103+
void SpdlogLogger::LogWithLocationRawImpl(LogLevel level, const std::string& message,
104+
const std::source_location& location) const {
105+
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
106+
auto spdlog_level = ToSpdlogLevel(level);
107+
108+
// Add source location information
109+
std::string full_message =
110+
std::format("[{}:{}:{}] {}", location.file_name(), location.line(),
111+
location.function_name(), message);
112+
113+
typed_logger->log(spdlog_level, full_message);
114+
}
115+
116+
void SpdlogLogger::SetLevelImpl(LogLevel level) {
117+
current_level_ = level;
118+
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
119+
typed_logger->set_level(ToSpdlogLevel(level));
120+
}
121+
122+
LogLevel SpdlogLogger::GetLevelImpl() const noexcept { return current_level_; }
123+
124+
// LoggerRegistry implementation
125+
LoggerRegistry& LoggerRegistry::Instance() {
126+
static LoggerRegistry instance;
127+
return instance;
128+
}
129+
130+
void LoggerRegistry::InitializeDefault(std::string_view logger_name) {
131+
auto spdlog_logger = std::make_shared<SpdlogLogger>(logger_name);
132+
SetDefaultLogger(spdlog_logger);
133+
}
134+
135+
} // namespace iceberg

0 commit comments

Comments
 (0)