diff --git a/td/telegram/Logging.cpp b/td/telegram/Logging.cpp index abf7ec71c..728dec8ac 100644 --- a/td/telegram/Logging.cpp +++ b/td/telegram/Logging.cpp @@ -36,6 +36,7 @@ #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/detail/NativeFd.h" +#include "td/utils/TsLog.h" #include #include diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 65e271762..fa0f548d6 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -45,6 +45,7 @@ #include "td/utils/Status.h" #include "td/utils/StringBuilder.h" #include "td/utils/Time.h" +#include "td/utils/TsLog.h" #include "td/utils/utf8.h" #ifndef USE_READLINE diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index 5f311cc8e..128c98fb7 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -116,6 +116,7 @@ set(TDUTILS_SOURCE td/utils/translit.cpp td/utils/TsCerr.cpp td/utils/TsFileLog.cpp + td/utils/TsLog.cpp td/utils/unicode.cpp td/utils/utf8.cpp @@ -261,6 +262,7 @@ set(TDUTILS_SOURCE td/utils/TsCerr.h td/utils/TsFileLog.h td/utils/TsList.h + td/utils/TsLog.h td/utils/type_traits.h td/utils/UInt.h td/utils/uint128.h diff --git a/tdutils/td/utils/TsLog.cpp b/tdutils/td/utils/TsLog.cpp new file mode 100644 index 000000000..fe63114de --- /dev/null +++ b/tdutils/td/utils/TsLog.cpp @@ -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 diff --git a/tdutils/td/utils/TsLog.h b/tdutils/td/utils/TsLog.h new file mode 100644 index 000000000..4dfb3e44d --- /dev/null +++ b/tdutils/td/utils/TsLog.h @@ -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 + +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 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 diff --git a/tdutils/td/utils/logging.cpp b/tdutils/td/utils/logging.cpp index 3133349cc..692b6cfb9 100644 --- a/tdutils/td/utils/logging.cpp +++ b/tdutils/td/utils/logging.cpp @@ -150,10 +150,6 @@ Logger::~Logger() { } } -void TsLog::exit_critical() { - lock_.clear(std::memory_order_release); -} - class DefaultLog : public LogInterface { void do_append(int log_level, CSlice slice) final { #if TD_ANDROID diff --git a/tdutils/td/utils/logging.h b/tdutils/td/utils/logging.h index e7ee57fde..1bfb8d693 100644 --- a/tdutils/td/utils/logging.h +++ b/tdutils/td/utils/logging.h @@ -146,16 +146,6 @@ inline int get_verbosity_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 { public: LogInterface() = default; @@ -228,6 +218,16 @@ class Logger { 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 { class Voidify { public: @@ -237,38 +237,4 @@ class Voidify { }; } // 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 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 diff --git a/tdutils/test/log.cpp b/tdutils/test/log.cpp index 63637ce36..de15185f3 100644 --- a/tdutils/test/log.cpp +++ b/tdutils/test/log.cpp @@ -17,6 +17,7 @@ #include "td/utils/SliceBuilder.h" #include "td/utils/tests.h" #include "td/utils/TsFileLog.h" +#include "td/utils/TsLog.h" #include #include