2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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/crypto.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/Storer.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
struct EmptyStorerImpl {
|
|
|
|
EmptyStorerImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline auto EmptyStorer() {
|
|
|
|
static const EmptyStorerImpl impl;
|
|
|
|
return create_default_storer(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t MAX_EVENT_SIZE = 1 << 24;
|
|
|
|
static constexpr size_t EVENT_HEADER_SIZE = 4 + 8 + 4 + 4 + 8;
|
|
|
|
static constexpr size_t EVENT_TAIL_SIZE = 4;
|
|
|
|
static constexpr size_t MIN_EVENT_SIZE = EVENT_HEADER_SIZE + EVENT_TAIL_SIZE;
|
|
|
|
|
|
|
|
extern int32 VERBOSITY_NAME(binlog);
|
|
|
|
|
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};
|
|
|
|
};
|
|
|
|
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
|
|
|
// TODO: smaller BinlogEvent
|
|
|
|
struct BinlogEvent {
|
|
|
|
int64 offset_;
|
|
|
|
|
|
|
|
uint32 size_;
|
|
|
|
uint64 id_;
|
|
|
|
int32 type_; // type can be merged with flags
|
|
|
|
int32 flags_;
|
|
|
|
uint64 extra_;
|
|
|
|
MutableSlice data_;
|
|
|
|
uint32 crc32_;
|
|
|
|
|
|
|
|
BufferSlice raw_event_;
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
raw_event_ = BufferSlice();
|
|
|
|
}
|
|
|
|
bool empty() const {
|
|
|
|
return raw_event_.empty();
|
|
|
|
}
|
|
|
|
BinlogEvent clone() const {
|
|
|
|
BinlogEvent result;
|
2018-06-28 19:00:11 +02:00
|
|
|
result.debug_info_ = BinlogDebugInfo{__FILE__, __LINE__};
|
2018-12-31 20:04:05 +01:00
|
|
|
result.init(raw_event_.clone()).ensure();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice data_as_buffer_slice() const {
|
|
|
|
return raw_event_.from_slice(data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinlogEvent() = default;
|
2018-06-28 19:00:11 +02:00
|
|
|
//explicit BinlogEvent(BufferSlice &&raw_event) {
|
|
|
|
//init(std::move(raw_event), false).ensure();
|
|
|
|
//}
|
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;
|
2018-06-28 19:00:11 +02:00
|
|
|
init(std::move(raw_event), false).ensure();
|
2018-06-28 17:12:20 +02:00
|
|
|
}
|
2018-06-28 19:00:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status init(BufferSlice &&raw_event, bool check_crc = true) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer);
|
2018-06-17 21:56:21 +02:00
|
|
|
std::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_)
|
2018-06-28 17:12:20 +02:00
|
|
|
<< tag("data", data_.size()) << "]" << debug_info_;
|
2018-06-17 21:48:18 +02:00
|
|
|
}
|
2018-07-09 12:48:22 +02:00
|
|
|
|
|
|
|
Status validate() const;
|
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_)
|
2018-06-28 17:12:20 +02:00
|
|
|
<< tag("flags", event.flags_) << tag("data", format::as_hex_dump<4>(event.data_)) << "]"
|
|
|
|
<< event.debug_info_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation
|
|
|
|
inline BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
|
|
|
|
auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE};
|
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin());
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_storer.store_int(narrow_cast<int32>(raw_event.size()));
|
|
|
|
tl_storer.store_long(id);
|
|
|
|
tl_storer.store_int(type);
|
|
|
|
tl_storer.store_int(flags);
|
|
|
|
tl_storer.store_long(0);
|
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_storer.store_storer(storer);
|
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE)));
|
|
|
|
|
|
|
|
return raw_event;
|
|
|
|
}
|
|
|
|
} // namespace td
|