From 3a7faffa3a8975c854a5f55399c31ca4f51f61a9 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 14 Nov 2019 20:03:44 +0300 Subject: [PATCH] Use thread_local counter in logs for thread index. --- Telegram/SourceFiles/logs.cpp | 14 +++++++++----- Telegram/SourceFiles/mtproto/connection.cpp | 2 +- Telegram/SourceFiles/mtproto/connection.h | 18 ------------------ 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/logs.cpp b/Telegram/SourceFiles/logs.cpp index 5a3955955..348a26213 100644 --- a/Telegram/SourceFiles/logs.cpp +++ b/Telegram/SourceFiles/logs.cpp @@ -8,10 +8,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "logs.h" #include "platform/platform_specific.h" -#include "mtproto/connection.h" #include "core/crash_reports.h" #include "core/launcher.h" +namespace { + +std::atomic ThreadCounter/* = 0*/; + +} // namespace + enum LogDataType { LogDataMain, LogDataDebug, @@ -45,11 +50,10 @@ QString _logsFilePath(LogDataType type, const QString &postfix = QString()) { int32 LogsStartIndexChosen = -1; QString _logsEntryStart() { - static int32 index = 0; - QDateTime tm(QDateTime::currentDateTime()); + static thread_local auto threadId = ThreadCounter++; + static auto index = 0; - auto thread = qobject_cast(QThread::currentThread()); - auto threadId = thread ? thread->getThreadIndex() : 0; + const auto tm = QDateTime::currentDateTime(); return QString("[%1 %2-%3]").arg(tm.toString("hh:mm:ss.zzz")).arg(QString("%1").arg(threadId, 2, 10, QChar('0'))).arg(++index, 7, 10, QChar('0')); } diff --git a/Telegram/SourceFiles/mtproto/connection.cpp b/Telegram/SourceFiles/mtproto/connection.cpp index 2cb5735da..d7e0a95bf 100644 --- a/Telegram/SourceFiles/mtproto/connection.cpp +++ b/Telegram/SourceFiles/mtproto/connection.cpp @@ -103,7 +103,7 @@ Connection::Connection(not_null instance) void Connection::start(SessionData *sessionData, ShiftedDcId shiftedDcId) { Expects(_thread == nullptr && _private == nullptr); - _thread = std::make_unique(); + _thread = std::make_unique(); auto newData = std::make_unique( _instance, _thread.get(), diff --git a/Telegram/SourceFiles/mtproto/connection.h b/Telegram/SourceFiles/mtproto/connection.h index 337525e6b..912ac8fec 100644 --- a/Telegram/SourceFiles/mtproto/connection.h +++ b/Telegram/SourceFiles/mtproto/connection.h @@ -33,24 +33,6 @@ class SessionData; class RSAPublicKey; struct ConnectionOptions; -class Thread : public QThread { - // The Q_OBJECT meta info is used for qobject_cast! - Q_OBJECT - -public: - Thread() { - static int ThreadCounter = 0; - _threadIndex = ++ThreadCounter; - } - int getThreadIndex() const { - return _threadIndex; - } - -private: - int _threadIndex = 0; - -}; - class Connection { public: enum ConnectionType {