Improve Console window output.

-> Log Pre- and Post-Launch command happenings
-> Enable the java part to specify the level

TODO: fix logging with mc 1.7's log4j logging infrastructure

Signed-off-by: Orochimarufan <orochimarufan.x3@gmail.com>
This commit is contained in:
Orochimarufan 2014-01-17 22:55:10 +01:00
parent 7b96d74d3b
commit 188d0d5886
10 changed files with 249 additions and 83 deletions

14
.gitignore vendored
View File

@ -1,17 +1,23 @@
Thumbs.db Thumbs.db
.kdev4 .kdev4
MultiMC5.kdev4
MultiMC.pro.user
CMakeLists.txt.user
.user .user
.directory .directory
build
resources/CMakeFiles resources/CMakeFiles
resources/MultiMCLauncher.jar resources/MultiMCLauncher.jar
*~ *~
*.swp *.swp
html/ html/
# Project Files
MultiMC5.kdev4
MultiMC.pro.user
CMakeLists.txt.user
CMakeLists.txt.user.*
# Build dirs
build
/build-*
# Ctags File # Ctags File
tags tags

View File

@ -379,6 +379,7 @@ void MultiMC::initGlobalSettings()
// Console // Console
m_settings->registerSetting("ShowConsole", true); m_settings->registerSetting("ShowConsole", true);
m_settings->registerSetting("AutoCloseConsole", true); m_settings->registerSetting("AutoCloseConsole", true);
m_settings->registerSetting("LogPrePostOutput", true);
// Console Colors // Console Colors
// m_settings->registerSetting("SysMessageColor", QColor(Qt::blue)); // m_settings->registerSetting("SysMessageColor", QColor(Qt::blue));

View File

@ -71,15 +71,15 @@ public class EntryPoint
if(param.equals("legacy")) if(param.equals("legacy"))
{ {
m_launcher = new LegacyLauncher(); m_launcher = new LegacyLauncher();
System.out.println("Using legacy launcher."); Utils.log("Using legacy launcher.");
System.out.println(); Utils.log();
return Action.Launch; return Action.Launch;
} }
if(param.equals("onesix")) if(param.equals("onesix"))
{ {
m_launcher = new OneSixLauncher(); m_launcher = new OneSixLauncher();
System.out.println("Using onesix launcher."); Utils.log("Using onesix launcher.");
System.out.println(); Utils.log();
return Action.Launch; return Action.Launch;
} }
else else

View File

@ -153,4 +153,27 @@ public class Utils
} }
return null; return null;
} }
/**
* Log to the MultiMC console
*
* @param message A String containing the message
* @param level A String containing the level name. See MinecraftProcess::getLevel()
*/
public static void log(String message, String level)
{
// Kinda dirty
String tag = "!![" + level + "]!";
System.out.println(tag + message.replace("\n", "\n" + tag));
}
public static void log(String message)
{
log(message, "MultiMC");
}
public static void log()
{
System.out.println();
}
} }

View File

