Skip to content

Commit

Permalink
add utils for trace log; shirakami_log_entry_lazy and span_printer
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Jan 22, 2025
1 parent 255c02a commit 4bd8469
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/shirakami/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>

#include "binary_printer.h"
#include "span_printer.h"

namespace shirakami {

Expand All @@ -21,6 +22,9 @@ static constexpr std::string_view log_location_prefix_timing_event =
#define shirakami_binstring(arg) \
" " #arg "(len=" << (arg).size() << "):\"" << binary_printer((arg)) \
<< "\"" //NOLINT
// receive std::vector only
#define shirakami_vecstring(arg) \
" " #arg "(len=" << (arg).size() << "):" << span_printer((arg))

/**
* @brief logging level constant for errors
Expand Down
59 changes: 59 additions & 0 deletions include/shirakami/span_printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025-2025 tsurugi project.
*
* Licensed 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

#include <ostream>
#include <string_view>
#include <vector>

namespace shirakami {

/**
* @brief debug support to print C++20-std::span-like range
*/
template<typename T>
class span_printer {
public:
constexpr explicit span_printer(const std::vector<T>& vec) noexcept
: data_(vec.data()), size_(vec.size()), sep_(", ") {}
constexpr span_printer(const std::vector<T>& vec, std::string_view sep) noexcept
: data_(vec.data()), size_(vec.size()), sep_(sep) {}
constexpr span_printer(const T* ptr, std::size_t size) noexcept
: data_(ptr), size_(size), sep_(", ") {}
constexpr span_printer(const T* ptr, std::size_t size, std::string_view sep) noexcept
: data_(ptr), size_(size), sep_(sep) {}

friend std::ostream& operator<<(std::ostream& out,
span_printer const& value) {
out << '[';
for (std::size_t i = 0; i < value.size_; i++) {
if (i > 0) {
out << value.sep_;
}
out << value.data_[i]; // NOLINT
}
out << ']';
return out;
}

private:
const T* data_{};
std::size_t size_{};
std::string_view sep_{};
};

} // namespace shirakami
3 changes: 3 additions & 0 deletions src/database/include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace shirakami::logging {

#define shirakami_log_entry \
VLOG(log_trace) << std::boolalpha << log_location_prefix << "-->" // NOLINT
#define shirakami_log_entry_lazy(data) \
if (VLOG_IS_ON(log_trace)) \
LOG(INFO) << std::boolalpha << log_location_prefix << "-->" << data
#define shirakami_log_exit \
VLOG(log_trace) << std::boolalpha << log_location_prefix << "<--" // NOLINT
#define shirakami_ex_log_entry \
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ else ()
"concurrency_control/short_tx/termination/*.cpp"
"concurrency_control/short_tx/tx_state/*.cpp"
"database_options/*.cpp"
"misc_ut/*.cpp"
"result/*.cpp"
"start/*.cpp"
"shirakami_issues/*.cpp"
Expand Down
79 changes: 79 additions & 0 deletions test/misc_ut/span_printer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

#include <sstream>

#include "glog/logging.h"
#include "gtest/gtest.h"

#include "shirakami/span_printer.h"

namespace shirakami::testing {

using namespace shirakami;

class span_printer_test : public ::testing::Test {
public:
static void call_once_f() {
google::InitGoogleLogging("shirakami-test-misc_ut-span_printer_test");
// FLAGS_stderrthreshold = 0;
}

void SetUp() override { std::call_once(init_google, call_once_f); }

void TearDown() override {}

private:
static inline std::once_flag init_google;
};

template<typename T>
std::string print(const std::vector<T>& v) {
std::stringstream ss{};
ss << span_printer(v);
return ss.str();
}

template<typename T>
std::string print(const std::vector<T>& v, std::string_view s) {
std::stringstream ss{};
ss << span_printer(v, s);
return ss.str();
}

template<typename T>
std::string print(const T* p, std::size_t n) {
std::stringstream ss{};
ss << span_printer(p, n);
return ss.str();
}

template<typename T>
std::string print(const T* p, std::size_t n, std::string_view s) {
std::stringstream ss{};
ss << span_printer(p, n, s);
return ss.str();
}

TEST_F(span_printer_test, charvec) {
EXPECT_EQ(print(std::vector<char>{}), "[]");
EXPECT_EQ(print(std::vector<char>{'a'}), "[a]");
EXPECT_EQ(print(std::vector<char>{'a', 'b'}), "[a, b]");
EXPECT_EQ(print(std::vector<char>{'c', 'h', 'a', 'r'}, ""), "[char]");
}

TEST_F(span_printer_test, uint64vec) {
EXPECT_EQ(print(std::vector<std::uint64_t>{}), "[]");
EXPECT_EQ(print(std::vector<std::uint64_t>{0}), "[0]");
EXPECT_EQ(print(std::vector<std::uint64_t>{100, 200}), "[100, 200]");
EXPECT_EQ(print(std::vector<std::uint64_t>{}, "x"), "[]");
EXPECT_EQ(print(std::vector<std::uint64_t>{100, 200, 30}, "x"), "[100x200x30]");
}

TEST_F(span_printer_test, uint64arr) {
EXPECT_EQ(print((std::uint64_t*)nullptr, 0), "[]");
std::uint64_t p1[] = {0U, 1U, 2U, 3U, 4U, 5U};
EXPECT_EQ(print(p1, 1), "[0]");
EXPECT_EQ(print(p1, 2), "[0, 1]");
EXPECT_EQ(print(&p1[3], 3, "x"), "[3x4x5]");
}

} // namespace shirakami::testing

0 comments on commit 4bd8469

Please sign in to comment.