2018-10-24 21:15:55 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-10-24 21:15:55 +02: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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/Logging.h"
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/mtproto/SessionConnection.h"
|
|
|
|
#include "td/mtproto/Transport.h"
|
|
|
|
|
2018-12-26 21:58:18 +01:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2019-01-14 19:46:04 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2018-11-15 22:25:08 +01:00
|
|
|
#include "td/telegram/files/FileGcWorker.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2018-10-28 18:30:47 +01:00
|
|
|
#include "td/telegram/net/ConnectionCreator.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/telegram/net/DcAuthManager.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2018-11-11 13:58:52 +01:00
|
|
|
#include "td/telegram/NotificationManager.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2018-11-15 18:25:54 +01:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
|
2021-06-16 04:23:22 +02:00
|
|
|
#include "td/db/binlog/Binlog.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/db/SqliteStatement.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
|
2019-02-04 15:02:21 +01:00
|
|
|
#include "td/net/GetHostByNameActor.h"
|
|
|
|
#include "td/net/TransparentProxy.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-10-11 13:45:22 +02:00
|
|
|
#include "td/utils/ExitGuard.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
#include "td/utils/FileLog.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2021-06-02 14:43:56 +02:00
|
|
|
#include "td/utils/NullLog.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2021-05-18 16:40:37 +02:00
|
|
|
#include "td/utils/TsLog.h"
|
2018-10-24 21:15:55 +02:00
|
|
|
|
2019-03-27 19:48:21 +01:00
|
|
|
#include <atomic>
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <map>
|
2018-10-24 21:15:55 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static std::mutex logging_mutex;
|
|
|
|
static FileLog file_log;
|
|
|
|
static TsLog ts_log(&file_log);
|
|
|
|
static NullLog null_log;
|
2020-10-11 13:45:22 +02:00
|
|
|
static ExitGuard exit_guard;
|
2018-10-24 21:15:55 +02:00
|
|
|
|
|
|
|
#define ADD_TAG(tag) \
|
|
|
|
{ #tag, &VERBOSITY_NAME(tag) }
|
2018-11-15 18:25:54 +01:00
|
|
|
static const std::map<Slice, int *> log_tags{
|
2020-10-05 21:08:24 +02:00
|
|
|
ADD_TAG(td_init), ADD_TAG(update_file), ADD_TAG(connections), ADD_TAG(binlog),
|
|
|
|
ADD_TAG(proxy), ADD_TAG(net_query), ADD_TAG(td_requests), ADD_TAG(dc),
|
|
|
|
ADD_TAG(file_loader), ADD_TAG(mtproto), ADD_TAG(raw_mtproto), ADD_TAG(fd),
|
|
|
|
ADD_TAG(actor), ADD_TAG(sqlite), ADD_TAG(notifications), ADD_TAG(get_difference),
|
2021-01-05 12:27:29 +01:00
|
|
|
ADD_TAG(file_gc), ADD_TAG(config_recoverer), ADD_TAG(dns_resolver), ADD_TAG(file_references),
|
2021-01-07 13:08:07 +01:00
|
|
|
ADD_TAG(messages), ADD_TAG(postponed_pts_update), ADD_TAG(add_pending_update)};
|
2018-10-24 21:15:55 +02:00
|
|
|
#undef ADD_TAG
|
|
|
|
|
|
|
|
Status Logging::set_current_stream(td_api::object_ptr<td_api::LogStream> stream) {
|
|
|
|
if (stream == nullptr) {
|
2020-05-18 21:54:18 +02:00
|
|
|
return Status::Error("Log stream must be non-empty");
|
2018-10-24 21:15:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
switch (stream->get_id()) {
|
|
|
|
case td_api::logStreamDefault::ID:
|
|
|
|
log_interface = default_log_interface;
|
|
|
|
return Status::OK();
|
|
|
|
case td_api::logStreamFile::ID: {
|
|
|
|
auto file_stream = td_api::move_object_as<td_api::logStreamFile>(stream);
|
|
|
|
auto max_log_file_size = file_stream->max_file_size_;
|
|
|
|
if (max_log_file_size <= 0) {
|
2020-03-02 10:52:13 +01:00
|
|
|
return Status::Error("Max log file size must be positive");
|
2018-10-24 21:15:55 +02:00
|
|
|
}
|
2020-09-27 13:37:35 +02:00
|
|
|
auto redirect_stderr = file_stream->redirect_stderr_;
|
2018-10-24 21:15:55 +02:00
|
|
|
|
2020-09-27 13:37:35 +02:00
|
|
|
TRY_STATUS(file_log.init(file_stream->path_, max_log_file_size, redirect_stderr));
|
2019-03-27 19:48:21 +01:00
|
|
|
std::atomic_thread_fence(std::memory_order_release); // better than nothing
|
2018-10-24 21:15:55 +02:00
|
|
|
log_interface = &ts_log;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
case td_api::logStreamEmpty::ID:
|
|
|
|
log_interface = &null_log;
|
|
|
|
return Status::OK();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<td_api::object_ptr<td_api::LogStream>> Logging::get_current_stream() {
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
if (log_interface == default_log_interface) {
|
|
|
|
return td_api::make_object<td_api::logStreamDefault>();
|
|
|
|
}
|
|
|
|
if (log_interface == &null_log) {
|
|
|
|
return td_api::make_object<td_api::logStreamEmpty>();
|
|
|
|
}
|
|
|
|
if (log_interface == &ts_log) {
|
2020-09-27 13:37:35 +02:00
|
|
|
return td_api::make_object<td_api::logStreamFile>(file_log.get_path().str(), file_log.get_rotate_threshold(),
|
|
|
|
file_log.get_redirect_stderr());
|
2018-10-24 21:15:55 +02:00
|
|
|
}
|
|
|
|
return Status::Error("Log stream is unrecognized");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Logging::set_verbosity_level(int new_verbosity_level) {
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
if (0 <= new_verbosity_level && new_verbosity_level <= VERBOSITY_NAME(NEVER)) {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + new_verbosity_level);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::Error("Wrong new verbosity level specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
int Logging::get_verbosity_level() {
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
return GET_VERBOSITY_LEVEL();
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> Logging::get_tags() {
|
|
|
|
return transform(log_tags, [](auto &tag) { return tag.first.str(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Logging::set_tag_verbosity_level(Slice tag, int new_verbosity_level) {
|
|
|
|
auto it = log_tags.find(tag);
|
|
|
|
if (it == log_tags.end()) {
|
|
|
|
return Status::Error("Log tag is not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
*it->second = clamp(new_verbosity_level, 1, VERBOSITY_NAME(NEVER));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int> Logging::get_tag_verbosity_level(Slice tag) {
|
|
|
|
auto it = log_tags.find(tag);
|
|
|
|
if (it == log_tags.end()) {
|
|
|
|
return Status::Error("Log tag is not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(logging_mutex);
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
2019-04-22 02:46:51 +02:00
|
|
|
void Logging::add_message(int log_verbosity_level, Slice message) {
|
|
|
|
int VERBOSITY_NAME(client) = clamp(log_verbosity_level, 0, VERBOSITY_NAME(NEVER));
|
2019-04-13 20:40:31 +02:00
|
|
|
VLOG(client) << message;
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:15:55 +02:00
|
|
|
} // namespace td
|