@ -102,20 +102,20 @@ public class LegacyLauncher implements Launcher
// print the pretty things // print the pretty things
{ {
System.out.println("Main Class:"); Utils.log("Main Class:");
System.out.println(mainClass); Utils.log(" " + mainClass);
System.out.println(); Utils.log();
System.out.println("Class Path:"); Utils.log("Class Path:");
for (URL s : classpath) for (URL s : classpath)
{ {
System.out.println(s); Utils.log(" " + s);
} }
System.out.println(); Utils.log();
System.out.println("Native Path:"); Utils.log("Native Path:");
System.out.println(nativesDir); Utils.log(" " + nativesDir);
System.out.println(); Utils.log();
} }
URLClassLoader cl = new URLClassLoader(classpath, LegacyLauncher.class.getClassLoader()); URLClassLoader cl = new URLClassLoader(classpath, LegacyLauncher.class.getClassLoader());
@ -149,7 +149,7 @@ public class LegacyLauncher implements Launcher
mcArgs[0] = userName; mcArgs[0] = userName;
mcArgs[1] = sessionId; mcArgs[1] = sessionId;
System.out.println("Launching with applet wrapper..."); Utils.log("Launching with applet wrapper...");
try try
{ {
Class<?> MCAppletClass = cl.loadClass("net.minecraft.client.MinecraftApplet"); Class<?> MCAppletClass = cl.loadClass("net.minecraft.client.MinecraftApplet");
@ -158,16 +158,16 @@ public class LegacyLauncher implements Launcher
mcWindow.start(mcappl, userName, sessionId, winSize, maximize); mcWindow.start(mcappl, userName, sessionId, winSize, maximize);
} catch (Exception e) } catch (Exception e)
{ {
System.err.println("Applet wrapper failed:"); Utils.log("Applet wrapper failed:", "Error");
e.printStackTrace(System.err); e.printStackTrace(System.err);
System.err.println(); Utils.log();
System.out.println("Falling back to compatibility mode."); Utils.log("Falling back to compatibility mode.");
try try
{ {
mc.getMethod("main", String[].class).invoke(null, (Object) mcArgs); mc.getMethod("main", String[].class).invoke(null, (Object) mcArgs);
} catch (Exception e1) } catch (Exception e1)
{ {
System.err.println("Failed to invoke the Minecraft main class:"); Utils.log("Failed to invoke the Minecraft main class:", "Fatal");
e1.printStackTrace(System.err); e1.printStackTrace(System.err);
return -1; return -1;
} }

View File

@ -74,37 +74,37 @@ public class OneSixLauncher implements Launcher
// print the pretty things // print the pretty things
{ {
System.out.println("Main Class:"); Utils.log("Main Class:");
System.out.println(mainClass); Utils.log(" " + mainClass);
System.out.println(); Utils.log();
System.out.println("Native paths:"); Utils.log("Native paths:");
for (String s : allNativePaths) for (String s : allNativePaths)
{ {
System.out.println(s); Utils.log(" " + s);
} }
System.out.println(); Utils.log();
System.out.println("Libraries:"); Utils.log("Libraries:");
for (String s : libraries) for (String s : libraries)
{ {
System.out.println(s); Utils.log(" " + s);
} }
System.out.println(); Utils.log();
if(mods.size() > 0) if(mods.size() > 0)
{ {
System.out.println("Class Path Mods:"); Utils.log("Class Path Mods:");
for (String s : mods) for (String s : mods)
{ {
System.out.println(s); Utils.log(" " + s);
} }
System.out.println(); Utils.log();
} }
System.out.println("Params:"); Utils.log("Params:");
System.out.println(mcparams.toString()); Utils.log(" " + mcparams.toString());
System.out.println(); Utils.log();
} }
final ClassLoader cl = ClassLoader.getSystemClassLoader(); final ClassLoader cl = ClassLoader.getSystemClassLoader();

View File

@ -140,6 +140,9 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
else if (mode == MessageLevel::Debug) else if (mode == MessageLevel::Debug)
while (iter.hasNext()) while (iter.hasNext())
writeColor(iter.next(), "green"); writeColor(iter.next(), "green");
else if (mode == MessageLevel::PrePost)
while (iter.hasNext())
writeColor(iter.next(), "grey");
// TODO: implement other MessageLevels // TODO: implement other MessageLevels
else else
while (iter.hasNext()) while (iter.hasNext())

View File

@ -82,6 +82,7 @@ BaseInstance::BaseInstance(BaseInstancePrivate *d_in, const QString &rootDir,
settings().registerSetting("OverrideConsole", false); settings().registerSetting("OverrideConsole", false);
settings().registerOverride(globalSettings->getSetting("ShowConsole")); settings().registerOverride(globalSettings->getSetting("ShowConsole"));
settings().registerOverride(globalSettings->getSetting("AutoCloseConsole")); settings().registerOverride(globalSettings->getSetting("AutoCloseConsole"));
settings().registerOverride(globalSettings->getSetting("LogPrePostOutput"));
} }
void BaseInstance::iconUpdated(QString key) void BaseInstance::iconUpdated(QString key)

View File

@ -22,6 +22,7 @@
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QRegularExpression>
#include "BaseInstance.h" #include "BaseInstance.h"
@ -42,6 +43,7 @@ MinecraftProcess::MinecraftProcess(BaseInstance *inst) : m_instance(inst)
#ifdef LINUX #ifdef LINUX
// Strip IBus // Strip IBus
// IBus is a Linux IME framework. For some reason, it breaks MC?
if (env.value("XMODIFIERS").contains(IBUS)) if (env.value("XMODIFIERS").contains(IBUS))
env.insert("XMODIFIERS", env.value("XMODIFIERS").replace(IBUS, "")); env.insert("XMODIFIERS", env.value("XMODIFIERS").replace(IBUS, ""));
#endif #endif
@ -57,6 +59,15 @@ MinecraftProcess::MinecraftProcess(BaseInstance *inst) : m_instance(inst)
// std channels // std channels
connect(this, SIGNAL(readyReadStandardError()), SLOT(on_stdErr())); connect(this, SIGNAL(readyReadStandardError()), SLOT(on_stdErr()));
connect(this, SIGNAL(readyReadStandardOutput()), SLOT(on_stdOut())); connect(this, SIGNAL(readyReadStandardOutput()), SLOT(on_stdOut()));
// Log prepost launch command output (can be disabled.)
if (m_instance->settings().get("LogPrePostOutput").toBool())
{
connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardError,
this, &MinecraftProcess::on_prepost_stdErr);
connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardOutput,
this, &MinecraftProcess::on_prepost_stdOut);
}
} }
void MinecraftProcess::setWorkdir(QString path) void MinecraftProcess::setWorkdir(QString path)
@ -97,43 +108,133 @@ QString MinecraftProcess::censorPrivateInfo(QString in)
} }
// console window // console window
MessageLevel::Enum MinecraftProcess::guessLevel(const QString &line, MessageLevel::Enum level)
{
if (line.contains("[INFO]") || line.contains("[CONFIG]") || line.contains("[FINE]") ||
line.contains("[FINER]") || line.contains("[FINEST]"))
level = MessageLevel::Message;
if (line.contains("[SEVERE]") || line.contains("[STDERR]"))
level = MessageLevel::Error;
if (line.contains("[WARNING]"))
level = MessageLevel::Warning;
if (line.contains("Exception in thread") || line.contains(" at "))
level = MessageLevel::Fatal;
if (line.contains("[DEBUG]"))
level = MessageLevel::Debug;
return level;
}
MessageLevel::Enum MinecraftProcess::getLevel(const QString &levelName)
{
if (levelName == "MultiMC")
return MessageLevel::MultiMC;
else if (levelName == "Debug")
return MessageLevel::Debug;
else if (levelName == "Info")
return MessageLevel::Info;
else if (levelName == "Message")
return MessageLevel::Message;
else if (levelName == "Warning")
return MessageLevel::Warning;
else if (levelName == "Error")
return MessageLevel::Error;
else if (levelName == "Fatal")
return MessageLevel::Fatal;
// Skip PrePost, it's not exposed to !![]!
else
return MessageLevel::Message;
}
void MinecraftProcess::logOutput(const QStringList &lines,
MessageLevel::Enum defaultLevel,
bool guessLevel, bool censor)
{
for (int i = 0; i < lines.size(); ++i)
logOutput(lines[i], defaultLevel, guessLevel, censor);
}
void MinecraftProcess::logOutput(QString line,
MessageLevel::Enum defaultLevel,
bool guessLevel, bool censor)
{
MessageLevel::Enum level = defaultLevel;
// Level prefix
int endmark = line.indexOf("]!");
if (line.startsWith("!![") && endmark != -1)
{
level = getLevel(line.left(endmark).mid(3));
line = line.mid(endmark + 2);
}
// Guess level
else if (guessLevel)
level = this->guessLevel(line, defaultLevel);
if (censor)
line = censorPrivateInfo(line);
emit log(line, level);
}
void MinecraftProcess::on_stdErr() void MinecraftProcess::on_stdErr()
{ {
QByteArray data = readAllStandardError(); QByteArray data = readAllStandardError();
QString str = m_err_leftover + QString::fromLocal8Bit(data); QString str = m_err_leftover + QString::fromLocal8Bit(data);
m_err_leftover.clear();
QStringList lines = str.split("\n");
bool complete = str.endsWith("\n");
for (int i = 0; i < lines.size() - 1; i++) QStringList lines = str.split("\n");
{ m_err_leftover = lines.takeLast();
QString &line = lines[i];
emit log(censorPrivateInfo(line), getLevel(line, MessageLevel::Error)); logOutput(lines, MessageLevel::Error);
}
if (!complete)
m_err_leftover = lines.last();
} }
void MinecraftProcess::on_stdOut() void MinecraftProcess::on_stdOut()
{ {
QByteArray data = readAllStandardOutput(); QByteArray data = readAllStandardOutput();
QString str = m_out_leftover + QString::fromLocal8Bit(data); QString str = m_out_leftover + QString::fromLocal8Bit(data);
m_out_leftover.clear();
QStringList lines = str.split("\n");
bool complete = str.endsWith("\n");
for (int i = 0; i < lines.size() - 1; i++) QStringList lines = str.split("\n");
{ m_out_leftover = lines.takeLast();
QString &line = lines[i];
emit log(censorPrivateInfo(line), getLevel(line, MessageLevel::Message)); logOutput(lines);
} }
if (!complete)
m_out_leftover = lines.last(); void MinecraftProcess::on_prepost_stdErr()
{
QByteArray data = m_prepostlaunchprocess.readAllStandardError();
QString str = m_err_leftover + QString::fromLocal8Bit(data);
QStringList lines = str.split("\n");
m_err_leftover = lines.takeLast();
logOutput(lines, MessageLevel::PrePost, false, false);
}
void MinecraftProcess::on_prepost_stdOut()
{
QByteArray data = m_prepostlaunchprocess.readAllStandardOutput();
QString str = m_out_leftover + QString::fromLocal8Bit(data);
QStringList lines = str.split("\n");
m_out_leftover = lines.takeLast();
logOutput(lines, MessageLevel::PrePost, false, false);
} }
// exit handler // exit handler
void MinecraftProcess::finish(int code, ExitStatus status) void MinecraftProcess::finish(int code, ExitStatus status)
{ {
// Flush console window
if (!m_err_leftover.isEmpty())
{
logOutput(m_err_leftover, MessageLevel::Error);
m_err_leftover.clear();
}
if (!m_out_leftover.isEmpty())
{
logOutput(m_out_leftover);
m_out_leftover.clear();
}
if (!killed) if (!killed)
{ {
if (status == NormalExit) if (status == NormalExit)
@ -156,15 +257,32 @@ void MinecraftProcess::finish(int code, ExitStatus status)
m_prepostlaunchprocess.processEnvironment().insert("INST_EXITCODE", QString(code)); m_prepostlaunchprocess.processEnvironment().insert("INST_EXITCODE", QString(code));
// run post-exit // run post-exit
if (!m_instance->settings().get("PostExitCommand").toString().isEmpty()) QString postlaunch_cmd = m_instance->settings().get("PostExitCommand").toString();
if (!postlaunch_cmd.isEmpty())
{ {
m_prepostlaunchprocess.start(m_instance->settings().get("PostExitCommand").toString()); emit log(tr("Running Post-Launch command: %1").arg(postlaunch_cmd));
m_prepostlaunchprocess.start(postlaunch_cmd);
m_prepostlaunchprocess.waitForFinished(); m_prepostlaunchprocess.waitForFinished();
// Flush console window
if (!m_err_leftover.isEmpty())
{
logOutput(m_err_leftover, MessageLevel::PrePost);
m_err_leftover.clear();
}
if (!m_out_leftover.isEmpty())
{
logOutput(m_out_leftover, MessageLevel::PrePost);
m_out_leftover.clear();
}
if (m_prepostlaunchprocess.exitStatus() != NormalExit) if (m_prepostlaunchprocess.exitStatus() != NormalExit)
{ {
emit log(tr("Post-Launch command failed with code %1.\n\n").arg(m_prepostlaunchprocess.exitCode()),
MessageLevel::Error);
emit postlaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(), emit postlaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(),
m_prepostlaunchprocess.exitStatus()); m_prepostlaunchprocess.exitStatus());
} }
else
emit log(tr("Post-Launch command ran successfully.\n\n"));
} }
m_instance->cleanupAfterRun(); m_instance->cleanupAfterRun();
emit ended(m_instance, code, status); emit ended(m_instance, code, status);
@ -178,17 +296,40 @@ void MinecraftProcess::killMinecraft()
void MinecraftProcess::launch() void MinecraftProcess::launch()
{ {
if (!m_instance->settings().get("PreLaunchCommand").toString().isEmpty()) emit log("MultiMC version: " + MMC->version().toString() + "\n\n");
emit log("Minecraft folder is:\n" + workingDirectory() + "\n\n");
QString prelaunch_cmd = m_instance->settings().get("PreLaunchCommand").toString();
if (!prelaunch_cmd.isEmpty())
{ {
m_prepostlaunchprocess.start(m_instance->settings().get("PreLaunchCommand").toString()); // Launch
emit log(tr("Running Pre-Launch command: %1").arg(prelaunch_cmd));
m_prepostlaunchprocess.start(prelaunch_cmd);
// Wait
m_prepostlaunchprocess.waitForFinished(); m_prepostlaunchprocess.waitForFinished();
// Flush console window
if (!m_err_leftover.isEmpty())
{
logOutput(m_err_leftover, MessageLevel::PrePost);
m_err_leftover.clear();
}
if (!m_out_leftover.isEmpty())
{
logOutput(m_out_leftover, MessageLevel::PrePost);
m_out_leftover.clear();
}
// Process return values
if (m_prepostlaunchprocess.exitStatus() != NormalExit) if (m_prepostlaunchprocess.exitStatus() != NormalExit)
{ {
emit log(tr("Pre-Launch command failed with code %1.\n\n").arg(m_prepostlaunchprocess.exitCode()),
MessageLevel::Fatal);
m_instance->cleanupAfterRun(); m_instance->cleanupAfterRun();
emit prelaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(), emit prelaunch_failed(m_instance, m_prepostlaunchprocess.exitCode(),
m_prepostlaunchprocess.exitStatus()); m_prepostlaunchprocess.exitStatus());
return; return;
} }
else
emit log(tr("Pre-Launch command ran successfully.\n\n"));
} }
m_instance->setLastLaunch(); m_instance->setLastLaunch();
@ -221,8 +362,6 @@ void MinecraftProcess::launch()
} }
QString JavaPath = m_instance->settings().get("JavaPath").toString(); QString JavaPath = m_instance->settings().get("JavaPath").toString();
emit log("MultiMC version: " + MMC->version().toString() + "\n\n");
emit log("Minecraft folder is:\n" + workingDirectory() + "\n\n");
emit log("Java path is:\n" + JavaPath + "\n\n"); emit log("Java path is:\n" + JavaPath + "\n\n");
QString allArgs = args.join(", "); QString allArgs = args.join(", ");
emit log("Java Arguments:\n[" + censorPrivateInfo(allArgs) + "]\n\n"); emit log("Java Arguments:\n[" + censorPrivateInfo(allArgs) + "]\n\n");
@ -241,19 +380,3 @@ void MinecraftProcess::launch()
QByteArray bytes = launchScript.toUtf8(); QByteArray bytes = launchScript.toUtf8();
writeData(bytes.constData(), bytes.length()); writeData(bytes.constData(), bytes.length());
} }
MessageLevel::Enum MinecraftProcess::getLevel(const QString &line, MessageLevel::Enum level)
{
if (line.contains("[INFO]") || line.contains("[CONFIG]") || line.contains("[FINE]") ||
line.contains("[FINER]") || line.contains("[FINEST]"))
level = MessageLevel::Message;
if (line.contains("[SEVERE]") || line.contains("[STDERR]"))
level = MessageLevel::Error;
if (line.contains("[WARNING]"))
level = MessageLevel::Warning;
if (line.contains("Exception in thread") || line.contains(" at "))
level = MessageLevel::Fatal;
if (line.contains("[DEBUG]"))
level = MessageLevel::Debug;
return level;
}

