Fix possible deadlock in debug logs.

This commit is contained in:
John Preston 2021-05-12 14:48:44 +04:00
parent 2d8f43bd8c
commit e7ca35a276
3 changed files with 22 additions and 4 deletions

View File

@ -420,7 +420,6 @@ void Launcher::prepareSettings() {
void Launcher::initQtMessageLogging() {
static QtMessageHandler OriginalMessageHandler = nullptr;
static bool WritingQtMessage = false;
OriginalMessageHandler = qInstallMessageHandler([](
QtMsgType type,
const QMessageLogContext &context,
@ -429,10 +428,9 @@ void Launcher::initQtMessageLogging() {
OriginalMessageHandler(type, context, msg);
}
if (Logs::DebugEnabled() || !Logs::started()) {
if (!WritingQtMessage) {
WritingQtMessage = true;
if (!Logs::WritingEntry()) {
// Sometimes Qt logs something inside our own logging.
LOG((msg));
WritingQtMessage = false;
}
}
});

View File

@ -14,6 +14,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace {
std::atomic<int> ThreadCounter/* = 0*/;
thread_local bool WritingEntryFlag/* = false*/;
class WritingEntryScope final {
public:
WritingEntryScope() {
WritingEntryFlag = true;
}
~WritingEntryScope() {
WritingEntryFlag = false;
}
};
} // namespace
@ -73,6 +84,8 @@ public:
void closeMain() {
QMutexLocker lock(_logsMutex(LogDataMain));
WritingEntryScope scope;
const auto file = files[LogDataMain].get();
if (file && file->isOpen()) {
file->close();
@ -98,6 +111,8 @@ public:
void write(LogDataType type, const QString &msg) {
QMutexLocker lock(_logsMutex(type));
WritingEntryScope scope;
if (type != LogDataMain) {
reopenDebug();
}
@ -323,6 +338,10 @@ bool DebugEnabled() {
#endif
}
bool WritingEntry() {
return WritingEntryFlag;
}
void start(not_null<Core::Launcher*> launcher) {
Assert(LogsData == nullptr);

View File

@ -19,6 +19,7 @@ namespace Logs {
void SetDebugEnabled(bool enabled);
bool DebugEnabled();
[[nodiscard]] bool WritingEntry();
void start(not_null<Core::Launcher*> launcher);
bool started();