Move TsLog out of logging.h.
This commit is contained in:
parent
df4c4b9471
commit
7edffa1bc7
@ -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 <atomic>
|
||||
#include <map>
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
21
tdutils/td/utils/TsLog.cpp
Normal file
21
tdutils/td/utils/TsLog.cpp
Normal 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
55
tdutils/td/utils/TsLog.h
Normal 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
|
@ -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
|
||||
|
@ -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<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
|
||||
|
@ -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 <functional>
|
||||
#include <limits>
|
||||
|
Loading…
Reference in New Issue
Block a user