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/telegram/Global.h"
|
|
|
|
#include "td/telegram/Version.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2019-05-03 15:53:34 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.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"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
namespace td {
|
2020-09-22 01:15:09 +02:00
|
|
|
namespace log_event {
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class ParentT>
|
|
|
|
class WithVersion : public ParentT {
|
|
|
|
public:
|
|
|
|
using ParentT::ParentT;
|
|
|
|
void set_version(int32 version) {
|
|
|
|
version_ = version;
|
|
|
|
}
|
|
|
|
int32 version() const {
|
|
|
|
return version_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
int32 version_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ParentT, class ContextT>
|
|
|
|
class WithContext : public ParentT {
|
|
|
|
public:
|
|
|
|
using ParentT::ParentT;
|
|
|
|
void set_context(ContextT context) {
|
|
|
|
context_ = context;
|
|
|
|
}
|
|
|
|
ContextT context() const {
|
|
|
|
return context_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
ContextT context_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class LogEvent {
|
|
|
|
public:
|
|
|
|
LogEvent() = default;
|
|
|
|
LogEvent(const LogEvent &) = delete;
|
|
|
|
LogEvent &operator=(const LogEvent &) = delete;
|
|
|
|
virtual ~LogEvent() = default;
|
|
|
|
enum HandlerType : uint32 {
|
|
|
|
SecretChats = 1,
|
|
|
|
Users = 2,
|
|
|
|
Chats = 3,
|
|
|
|
Channels = 4,
|
|
|
|
SecretChatInfos = 5,
|
|
|
|
WebPages = 0x10,
|
2019-02-21 15:40:37 +01:00
|
|
|
SetPollAnswer = 0x20,
|
2019-02-22 16:09:55 +01:00
|
|
|
StopPoll = 0x21,
|
2018-12-31 20:04:05 +01:00
|
|
|
SendMessage = 0x100,
|
|
|
|
DeleteMessage = 0x101,
|
2021-10-27 14:38:09 +02:00
|
|
|
DeleteMessagesOnServer = 0x102,
|
2018-12-31 20:04:05 +01:00
|
|
|
ReadHistoryOnServer = 0x103,
|
|
|
|
ForwardMessages = 0x104,
|
|
|
|
ReadMessageContentsOnServer = 0x105,
|
|
|
|
SendBotStartMessage = 0x106,
|
|
|
|
SendScreenshotTakenNotificationMessage = 0x107,
|
|
|
|
SendInlineQueryResultMessage = 0x108,
|
2021-10-27 14:38:09 +02:00
|
|
|
DeleteDialogHistoryOnServer = 0x109,
|
2018-12-31 20:04:05 +01:00
|
|
|
ReadAllDialogMentionsOnServer = 0x10a,
|
2021-11-19 14:00:21 +01:00
|
|
|
DeleteAllChannelMessagesFromSenderOnServer = 0x10b,
|
2018-12-31 20:04:05 +01:00
|
|
|
ToggleDialogIsPinnedOnServer = 0x10c,
|
|
|
|
ReorderPinnedDialogsOnServer = 0x10d,
|
|
|
|
SaveDialogDraftMessageOnServer = 0x10e,
|
2018-04-28 21:31:42 +02:00
|
|
|
UpdateDialogNotificationSettingsOnServer = 0x10f,
|
|
|
|
UpdateScopeNotificationSettingsOnServer = 0x110,
|
2018-04-28 21:50:12 +02:00
|
|
|
ResetAllNotificationSettingsOnServer = 0x111,
|
2021-09-24 09:42:39 +02:00
|
|
|
ToggleDialogReportSpamStateOnServer = 0x112,
|
2021-10-27 14:27:09 +02:00
|
|
|
RegetDialog = 0x113,
|
2018-06-20 03:02:02 +02:00
|
|
|
ReadHistoryInSecretChat = 0x114,
|
2018-06-27 23:08:44 +02:00
|
|
|
ToggleDialogIsMarkedAsUnreadOnServer = 0x115,
|
2019-08-27 16:23:01 +02:00
|
|
|
SetDialogFolderIdOnServer = 0x116,
|
2021-10-27 14:38:09 +02:00
|
|
|
DeleteScheduledMessagesOnServer = 0x117,
|
2020-09-20 02:00:01 +02:00
|
|
|
ToggleDialogIsBlockedOnServer = 0x118,
|
2020-09-22 00:13:06 +02:00
|
|
|
ReadMessageThreadHistoryOnServer = 0x119,
|
2020-10-18 00:26:36 +02:00
|
|
|
BlockMessageSenderFromRepliesOnServer = 0x120,
|
2020-10-23 00:56:06 +02:00
|
|
|
UnpinAllDialogMessagesOnServer = 0x121,
|
2021-10-27 14:38:09 +02:00
|
|
|
DeleteAllCallMessagesOnServer = 0x122,
|
2021-10-25 19:39:22 +02:00
|
|
|
DeleteDialogMessagesByDateOnServer = 0x123,
|
2022-01-30 10:37:24 +01:00
|
|
|
ReadAllDialogReactionsOnServer = 0x124,
|
2022-10-29 23:35:37 +02:00
|
|
|
DeleteTopicHistoryOnServer = 0x125,
|
2023-01-20 10:38:25 +01:00
|
|
|
ToggleDialogIsTranslatableOnServer = 0x126,
|
2018-12-31 20:04:05 +01:00
|
|
|
GetChannelDifference = 0x140,
|
2019-03-31 03:30:25 +02:00
|
|
|
AddMessagePushNotification = 0x200,
|
2019-04-07 22:57:54 +02:00
|
|
|
EditMessagePushNotification = 0x201,
|
2022-07-18 18:21:47 +02:00
|
|
|
SaveAppLog = 0x300,
|
2023-06-01 16:41:46 +02:00
|
|
|
DeleteStoryOnServer = 0x400,
|
2023-06-15 14:25:32 +02:00
|
|
|
ReadStoriesOnServer = 0x401,
|
2023-06-28 16:52:33 +02:00
|
|
|
LoadDialogExpiringStories = 0x402,
|
2023-07-03 21:03:29 +02:00
|
|
|
SendStory = 0x403,
|
2023-07-04 17:42:43 +02:00
|
|
|
EditStory = 0x404,
|
2023-09-22 13:22:24 +02:00
|
|
|
ChangeAuthorizationSettingsOnServer = 0x500,
|
2023-09-22 13:40:53 +02:00
|
|
|
ResetAuthorizationOnServer = 0x501,
|
2023-09-22 13:50:44 +02:00
|
|
|
ResetAuthorizationsOnServer = 0x502,
|
2023-09-22 14:03:02 +02:00
|
|
|
SetDefaultHistoryTtlOnServer = 0x503,
|
2023-09-22 14:06:53 +02:00
|
|
|
SetAccountTtlOnServer = 0x504,
|
2023-09-22 14:49:51 +02:00
|
|
|
SetAuthorizationTtlOnServer = 0x505,
|
2018-12-31 20:04:05 +01:00
|
|
|
ConfigPmcMagic = 0x1f18,
|
|
|
|
BinlogPmcMagic = 0x4327
|
|
|
|
};
|
|
|
|
|
|
|
|
using Id = uint64;
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
Id log_event_id() const {
|
|
|
|
return log_event_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
void set_log_event_id(Id log_event_id) {
|
|
|
|
log_event_id_ = log_event_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual StringBuilder &print(StringBuilder &sb) const {
|
2020-09-22 01:15:09 +02:00
|
|
|
return sb << "[Logevent " << tag("id", log_event_id()) << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-22 01:15:09 +02:00
|
|
|
Id log_event_id_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2021-07-05 04:25:23 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const LogEvent &log_event) {
|
|
|
|
return log_event.print(sb);
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogEventParser final : public WithVersion<WithContext<TlParser, Global *>> {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit LogEventParser(Slice data) : WithVersion<WithContext<TlParser, Global *>>(data) {
|
|
|
|
set_version(fetch_int());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(version() < static_cast<int32>(Version::Next)) << "Wrong version " << version();
|
2018-12-31 20:04:05 +01:00
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogEventStorerCalcLength final : public WithContext<TlStorerCalcLength, Global *> {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
LogEventStorerCalcLength() : WithContext<TlStorerCalcLength, Global *>() {
|
|
|
|
store_int(static_cast<int32>(Version::Next) - 1);
|
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogEventStorerUnsafe final : public WithContext<TlStorerUnsafe, Global *> {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-06-25 01:58:43 +02:00
|
|
|
explicit LogEventStorerUnsafe(unsigned char *buf) : WithContext<TlStorerUnsafe, Global *>(buf) {
|
2018-12-31 20:04:05 +01:00
|
|
|
store_int(static_cast<int32>(Version::Next) - 1);
|
|
|
|
set_context(G());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogEventStorerImpl final : public Storer {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit LogEventStorerImpl(const T &event) : event_(event) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t size() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventStorerCalcLength storer;
|
|
|
|
td::store(event_, storer);
|
|
|
|
return storer.get_length();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
size_t store(uint8 *ptr) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventStorerUnsafe storer(ptr);
|
|
|
|
td::store(event_, storer);
|
|
|
|
#ifdef TD_DEBUG
|
|
|
|
T check_result;
|
|
|
|
log_event_parse(check_result, Slice(ptr, storer.get_buf())).ensure();
|
|
|
|
#endif
|
2018-06-25 01:58:43 +02:00
|
|
|
return static_cast<size_t>(storer.get_buf() - ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T &event_;
|
|
|
|
};
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
} // namespace log_event
|
2020-09-22 00:39:45 +02:00
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
using LogEvent = log_event::LogEvent;
|
|
|
|
using LogEventParser = log_event::LogEventParser;
|
|
|
|
using LogEventStorerCalcLength = log_event::LogEventStorerCalcLength;
|
|
|
|
using LogEventStorerUnsafe = log_event::LogEventStorerUnsafe;
|
2020-09-22 00:39:45 +02:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status log_event_parse(T &data, Slice slice) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status log_event_parse(T &data, Slice slice) {
|
|
|
|
LogEventParser parser(slice);
|
|
|
|
parse(data, parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
return parser.get_status();
|
|
|
|
}
|
|
|
|
|
2023-05-03 19:55:28 +02:00
|
|
|
inline int32 log_event_get_version(Slice slice) {
|
|
|
|
LogEventParser parser(slice);
|
|
|
|
return parser.version();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
2022-06-28 14:18:16 +02:00
|
|
|
BufferSlice log_event_store_impl(const T &data, const char *file, int line) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventStorerCalcLength storer_calc_length;
|
|
|
|
store(data, storer_calc_length);
|
|
|
|
|
|
|
|
BufferSlice value_buffer{storer_calc_length.get_length()};
|
2023-02-02 15:40:47 +01:00
|
|
|
auto ptr = value_buffer.as_mutable_slice().ubegin();
|
2019-05-03 15:53:34 +02:00
|
|
|
LOG_CHECK(is_aligned_pointer<4>(ptr)) << ptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-03 15:53:34 +02:00
|
|
|
LogEventStorerUnsafe storer_unsafe(ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(data, storer_unsafe);
|
|
|
|
|
|
|
|
#ifdef TD_DEBUG
|
|
|
|
T check_result;
|
2022-06-28 14:18:16 +02:00
|
|
|
auto status = log_event_parse(check_result, value_buffer.as_slice());
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(FATAL) << status << ' ' << file << ' ' << line;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
return value_buffer;
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:18:16 +02:00
|
|
|
#define log_event_store(data) log_event_store_impl((data), __FILE__, __LINE__)
|
|
|
|
|
2020-09-22 00:39:45 +02:00
|
|
|
template <class T>
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event::LogEventStorerImpl<T> get_log_event_storer(const T &event) {
|
|
|
|
return log_event::LogEventStorerImpl<T>(event);
|
2020-09-22 00:39:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|