Disable logging after program exit.

GitOrigin-RevId: 8ca24e1999fe95f171e18477baa2e56912a2e895
This commit is contained in:
levlam 2020-10-11 14:45:22 +03:00
parent a67225d358
commit 10a7edfbea
6 changed files with 34 additions and 12 deletions

View File

@ -29,6 +29,7 @@
#include "td/actor/actor.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/FileLog.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@ -44,6 +45,7 @@ static std::mutex logging_mutex;
static FileLog file_log;
static TsLog ts_log(&file_log);
static NullLog null_log;
static ExitGuard exit_guard;
#define ADD_TAG(tag) \
{ #tag, &VERBOSITY_NAME(tag) }

View File

@ -20,6 +20,7 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/FileLog.h"
#include "td/utils/format.h"
#include "td/utils/JsonBuilder.h"
@ -4442,6 +4443,7 @@ static void on_fatal_error(const char *error) {
}
void main(int argc, char **argv) {
ExitGuard exit_guard;
ignore_signal(SignalType::HangUp).ensure();
ignore_signal(SignalType::Pipe).ensure();
set_signal_handler(SignalType::Error, fail_signal).ensure();

View File

@ -6,8 +6,15 @@
//
#include "td/utils/ExitGuard.h"
#include "td/utils/logging.h"
namespace td {
std::atomic<bool> ExitGuard::is_exited_{false};
ExitGuard::~ExitGuard() {
is_exited_.store(true, std::memory_order_relaxed);
set_verbosity_level(VERBOSITY_NAME(FATAL));
}
} // namespace td

View File

@ -17,9 +17,7 @@ class ExitGuard {
ExitGuard &operator=(ExitGuard &&) = delete;
ExitGuard(const ExitGuard &) = delete;
ExitGuard &operator=(const ExitGuard &) = delete;
~ExitGuard() {
is_exited_.store(true, std::memory_order_relaxed);
}
~ExitGuard();
static bool is_exited() {
return is_exited_.load(std::memory_order_relaxed);

View File

@ -6,6 +6,7 @@
//
#include "td/utils/logging.h"
#include "td/utils/ExitGuard.h"
#include "td/utils/port/Clocks.h"
#include "td/utils/port/StdStreams.h"
#include "td/utils/port/thread_local.h"
@ -43,6 +44,9 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic
if (!options_.add_info) {
return;
}
if (ExitGuard::is_exited()) {
return;
}
// log level
sb_ << '[';
@ -104,6 +108,9 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic
}
Logger::~Logger() {
if (ExitGuard::is_exited()) {
return;
}
if (options_.fix_newlines) {
sb_ << '\n';
auto slice = as_cslice();
@ -152,7 +159,7 @@ TsCerr &TsCerr::operator<<(Slice slice) {
}
void TsCerr::enterCritical() {
while (lock_.test_and_set(std::memory_order_acquire)) {
while (lock_.test_and_set(std::memory_order_acquire) && !ExitGuard::is_exited()) {
// spin
}
}
@ -162,6 +169,16 @@ void TsCerr::exitCritical() {
}
TsCerr::Lock TsCerr::lock_ = ATOMIC_FLAG_INIT;
void TsLog::enter_critical() {
while (lock_.test_and_set(std::memory_order_acquire) && !ExitGuard::is_exited()) {
// spin
}
}
void TsLog::exit_critical() {
lock_.clear(std::memory_order_release);
}
class DefaultLog : public LogInterface {
public:
void append(CSlice slice, int log_level) override {
@ -290,4 +307,6 @@ ScopedDisableLog::~ScopedDisableLog() {
}
}
static ExitGuard exit_guard;
} // namespace td

View File

@ -321,14 +321,8 @@ class TsLog : public LogInterface {
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);
}
void enter_critical();
void exit_critical();
};
} // namespace td