Commit bc319fa4 authored by DonLakeFlyer's avatar DonLakeFlyer

Allow logging console output to file from command line

parent 928ad194
...@@ -138,31 +138,22 @@ static QObject* qgroundcontrolQmlGlobalSingletonFactory(QQmlEngine*, QJSEngine*) ...@@ -138,31 +138,22 @@ static QObject* qgroundcontrolQmlGlobalSingletonFactory(QQmlEngine*, QJSEngine*)
return qmlGlobal; return qmlGlobal;
} }
/** QGCApplication::QGCApplication(int &argc, char* argv[], bool logOutput, bool unitTesting)
* @brief Constructor for the main application.
*
* This constructor initializes and starts the whole application. It takes standard
* command-line parameters
*
* @param argc The number of command-line parameters
* @param argv The string array of parameters
**/
QGCApplication::QGCApplication(int &argc, char* argv[], bool unitTesting)
#ifdef __mobile__ #ifdef __mobile__
: QGuiApplication(argc, argv) : QGuiApplication (argc, argv)
, _qmlAppEngine(NULL) , _qmlAppEngine (NULL)
#else #else
: QApplication(argc, argv) : QApplication (argc, argv)
#endif #endif
, _runningUnitTests(unitTesting) , _runningUnitTests (unitTesting)
, _fakeMobile(false) , _logOutput (logOutput)
, _settingsUpgraded(false) , _fakeMobile (false)
#ifdef QT_DEBUG , _settingsUpgraded (false)
, _testHighDPI(false) #ifdef QT_DEBUG
#endif , _testHighDPI (false)
, _toolbox(NULL) #endif
, _bluetoothAvailable(false) , _toolbox (NULL)
, _bluetoothAvailable (false)
{ {
_app = this; _app = this;
......
...@@ -60,7 +60,7 @@ class QGCApplication : public ...@@ -60,7 +60,7 @@ class QGCApplication : public
Q_OBJECT Q_OBJECT
public: public:
QGCApplication(int &argc, char* argv[], bool unitTesting); QGCApplication(int &argc, char* argv[], bool logOutput, bool unitTesting);
~QGCApplication(); ~QGCApplication();
/// @brief Sets the persistent flag to delete all settings the next time QGroundControl is started. /// @brief Sets the persistent flag to delete all settings the next time QGroundControl is started.
...@@ -69,9 +69,12 @@ public: ...@@ -69,9 +69,12 @@ public:
/// @brief Clears the persistent flag to delete all settings the next time QGroundControl is started. /// @brief Clears the persistent flag to delete all settings the next time QGroundControl is started.
void clearDeleteAllSettingsNextBoot(void); void clearDeleteAllSettingsNextBoot(void);
/// @brief Returns truee if unit test are being run /// @brief Returns true if unit tests are being run
bool runningUnitTests(void) { return _runningUnitTests; } bool runningUnitTests(void) { return _runningUnitTests; }
/// @brief Returns true if Qt debug output should be logged to a file
bool logOutput(void) { return _logOutput; }
/// Used to report a missing Parameter. Warning will be displayed to user. Method may be called /// Used to report a missing Parameter. Warning will be displayed to user. Method may be called
/// multiple times. /// multiple times.
void reportMissingParameter(int componentId, const QString& name); void reportMissingParameter(int componentId, const QString& name);
...@@ -158,6 +161,7 @@ private: ...@@ -158,6 +161,7 @@ private:
#endif #endif
bool _runningUnitTests; ///< true: running unit tests, false: normal app bool _runningUnitTests; ///< true: running unit tests, false: normal app
bool _logOutput; ///< true: Log Qt debug output to file
static const char* _darkStyleFile; static const char* _darkStyleFile;
static const char* _lightStyleFile; static const char* _lightStyleFile;
......
...@@ -11,8 +11,12 @@ ...@@ -11,8 +11,12 @@
// Allows QGlobalStatic to work on this translation unit // Allows QGlobalStatic to work on this translation unit
#define _LOG_CTOR_ACCESS_ public #define _LOG_CTOR_ACCESS_ public
#include "AppMessages.h" #include "AppMessages.h"
#include <QFile> #include "QGCApplication.h"
#include "SettingsManager.h"
#include "AppSettings.h"
#include <QStringListModel> #include <QStringListModel>
#include <QtConcurrent> #include <QtConcurrent>
#include <QTextStream> #include <QTextStream>
...@@ -87,4 +91,26 @@ void AppLogModel::threadsafeLog(const QString message) ...@@ -87,4 +91,26 @@ void AppLogModel::threadsafeLog(const QString message)
const int line = rowCount(); const int line = rowCount();
insertRows(line, 1); insertRows(line, 1);
setData(index(line), message, Qt::DisplayRole); setData(index(line), message, Qt::DisplayRole);
if (qgcApp()->logOutput()) {
if (_logFile.fileName().isEmpty()) {
QGCToolbox* toolbox = qgcApp()->toolbox();
// Be careful of toolbox not being open yet
if (toolbox) {
QString saveDirPath = qgcApp()->toolbox()->settingsManager()->appSettings()->crashSavePath();
QDir saveDir(saveDirPath);
QString saveFilePath = saveDir.absoluteFilePath(QStringLiteral("QGCConsole.log"));
_logFile.setFileName(saveFilePath);
if (!_logFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qgcApp()->showMessage(tr("Open console log output file failed %1 : %2").arg(_logFile.fileName()).arg(_logFile.errorString()));
}
}
}
if (_logFile.isOpen()) {
QTextStream out(&_logFile);
out << message << "\n";
}
}
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <QObject> #include <QObject>
#include <QStringListModel> #include <QStringListModel>
#include <QUrl> #include <QUrl>
#include <QFile>
// Hackish way to force only this translation unit to have public ctor access // Hackish way to force only this translation unit to have public ctor access
#ifndef _LOG_CTOR_ACCESS_ #ifndef _LOG_CTOR_ACCESS_
...@@ -34,6 +35,9 @@ signals: ...@@ -34,6 +35,9 @@ signals:
private slots: private slots:
void threadsafeLog(const QString message); void threadsafeLog(const QString message);
private:
QFile _logFile;
_LOG_CTOR_ACCESS_: _LOG_CTOR_ACCESS_:
AppLogModel(); AppLogModel();
}; };
......
...@@ -176,12 +176,14 @@ int main(int argc, char *argv[]) ...@@ -176,12 +176,14 @@ int main(int argc, char *argv[])
bool stressUnitTests = false; // Stress test unit tests bool stressUnitTests = false; // Stress test unit tests
bool quietWindowsAsserts = false; // Don't let asserts pop dialog boxes bool quietWindowsAsserts = false; // Don't let asserts pop dialog boxes
bool logOutput = false; // true: Log Qt debug output to file
QString unitTestOptions; QString unitTestOptions;
CmdLineOpt_t rgCmdLineOptions[] = { CmdLineOpt_t rgCmdLineOptions[] = {
{ "--unittest", &runUnitTests, &unitTestOptions }, { "--unittest", &runUnitTests, &unitTestOptions },
{ "--unittest-stress", &stressUnitTests, &unitTestOptions }, { "--unittest-stress", &stressUnitTests, &unitTestOptions },
{ "--no-windows-assert-ui", &quietWindowsAsserts, NULL }, { "--no-windows-assert-ui", &quietWindowsAsserts, NULL },
{ "--log-output", &logOutput, NULL },
// Add additional command line option flags here // Add additional command line option flags here
}; };
...@@ -206,7 +208,7 @@ int main(int argc, char *argv[]) ...@@ -206,7 +208,7 @@ int main(int argc, char *argv[])
#endif #endif
#endif // QT_DEBUG #endif // QT_DEBUG
QGCApplication* app = new QGCApplication(argc, argv, runUnitTests); QGCApplication* app = new QGCApplication(argc, argv, logOutput, runUnitTests);
Q_CHECK_PTR(app); Q_CHECK_PTR(app);
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment