Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ci/scripts/build_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ source_dir=${1}
build_dir=${1}/build
run_example=${ICEBERG_RUN_EXAMPLE:-OFF}

mkdir ${build_dir}
rm -rf "${build_dir}"
mkdir "${build_dir}"
pushd ${build_dir}

is_windows() {
Expand Down Expand Up @@ -60,6 +61,3 @@ else
fi

popd

# clean up between builds
rm -rf ${build_dir}
2 changes: 2 additions & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ set(ICEBERG_SOURCES
inheritable_metadata.cc
json_serde.cc
location_provider.cc
logging/cerr_logger.cc
logging/logger.cc
manifest/manifest_adapter.cc
manifest/manifest_entry.cc
Expand Down Expand Up @@ -133,6 +134,7 @@ set(ICEBERG_SOURCES
util/struct_like_set.cc
util/task_group.cc
util/temporal_util.cc
util/thread_util.cc
util/timepoint.cc
util/transform_util.cc
util/truncate_util.cc
Expand Down
77 changes: 77 additions & 0 deletions src/iceberg/logging/cerr_logger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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/logging/cerr_logger.h"

#include <chrono>
#include <format>
#include <iostream>
#include <mutex>
#include <string>
#include <string_view>

#include "iceberg/util/thread_util_internal.h"

namespace iceberg {

namespace {

/// \brief Trailing path component of a source file path.
std::string_view Basename(std::string_view path) noexcept {
auto pos = path.find_last_of("/\\");
return pos == std::string_view::npos ? path : path.substr(pos + 1);
}

/// \brief Format a record into a single newline-terminated line.
std::string FormatLine(const LogMessage& message) {
auto now =
std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now());
return std::format("{:%Y-%m-%dT%H:%M:%S}Z {} [{}] [{}:{}] {}\n", now,
ToString(message.level), OsThreadId(),
Basename(message.location.file_name()), message.location.line(),
message.message);
}

} // namespace

void CerrLogger::Log(LogMessage&& message) noexcept {
try {
std::string line = FormatLine(message);
std::lock_guard<std::mutex> lock(mutex_);
std::cerr << line;
} catch (...) {
// Logging must never throw. Reached if either formatting or the write fails;
// emit a short marker best-effort and swallow anything further.
try {
std::lock_guard<std::mutex> lock(mutex_);
std::cerr << "<fmt error>\n";
} catch (...) {
}
}
}

void CerrLogger::Flush() noexcept {
try {
std::lock_guard<std::mutex> lock(mutex_);
std::cerr.flush();
} catch (...) {
}
}

} // namespace iceberg
61 changes: 61 additions & 0 deletions src/iceberg/logging/cerr_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.
*/

#pragma once

/// \file iceberg/logging/cerr_logger.h
/// \brief Always-available std::cerr logging backend.

#include <atomic>
#include <mutex>

#include "iceberg/iceberg_export.h"
#include "iceberg/logging/log_level.h"
#include "iceberg/logging/logger.h"

namespace iceberg {

/// \brief Logger that writes one line per record to std::cerr.
///
/// Line layout: `YYYY-MM-DDThh:mm:ss.mmmZ LEVEL [tid] [file:line] message`.
/// The minimum level is held in a lock-free atomic; a mutex serializes the
/// whole-line write so concurrent records never interleave. Pure standard
/// library -- always compiled, regardless of ICEBERG_SPDLOG.
class ICEBERG_EXPORT CerrLogger : public Logger {
Comment thread
wgtmac marked this conversation as resolved.
public:
explicit CerrLogger(LogLevel level = LogLevel::kInfo) : level_(level) {}

bool ShouldLog(LogLevel level) const noexcept override {
return level >= level_.load(std::memory_order_relaxed);
}
void Log(LogMessage&& message) noexcept override;
void SetLevel(LogLevel level) noexcept override {
level_.store(level, std::memory_order_relaxed);
}
LogLevel level() const noexcept override {
return level_.load(std::memory_order_relaxed);
}
void Flush() noexcept override;

private:
std::atomic<LogLevel> level_;
std::mutex mutex_;
};

} // namespace iceberg
9 changes: 5 additions & 4 deletions src/iceberg/logging/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <tuple>
#include <utility>

#include "iceberg/logging/cerr_logger.h"

namespace iceberg {

namespace {
Expand All @@ -42,10 +44,9 @@ class NoopLogger final : public Logger {

/// \brief Construct the process default logger for this build configuration.
///
/// This block ships only the interface and the no-op logger; the concrete
/// std::cerr and spdlog sinks (and the build-config selection between them)
/// arrive in later blocks, which update this factory.
std::shared_ptr<Logger> MakeDefaultLogger() { return std::make_shared<NoopLogger>(); }
/// Uses the always-available std::cerr sink. The spdlog backend (preferred when
/// compiled in) is wired into this factory in a later block.
std::shared_ptr<Logger> MakeDefaultLogger() { return std::make_shared<CerrLogger>(); }

/// \brief The process-global default-logger slot.
struct DefaultSlot {
Expand Down
8 changes: 4 additions & 4 deletions src/iceberg/logging/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class ICEBERG_EXPORT LogMessage::Builder {
/// \brief Well-known Logger::Initialize() property keys.
///
/// `level` is honored by the base Logger::Initialize (parsed via
/// LogLevelFromString). `pattern` is honored by the formatting sinks
/// (CerrLogger, SpdLogger).
/// LogLevelFromString) on every backend. `pattern` is honored only by the
/// spdlog backend; CerrLogger uses a fixed layout and ignores it.
Comment thread
wgtmac marked this conversation as resolved.
inline constexpr std::string_view kLevelProperty = "level";
inline constexpr std::string_view kPatternProperty = "pattern";

Expand All @@ -150,8 +150,8 @@ class ICEBERG_EXPORT Logger {
///
/// The base implementation applies the "level" property (parsed via
/// LogLevelFromString); an unrecognized value is an InvalidArgument error.
/// Formatting sinks override this to also apply "pattern" and then delegate
/// to this base for "level".
/// The spdlog backend overrides this to also apply "pattern" and then delegates
/// to this base for "level"; CerrLogger uses the base as-is (fixed layout).
virtual Status Initialize(
const std::unordered_map<std::string, std::string>& properties) {
if (auto it = properties.find(std::string(kLevelProperty)); it != properties.end()) {
Expand Down
5 changes: 4 additions & 1 deletion src/iceberg/logging/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
# specific language governing permissions and limitations
# under the License.

install_headers(['log_level.h', 'logger.h'], subdir: 'iceberg/logging')
install_headers(
['cerr_logger.h', 'log_level.h', 'logger.h'],
subdir: 'iceberg/logging',
)
2 changes: 2 additions & 0 deletions src/iceberg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ iceberg_sources = files(
'inspect/snapshots_table.cc',
'json_serde.cc',
'location_provider.cc',
'logging/cerr_logger.cc',
'logging/logger.cc',
'manifest/manifest_adapter.cc',
'manifest/manifest_entry.cc',
Expand Down Expand Up @@ -158,6 +159,7 @@ iceberg_sources = files(
'util/struct_like_set.cc',
'util/task_group.cc',
'util/temporal_util.cc',
'util/thread_util.cc',
'util/timepoint.cc',
'util/transform_util.cc',
'util/truncate_util.cc',
Expand Down
6 changes: 5 additions & 1 deletion src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ add_iceberg_test(table_test
table_test.cc
table_update_test.cc)

add_iceberg_test(logging_test SOURCES log_level_test.cc logger_test.cc)
add_iceberg_test(logging_test
SOURCES
cerr_logger_test.cc
log_level_test.cc
logger_test.cc)

add_iceberg_test(expression_test
SOURCES
Expand Down
Loading
Loading