Replace cDebug() with Logs::DebugEnabled().

This commit is contained in:
John Preston 2018-06-05 16:32:26 +03:00
parent 9055b33e92
commit 72f95b984f
22 changed files with 58 additions and 45 deletions

View File

@ -750,7 +750,7 @@ bool RecoverBox::codeSubmitFail(const RPCError &error) {
_recoverCode->showError(); _recoverCode->showError();
return true; return true;
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
_error = err + ": " + error.description(); _error = err + ": " + error.description();
} else { } else {
_error = Lang::Hard::ServerError(); _error = Lang::Hard::ServerError();

View File

@ -529,7 +529,7 @@ void Call::createAndStartController(const MTPDphoneCall &call) {
config.enableAGC = true; config.enableAGC = true;
config.initTimeout = Global::CallConnectTimeoutMs() / 1000; config.initTimeout = Global::CallConnectTimeoutMs() / 1000;
config.recvTimeout = Global::CallPacketTimeoutMs() / 1000; config.recvTimeout = Global::CallPacketTimeoutMs() / 1000;
if (cDebug()) { if (Logs::DebugEnabled()) {
auto callLogFolder = cWorkingDir() + qsl("DebugLogs"); auto callLogFolder = cWorkingDir() + qsl("DebugLogs");
auto callLogPath = callLogFolder + qsl("/last_call_log.txt"); auto callLogPath = callLogFolder + qsl("/last_call_log.txt");
auto callLogNative = QFile::encodeName(QDir::toNativeSeparators(callLogPath)); auto callLogNative = QFile::encodeName(QDir::toNativeSeparators(callLogPath));

View File

@ -108,7 +108,8 @@ void TopBar::initControls() {
setInfoLabels(); setInfoLabels();
_info->setClickedCallback([this] { _info->setClickedCallback([this] {
if (auto call = _call.get()) { if (auto call = _call.get()) {
if (cDebug() && (_info->clickModifiers() & Qt::ControlModifier)) { if (Logs::DebugEnabled()
&& (_info->clickModifiers() & Qt::ControlModifier)) {
Ui::show(Box<DebugInfoBox>(_call)); Ui::show(Box<DebugInfoBox>(_call));
} else { } else {
Current().showInfoPanel(call); Current().showInfoPanel(call);

View File

@ -215,7 +215,7 @@ void Launcher::processArguments() {
} }
gTestMode = parseResult.contains("-testmode"); gTestMode = parseResult.contains("-testmode");
gDebug = parseResult.contains("-debug"); Logs::SetDebugEnabled(parseResult.contains("-debug"));
gManyInstance = parseResult.contains("-many"); gManyInstance = parseResult.contains("-many");
gKeyFile = parseResult.value("-key", QStringList()).join(QString()); gKeyFile = parseResult.value("-key", QStringList()).join(QString());
gLaunchMode = parseResult.contains("-autostart") ? LaunchModeAutoStart gLaunchMode = parseResult.contains("-autostart") ? LaunchModeAutoStart

View File

@ -1756,7 +1756,7 @@ void Updater::start(bool forceWait) {
sendRequest = true; sendRequest = true;
} }
} }
if (cManyInstance() && !cDebug()) { if (cManyInstance() && !Logs::DebugEnabled()) {
// Only main instance is updating. // Only main instance is updating.
return; return;
} }

View File

@ -410,8 +410,9 @@ void WorkingDirReady() {
if (QFile(cWorkingDir() + qsl("tdata/withtestmode")).exists()) { if (QFile(cWorkingDir() + qsl("tdata/withtestmode")).exists()) {
cSetTestMode(true); cSetTestMode(true);
} }
if (!cDebug() && QFile(cWorkingDir() + qsl("tdata/withdebug")).exists()) { if (!Logs::DebugEnabled()
cSetDebug(true); && QFile(cWorkingDir() + qsl("tdata/withdebug")).exists()) {
Logs::SetDebugEnabled(true);
} }
if (cBetaVersion()) { if (cBetaVersion()) {
cSetAlphaVersion(false); cSetAlphaVersion(false);

View File

@ -242,7 +242,7 @@ bool CodeWidget::codeSubmitFail(const RPCError &error) {
_sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&CodeWidget::gotPassword), rpcFail(&CodeWidget::codeSubmitFail)); _sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&CodeWidget::gotPassword), rpcFail(&CodeWidget::codeSubmitFail));
return true; return true;
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description(); auto text = err + ": " + error.description();
showCodeError([text] { return text; }); showCodeError([text] { return text; });
} else { } else {
@ -351,7 +351,7 @@ bool CodeWidget::noTelegramCodeFail(const RPCError &error) {
} }
if (MTP::isDefaultHandledError(error)) return false; if (MTP::isDefaultHandledError(error)) return false;
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = error.type() + ": " + error.description(); auto text = error.type() + ": " + error.description();
showCodeError([text] { return text; }); showCodeError([text] { return text; });
} else { } else {

View File

@ -254,7 +254,7 @@ bool PhoneWidget::phoneSubmitFail(const RPCError &error) {
[phone] { SendToBannedHelp(phone); Ui::hideLayer(); })); [phone] { SendToBannedHelp(phone); Ui::hideLayer(); }));
return true; return true;
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description(); auto text = err + ": " + error.description();
showPhoneError([text] { return text; }); showPhoneError([text] { return text; });
} else { } else {

View File

@ -151,7 +151,7 @@ bool PwdCheckWidget::pwdSubmitFail(const RPCError &error) {
} else if (err == qstr("PASSWORD_EMPTY")) { } else if (err == qstr("PASSWORD_EMPTY")) {
goBack(); goBack();
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description(); auto text = err + ": " + error.description();
showError([text] { return text; }); showError([text] { return text; });
} else { } else {
@ -188,7 +188,7 @@ bool PwdCheckWidget::codeSubmitFail(const RPCError &error) {
_codeField->showError(); _codeField->showError();
return true; return true;
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description(); auto text = err + ": " + error.description();
showError([text] { return text; }); showError([text] { return text; });
} else { } else {

View File

@ -161,7 +161,7 @@ bool SignupWidget::nameSubmitFail(const RPCError &error) {
_last->setFocus(); _last->setFocus();
return true; return true;
} }
if (cDebug()) { // internal server error if (Logs::DebugEnabled()) { // internal server error
auto text = err + ": " + error.description(); auto text = err + ": " + error.description();
showError([text] { return text; }); showError([text] { return text; });
} else { } else {

View File

@ -243,7 +243,7 @@ QString LogsBeforeSingleInstanceChecked; // LogsInMemory already dumped in LogsD
void _logsWrite(LogDataType type, const QString &msg) { void _logsWrite(LogDataType type, const QString &msg) {
if (LogsData && (type == LogDataMain || LogsStartIndexChosen < 0)) { if (LogsData && (type == LogDataMain || LogsStartIndexChosen < 0)) {
if (type == LogDataMain || cDebug()) { if (type == LogDataMain || Logs::DebugEnabled()) {
LogsData->write(type, msg); LogsData->write(type, msg);
} }
} else if (LogsInMemory != DeletedLogsInMemory) { } else if (LogsInMemory != DeletedLogsInMemory) {
@ -259,6 +259,8 @@ void _logsWrite(LogDataType type, const QString &msg) {
namespace Logs { namespace Logs {
namespace { namespace {
bool DebugModeEnabled = false;
void MoveOldDataFiles(const QString &wasDir) { void MoveOldDataFiles(const QString &wasDir) {
QFile data(wasDir + "data"), dataConfig(wasDir + "data_config"), tdataConfig(wasDir + "tdata/config"); QFile data(wasDir + "data"), dataConfig(wasDir + "data_config"), tdataConfig(wasDir + "tdata/config");
if (data.exists() && dataConfig.exists() && !QFileInfo(cWorkingDir() + "data").exists() && !QFileInfo(cWorkingDir() + "data_config").exists()) { // move to home dir if (data.exists() && dataConfig.exists() && !QFileInfo(cWorkingDir() + "data").exists() && !QFileInfo(cWorkingDir() + "data_config").exists()) { // move to home dir
@ -307,6 +309,18 @@ void MoveOldDataFiles(const QString &wasDir) {
} // namespace } // namespace
void SetDebugEnabled(bool enabled) {
DebugModeEnabled = enabled;
}
bool DebugEnabled() {
#if defined _DEBUG
return true;
#else
return DebugModeEnabled;
#endif
}
void start(not_null<Core::Launcher*> launcher) { void start(not_null<Core::Launcher*> launcher) {
Assert(LogsData == 0); Assert(LogsData == 0);
@ -319,7 +333,7 @@ void start(not_null<Core::Launcher*> launcher) {
auto workingDirChosen = false; auto workingDirChosen = false;
if (cBetaVersion()) { if (cBetaVersion()) {
cSetDebug(true); SetDebugEnabled(true);
workingDirChosen = true; workingDirChosen = true;
#if defined Q_OS_MAC || defined Q_OS_LINUX #if defined Q_OS_MAC || defined Q_OS_LINUX
} else { } else {
@ -385,7 +399,7 @@ void start(not_null<Core::Launcher*> launcher) {
LogsData = 0; LogsData = 0;
} }
LOG(("Launched version: %1, alpha: %2, beta: %3, debug mode: %4, test dc: %5").arg(AppVersion).arg(Logs::b(cAlphaVersion())).arg(cBetaVersion()).arg(Logs::b(cDebug())).arg(Logs::b(cTestMode()))); LOG(("Launched version: %1, alpha: %2, beta: %3, debug mode: %4, test dc: %5").arg(AppVersion).arg(Logs::b(cAlphaVersion())).arg(cBetaVersion()).arg(Logs::b(DebugEnabled())).arg(Logs::b(cTestMode())));
LOG(("Executable dir: %1, name: %2").arg(cExeDir()).arg(cExeName())); LOG(("Executable dir: %1, name: %2").arg(cExeDir()).arg(cExeName()));
LOG(("Initial working dir: %1").arg(initialWorkingDir)); LOG(("Initial working dir: %1").arg(initialWorkingDir));
LOG(("Working dir: %1").arg(cWorkingDir())); LOG(("Working dir: %1").arg(cWorkingDir()));
@ -475,7 +489,7 @@ void multipleInstances() {
} }
LogsInMemory = DeletedLogsInMemory; LogsInMemory = DeletedLogsInMemory;
if (cDebug()) { if (Logs::DebugEnabled()) {
LOG(("WARNING: debug logs are not written in multiple instances mode!")); LOG(("WARNING: debug logs are not written in multiple instances mode!"));
} }
LogsBeforeSingleInstanceChecked.clear(); LogsBeforeSingleInstanceChecked.clear();

View File

@ -13,6 +13,9 @@ class Launcher;
namespace Logs { namespace Logs {
void SetDebugEnabled(bool enabled);
bool DebugEnabled();
void start(not_null<Core::Launcher*> launcher); void start(not_null<Core::Launcher*> launcher);
bool started(); bool started();
void finish(); void finish();
@ -65,11 +68,11 @@ inline MemoryBuffer mb(const void *ptr, uint32 size) {
#define LOG(msg) (Logs::writeMain(QString msg)) #define LOG(msg) (Logs::writeMain(QString msg))
//usage LOG(("log: %1 %2").arg(1).arg(2)) //usage LOG(("log: %1 %2").arg(1).arg(2))
#define DEBUG_LOG(msg) { if (cDebug() || !Logs::started()) Logs::writeDebug(__FILE__, __LINE__, QString msg); } #define DEBUG_LOG(msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeDebug(__FILE__, __LINE__, QString msg); }
//usage DEBUG_LOG(("log: %1 %2").arg(1).arg(2)) //usage DEBUG_LOG(("log: %1 %2").arg(1).arg(2))
#define TCP_LOG(msg) { if (cDebug() || !Logs::started()) Logs::writeTcp(QString msg); } #define TCP_LOG(msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeTcp(QString msg); }
//usage TCP_LOG(("log: %1 %2").arg(1).arg(2)) //usage TCP_LOG(("log: %1 %2").arg(1).arg(2))
#define MTP_LOG(dc, msg) { if (cDebug() || !Logs::started()) Logs::writeMtp(dc, QString msg); } #define MTP_LOG(dc, msg) { if (Logs::DebugEnabled() || !Logs::started()) Logs::writeMtp(dc, QString msg); }
//usage MTP_LOG(dc, ("log: %1 %2").arg(1).arg(2)) //usage MTP_LOG(dc, ("log: %1 %2").arg(1).arg(2))

View File

@ -493,7 +493,9 @@ void MainWindow::setInnerFocus() {
bool MainWindow::eventFilter(QObject *object, QEvent *e) { bool MainWindow::eventFilter(QObject *object, QEvent *e) {
switch (e->type()) { switch (e->type()) {
case QEvent::KeyPress: { case QEvent::KeyPress: {
if (cDebug() && e->type() == QEvent::KeyPress && object == windowHandle()) { if (Logs::DebugEnabled()
&& (e->type() == QEvent::KeyPress)
&& object == windowHandle()) {
auto key = static_cast<QKeyEvent*>(e)->key(); auto key = static_cast<QKeyEvent*>(e)->key();
FeedLangTestingKey(key); FeedLangTestingKey(key);
} }

View File

@ -744,12 +744,12 @@ void Messenger::photoUpdated(const FullMsgId &msgId, const MTPInputFile &file) {
} }
void Messenger::onSwitchDebugMode() { void Messenger::onSwitchDebugMode() {
if (cDebug()) { if (Logs::DebugEnabled()) {
QFile(cWorkingDir() + qsl("tdata/withdebug")).remove(); QFile(cWorkingDir() + qsl("tdata/withdebug")).remove();
cSetDebug(false); Logs::SetDebugEnabled(false);
App::restart(); App::restart();
} else { } else {
cSetDebug(true); Logs::SetDebugEnabled(true);
DEBUG_LOG(("Debug logs started.")); DEBUG_LOG(("Debug logs started."));
QFile f(cWorkingDir() + qsl("tdata/withdebug")); QFile f(cWorkingDir() + qsl("tdata/withdebug"));
if (f.open(QIODevice::WriteOnly)) { if (f.open(QIODevice::WriteOnly)) {

View File

@ -1705,7 +1705,7 @@ ConnectionPrivate::HandleResult ConnectionPrivate::handleOneReceived(const mtpPr
|| (errorCode == 17) // bad msg_id || (errorCode == 17) // bad msg_id
|| (errorCode == 64); // bad container || (errorCode == 64); // bad container
if (errorCode == 64) { // bad container! if (errorCode == 64) { // bad container!
if (cDebug()) { if (Logs::DebugEnabled()) {
mtpRequest request; mtpRequest request;
{ {
QWriteLocker locker(sessionData->haveSentMutex()); QWriteLocker locker(sessionData->haveSentMutex());

View File

@ -919,7 +919,7 @@ void Instance::Private::clearCallbacksDelayed(
return; return;
} }
if (cDebug()) { if (Logs::DebugEnabled()) {
auto idsString = QStringList(); auto idsString = QStringList();
idsString.reserve(ids.size()); idsString.reserve(ids.size());
for (auto &value : ids) { for (auto &value : ids) {
@ -939,7 +939,7 @@ void Instance::Private::clearCallbacks(
Expects(!ids.empty()); Expects(!ids.empty());
for (const auto &clearRequest : ids) { for (const auto &clearRequest : ids) {
if (cDebug()) { if (Logs::DebugEnabled()) {
QMutexLocker locker(&_parserMapLock); QMutexLocker locker(&_parserMapLock);
if (_parserMap.find(clearRequest.requestId) != _parserMap.end()) { if (_parserMap.find(clearRequest.requestId) != _parserMap.end()) {
DEBUG_LOG(("RPC Info: " DEBUG_LOG(("RPC Info: "

View File

@ -74,7 +74,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
if (cLaunchMode() == LaunchModeAutoStart) { if (cLaunchMode() == LaunchModeAutoStart) {
argumentsList.push("-autostart"); argumentsList.push("-autostart");
} }
if (cDebug()) { if (Logs::DebugEnabled()) {
argumentsList.push("-debug"); argumentsList.push("-debug");
} }
if (cStartInTray()) { if (cStartInTray()) {

View File

@ -122,7 +122,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
if (cRestartingToSettings()) [args addObject:@"-tosettings"]; if (cRestartingToSettings()) [args addObject:@"-tosettings"];
if (action == UpdaterLaunch::JustRelaunch) [args addObject:@"-noupdate"]; if (action == UpdaterLaunch::JustRelaunch) [args addObject:@"-noupdate"];
if (cLaunchMode() == LaunchModeAutoStart) [args addObject:@"-autostart"]; if (cLaunchMode() == LaunchModeAutoStart) [args addObject:@"-autostart"];
if (cDebug()) [args addObject:@"-debug"]; if (Logs::DebugEnabled()) [args addObject:@"-debug"];
if (cStartInTray()) [args addObject:@"-startintray"]; if (cStartInTray()) [args addObject:@"-startintray"];
if (cTestMode()) [args addObject:@"-testmode"]; if (cTestMode()) [args addObject:@"-testmode"];
if (cDataFile() != qsl("data")) { if (cDataFile() != qsl("data")) {

View File

@ -80,7 +80,7 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
if (cLaunchMode() == LaunchModeAutoStart) { if (cLaunchMode() == LaunchModeAutoStart) {
pushArgument(qsl("-autostart")); pushArgument(qsl("-autostart"));
} }
if (cDebug()) { if (Logs::DebugEnabled()) {
pushArgument(qsl("-debug")); pushArgument(qsl("-debug"));
} }
if (cStartInTray()) { if (cStartInTray()) {

View File

@ -16,7 +16,6 @@ uint64 gRealBetaVersion = AppBetaVersion;
QByteArray gBetaPrivateKey; QByteArray gBetaPrivateKey;
bool gTestMode = false; bool gTestMode = false;
bool gDebug = false;
bool gManyInstance = false; bool gManyInstance = false;
QString gKeyFile; QString gKeyFile;
QString gWorkingDir, gExeDir, gExeName; QString gWorkingDir, gExeDir, gExeName;

View File

@ -7,18 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#pragma once #pragma once
extern bool gDebug;
inline bool cDebug() {
#if defined _DEBUG
return true;
#else
return gDebug;
#endif
}
inline void cSetDebug(bool debug) {
gDebug = debug;
}
#define DeclareReadSetting(Type, Name) extern Type g##Name; \ #define DeclareReadSetting(Type, Name) extern Type g##Name; \
inline const Type &c##Name() { \ inline const Type &c##Name() { \
return g##Name; \ return g##Name; \

View File

@ -39,7 +39,10 @@ QMap<QString, Fn<void()>> Codes;
void fillCodes() { void fillCodes() {
Codes.insert(qsl("debugmode"), [] { Codes.insert(qsl("debugmode"), [] {
QString text = cDebug() ? qsl("Do you want to disable DEBUG logs?") : qsl("Do you want to enable DEBUG logs?\n\nAll network events will be logged."); QString text = Logs::DebugEnabled()
? qsl("Do you want to disable DEBUG logs?")
: qsl("Do you want to enable DEBUG logs?\n\n"
"All network events will be logged.");
Ui::show(Box<ConfirmBox>(text, [] { Ui::show(Box<ConfirmBox>(text, [] {
Messenger::Instance().onSwitchDebugMode(); Messenger::Instance().onSwitchDebugMode();
})); }));
@ -59,7 +62,9 @@ void fillCodes() {
Lang::CurrentCloudManager().switchToLanguage(qsl("custom")); Lang::CurrentCloudManager().switchToLanguage(qsl("custom"));
}); });
Codes.insert(qsl("debugfiles"), [] { Codes.insert(qsl("debugfiles"), [] {
if (!cDebug()) return; if (!Logs::DebugEnabled()) {
return;
}
if (DebugLogging::FileLoader()) { if (DebugLogging::FileLoader()) {
Global::RefDebugLoggingFlags() &= ~DebugLogging::FileLoaderFlag; Global::RefDebugLoggingFlags() &= ~DebugLogging::FileLoaderFlag;
} else { } else {