add logging for failed commands

pull/14735/head
Steven Powell 2022-08-04 15:17:31 -07:00
parent 7fdc9422df
commit ed05169a38
8 changed files with 77 additions and 12 deletions

View File

@ -7,16 +7,34 @@
#include <QJsonParseError>
#include <QDebug>
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;

View File

@ -2,6 +2,7 @@
#define COMMANDRUNNER_H
#include "cluster.h"
#include "logger.h"
#include <QString>
#include <QDialog>
@ -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;
};

26
gui/logger.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "logger.h"
#include <QStandardPaths>
#include <QFile>
#include <QDir>
#include <QTextStream>
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();
}

18
gui/logger.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <QObject>
class Logger : public QObject
{
Q_OBJECT
public:
explicit Logger();
void log(QString message);
private:
QString m_logPath;
};
#endif // LOGGER_H

View File

@ -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
}

View File

@ -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 \

View File

@ -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);

View File

@ -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