From c360bb9da41d1135f22b53ff5a621888773ee072 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 20 Apr 2021 16:40:54 +0400 Subject: [PATCH] Use LOG/DEBUG_LOG from lib_base. --- .../SourceFiles/core/base_integration.cpp | 17 ++++---- Telegram/SourceFiles/core/base_integration.h | 4 +- Telegram/SourceFiles/core/launcher.cpp | 34 +++++++++------ Telegram/SourceFiles/core/launcher.h | 1 + Telegram/SourceFiles/core/ui_integration.cpp | 10 ----- Telegram/SourceFiles/core/ui_integration.h | 5 +-- .../SourceFiles/lang/lang_file_parser.cpp | 15 +++---- Telegram/SourceFiles/logs.cpp | 42 +++++++------------ Telegram/SourceFiles/logs.h | 18 +------- Telegram/lib_base | 2 +- Telegram/lib_spellcheck | 2 +- Telegram/lib_ui | 2 +- 12 files changed, 65 insertions(+), 87 deletions(-) diff --git a/Telegram/SourceFiles/core/base_integration.cpp b/Telegram/SourceFiles/core/base_integration.cpp index 5228b988a..4b3f7e65c 100644 --- a/Telegram/SourceFiles/core/base_integration.cpp +++ b/Telegram/SourceFiles/core/base_integration.cpp @@ -21,17 +21,20 @@ void BaseIntegration::enterFromEventLoop(FnMut &&method) { std::move(method)); } +bool BaseIntegration::logSkipDebug() { + return !Logs::DebugEnabled() && Logs::started(); +} + +void BaseIntegration::logMessageDebug(const QString &message) { + Logs::writeDebug(message); +} + void BaseIntegration::logMessage(const QString &message) { -#ifdef DEBUG_LOG - DEBUG_LOG((message)); -#endif // DEBUG_LOG + Logs::writeMain(message); } void BaseIntegration::logAssertionViolation(const QString &info) { -#ifdef LOG - LOG(("Assertion Failed! ") + info); -#endif // LOG - + Logs::writeMain("Assertion Failed! " + info); CrashReports::SetAnnotation("Assertion", info); } diff --git a/Telegram/SourceFiles/core/base_integration.h b/Telegram/SourceFiles/core/base_integration.h index 11652830e..b1f50be95 100644 --- a/Telegram/SourceFiles/core/base_integration.h +++ b/Telegram/SourceFiles/core/base_integration.h @@ -11,11 +11,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Core { -class BaseIntegration : public base::Integration { +class BaseIntegration final : public base::Integration { public: BaseIntegration(int argc, char *argv[]); void enterFromEventLoop(FnMut &&method) override; + bool logSkipDebug() override; + void logMessageDebug(const QString &message) override; void logMessage(const QString &message) override; void logAssertionViolation(const QString &info) override; diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index f1d6931de..85079a923 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -279,19 +279,7 @@ void Launcher::init() { _arguments = readArguments(_argc, _argv); prepareSettings(); - - static QtMessageHandler originalMessageHandler = nullptr; - originalMessageHandler = qInstallMessageHandler([]( - QtMsgType type, - const QMessageLogContext &context, - const QString &msg) { - if (originalMessageHandler) { - originalMessageHandler(type, context, msg); - } - if (Logs::DebugEnabled() || !Logs::started()) { - LOG((msg)); - } - }); + initQtMessageLogging(); QApplication::setApplicationName(qsl("TelegramDesktop")); @@ -430,6 +418,26 @@ void Launcher::prepareSettings() { processArguments(); } +void Launcher::initQtMessageLogging() { + static QtMessageHandler OriginalMessageHandler = nullptr; + static bool WritingQtMessage = false; + OriginalMessageHandler = qInstallMessageHandler([]( + QtMsgType type, + const QMessageLogContext &context, + const QString &msg) { + if (OriginalMessageHandler) { + OriginalMessageHandler(type, context, msg); + } + if (Logs::DebugEnabled() || !Logs::started()) { + if (!WritingQtMessage) { + WritingQtMessage = true; + LOG((msg)); + WritingQtMessage = false; + } + } + }); +} + uint64 Launcher::installationTag() const { return InstallationTag; } diff --git a/Telegram/SourceFiles/core/launcher.h b/Telegram/SourceFiles/core/launcher.h index 8847fec81..0d6959b0c 100644 --- a/Telegram/SourceFiles/core/launcher.h +++ b/Telegram/SourceFiles/core/launcher.h @@ -41,6 +41,7 @@ protected: private: void prepareSettings(); + void initQtMessageLogging(); void processArguments(); QStringList readArguments(int argc, char *argv[]) const; diff --git a/Telegram/SourceFiles/core/ui_integration.cpp b/Telegram/SourceFiles/core/ui_integration.cpp index 5ee5e03e5..28696eb4d 100644 --- a/Telegram/SourceFiles/core/ui_integration.cpp +++ b/Telegram/SourceFiles/core/ui_integration.cpp @@ -98,10 +98,6 @@ void UiIntegration::unregisterLeaveSubscription(not_null widget) { Core::App().unregisterLeaveSubscription(widget); } -void UiIntegration::writeLogEntry(const QString &entry) { - Logs::writeMain(entry); -} - QString UiIntegration::emojiCacheFolder() { return cWorkingDir() + "tdata/emoji"; } @@ -116,12 +112,6 @@ void UiIntegration::activationFromTopPanel() { Platform::IgnoreApplicationActivationRightNow(); } -void UiIntegration::startFontsBegin() { -} - -void UiIntegration::startFontsEnd() { -} - QString UiIntegration::timeFormat() { return cTimeFormat(); } diff --git a/Telegram/SourceFiles/core/ui_integration.h b/Telegram/SourceFiles/core/ui_integration.h index 2aac9a0aa..2bcf54fda 100644 --- a/Telegram/SourceFiles/core/ui_integration.h +++ b/Telegram/SourceFiles/core/ui_integration.h @@ -30,20 +30,17 @@ struct MarkedTextContext { HashtagMentionType type = HashtagMentionType::Telegram; }; -class UiIntegration : public Ui::Integration { +class UiIntegration final : public Ui::Integration { public: void postponeCall(FnMut &&callable) override; void registerLeaveSubscription(not_null widget) override; void unregisterLeaveSubscription(not_null widget) override; - void writeLogEntry(const QString &entry) override; QString emojiCacheFolder() override; void textActionsUpdated() override; void activationFromTopPanel() override; - void startFontsBegin() override; - void startFontsEnd() override; QString timeFormat() override; std::shared_ptr createLinkHandler( diff --git a/Telegram/SourceFiles/lang/lang_file_parser.cpp b/Telegram/SourceFiles/lang/lang_file_parser.cpp index 8aa860cf8..740586926 100644 --- a/Telegram/SourceFiles/lang/lang_file_parser.cpp +++ b/Telegram/SourceFiles/lang/lang_file_parser.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_file_parser.h" #include "base/parse_helper.h" -#include "ui/integration.h" +#include "base/debug_log.h" #include #include @@ -155,18 +155,19 @@ bool FileParser::readKeyValue(const char *&from, const char *end) { QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &relativePath) { QFile file(QFileInfo::exists(relativePath) ? relativePath : absolutePath); if (!file.open(QIODevice::ReadOnly)) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not open file at '%1' ('%2')"_q.arg(relativePath, absolutePath)); + LOG(("Lang Error: Could not open file at '%1' ('%2')") + .arg(relativePath, absolutePath)); return QByteArray(); } if (file.size() > kLangFileLimit) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: File is too big: %1"_q.arg(file.size())); + LOG(("Lang Error: File is too big: %1").arg(file.size())); return QByteArray(); } constexpr auto kCodecMagicSize = 3; auto codecMagic = file.read(kCodecMagicSize); if (codecMagic.size() < kCodecMagicSize) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: Found bad file at '%1' ('%2')"_q.arg(relativePath, absolutePath)); + LOG(("Lang Error: Found bad file at '%1' ('%2')").arg(relativePath, absolutePath)); return QByteArray(); } file.seek(0); @@ -177,11 +178,11 @@ QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &rela stream.setCodec("UTF-16"); auto string = stream.readAll(); if (stream.status() != QTextStream::Ok) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not read UTF-16 data from '%1' ('%2')"_q.arg(relativePath, absolutePath)); + LOG(("Lang Error: Could not read UTF-16 data from '%1' ('%2')").arg(relativePath, absolutePath)); return QByteArray(); } if (string.isEmpty()) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-16 content in '%1' ('%2')"_q.arg(relativePath, absolutePath)); + LOG(("Lang Error: Empty UTF-16 content in '%1' ('%2')").arg(relativePath, absolutePath)); return QByteArray(); } return string.toUtf8(); @@ -197,7 +198,7 @@ QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &rela data = data.mid(3); // skip UTF-8 BOM } if (data.isEmpty()) { - Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-8 content in '%1' ('%2')"_q.arg(relativePath, absolutePath)); + LOG(("Lang Error: Empty UTF-8 content in '%1' ('%2')").arg(relativePath, absolutePath)); return QByteArray(); } return data; diff --git a/Telegram/SourceFiles/logs.cpp b/Telegram/SourceFiles/logs.cpp index 9941ab5ff..7ad8bbbda 100644 --- a/Telegram/SourceFiles/logs.cpp +++ b/Telegram/SourceFiles/logs.cpp @@ -323,11 +323,6 @@ bool DebugEnabled() { #endif } -QString ProfilePrefix() { - const auto now = crl::profile(); - return '[' + QString::number(now / 1000., 'f', 3) + "] "; -} - void start(not_null launcher) { Assert(LogsData == nullptr); @@ -527,29 +522,21 @@ void writeMain(const QString &v) { struct tm tm; mylocaltime(&tm, &t); - QString msg(QString("[%1.%2.%3 %4:%5:%6] %7\n").arg(tm.tm_year + 1900).arg(tm.tm_mon + 1, 2, 10, QChar('0')).arg(tm.tm_mday, 2, 10, QChar('0')).arg(tm.tm_hour, 2, 10, QChar('0')).arg(tm.tm_min, 2, 10, QChar('0')).arg(tm.tm_sec, 2, 10, QChar('0')).arg(v)); + const auto msg = QString("[%1.%2.%3 %4:%5:%6] %7\n" + ).arg(tm.tm_year + 1900 + ).arg(tm.tm_mon + 1, 2, 10, QChar('0') + ).arg(tm.tm_mday, 2, 10, QChar('0') + ).arg(tm.tm_hour, 2, 10, QChar('0') + ).arg(tm.tm_min, 2, 10, QChar('0') + ).arg(tm.tm_sec, 2, 10, QChar('0') + ).arg(v); _logsWrite(LogDataMain, msg); - QString debugmsg(QString("%1 %2\n").arg(_logsEntryStart(), v)); - _logsWrite(LogDataDebug, debugmsg); + writeDebug(v); } -void writeDebug(const char *file, int32 line, const QString &v) { - const char *last = strstr(file, "/"), *found = 0; - while (last) { - found = last; - last = strstr(last + 1, "/"); - } - last = strstr(file, "\\"); - while (last) { - found = last; - last = strstr(last + 1, "\\"); - } - if (found) { - file = found + 1; - } - - QString msg(QString("%1 %2 (%3 : %4)\n").arg(_logsEntryStart(), v, file, QString::number(line))); +void writeDebug(const QString &v) { + const auto msg = QString("%1 %2\n").arg(_logsEntryStart(), v); _logsWrite(LogDataDebug, msg); #ifdef Q_OS_WIN @@ -562,12 +549,15 @@ void writeDebug(const char *file, int32 line, const QString &v) { } void writeTcp(const QString &v) { - QString msg(QString("%1 %2\n").arg(_logsEntryStart(), v)); + const auto msg = QString("%1 %2\n").arg(_logsEntryStart(), v); _logsWrite(LogDataTcp, msg); } void writeMtp(int32 dc, const QString &v) { - QString msg(QString("%1 (dc:%2) %3\n").arg(_logsEntryStart()).arg(dc).arg(v)); + const auto msg = QString("%1 (dc:%2) %3\n").arg( + _logsEntryStart(), + QString::number(dc), + v); _logsWrite(LogDataMtp, msg); } diff --git a/Telegram/SourceFiles/logs.h b/Telegram/SourceFiles/logs.h index 14d2d216e..e42bf8b22 100644 --- a/Telegram/SourceFiles/logs.h +++ b/Telegram/SourceFiles/logs.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/basic_types.h" #include "base/assertion.h" +#include "base/debug_log.h" namespace Core { class Launcher; @@ -19,8 +20,6 @@ namespace Logs { void SetDebugEnabled(bool enabled); bool DebugEnabled(); -QString ProfilePrefix(); - void start(not_null launcher); bool started(); void finish(); @@ -31,8 +30,7 @@ void multipleInstances(); void closeMain(); void writeMain(const QString &v); - -void writeDebug(const char *file, int32 line, const QString &v); +void writeDebug(const QString &v); void writeTcp(const QString &v); void writeMtp(int32 dc, const QString &v); @@ -70,18 +68,6 @@ inline MemoryBuffer mb(const void *ptr, uint32 size) { } // namespace Logs -#define LOG(msg) (Logs::writeMain(QString msg)) -//usage LOG(("log: %1 %2").arg(1).arg(2)) - -#define PROFILE_LOG(msg) (Logs::writeMain(Logs::ProfilePrefix() + QString msg)) - -#define DEBUG_LOG(msg) {\ - if (Logs::DebugEnabled() || !Logs::started()) {\ - Logs::writeDebug(SOURCE_FILE_BASENAME, __LINE__, QString msg);\ - }\ -} -//usage DEBUG_LOG(("log: %1 %2").arg(1).arg(2)) - #define TCP_LOG(msg) {\ if (Logs::DebugEnabled() || !Logs::started()) {\ Logs::writeTcp(QString msg);\ diff --git a/Telegram/lib_base b/Telegram/lib_base index 443b7b6b6..053ab218d 160000 --- a/Telegram/lib_base +++ b/Telegram/lib_base @@ -1 +1 @@ -Subproject commit 443b7b6b650e48a7852515e9546d543e59459f97 +Subproject commit 053ab218dfb65e9cbd7c7a1d9594fa886ba27f0b diff --git a/Telegram/lib_spellcheck b/Telegram/lib_spellcheck index 5ae687774..d35fe8aa3 160000 --- a/Telegram/lib_spellcheck +++ b/Telegram/lib_spellcheck @@ -1 +1 @@ -Subproject commit 5ae68777478250c49520629420290f231173e1ca +Subproject commit d35fe8aa38a26bfcefd32286d48c371e1c7317b0 diff --git a/Telegram/lib_ui b/Telegram/lib_ui index a37e28d2f..98e3ba2c5 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit a37e28d2f3bb7ef306ce1b54db3cace902d363e6 +Subproject commit 98e3ba2c582a0eeb6d0474c4c6b2ccd814fa8d54