Add voluntary spin lock for logging to reduce conflicts with stderr.

This commit is contained in:
levlam 2022-11-14 14:34:31 +03:00
parent 0461aff289
commit 59d4248efb
4 changed files with 19 additions and 1 deletions

View File

@ -40,7 +40,6 @@ Status AsyncFileLog::init(string path, int64 rotate_threshold, bool redirect_std
logging_thread_ = td::thread(
[queue = queue_.get(), fd = std::move(fd), path = path_, size, rotate_threshold, redirect_stderr]() mutable {
auto after_rotation = [&] {
ScopedDisableLog disable_log; // to ensure that nothing will be printed to the closed log
fd.close();
auto r_fd = FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append);
if (r_fd.is_error()) {
@ -61,6 +60,12 @@ Status AsyncFileLog::init(string path, int64 rotate_threshold, bool redirect_std
after_rotation();
}
while (!slice.empty()) {
if (redirect_stderr) {
auto &guard = get_log_guard();
while (guard.load() != 0) {
// spin
}
}
auto r_size = fd.write(slice);
if (r_size.is_error()) {
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');

View File

@ -82,6 +82,12 @@ void FileLog::do_append(int log_level, CSlice slice) {
do_after_rotation();
}
while (!slice.empty()) {
if (redirect_stderr_) {
auto &guard = get_log_guard();
while (guard.load() != 0) {
// spin
}
}
auto r_size = fd_.write(slice);
if (r_size.is_error()) {
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__ << '\n');

View File

@ -250,6 +250,11 @@ void process_fatal_error(CSlice message) {
std::abort();
}
std::atomic<uint32> &get_log_guard() {
static std::atomic<uint32> log_guard;
return log_guard;
}
namespace {
std::mutex sdl_mutex;
int sdl_cnt = 0;

View File

@ -221,6 +221,8 @@ class Logger {
int log_level_;
};
std::atomic<uint32> &get_log_guard();
class ScopedDisableLog {
public:
ScopedDisableLog();