2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace td {
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <int buffer_size = 32 * (1 << 10)>
|
2021-07-04 04:58:54 +02:00
|
|
|
class MemoryLog final : public LogInterface {
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr size_t MAX_OUTPUT_SIZE = buffer_size / 16 < (8 << 10) ? buffer_size / 16 : (8 << 10);
|
2020-08-04 00:23:48 +02:00
|
|
|
|
|
|
|
static_assert((buffer_size & (buffer_size - 1)) == 0, "Buffer size must be power of 2");
|
|
|
|
static_assert(buffer_size >= (8 << 10), "Too small buffer size");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
MemoryLog() {
|
|
|
|
std::memset(buffer_, ' ', sizeof(buffer_));
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:18:19 +02:00
|
|
|
Slice get_buffer() const {
|
|
|
|
return Slice(buffer_, sizeof(buffer_));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_pos() const {
|
|
|
|
return pos_ & (buffer_size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void do_append(int log_level, CSlice new_slice) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
Slice slice = new_slice;
|
|
|
|
slice.truncate(MAX_OUTPUT_SIZE);
|
|
|
|
while (!slice.empty() && slice.back() == '\n') {
|
|
|
|
slice.remove_suffix(1);
|
|
|
|
}
|
|
|
|
size_t slice_size = slice.size();
|
|
|
|
CHECK(slice_size * 3 < buffer_size);
|
|
|
|
size_t pad_size = ((slice_size + 15) & ~15) - slice_size;
|
2020-06-22 01:40:05 +02:00
|
|
|
constexpr size_t MAGIC_SIZE = 16;
|
2021-10-20 00:27:02 +02:00
|
|
|
auto total_size = static_cast<uint32>(slice_size + pad_size + MAGIC_SIZE);
|
|
|
|
auto real_pos = pos_.fetch_add(total_size, std::memory_order_relaxed);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK((total_size & 15) == 0);
|
|
|
|
|
|
|
|
uint32 start_pos = real_pos & (buffer_size - 1);
|
|
|
|
uint32 end_pos = start_pos + total_size;
|
|
|
|
if (likely(end_pos <= buffer_size)) {
|
2020-06-22 01:40:05 +02:00
|
|
|
std::memcpy(&buffer_[start_pos + MAGIC_SIZE], slice.data(), slice_size);
|
2020-08-04 00:19:45 +02:00
|
|
|
std::memcpy(&buffer_[start_pos + MAGIC_SIZE + slice_size], " ", pad_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-06-22 01:40:05 +02:00
|
|
|
size_t first = buffer_size - start_pos - MAGIC_SIZE;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t second = slice_size - first;
|
2020-06-22 01:40:05 +02:00
|
|
|
std::memcpy(&buffer_[start_pos + MAGIC_SIZE], slice.data(), first);
|
2018-12-31 20:04:05 +01:00
|
|
|
std::memcpy(&buffer_[0], slice.data() + first, second);
|
2020-08-04 00:19:45 +02:00
|
|
|
std::memcpy(&buffer_[second], " ", pad_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK((start_pos & 15) == 0);
|
2020-06-22 01:40:05 +02:00
|
|
|
CHECK(start_pos <= buffer_size - MAGIC_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
buffer_[start_pos] = '\n';
|
2020-06-22 01:40:05 +02:00
|
|
|
size_t printed = std::snprintf(&buffer_[start_pos + 1], MAGIC_SIZE - 1, "LOG:%08x: ", real_pos);
|
|
|
|
CHECK(printed == MAGIC_SIZE - 2);
|
|
|
|
buffer_[start_pos + MAGIC_SIZE - 1] = ' ';
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char buffer_[buffer_size];
|
2019-01-24 04:18:14 +01:00
|
|
|
std::atomic<uint32> pos_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|