From 6426cc016748560c2a0774e8a8eec77ddc2180a8 Mon Sep 17 00:00:00 2001 From: gulikoza Date: Mon, 9 Sep 2019 17:31:42 +0200 Subject: [PATCH] Add support for writing log file. Writes robo3t log file into the same directory where settings are stored. Log file has the same contents as log tab shown in the app and contains history of all commands executed. Enabled by default, can be disabled by changing: "writeLogFile": false in the settings. --- .../core/settings/SettingsManager.cpp | 3 ++ src/robomongo/core/settings/SettingsManager.h | 2 ++ src/robomongo/core/utils/Logger.cpp | 34 +++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/robomongo/core/settings/SettingsManager.cpp b/src/robomongo/core/settings/SettingsManager.cpp index 4db364e68..340fb67f5 100644 --- a/src/robomongo/core/settings/SettingsManager.cpp +++ b/src/robomongo/core/settings/SettingsManager.cpp @@ -90,6 +90,7 @@ namespace Robomongo _autocompletionMode(AutocompleteAll), _loadMongoRcJs(false), _minimizeToTray(false), + _writeLogFile(true), _lineNumbers(false), _disableConnectionShortcuts(false), _batchSize(50), @@ -204,6 +205,7 @@ namespace Robomongo _autoExpand = map.contains("autoExpand") ? map.value("autoExpand").toBool() : true; _autoExec = map.contains("autoExec") ? map.value("autoExec").toBool() : true; _minimizeToTray = map.contains("minimizeToTray") ? map.value("minimizeToTray").toBool() : false; + _writeLogFile = map.contains("writeLogFile") ? map.value("writeLogFile").toBool() : true; _lineNumbers = map.contains("lineNumbers") ? map.value("lineNumbers").toBool() : false; _imported = map.contains("imported") ? map.value("imported").toBool() : false; _programExitedNormally = map.contains("programExitedNormally") ? @@ -390,6 +392,7 @@ namespace Robomongo map.insert("autoExec", _autoExec); map.insert("minimizeToTray", _minimizeToTray); + map.insert("writeLogFile", _writeLogFile); map.insert("toolbars", _toolbars); map.insert("imported", _imported); map.insert("anonymousID", _anonymousID); diff --git a/src/robomongo/core/settings/SettingsManager.h b/src/robomongo/core/settings/SettingsManager.h index 6405edee0..90c180f87 100644 --- a/src/robomongo/core/settings/SettingsManager.h +++ b/src/robomongo/core/settings/SettingsManager.h @@ -148,6 +148,7 @@ namespace Robomongo void setMinimizeToTray(bool isMinimizingToTray) { _minimizeToTray = isMinimizingToTray; } bool minimizeToTray() const { return _minimizeToTray; } + bool writeLogFile() const { return _writeLogFile; } void setLineNumbers(bool showLineNumbers) { _lineNumbers = showLineNumbers; } bool lineNumbers() const { return _lineNumbers; } @@ -245,6 +246,7 @@ namespace Robomongo bool _autoExpand; bool _autoExec; bool _minimizeToTray; + bool _writeLogFile; bool _lineNumbers; bool _disableConnectionShortcuts; bool _programExitedNormally = true; diff --git a/src/robomongo/core/utils/Logger.cpp b/src/robomongo/core/utils/Logger.cpp index 65f947f57..af0a26636 100644 --- a/src/robomongo/core/utils/Logger.cpp +++ b/src/robomongo/core/utils/Logger.cpp @@ -2,8 +2,14 @@ #include #include +#include +#include +#include +#include #include "robomongo/core/utils/QtUtils.h" +#include "robomongo/core/settings/SettingsManager.h" +#include "robomongo/core/AppRegistry.h" namespace { @@ -14,6 +20,14 @@ namespace return path; } + + std::string getRobo3tLogPath() + { + static std::string path = + Robomongo::QtUtils::toStdString(QString("%1/.3T/robo-3t/%2/robo3t.log").arg(QDir::homePath()).arg(PROJECT_VERSION)); + + return path; + } } namespace Robomongo @@ -32,7 +46,7 @@ namespace Robomongo } Logger::~Logger() - { + { } void Logger::print(const char *mess, mongo::logger::LogSeverity level, bool notify) @@ -41,7 +55,7 @@ namespace Robomongo } void Logger::print(const std::string &mess, mongo::logger::LogSeverity level, bool notify) - { + { print(QtUtils::toQString(mess), level, notify); } @@ -57,6 +71,22 @@ namespace Robomongo logLevelStr[0] = logLevelStr[0].toUpper(); logLevelStr += ": "; } + + if (AppRegistry::instance().settingsManager()->writeLogFile()) { + std::string path = getRobo3tLogPath(); + QLockFile lockfile(QtUtils::toQString(path + ".lock")); + QFile file(QtUtils::toQString(path)); + + if (lockfile.lock()) { + if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { + QTextStream out(&file); + out << QDate::currentDate().toString(Qt::ISODate) << " " << QTime::currentTime().toString(Qt::TextDate) << " " << logLevelStr << mess << "\n"; + file.close(); + } + lockfile.unlock(); + } + } + emit printed(logLevelStr + mess, level); } }