Skip to content

Commit 44d21ec

Browse files
committed
New API for managing TDLib logs.
GitOrigin-RevId: 60f520027bf16f53b927c72d0151884fda64ebbc
1 parent 4852e29 commit 44d21ec

14 files changed

+214
-102
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ set(TDLIB_SOURCE
377377
td/telegram/InputMessageText.cpp
378378
td/telegram/LanguagePackManager.cpp
379379
td/telegram/Location.cpp
380+
td/telegram/Logging.cpp
380381
td/telegram/MessageContent.cpp
381382
td/telegram/MessageEntity.cpp
382383
td/telegram/MessagesDb.cpp
@@ -500,6 +501,7 @@ set(TDLIB_SOURCE
500501
td/telegram/logevent/LogEvent.h
501502
td/telegram/logevent/LogEventHelper.h
502503
td/telegram/logevent/SecretChatEvent.h
504+
td/telegram/Logging.h
503505
td/telegram/MessageContent.h
504506
td/telegram/MessageEntity.h
505507
td/telegram/MessageId.h

SplitSource.php

-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ function split_file($file, $chunks, $undo) {
147147
'(CREATE_REQUEST|CREATE_NO_ARGS_REQUEST)[(](?<name>[A-Z][A-Za-z]*)|'.
148148
'(?<name>complete_pending_preauthentication_requests)|'.
149149
'(Up|Down)load[a-zA-Z]*C(?<name>allback)|(up|down)load_[a-z_]*_c(?<name>allback)_|'.
150-
'(?<name>V)(LOG.td_init|ERBOSITY_NAME)|'.
151150
'(?<name>LogEvent)[^sA]|'.
152151
'(?<name>parse)[(]|'.
153152
'(?<name>store)[(]/', $f, $matches, PREG_SET_ORDER)) {

td/generate/DotnetTlDocumentationGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ protected function escapeDocumentation($doc)
88
{
99
$doc = htmlspecialchars($doc, ENT_XML1);
1010
$doc = str_replace('*/', '*&#47;', $doc);
11-
$doc = preg_replace_callback('/_([A-Za-z])/', function ($matches) {return strtoupper($matches[1]);}, $doc);
11+
$doc = preg_replace_callback('/(?<!")_([A-Za-z])/', function ($matches) {return strtoupper($matches[1]);}, $doc);
1212
return $doc;
1313
}
1414

td/generate/JavadocTlDocumentationGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected function escapeDocumentation($doc)
1212
{
1313
$doc = htmlspecialchars($doc);
1414
$doc = str_replace('*/', '*&#47;', $doc);
15-
$doc = preg_replace_callback('/_([A-Za-z])/', function ($matches) {return strtoupper($matches[1]);}, $doc);
15+
$doc = preg_replace_callback('/(?<!")_([A-Za-z])/', function ($matches) {return strtoupper($matches[1]);}, $doc);
1616
return $doc;
1717
}
1818

td/generate/scheme/td_api.tl

+43-4
Original file line numberDiff line numberDiff line change
@@ -2317,6 +2317,25 @@ updateNewCustomQuery id:int64 data:string timeout:int32 = Update;
23172317
updates updates:vector<Update> = Updates;
23182318

23192319

2320+
//@class LogStream @description Describes a stream to which TDLib internal log is written
2321+
2322+
//@description The log is written to stderr or an OS specific log
2323+
logStreamDefault = LogStream;
2324+
2325+
//@description TDLib needs TdlibParameters for initialization @path Path to the file to where the internal TDLib log will be written @max_file_size Maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated
2326+
logStreamFile path:string max_file_size:int53 = LogStream;
2327+
2328+
//@description The log is written nowhere
2329+
logStreamEmpty = LogStream;
2330+
2331+
2332+
//@description Contains a TDLib internal log verbosity level @verbosity_level Log verbosity level
2333+
logVerbosityLevel verbosity_level:int32 = LogVerbosityLevel;
2334+
2335+
//@description Contains a list of available TDLib internal log tags @tags List of log tags
2336+
logTags tags:vector<string> = LogTags;
2337+
2338+
23202339
//@description A simple object containing a number; for testing only @value Number
23212340
testInt value:int32 = TestInt;
23222341
//@description A simple object containing a string; for testing only @value String
@@ -3378,6 +3397,30 @@ getProxyLink proxy_id:int32 = Text;
33783397
pingProxy proxy_id:int32 = Seconds;
33793398

33803399

3400+
//@description Sets new log stream for internal logging of TDLib. This is an offline method. Can be called before authorization. Can be called synchronously @log_stream New log stream
3401+
setLogStream log_stream:LogStream = Ok;
3402+
3403+
//@description Returns information about currently used log stream for internal logging of TDLib. This is an offline method. Can be called before authorization. Can be called synchronously
3404+
getLogStream = LogStream;
3405+
3406+
//@description Sets the verbosity level of the internal logging of TDLib. This is an offline method. Can be called before authorization. Can be called synchronously
3407+
//@new_verbosity_level New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1 corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value greater than 5 and up to 1023 can be used to enable even more logging
3408+
setLogVerbosityLevel new_verbosity_level:int32 = Ok;
3409+
3410+
//@description Returns current verbosity level of the internal logging of TDLib. This is an offline method. Can be called before authorization. Can be called synchronously
3411+
getLogVerbosityLevel = LogVerbosityLevel;
3412+
3413+
//@description Returns list of available TDLib internal log tags. Currently returns ["td_init", "update_file", "connections", "binlog", "proxy", "net_query", "td_requests", "dc", "files", "mtproto", "raw_mtproto", "fd", "actor", "buffer", "sqlite"]
3414+
getLogTags = LogTags;
3415+
3416+
//@description Sets the verbosity level for a specified TDLib internal log tag. This is an offline method. Can be called before authorization. Can be called synchronously
3417+
//@tag Logging tag to change verbosity level @new_verbosity_level New verbosity level; 1-1024
3418+
setLogTagVerbosityLevel tag:string new_verbosity_level:int32 = Ok;
3419+
3420+
//@description Returns current verbosity level for a specified TDLib internal log tag. This is an offline method. Can be called before authorization. Can be called synchronously @tag Logging tag to change verbosity level
3421+
getLogTagVerbosityLevel tag:string = LogVerbosityLevel;
3422+
3423+
33813424
//@description Does nothing; for testing only
33823425
testCallEmpty = Ok;
33833426
//@description Returns the received string; for testing only @x String to return
@@ -3402,7 +3445,3 @@ testGetDifference = Ok;
34023445
testUseUpdate = Update;
34033446
//@description Does nothing and ensures that the Error object is used; for testing only
34043447
testUseError = Error;
3405-
//@description Changes verbosity level for a specified log tag; for testing only. This is an offline method. Can be called before authorization. Can be called synchronously
3406-
//@tag Logging tag to change verbosity level (one of "td_init", "update_file", "connections", "binlog", "proxy", "net_query", "td_requests", "dc", "files", "mtproto", "raw_mtproto", "fd", "actor", "buffer", "sqlite")
3407-
//@new_verbosity_level New verbosity level; 1-1024
3408-
testSetLogTagVerbosityLevel tag:string new_verbosity_level:int32 = Ok;

td/generate/scheme/td_api.tlo

1 KB
Binary file not shown.

td/telegram/Log.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
//
77
#include "td/telegram/Log.h"
88

9+
#include "td/telegram/Logging.h"
10+
911
#include "td/utils/common.h"
10-
#include "td/utils/FileLog.h"
1112
#include "td/utils/logging.h"
1213
#include "td/utils/Slice.h"
1314

@@ -16,8 +17,7 @@
1617
namespace td {
1718

1819
static std::mutex log_mutex;
19-
static FileLog file_log;
20-
static TsLog ts_log(&file_log);
20+
static string log_file_path;
2121
static int64 max_log_file_size = 10 << 20;
2222
static Log::FatalErrorCallbackPtr fatal_error_callback;
2323

@@ -29,12 +29,12 @@ static void fatal_error_callback_wrapper(CSlice message) {
2929
bool Log::set_file_path(string file_path) {
3030
std::lock_guard<std::mutex> lock(log_mutex);
3131
if (file_path.empty()) {
32-
log_interface = default_log_interface;
33-
return true;
32+
log_file_path.clear();
33+
return Logging::set_current_stream(td_api::make_object<td_api::logStreamDefault>()).is_ok();
3434
}
3535

36-
if (file_log.init(file_path, max_log_file_size)) {
37-
log_interface = &ts_log;
36+
if (Logging::set_current_stream(td_api::make_object<td_api::logStreamFile>(file_path, max_log_file_size)).is_ok()) {
37+
log_file_path = std::move(file_path);
3838
return true;
3939
}
4040

@@ -43,15 +43,13 @@ bool Log::set_file_path(string file_path) {
4343

4444
void Log::set_max_file_size(int64 max_file_size) {
4545
std::lock_guard<std::mutex> lock(log_mutex);
46-
max_log_file_size = max(max_file_size, static_cast<int64>(0));
47-
file_log.set_rotate_threshold(max_log_file_size);
46+
max_log_file_size = max(max_file_size, static_cast<int64>(1));
47+
Logging::set_current_stream(td_api::make_object<td_api::logStreamFile>(log_file_path, max_log_file_size)).ignore();
4848
}
4949

5050
void Log::set_verbosity_level(int new_verbosity_level) {
5151
std::lock_guard<std::mutex> lock(log_mutex);
52-
if (0 <= new_verbosity_level && new_verbosity_level <= 1024) {
53-
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + new_verbosity_level);
54-
}
52+
Logging::set_verbosity_level(new_verbosity_level).ignore();
5553
}
5654

5755
void Log::set_fatal_error_callback(FatalErrorCallbackPtr callback) {

td/telegram/Td.cpp

+86-63
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "td/telegram/HashtagHints.h"
4040
#include "td/telegram/InlineQueriesManager.h"
4141
#include "td/telegram/LanguagePackManager.h"
42+
#include "td/telegram/Logging.h"
4243
#include "td/telegram/MessageEntity.h"
4344
#include "td/telegram/MessageId.h"
4445
#include "td/telegram/MessagesManager.h"
@@ -76,8 +77,6 @@
7677

7778
#include "td/mtproto/utils.h" // for create_storer, fetch_result, etc, TODO
7879

79-
#include "tdnet/td/net/TransparentProxy.h"
80-
8180
#include "td/utils/buffer.h"
8281
#include "td/utils/filesystem.h"
8382
#include "td/utils/format.h"
@@ -3116,7 +3115,13 @@ bool Td::is_synchronous_request(int32 id) {
31163115
case td_api::getFileExtension::ID:
31173116
case td_api::cleanFileName::ID:
31183117
case td_api::getLanguagePackString::ID:
3119-
case td_api::testSetLogTagVerbosityLevel::ID:
3118+
case td_api::setLogStream::ID:
3119+
case td_api::getLogStream::ID:
3120+
case td_api::setLogVerbosityLevel::ID:
3121+
case td_api::getLogVerbosityLevel::ID:
3122+
case td_api::getLogTags::ID:
3123+
case td_api::setLogTagVerbosityLevel::ID:
3124+
case td_api::getLogTagVerbosityLevel::ID:
31203125
return true;
31213126
default:
31223127
return false;
@@ -6715,6 +6720,34 @@ void Td::on_request(uint64 id, const td_api::getLanguagePackString &request) {
67156720
UNREACHABLE();
67166721
}
67176722

6723+
void Td::on_request(uint64 id, const td_api::setLogStream &request) {
6724+
UNREACHABLE();
6725+
}
6726+
6727+
void Td::on_request(uint64 id, const td_api::getLogStream &request) {
6728+
UNREACHABLE();
6729+
}
6730+
6731+
void Td::on_request(uint64 id, const td_api::setLogVerbosityLevel &request) {
6732+
UNREACHABLE();
6733+
}
6734+
6735+
void Td::on_request(uint64 id, const td_api::getLogVerbosityLevel &request) {
6736+
UNREACHABLE();
6737+
}
6738+
6739+
void Td::on_request(uint64 id, const td_api::getLogTags &request) {
6740+
UNREACHABLE();
6741+
}
6742+
6743+
void Td::on_request(uint64 id, const td_api::setLogTagVerbosityLevel &request) {
6744+
UNREACHABLE();
6745+
}
6746+
6747+
void Td::on_request(uint64 id, const td_api::getLogTagVerbosityLevel &request) {
6748+
UNREACHABLE();
6749+
}
6750+
67186751
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getTextEntities &request) {
67196752
if (!check_utf8(request.text_)) {
67206753
return make_error(400, "Text must be encoded in UTF-8");
@@ -6769,77 +6802,67 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getLangua
67696802
request.language_pack_database_path_, request.localization_target_, request.language_pack_id_, request.key_);
67706803
}
67716804

6772-
// test
6773-
void Td::on_request(uint64 id, td_api::testNetwork &request) {
6774-
create_handler<TestQuery>(id)->send();
6805+
td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::setLogStream &request) {
6806+
auto result = Logging::set_current_stream(std::move(request.log_stream_));
6807+
if (result.is_ok()) {
6808+
return td_api::make_object<td_api::ok>();
6809+
} else {
6810+
return make_error(400, result.message());
6811+
}
67756812
}
67766813

6777-
void Td::on_request(uint64 id, td_api::testGetDifference &request) {
6778-
updates_manager_->get_difference("testGetDifference");
6779-
send_closure(actor_id(this), &Td::send_result, id, make_tl_object<td_api::ok>());
6814+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getLogStream &request) {
6815+
auto result = Logging::get_current_stream();
6816+
if (result.is_ok()) {
6817+
return result.move_as_ok();
6818+
} else {
6819+
return make_error(400, result.error().message());
6820+
}
67806821
}
67816822

6782-
int *Td::get_log_verbosity_level(Slice name) {
6783-
if (name == "td_init") {
6784-
return &VERBOSITY_NAME(td_init);
6785-
}
6786-
if (name == "update_file") {
6787-
return &VERBOSITY_NAME(update_file);
6788-
}
6789-
if (name == "connections") {
6790-
return &VERBOSITY_NAME(connections);
6791-
}
6792-
if (name == "binlog") {
6793-
return &VERBOSITY_NAME(binlog);
6794-
}
6795-
if (name == "proxy") {
6796-
return &VERBOSITY_NAME(proxy);
6797-
}
6798-
if (name == "net_query") {
6799-
return &VERBOSITY_NAME(net_query);
6800-
}
6801-
if (name == "td_requests") {
6802-
return &VERBOSITY_NAME(td_requests);
6803-
}
6804-
if (name == "dc") {
6805-
return &VERBOSITY_NAME(dc);
6806-
}
6807-
if (name == "files") {
6808-
return &VERBOSITY_NAME(files);
6809-
}
6810-
if (name == "mtproto") {
6811-
return &VERBOSITY_NAME(mtproto);
6812-
}
6813-
if (name == "raw_mtproto") {
6814-
return &VERBOSITY_NAME(raw_mtproto);
6815-
}
6816-
if (name == "fd") {
6817-
return &VERBOSITY_NAME(fd);
6818-
}
6819-
if (name == "actor") {
6820-
return &VERBOSITY_NAME(actor);
6821-
}
6822-
if (name == "buffer") {
6823-
return &VERBOSITY_NAME(buffer);
6824-
}
6825-
if (name == "sqlite") {
6826-
return &VERBOSITY_NAME(sqlite);
6823+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::setLogVerbosityLevel &request) {
6824+
auto result = Logging::set_verbosity_level(static_cast<int>(request.new_verbosity_level_));
6825+
if (result.is_ok()) {
6826+
return td_api::make_object<td_api::ok>();
6827+
} else {
6828+
return make_error(400, result.message());
68276829
}
6828-
return nullptr;
68296830
}
68306831

6831-
void Td::on_request(uint64 id, td_api::testSetLogTagVerbosityLevel &request) {
6832-
UNREACHABLE();
6832+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getLogVerbosityLevel &request) {
6833+
return td_api::make_object<td_api::logVerbosityLevel>(Logging::get_verbosity_level());
68336834
}
68346835

6835-
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::testSetLogTagVerbosityLevel &request) {
6836-
int *level = get_log_verbosity_level(request.tag_);
6837-
if (level == nullptr) {
6838-
return td_api::make_object<td_api::error>(400, "Log tag is not found");
6836+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getLogTags &request) {
6837+
return td_api::make_object<td_api::logTags>(Logging::get_tags());
6838+
}
6839+
6840+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::setLogTagVerbosityLevel &request) {
6841+
auto result = Logging::set_tag_verbosity_level(request.tag_, static_cast<int>(request.new_verbosity_level_));
6842+
if (result.is_ok()) {
6843+
return td_api::make_object<td_api::ok>();
6844+
} else {
6845+
return make_error(400, result.message());
6846+
}
6847+
}
6848+
6849+
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getLogTagVerbosityLevel &request) {
6850+
auto result = Logging::get_tag_verbosity_level(request.tag_);
6851+
if (result.is_ok()) {
6852+
return td_api::make_object<td_api::logVerbosityLevel>(result.ok());
6853+
} else {
6854+
return make_error(400, result.error().message());
68396855
}
6840-
*level = clamp(static_cast<int>(request.new_verbosity_level_), 1, VERBOSITY_NAME(NEVER));
6856+
}
68416857

6842-
return td_api::make_object<td_api::ok>();
6858+
// test
6859+
void Td::on_request(uint64 id, td_api::testNetwork &request) {
6860+
create_handler<TestQuery>(id)->send();
6861+
}
6862+
6863+
void Td::on_request(uint64 id, td_api::testGetDifference &request) {
6864+
updates_manager_->get_difference("testGetDifference");
6865+
send_closure(actor_id(this), &Td::send_result, id, make_tl_object<td_api::ok>());
68436866
}
68446867

68456868
void Td::on_request(uint64 id, td_api::testUseUpdate &request) {

0 commit comments

Comments
 (0)