View File

@ -31,11 +31,12 @@ enum Enum
{ {
MultiMC, /**< MultiMC Messages */ MultiMC, /**< MultiMC Messages */
Debug, /**< Debug Messages */ Debug, /**< Debug Messages */
Info, /**< Info Messages */ Info, /**< Info Messages */
Message, /**< Standard Messages */ Message, /**< Standard Messages */
Warning, /**< Warnings */ Warning, /**< Warnings */
Error, /**< Errors */ Error, /**< Errors */
Fatal /**< Fatal Errors */ Fatal, /**< Fatal Errors */
PrePost, /**< Pre/Post Launch command output */
}; };
} }
@ -125,9 +126,17 @@ slots:
void finish(int, QProcess::ExitStatus status); void finish(int, QProcess::ExitStatus status);
void on_stdErr(); void on_stdErr();
void on_stdOut(); void on_stdOut();
void on_prepost_stdOut();
void on_prepost_stdErr();
void logOutput(const QStringList &lines,
MessageLevel::Enum defaultLevel = MessageLevel::Message,
bool guessLevel = true, bool censor = true);
void logOutput(QString line,
MessageLevel::Enum defaultLevel = MessageLevel::Message,
bool guessLevel = true, bool censor = true);
private: private:
QString censorPrivateInfo(QString in); QString censorPrivateInfo(QString in);
MessageLevel::Enum getLevel(const QString &message, MessageLevel::Enum defaultLevel); MessageLevel::Enum guessLevel(const QString &message, MessageLevel::Enum defaultLevel);
MessageLevel::Enum getLevel(const QString &levelName);
}; };