2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/Storer.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct EmptyStorerImpl {
|
|
|
|
EmptyStorerImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline auto EmptyStorer() {
|
|
|
|
static const EmptyStorerImpl impl;
|
|
|
|
return create_default_storer(impl);
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:12:20 +02:00
|
|
|
struct BinlogDebugInfo {
|
|
|
|
BinlogDebugInfo() = default;
|
|
|
|
BinlogDebugInfo(const char *file, int line) : file(file), line(line) {
|
|
|
|
}
|
|
|
|
const char *file{""};
|
|
|
|
int line{0};
|
|
|
|
};
|
2020-06-11 17:21:18 +02:00
|
|
|
|
2018-06-28 17:12:20 +02:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const BinlogDebugInfo &info) {
|
|
|
|
if (info.line == 0) {
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
return sb << "[" << info.file << ":" << info.line << "]";
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct BinlogEvent {
|
2020-06-11 17:21:18 +02:00
|
|
|
static constexpr size_t MAX_SIZE = 1 << 24;
|
|
|
|
static constexpr size_t HEADER_SIZE = 4 + 8 + 4 + 4 + 8;
|
|
|
|
static constexpr size_t TAIL_SIZE = 4;
|
|
|
|
static constexpr size_t MIN_SIZE = HEADER_SIZE + TAIL_SIZE;
|
|
|
|
|
2023-02-02 13:45:20 +01:00
|
|
|
int64 offset_ = -1;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-02-02 19:13:52 +01:00
|
|
|
uint32 size_ = 0;
|
|
|
|
uint64 id_ = 0;
|
|
|
|
int32 type_ = 0; // type can be merged with flags
|
|
|
|
int32 flags_ = 0;
|
|
|
|
uint64 extra_ = 0;
|
|
|
|
uint32 crc32_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-02-08 13:43:25 +01:00
|
|
|
string raw_event_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-28 17:12:20 +02:00
|
|
|
BinlogDebugInfo debug_info_;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
enum ServiceTypes { Header = -1, Empty = -2, AesCtrEncryption = -3, NoEncryption = -4 };
|
|
|
|
enum Flags { Rewrite = 1, Partial = 2 };
|
|
|
|
|
2023-02-02 01:08:26 +01:00
|
|
|
Slice get_data() const;
|
|
|
|
|
2023-02-02 19:13:52 +01:00
|
|
|
bool is_empty() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return raw_event_.empty();
|
|
|
|
}
|
2023-02-02 02:03:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
BinlogEvent clone() const {
|
|
|
|
BinlogEvent result;
|
2018-06-28 19:00:11 +02:00
|
|
|
result.debug_info_ = BinlogDebugInfo{__FILE__, __LINE__};
|
2023-02-08 13:43:25 +01:00
|
|
|
result.init(raw_event_);
|
2023-02-02 02:03:17 +01:00
|
|
|
result.validate().ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice data_as_buffer_slice() const {
|
2023-02-08 13:43:25 +01:00
|
|
|
return BufferSlice(get_data());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BinlogEvent() = default;
|
2023-02-02 02:03:17 +01:00
|
|
|
|
2018-07-01 16:19:59 +02:00
|
|
|
BinlogEvent(BufferSlice &&raw_event, BinlogDebugInfo info) {
|
2018-06-28 17:12:20 +02:00
|
|
|
debug_info_ = info;
|
2023-02-08 13:43:25 +01:00
|
|
|
init(raw_event.as_slice().str());
|
2018-06-28 17:12:20 +02:00
|
|
|
}
|
2018-06-28 19:00:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer);
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2023-02-02 15:40:47 +01:00
|
|
|
string public_to_string() const {
|
2018-06-17 21:48:18 +02:00
|
|
|
return PSTRING() << "LogEvent[" << tag("id", format::as_hex(id_)) << tag("type", type_) << tag("flags", flags_)
|
2023-02-02 01:08:26 +01:00
|
|
|
<< tag("data", get_data().size()) << "]" << debug_info_;
|
2018-06-17 21:48:18 +02:00
|
|
|
}
|
2018-07-09 12:48:22 +02:00
|
|
|
|
2023-02-08 13:43:25 +01:00
|
|
|
void init(string raw_event);
|
2023-02-02 02:03:17 +01:00
|
|
|
|
|
|
|
Status validate() const TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const BinlogEvent &event) {
|
|
|
|
return sb << "LogEvent[" << tag("id", format::as_hex(event.id_)) << tag("type", event.type_)
|
2023-02-02 01:08:26 +01:00
|
|
|
<< tag("flags", event.flags_) << tag("data", format::as_hex_dump<4>(event.get_data())) << "]"
|
2018-06-28 17:12:20 +02:00
|
|
|
<< event.debug_info_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|