diff --git a/gui/commandrunner.cpp b/gui/commandrunner.cpp index 6989e89d6a..d7e7abf476 100644 --- a/gui/commandrunner.cpp +++ b/gui/commandrunner.cpp @@ -7,16 +7,34 @@ #include #include -CommandRunner::CommandRunner(QDialog *parent) +CommandRunner::CommandRunner(QDialog *parent, Logger *logger) { m_env = QProcessEnvironment::systemEnvironment(); m_parent = parent; + m_logger = logger; minikubePath(); #if __APPLE__ setMinikubePath(); #endif } +void CommandRunner::executeCommand(QString program, QStringList args) +{ + QProcess *process = new QProcess(this); + process->setProcessEnvironment(m_env); + process->start(program, args); + process->waitForFinished(-1); + if (process->exitCode() == 0) { + return; + } + QString out = process->readAllStandardOutput(); + QString err = process->readAllStandardError(); + QString log = QString("The following command failed:\n%1 %2\n\nStdout:\n%3\n\nStderr:\n%4\n\n") + .arg(program, args.join(" "), out, err); + m_logger->log(log); + delete process; +} + void CommandRunner::executeMinikubeCommand(QStringList args) { m_isRunning = true; diff --git a/gui/commandrunner.h b/gui/commandrunner.h index 2d836d9f32..dfcf20353c 100644 --- a/gui/commandrunner.h +++ b/gui/commandrunner.h @@ -2,6 +2,7 @@ #define COMMANDRUNNER_H #include "cluster.h" +#include "logger.h" #include #include @@ -16,8 +17,9 @@ class CommandRunner : public QObject Q_OBJECT public: - CommandRunner(QDialog *parent); + CommandRunner(QDialog *parent, Logger *logger); + void executeCommand(QString program, QStringList args); void startMinikube(QStringList args); void stopMinikube(QStringList args); void pauseMinikube(QStringList args); @@ -53,6 +55,7 @@ private: QString m_minikubePath; QString m_command; QDialog *m_parent; + Logger *m_logger; QStringList m_args; bool m_isRunning; }; diff --git a/gui/logger.cpp b/gui/logger.cpp new file mode 100644 index 0000000000..a18aa03755 --- /dev/null +++ b/gui/logger.cpp @@ -0,0 +1,26 @@ +#include "logger.h" + +#include +#include +#include +#include + +Logger::Logger() +{ + QDir dir = QDir(QDir::homePath() + "/.minikube-gui"); + if (!dir.exists()) { + dir.mkpath("."); + } + m_logPath = dir.filePath("logs.txt"); +} + +void Logger::log(QString message) +{ + QFile file(m_logPath); + if (!file.open(QIODevice::Append)) { + return; + } + QTextStream stream(&file); + stream << message << "\n"; + file.close(); +} diff --git a/gui/logger.h b/gui/logger.h new file mode 100644 index 0000000000..687ced4b1b --- /dev/null +++ b/gui/logger.h @@ -0,0 +1,18 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include + +class Logger : public QObject +{ + Q_OBJECT + +public: + explicit Logger(); + void log(QString message); + +private: + QString m_logPath; +}; + +#endif // LOGGER_H diff --git a/gui/operator.cpp b/gui/operator.cpp index dbc2e5eb30..d8e1402f95 100644 --- a/gui/operator.cpp +++ b/gui/operator.cpp @@ -335,8 +335,7 @@ void Operator::sshConsole() "-e", "do script \"" + command + "\"", "-e", "activate", "-e", "end tell" }; - QProcess *process = new QProcess(this); - process->start("/usr/bin/osascript", arguments); + m_commandRunner->executeCommand("/usr/bin/osascript", arguments); #else QString terminal = qEnvironmentVariable("TERMINAL"); if (terminal.isEmpty()) { @@ -346,8 +345,7 @@ void Operator::sshConsole() } } - QProcess *process = new QProcess(this); - process->start(QStandardPaths::findExecutable(terminal), { "-e", command }); + m_commandRunner->executeCommand(QStandardPaths::findExecutable(terminal), { "-e", command }); #endif } @@ -383,8 +381,7 @@ void Operator::dockerEnv() "-e", "do script \"" + command + "\"", "-e", "activate", "-e", "end tell" }; - QProcess *process = new QProcess(this); - process->start("/usr/bin/osascript", arguments); + m_commandRunner->executeCommand("/usr/bin/osascript", arguments); #else QString terminal = qEnvironmentVariable("TERMINAL"); if (terminal.isEmpty()) { @@ -394,8 +391,7 @@ void Operator::dockerEnv() } } - QProcess *process = new QProcess(this); - process->start(QStandardPaths::findExecutable(terminal), { "-e", command }); + m_commandRunner->executeCommand(QStandardPaths::findExecutable(terminal), { "-e", command }); #endif } diff --git a/gui/systray.pro b/gui/systray.pro index 9430a3673e..389b6eaa68 100644 --- a/gui/systray.pro +++ b/gui/systray.pro @@ -5,6 +5,7 @@ HEADERS = window.h \ commandrunner.h \ errormessage.h \ hyperkit.h \ + logger.h \ operator.h \ progresswindow.h \ tray.h \ @@ -16,6 +17,7 @@ SOURCES = main.cpp \ commandrunner.cpp \ errormessage.cpp \ hyperkit.cpp \ + logger.cpp \ operator.cpp \ progresswindow.cpp \ tray.cpp \ diff --git a/gui/window.cpp b/gui/window.cpp index 02c1e5aa83..ae134b0a03 100644 --- a/gui/window.cpp +++ b/gui/window.cpp @@ -99,7 +99,8 @@ Window::Window() checkForMinikube(); stackedWidget = new QStackedWidget; - commandRunner = new CommandRunner(this); + logger = new Logger(); + commandRunner = new CommandRunner(this, logger); basicView = new BasicView(); advancedView = new AdvancedView(*trayIconIcon); errorMessage = new ErrorMessage(this, *trayIconIcon); diff --git a/gui/window.h b/gui/window.h index 55a120ce77..79bc634936 100644 --- a/gui/window.h +++ b/gui/window.h @@ -83,7 +83,6 @@ class QTableView; class QProcess; QT_END_NAMESPACE -#include "cluster.h" #include "basicview.h" #include "advancedview.h" #include "progresswindow.h" @@ -92,6 +91,7 @@ QT_END_NAMESPACE #include "tray.h" #include "hyperkit.h" #include "updater.h" +#include "logger.h" class Window : public QDialog { @@ -125,6 +125,7 @@ private: HyperKit *hyperKit; Updater *updater; QVBoxLayout *layout; + Logger *logger; }; #endif // QT_NO_SYSTEMTRAYICON