Move TsLog out of logging.h.

This commit is contained in:
levlam 2021-05-18 17:40:37 +03:00
parent df4c4b9471
commit 7edffa1bc7
8 changed files with 91 additions and 48 deletions

View File

@ -36,6 +36,7 @@
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/NativeFd.h"
#include "td/utils/TsLog.h"
#include <atomic> #include <atomic>
#include <map> #include <map>

View File

@ -45,6 +45,7 @@
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
#include "td/utils/Time.h" #include "td/utils/Time.h"
#include "td/utils/TsLog.h"
#include "td/utils/utf8.h" #include "td/utils/utf8.h"
#ifndef USE_READLINE #ifndef USE_READLINE

View File

@ -116,6 +116,7 @@ set(TDUTILS_SOURCE
td/utils/translit.cpp td/utils/translit.cpp
td/utils/TsCerr.cpp td/utils/TsCerr.cpp
td/utils/TsFileLog.cpp td/utils/TsFileLog.cpp
td/utils/TsLog.cpp
td/utils/unicode.cpp td/utils/unicode.cpp
td/utils/utf8.cpp td/utils/utf8.cpp
@ -261,6 +262,7 @@ set(TDUTILS_SOURCE
td/utils/TsCerr.h td/utils/TsCerr.h
td/utils/TsFileLog.h td/utils/TsFileLog.h
td/utils/TsList.h td/utils/TsList.h
td/utils/TsLog.h
td/utils/type_traits.h td/utils/type_traits.h
td/utils/UInt.h td/utils/UInt.h
td/utils/uint128.h td/utils/uint128.h

View File

@ -0,0 +1,21 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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/utils/TsLog.h"
#include "td/utils/ExitGuard.h"
namespace td {
void TsLog::enter_critical() {
while (lock_.test_and_set(std::memory_order_acquire) && !ExitGuard::is_exited()) {
// spin
}
}
static ExitGuard exit_guard;
} // namespace td

55
tdutils/td/utils/TsLog.h Normal file
View File

@ -0,0 +1,55 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
#include <atomic>
namespace td {
class TsLog : public LogInterface {
public:
explicit TsLog(LogInterface *log) : log_(log) {
}
void init(LogInterface *log) {
enter_critical();
log_ = log;
exit_critical();
}
void after_rotation() final {
enter_critical();
log_->after_rotation();
exit_critical();
}
vector<string> get_file_paths() final {
enter_critical();
auto result = log_->get_file_paths();
exit_critical();
return result;
}
private:
void do_append(int log_level, CSlice slice) final {
enter_critical();
log_->do_append(log_level, slice);
exit_critical();
}
void enter_critical();
void exit_critical() {
lock_.clear(std::memory_order_release);
}
LogInterface *log_ = nullptr;
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
};
} // namespace td

View File

@ -150,10 +150,6 @@ Logger::~Logger() {
} }
} }
void TsLog::exit_critical() {
lock_.clear(std::memory_order_release);
}
class DefaultLog : public LogInterface { class DefaultLog : public LogInterface {
void do_append(int log_level, CSlice slice) final { void do_append(int log_level, CSlice slice) final {
#if TD_ANDROID #if TD_ANDROID

View File

@ -146,16 +146,6 @@ inline int get_verbosity_level() {
return log_options.get_level(); return log_options.get_level();
} }
class ScopedDisableLog {
public:
ScopedDisableLog();
ScopedDisableLog(const ScopedDisableLog &) = delete;
ScopedDisableLog &operator=(const ScopedDisableLog &) = delete;
ScopedDisableLog(ScopedDisableLog &&) = delete;
ScopedDisableLog &operator=(ScopedDisableLog &&) = delete;
~ScopedDisableLog();
};
class LogInterface { class LogInterface {
public: public:
LogInterface() = default; LogInterface() = default;
@ -228,6 +218,16 @@ class Logger {
int log_level_; int log_level_;
}; };
class ScopedDisableLog {
public:
ScopedDisableLog();
ScopedDisableLog(const ScopedDisableLog &) = delete;
ScopedDisableLog &operator=(const ScopedDisableLog &) = delete;
ScopedDisableLog(ScopedDisableLog &&) = delete;
ScopedDisableLog &operator=(ScopedDisableLog &&) = delete;
~ScopedDisableLog();
};
namespace detail { namespace detail {
class Voidify { class Voidify {
public: public:
@ -237,38 +237,4 @@ class Voidify {
}; };
} // namespace detail } // namespace detail
class TsLog : public LogInterface {
public:
explicit TsLog(LogInterface *log) : log_(log) {
}
void init(LogInterface *log) {
enter_critical();
log_ = log;
exit_critical();
}
void after_rotation() final {
enter_critical();
log_->after_rotation();
exit_critical();
}
vector<string> get_file_paths() final {
enter_critical();
auto result = log_->get_file_paths();
exit_critical();
return result;
}
private:
void do_append(int log_level, CSlice slice) final {
enter_critical();
log_->do_append(log_level, slice);
exit_critical();
}
LogInterface *log_ = nullptr;
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
void enter_critical();
void exit_critical();
};
} // namespace td } // namespace td

View File

@ -17,6 +17,7 @@
#include "td/utils/SliceBuilder.h" #include "td/utils/SliceBuilder.h"
#include "td/utils/tests.h" #include "td/utils/tests.h"
#include "td/utils/TsFileLog.h" #include "td/utils/TsFileLog.h"
#include "td/utils/TsLog.h"
#include <functional> #include <functional>
#include <limits> #include <limits>