-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathlogger.cc
More file actions
131 lines (112 loc) · 3.99 KB
/
logger.cc
File metadata and controls
131 lines (112 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "iceberg/util/logger.h"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include "iceberg/util/spdlog_logger.h"
namespace iceberg {
namespace {
/// \brief Convert iceberg LogLevel to spdlog level
spdlog::level::level_enum ToSpdlogLevel(LogLevel level) {
switch (level) {
case LogLevel::kTrace:
return spdlog::level::trace;
case LogLevel::kDebug:
return spdlog::level::debug;
case LogLevel::kInfo:
return spdlog::level::info;
case LogLevel::kWarn:
return spdlog::level::warn;
case LogLevel::kError:
return spdlog::level::err;
case LogLevel::kCritical:
return spdlog::level::critical;
case LogLevel::kOff:
return spdlog::level::off;
default:
return spdlog::level::info;
}
}
/**
* \brief Convert spdlog level to iceberg LogLevel
*/
LogLevel FromSpdlogLevel(spdlog::level::level_enum level) {
switch (level) {
case spdlog::level::trace:
return LogLevel::kTrace;
case spdlog::level::debug:
return LogLevel::kDebug;
case spdlog::level::info:
return LogLevel::kInfo;
case spdlog::level::warn:
return LogLevel::kWarn;
case spdlog::level::err:
return LogLevel::kError;
case spdlog::level::critical:
return LogLevel::kCritical;
case spdlog::level::off:
return LogLevel::kOff;
default:
return LogLevel::kInfo;
}
}
} // namespace
// SpdlogLogger implementation
SpdlogLogger::SpdlogLogger(std::string_view logger_name) {
auto spdlog_logger = spdlog::get(std::string(logger_name));
if (!spdlog_logger) {
spdlog_logger = spdlog::stdout_color_mt(std::string(logger_name));
}
logger_ = std::static_pointer_cast<void>(spdlog_logger);
current_level_ = FromSpdlogLevel(spdlog_logger->level());
}
SpdlogLogger::SpdlogLogger(std::shared_ptr<void> spdlog_logger)
: logger_(std::move(spdlog_logger)) {
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
current_level_ = FromSpdlogLevel(typed_logger->level());
}
bool SpdlogLogger::ShouldLogImpl(LogLevel level) const noexcept {
return level >= current_level_;
}
void SpdlogLogger::LogRawImpl(LogLevel level, const std::source_location& location,
const std::string& message) const {
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
auto spdlog_level = ToSpdlogLevel(level);
// Add source location information
std::string full_message =
std::format("[{}:{}:{}] {}", location.file_name(), location.line(),
location.function_name(), message);
typed_logger->log(spdlog_level, full_message);
}
void SpdlogLogger::SetLevelImpl(LogLevel level) {
current_level_ = level;
auto typed_logger = std::static_pointer_cast<spdlog::logger>(logger_);
typed_logger->set_level(ToSpdlogLevel(level));
}
LogLevel SpdlogLogger::GetLevelImpl() const noexcept { return current_level_; }
// LoggerRegistry implementation
LoggerRegistry& LoggerRegistry::Instance() {
static LoggerRegistry instance;
return instance;
}
void LoggerRegistry::InitializeDefault(std::string_view logger_name) {
auto spdlog_logger = std::make_shared<SpdlogLogger>(logger_name);
SetDefaultLogger(spdlog_logger);
}
} // namespace iceberg