Add missing Logging.* files.

GitOrigin-RevId: d1aec3ed0c54ee93b69fffc930b2ebe2665356cf
This commit is contained in:
levlam 2018-10-24 22:15:55 +03:00
parent f571a79f13
commit 54ec85ff22
2 changed files with 157 additions and 0 deletions

123
td/telegram/Logging.cpp Normal file
View File

@ -0,0 +1,123 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// 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"
#include "td/telegram/net/ConnectionCreator.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/Td.h"
#include "tddb/td/db/binlog/BinlogEvent.h"
#include "tdnet/td/net/TransparentProxy.h"
#include "td/utils/FileLog.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include <mutex>
#include <unordered_map>
namespace td {
static std::mutex logging_mutex;
static FileLog file_log;
static TsLog ts_log(&file_log);
static NullLog null_log;
#define ADD_TAG(tag) \
{ #tag, &VERBOSITY_NAME(tag) }
static const std::unordered_map<Slice, int *, SliceHash> log_tags{
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(files), ADD_TAG(mtproto),
ADD_TAG(raw_mtproto), ADD_TAG(fd), ADD_TAG(buffer), ADD_TAG(sqlite)};
#undef ADD_TAG
Status Logging::set_current_stream(td_api::object_ptr<td_api::LogStream> stream) {
if (stream == nullptr) {
return Status::Error("Log stream must not be empty");
}
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) {
return Status::Error("Max log file size should be positive");
}
TRY_STATUS(file_log.init(file_stream->path_, max_log_file_size));
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) {
return td_api::make_object<td_api::logStreamFile>(file_log.get_path().str(), file_log.get_rotate_threshold());
}
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;
}
} // namespace td

34
td/telegram/Logging.h Normal file
View File

@ -0,0 +1,34 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// 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/td_api.h"
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
namespace td {
class Logging {
public:
static Status set_current_stream(td_api::object_ptr<td_api::LogStream> stream);
static Result<td_api::object_ptr<td_api::LogStream>> get_current_stream();
static Status set_verbosity_level(int new_verbosity_level);
static int get_verbosity_level();
static vector<string> get_tags();
static Status set_tag_verbosity_level(Slice tag, int new_verbosity_level);
static Result<int> get_tag_verbosity_level(Slice tag);
};
} // namespace td