2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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
|
|
|
|
|
2018-03-16 16:29:16 +01:00
|
|
|
/*
|
2018-12-31 20:04:05 +01:00
|
|
|
* Simple logging.
|
|
|
|
*
|
|
|
|
* Predefined log levels: FATAL, ERROR, WARNING, INFO, DEBUG
|
|
|
|
*
|
|
|
|
* LOG(WARNING) << "Hello world!";
|
2018-02-11 15:07:16 +01:00
|
|
|
* LOG(INFO) << "Hello " << 1234 << " world!";
|
2018-12-31 20:04:05 +01:00
|
|
|
* LOG_IF(INFO, condition) << "Hello world if condition!";
|
|
|
|
*
|
|
|
|
* Custom log levels may be defined and used using VLOG:
|
|
|
|
* int VERBOSITY_NAME(custom) = VERBOSITY_NAME(WARNING);
|
|
|
|
* VLOG(custom) << "Hello custom world!"
|
|
|
|
*
|
2018-02-11 15:07:16 +01:00
|
|
|
* LOG(FATAL) << "Power is off";
|
2018-12-31 20:04:05 +01:00
|
|
|
* CHECK(condition) <===> LOG_IF(FATAL, !(condition))
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/port/thread_local.h"
|
2019-02-12 21:19:11 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#define PSTR_IMPL() ::td::Logger(::td::NullLog().ref(), ::td::LogOptions::plain(), 0)
|
2018-02-11 16:37:30 +01:00
|
|
|
#define PSLICE() ::td::detail::Slicify() & PSTR_IMPL()
|
|
|
|
#define PSTRING() ::td::detail::Stringify() & PSTR_IMPL()
|
|
|
|
#define PSLICE_SAFE() ::td::detail::SlicifySafe() & PSTR_IMPL()
|
|
|
|
#define PSTRING_SAFE() ::td::detail::StringifySafe() & PSTR_IMPL()
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#define VERBOSITY_NAME(x) verbosity_##x
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#define GET_VERBOSITY_LEVEL() (::td::get_verbosity_level())
|
|
|
|
#define SET_VERBOSITY_LEVEL(new_level) (::td::set_verbosity_level(new_level))
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#ifndef STRIP_LOG
|
|
|
|
#define STRIP_LOG VERBOSITY_NAME(DEBUG)
|
|
|
|
#endif
|
|
|
|
#define LOG_IS_STRIPPED(strip_level) \
|
2020-07-19 21:00:47 +02:00
|
|
|
(::std::integral_constant<int, VERBOSITY_NAME(strip_level)>() > ::std::integral_constant<int, STRIP_LOG>())
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#define LOGGER(interface, options, level, comment) ::td::Logger(interface, options, level, __FILE__, __LINE__, comment)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#define LOG_IMPL_FULL(interface, options, strip_level, runtime_level, condition, comment) \
|
2020-06-24 13:47:36 +02:00
|
|
|
LOG_IS_STRIPPED(strip_level) || runtime_level > options.get_level() || !(condition) \
|
2018-08-13 19:15:09 +02:00
|
|
|
? (void)0 \
|
|
|
|
: ::td::detail::Voidify() & LOGGER(interface, options, runtime_level, comment)
|
|
|
|
|
|
|
|
#define LOG_IMPL(strip_level, level, condition, comment) \
|
|
|
|
LOG_IMPL_FULL(*::td::log_interface, ::td::log_options, strip_level, VERBOSITY_NAME(level), condition, comment)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-11 16:37:30 +01:00
|
|
|
#define LOG(level) LOG_IMPL(level, level, true, ::td::Slice())
|
|
|
|
#define LOG_IF(level, condition) LOG_IMPL(level, level, condition, #condition)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-11 16:37:30 +01:00
|
|
|
#define VLOG(level) LOG_IMPL(DEBUG, level, true, TD_DEFINE_STR(level))
|
|
|
|
#define VLOG_IF(level, condition) LOG_IMPL(DEBUG, level, condition, TD_DEFINE_STR(level) " " #condition)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#define LOG_ROTATE() ::td::log_interface->rotate()
|
|
|
|
|
|
|
|
#define LOG_TAG ::td::Logger::tag_
|
|
|
|
#define LOG_TAG2 ::td::Logger::tag2_
|
|
|
|
|
|
|
|
#if TD_CLANG
|
|
|
|
bool no_return_func() __attribute__((analyzer_noreturn));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline bool no_return_func() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clang-format off
|
2019-02-12 16:45:14 +01:00
|
|
|
#define DUMMY_LOG_CHECK(condition) LOG_IF(NEVER, !(condition))
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#ifdef TD_DEBUG
|
|
|
|
#if TD_MSVC
|
2019-02-12 16:45:14 +01:00
|
|
|
#define LOG_CHECK(condition) \
|
2018-12-31 20:04:05 +01:00
|
|
|
__analysis_assume(!!(condition)); \
|
2018-02-11 16:37:30 +01:00
|
|
|
LOG_IMPL(FATAL, FATAL, !(condition), #condition)
|
2018-12-31 20:04:05 +01:00
|
|
|
#else
|
2019-02-12 16:45:14 +01:00
|
|
|
#define LOG_CHECK(condition) LOG_IMPL(FATAL, FATAL, !(condition) && no_return_func(), #condition)
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
#else
|
2019-02-12 16:45:14 +01:00
|
|
|
#define LOG_CHECK DUMMY_LOG_CHECK
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#if NDEBUG
|
2019-02-12 16:45:14 +01:00
|
|
|
#define LOG_DCHECK DUMMY_LOG_CHECK
|
2018-08-13 19:15:09 +02:00
|
|
|
#else
|
2019-02-12 16:45:14 +01:00
|
|
|
#define LOG_DCHECK LOG_CHECK
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
constexpr int VERBOSITY_NAME(PLAIN) = -1;
|
|
|
|
constexpr int VERBOSITY_NAME(FATAL) = 0;
|
|
|
|
constexpr int VERBOSITY_NAME(ERROR) = 1;
|
|
|
|
constexpr int VERBOSITY_NAME(WARNING) = 2;
|
|
|
|
constexpr int VERBOSITY_NAME(INFO) = 3;
|
|
|
|
constexpr int VERBOSITY_NAME(DEBUG) = 4;
|
|
|
|
constexpr int VERBOSITY_NAME(NEVER) = 1024;
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
// TODO Not part of utils. Should be in some separate file
|
|
|
|
extern int VERBOSITY_NAME(mtproto);
|
|
|
|
extern int VERBOSITY_NAME(raw_mtproto);
|
|
|
|
extern int VERBOSITY_NAME(dc);
|
|
|
|
extern int VERBOSITY_NAME(fd);
|
|
|
|
extern int VERBOSITY_NAME(net_query);
|
|
|
|
extern int VERBOSITY_NAME(td_requests);
|
|
|
|
extern int VERBOSITY_NAME(actor);
|
|
|
|
extern int VERBOSITY_NAME(files);
|
|
|
|
extern int VERBOSITY_NAME(sqlite);
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
struct LogOptions {
|
2020-06-24 13:47:36 +02:00
|
|
|
std::atomic<int> level{VERBOSITY_NAME(DEBUG) + 1};
|
2018-08-13 19:15:09 +02:00
|
|
|
bool fix_newlines{true};
|
|
|
|
bool add_info{true};
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
int get_level() const {
|
|
|
|
return level.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
int set_level(int new_level) {
|
|
|
|
return level.exchange(new_level);
|
|
|
|
}
|
|
|
|
|
2020-07-12 14:08:50 +02:00
|
|
|
static const LogOptions &plain() {
|
|
|
|
static LogOptions plain_options{0, false, false};
|
|
|
|
return plain_options;
|
2018-09-07 02:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr LogOptions() = default;
|
|
|
|
constexpr LogOptions(int level, bool fix_newlines, bool add_info)
|
|
|
|
: level(level), fix_newlines(fix_newlines), add_info(add_info) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
|
|
|
|
LogOptions(const LogOptions &other) : LogOptions(other.level.load(), other.fix_newlines, other.add_info) {
|
|
|
|
}
|
|
|
|
|
|
|
|
LogOptions &operator=(const LogOptions &other) {
|
|
|
|
level = other.level.load();
|
|
|
|
fix_newlines = other.fix_newlines;
|
|
|
|
add_info = other.add_info;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-07-12 14:08:50 +02:00
|
|
|
LogOptions(LogOptions &&) = delete;
|
|
|
|
LogOptions &operator=(LogOptions &&) = delete;
|
2020-06-26 01:24:13 +02:00
|
|
|
~LogOptions() = default;
|
2018-08-13 19:15:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern LogOptions log_options;
|
2020-06-24 13:47:36 +02:00
|
|
|
inline int set_verbosity_level(int level) {
|
|
|
|
return log_options.set_level(level);
|
|
|
|
}
|
|
|
|
inline int get_verbosity_level() {
|
|
|
|
return log_options.get_level();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScopedDisableLog {
|
|
|
|
public:
|
|
|
|
ScopedDisableLog();
|
2020-06-26 01:24:13 +02:00
|
|
|
ScopedDisableLog(const ScopedDisableLog &) = delete;
|
|
|
|
ScopedDisableLog &operator=(const ScopedDisableLog &) = delete;
|
|
|
|
ScopedDisableLog(ScopedDisableLog &&) = delete;
|
|
|
|
ScopedDisableLog &operator=(ScopedDisableLog &&) = delete;
|
2020-06-24 13:47:36 +02:00
|
|
|
~ScopedDisableLog();
|
|
|
|
};
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class LogInterface {
|
|
|
|
public:
|
|
|
|
LogInterface() = default;
|
|
|
|
LogInterface(const LogInterface &) = delete;
|
|
|
|
LogInterface &operator=(const LogInterface &) = delete;
|
|
|
|
LogInterface(LogInterface &&) = delete;
|
|
|
|
LogInterface &operator=(LogInterface &&) = delete;
|
|
|
|
virtual ~LogInterface() = default;
|
2018-08-13 19:15:09 +02:00
|
|
|
virtual void append(CSlice slice) {
|
|
|
|
append(slice, -1);
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
virtual void append(CSlice slice, int /*log_level*/) {
|
2018-08-13 19:15:09 +02:00
|
|
|
append(slice);
|
|
|
|
}
|
|
|
|
virtual void rotate() {
|
|
|
|
}
|
2019-01-17 23:17:20 +01:00
|
|
|
virtual vector<string> get_file_paths() {
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class NullLog : public LogInterface {
|
|
|
|
public:
|
2018-09-07 02:41:21 +02:00
|
|
|
void append(CSlice /*slice*/, int /*log_level*/) override {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void rotate() override {
|
|
|
|
}
|
|
|
|
NullLog &ref() {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern LogInterface *const default_log_interface;
|
|
|
|
extern LogInterface *log_interface;
|
2018-01-24 16:45:57 +01:00
|
|
|
|
|
|
|
using OnFatalErrorCallback = void (*)(CSlice message);
|
|
|
|
void set_log_fatal_error_callback(OnFatalErrorCallback callback);
|
|
|
|
|
|
|
|
[[noreturn]] void process_fatal_error(CSlice message);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-02 12:42:01 +02:00
|
|
|
#define TC_RED "\x1b[1;31m"
|
|
|
|
#define TC_BLUE "\x1b[1;34m"
|
|
|
|
#define TC_CYAN "\x1b[1;36m"
|
|
|
|
#define TC_GREEN "\x1b[1;32m"
|
|
|
|
#define TC_YELLOW "\x1b[1;33m"
|
|
|
|
#define TC_EMPTY "\x1b[0m"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class TsCerr {
|
|
|
|
public:
|
|
|
|
TsCerr();
|
|
|
|
TsCerr(const TsCerr &) = delete;
|
|
|
|
TsCerr &operator=(const TsCerr &) = delete;
|
|
|
|
TsCerr(TsCerr &&) = delete;
|
|
|
|
TsCerr &operator=(TsCerr &&) = delete;
|
|
|
|
~TsCerr();
|
|
|
|
TsCerr &operator<<(Slice slice);
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Lock = std::atomic_flag;
|
|
|
|
static Lock lock_;
|
|
|
|
|
|
|
|
void enterCritical();
|
|
|
|
void exitCritical();
|
|
|
|
};
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
public:
|
|
|
|
static const int BUFFER_SIZE = 128 * 1024;
|
2018-08-13 19:15:09 +02:00
|
|
|
Logger(LogInterface &log, const LogOptions &options, int log_level)
|
2018-12-31 20:04:05 +01:00
|
|
|
: buffer_(StackAllocator::alloc(BUFFER_SIZE))
|
|
|
|
, log_(log)
|
|
|
|
, sb_(buffer_.as_slice())
|
2018-08-13 19:15:09 +02:00
|
|
|
, options_(options)
|
|
|
|
, log_level_(log_level) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Logger(LogInterface &log, const LogOptions &options, int log_level, Slice file_name, int line_num, Slice comment);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Logger &operator<<(const T &other) {
|
|
|
|
sb_ << other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableCSlice as_cslice() {
|
|
|
|
return sb_.as_cslice();
|
|
|
|
}
|
|
|
|
bool is_error() const {
|
|
|
|
return sb_.is_error();
|
|
|
|
}
|
|
|
|
Logger(const Logger &) = delete;
|
|
|
|
Logger &operator=(const Logger &) = delete;
|
|
|
|
Logger(Logger &&) = delete;
|
|
|
|
Logger &operator=(Logger &&) = delete;
|
|
|
|
~Logger();
|
|
|
|
|
|
|
|
static TD_THREAD_LOCAL const char *tag_;
|
|
|
|
static TD_THREAD_LOCAL const char *tag2_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
decltype(StackAllocator::alloc(0)) buffer_;
|
|
|
|
LogInterface &log_;
|
|
|
|
StringBuilder sb_;
|
2018-08-13 19:15:09 +02:00
|
|
|
const LogOptions &options_;
|
|
|
|
int log_level_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class Voidify {
|
|
|
|
public:
|
|
|
|
template <class T>
|
|
|
|
void operator&(const T &) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Slicify {
|
|
|
|
public:
|
|
|
|
CSlice operator&(Logger &logger) {
|
|
|
|
return logger.as_cslice();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Stringify {
|
|
|
|
public:
|
|
|
|
string operator&(Logger &logger) {
|
|
|
|
return logger.as_cslice().str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
class TsLog : public LogInterface {
|
|
|
|
public:
|
|
|
|
explicit TsLog(LogInterface *log) : log_(log) {
|
|
|
|
}
|
|
|
|
void init(LogInterface *log) {
|
|
|
|
enter_critical();
|
|
|
|
log_ = log;
|
|
|
|
exit_critical();
|
|
|
|
}
|
|
|
|
void append(CSlice slice, int level) override {
|
|
|
|
enter_critical();
|
|
|
|
log_->append(slice, level);
|
|
|
|
exit_critical();
|
|
|
|
}
|
|
|
|
void rotate() override {
|
|
|
|
enter_critical();
|
|
|
|
log_->rotate();
|
|
|
|
exit_critical();
|
|
|
|
}
|
2019-01-17 23:17:20 +01:00
|
|
|
vector<string> get_file_paths() override {
|
|
|
|
enter_critical();
|
|
|
|
auto result = log_->get_file_paths();
|
|
|
|
exit_critical();
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
LogInterface *log_ = nullptr;
|
|
|
|
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
|
|
|
|
void enter_critical() {
|
|
|
|
while (lock_.test_and_set(std::memory_order_acquire)) {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void exit_critical() {
|
|
|
|
lock_.clear(std::memory_order_release);
|
|
|
|
}
|
|
|
|
};
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|