Logger fixes

- Fixed a race condition when stopping the logger
- Flush log file only once per batch of log lines
pull/144/head
SChernykh 2022-04-07 16:49:01 +02:00
parent 094209f311
commit 9e287ab4f0
1 changed files with 7 additions and 4 deletions

View File

@ -32,7 +32,7 @@ bool CONSOLE_COLORS = true;
#ifndef P2POOL_LOG_DISABLE #ifndef P2POOL_LOG_DISABLE
static volatile bool stopped = false; static std::atomic<bool> stopped{ false };
static volatile bool worker_started = false; static volatile bool worker_started = false;
#ifdef _WIN32 #ifdef _WIN32
@ -102,11 +102,10 @@ public:
FORCEINLINE void stop() FORCEINLINE void stop()
{ {
if (stopped) { if (stopped.exchange(true)) {
return; return;
} }
stopped = true;
LOGINFO(0, "stopped"); LOGINFO(0, "stopped");
uv_thread_join(&m_worker); uv_thread_join(&m_worker);
uv_cond_destroy(&m_cond); uv_cond_destroy(&m_cond);
@ -208,7 +207,6 @@ private:
} }
m_logFile.write(p, size); m_logFile.write(p, size);
m_logFile.flush();
} }
} }
@ -218,6 +216,11 @@ private:
m_readPos += SLOT_SIZE; m_readPos += SLOT_SIZE;
} while (m_readPos < writePos); } while (m_readPos < writePos);
} }
// Flush the log file only after all pending log lines have been written
if (m_logFile.is_open()) {
m_logFile.flush();
}
} while (!stopped); } while (!stopped);
} }