From 7beed7149078783c98578c36334e20001c48e12b Mon Sep 17 00:00:00 2001 From: Lukas Holecek <hluk@email.cz> Date: Wed, 15 Apr 2020 19:32:24 +0200 Subject: [PATCH] Simplify set/get log label Signed-off-by: Lukas Holecek <hluk@email.cz> --- src/app/clipboardclient.cpp | 4 ++-- src/app/clipboardserver.cpp | 2 +- src/common/common.cpp | 2 +- src/common/log.cpp | 27 ++++++--------------------- src/common/log.h | 4 ++-- src/main.cpp | 2 +- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/app/clipboardclient.cpp b/src/app/clipboardclient.cpp index 769a2ae2d..d14fb9d27 100644 --- a/src/app/clipboardclient.cpp +++ b/src/app/clipboardclient.cpp @@ -62,12 +62,12 @@ QCoreApplication *createClientApplication(int &argc, char **argv, const QStringL QGuiApplication::setDesktopSettingsAware(false); const auto app = platformNativeInterface() ->createClipboardProviderApplication(argc, argv); - setCurrentThreadName(arguments[1]); + setLogLabel(arguments[1].toUtf8()); return app; } const auto app = platformNativeInterface()->createClientApplication(argc, argv); - setCurrentThreadName("Client"); + setLogLabel("Client"); return app; } diff --git a/src/app/clipboardserver.cpp b/src/app/clipboardserver.cpp index 86c5efcdd..77207bdec 100644 --- a/src/app/clipboardserver.cpp +++ b/src/app/clipboardserver.cpp @@ -96,7 +96,7 @@ ClipboardServer::ClipboardServer(QApplication *app, const QString &sessionName) , m_shortcutActions() , m_ignoreKeysTimer() { - setCurrentThreadName("Server"); + setLogLabel("Server"); const QString serverName = clipboardServerName(); m_server = new Server(serverName, this); diff --git a/src/common/common.cpp b/src/common/common.cpp index 30727bb6a..e7a598c38 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -696,7 +696,7 @@ QByteArray makeClipboardOwnerData() { static int id = 0; return qgetenv("COPYQ_SESSION_NAME") - + " " + currentThreadLabel() + + " " + logLabel() + "/" + QByteArray::number(++id); } diff --git a/src/common/log.cpp b/src/common/log.cpp index b3d8a6a46..88959f089 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -25,7 +25,6 @@ #include <QFile> #include <QString> #include <QSystemSemaphore> -#include <QThread> #include <QtGlobal> #include <QVariant> @@ -35,12 +34,6 @@ #include <cmath> #include <memory> -#ifdef Q_OS_MAC -# define THREAD_LOCAL __thread -#else -# define THREAD_LOCAL thread_local -#endif - /// System-wide mutex class SystemMutex final { public: @@ -82,11 +75,6 @@ Q_DECLARE_METATYPE(SystemMutexPtr) namespace { -// Avoid heap allocation for thread local variable. -constexpr int maxThreadLabelSize = 48; -THREAD_LOCAL char currentThreadLabel_[maxThreadLabelSize] = ""; - - const int logFileSize = 512 * 1024; const int logFileCount = 10; @@ -268,7 +256,7 @@ QByteArray createLogMessage(const QByteArray &text, const LogLevel level) { const auto timeStamp = QDateTime::currentDateTime().toString(" [yyyy-MM-dd hh:mm:ss.zzz] ").toUtf8(); - const auto label = "CopyQ " + logLevelLabel(level) + timeStamp + currentThreadLabel() + ": "; + const auto label = "CopyQ " + logLevelLabel(level) + timeStamp + logLabel() + ": "; return createLogMessage(label, text); } @@ -364,17 +352,14 @@ void log(const QString &text, const LogLevel level) } } -void setCurrentThreadName(const QString &name) +void setLogLabel(const QByteArray &name) { - Q_ASSERT(qApp != nullptr); - const auto id = QCoreApplication::applicationPid(); - const auto threadLabel = "<" + name.toUtf8() + "-" + QByteArray::number(id) + ">"; - const auto size = std::min( maxThreadLabelSize, threadLabel.size() ); - std::memcpy( currentThreadLabel_, threadLabel.constData(), static_cast<size_t>(size) ); + logLabel() = "<" + name + "-" + QByteArray::number(id) + ">"; } -QByteArray currentThreadLabel() +QByteArray &logLabel() { - return QByteArray(currentThreadLabel_); + static QByteArray label; + return label; } diff --git a/src/common/log.h b/src/common/log.h index 4108c98ac..c97c21f20 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -49,8 +49,8 @@ QByteArray logLevelLabel(LogLevel level); void log(const QString &text, LogLevel level = LogNote); -void setCurrentThreadName(const QString &name); +void setLogLabel(const QByteArray &name); -QByteArray currentThreadLabel(); +QByteArray &logLabel(); #endif // LOG_H diff --git a/src/main.cpp b/src/main.cpp index 5ab294847..c0bdd9a6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,7 @@ int evaluate( const QString &sessionName) { App app( platformNativeInterface()->createConsoleApplication(argc, argv), sessionName ); - setCurrentThreadName("Prompt"); + setLogLabel("Prompt"); QScriptEngine engine; Scriptable scriptable(&engine, nullptr); -- GitLab