iceberg-cpp
Loading...
Searching...
No Matches
logging_test_helpers.h
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#pragma once
21
22#include <atomic>
23#include <memory>
24#include <mutex>
25#include <utility>
26#include <vector>
27
29
30namespace iceberg {
31
33class CapturingLogger : public Logger {
34 public:
35 bool ShouldLog(LogLevel level) const noexcept override {
36 return level >= level_.load(std::memory_order_relaxed);
37 }
38
39 void Log(LogMessage&& message) noexcept override {
40 std::lock_guard<std::mutex> lock(mutex_);
41 records_.push_back(std::move(message));
42 }
43
44 void SetLevel(LogLevel level) noexcept override {
45 level_.store(level, std::memory_order_relaxed);
46 }
47 LogLevel level() const noexcept override {
48 return level_.load(std::memory_order_relaxed);
49 }
50
51 std::vector<LogMessage> records() const {
52 std::lock_guard<std::mutex> lock(mutex_);
53 return records_;
54 }
55
56 std::size_t count() const {
57 std::lock_guard<std::mutex> lock(mutex_);
58 return records_.size();
59 }
60
61 private:
62 mutable std::mutex mutex_;
63 std::atomic<LogLevel> level_ = LogLevel::kTrace;
64 std::vector<LogMessage> records_;
65};
66
70 public:
71 explicit ScopedDefaultLogger(std::shared_ptr<Logger> logger)
72 : previous_(GetDefaultLogger()) {
73 SetDefaultLogger(std::move(logger));
74 }
75 ~ScopedDefaultLogger() { SetDefaultLogger(previous_); }
76
78 ScopedDefaultLogger& operator=(const ScopedDefaultLogger&) = delete;
79
80 private:
81 std::shared_ptr<Logger> previous_;
82};
83
84} // namespace iceberg
Test sink that records every emitted LogMessage under a mutex.
Definition logging_test_helpers.h:33
bool ShouldLog(LogLevel level) const noexcept override
Cheap check whether a record at level would be emitted.
Definition logging_test_helpers.h:35
void Log(LogMessage &&message) noexcept override
Emit one (already-formatted) record, taking ownership. Must not throw.
Definition logging_test_helpers.h:39
void SetLevel(LogLevel level) noexcept override
Set the minimum level this logger emits.
Definition logging_test_helpers.h:44
LogLevel level() const noexcept override
Return the minimum level this logger emits.
Definition logging_test_helpers.h:47
Pluggable logging sink.
Definition logger.h:145
RAII guard that restores the process default logger on scope exit, so tests that swap the global defa...
Definition logging_test_helpers.h:69
LogLevel
Logging severity level, ordered from most to least verbose.
Definition log_level.h:38
Pluggable logging interface and the process-global default logger.
A single log record handed to a Logger.
Definition logger.h:64