Skip to content

Commit

Permalink
Removed funtionalities of Log4jHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghongxun committed Nov 4, 2017
1 parent 47d8b59 commit 3721842
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 143 deletions.
4 changes: 2 additions & 2 deletions HMCL/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def buildnumber = System.getenv("TRAVIS_BUILD_NUMBER")
if (buildnumber == null)
buildnumber = System.getenv("BUILD_NUMBER")
if (buildnumber == null)
buildnumber = "42"
buildnumber = "51"

def versionroot = System.getenv("VERSION_ROOT")
if (versionroot == null)
versionroot = "2.7.8"
versionroot = "2.7.9"

String mavenGroupId = 'HMCL'
String mavenVersion = versionroot + '.' + buildnumber
Expand Down
20 changes: 9 additions & 11 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import org.jackhuang.hmcl.api.ui.Theme;
import org.jackhuang.hmcl.api.ui.TopTabPage;


/**
*
* @author huangyuhui
Expand Down Expand Up @@ -256,7 +255,7 @@ private void initComponents() {

realPanel.setBounds(1, 0, 800, 511);
add(realPanel);

reloadColor(Settings.getInstance().getTheme());
}

Expand Down Expand Up @@ -396,11 +395,10 @@ private void initBorderColor() {
public void reloadColor(Theme t) {
if (isShowedMessage)
return;
for (Map.Entry<String, String> entry : t.settings.entrySet()) {
for (Map.Entry<String, String> entry : t.settings.entrySet())
if (entry.getValue().startsWith("#"))
UIManager.put(entry.getKey(), GraphicsUtils.getWebColor(entry.getValue()));
}


initBorderColor();
if (border != null)
border.setColor(borderColor);
Expand All @@ -423,14 +421,14 @@ private void paintImpl(Graphics g) {

@Override
public void paint(Graphics g) {
if (!enableShadow)
int off = enableShadow ? 16 : 0;
int width = this.getWidth();
int height = this.getHeight();
int contentWidth = width - off - off;
int contentHeight = height - off - off;
if (!enableShadow || contentWidth <= 0 || contentHeight <= 0)
paintImpl(g);
else {
int off = enableShadow ? 16 : 0;
int width = this.getWidth();
int height = this.getHeight();
int contentWidth = width - off - off;
int contentHeight = height - off - off;
BufferedImage contentImage = new BufferedImage(contentWidth,
contentHeight, Transparency.OPAQUE);
Graphics2D contentG2d = contentImage.createGraphics();
Expand Down
89 changes: 10 additions & 79 deletions HMCL/src/main/java/org/jackhuang/hmcl/util/Log4jHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,101 +17,51 @@
*/
package org.jackhuang.hmcl.util;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.xml.parsers.ParserConfigurationException;
import org.jackhuang.hmcl.api.HMCLApi;
import org.jackhuang.hmcl.api.HMCLog;
import org.jackhuang.hmcl.api.event.process.JavaProcessStoppedEvent;
import org.jackhuang.hmcl.api.func.Consumer;
import org.jackhuang.hmcl.ui.LogWindow;
import org.jackhuang.hmcl.util.log.Level;
import org.jackhuang.hmcl.util.sys.ProcessMonitor;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
*
* @author huang
*/
public class Log4jHandler extends Thread implements Consumer<JavaProcessStoppedEvent> {

XMLReader reader;
ProcessMonitor monitor;
PipedInputStream inputStream;
PipedOutputStream outputStream;
List<Pair<String, String>> forbiddenTokens = new LinkedList<>();
AtomicBoolean interrupted = new AtomicBoolean(false);
ExecutorService executorService = Executors.newSingleThreadExecutor();
boolean enabled = true;

public Log4jHandler(ProcessMonitor monitor, PipedOutputStream outputStream, boolean enabled) throws ParserConfigurationException, IOException, SAXException {
reader = XMLReaderFactory.createXMLReader();
inputStream = new PipedInputStream(outputStream);
this.outputStream = outputStream;
public Log4jHandler(ProcessMonitor monitor, PipedOutputStream outputStream, boolean enabled) {
this.monitor = monitor;
this.enabled = enabled;

HMCLApi.EVENT_BUS.channel(JavaProcessStoppedEvent.class).register((Consumer<JavaProcessStoppedEvent>) this);
}

public void addForbiddenToken(String token, String replacement) {
forbiddenTokens.add(new Pair<>(token, replacement));
}

@Override
public void run() {
try {
setName("log4j-handler");
if (enabled)
newLogLine("<output>");
reader.setContentHandler(new Log4jHandlerImpl());
reader.parse(new InputSource(inputStream));
} catch (InterruptedIOException e) {
interrupted.set(true);
} catch (SAXException | IOException e) {
throw new Error(e);
} finally {
executorService.shutdown();
}
}

@Override
public void accept(JavaProcessStoppedEvent t) {
if (!interrupted.get())
return;
if (t.getSource() == monitor) {
try {
executorService.submit(() -> {
if (!interrupted.get()) {
if (enabled) {
Future f = newLogLine("</output>");
if (f != null)
f.get();
}
outputStream.close();
join();
}
return null;
}).get();
} catch (Exception e) {
HMCLog.err("Please contact author", e);
} finally {
executorService.shutdown();
}
}
}

/**
Expand All @@ -126,32 +76,12 @@ public void accept(JavaProcessStoppedEvent t) {
* @param content The content to be written to the log
*/
public Future newLogLine(String content) {
try {
return executorService.submit(() -> {
if (enabled)
try {
String log = content;
if (!log.trim().startsWith("<")) { // without logging configuration.
log = "<![CDATA[" + log.replace("]]>", "") + "]]>";
}
outputStream.write(log
.replace("log4j:Event", "log4j_Event")
.replace("log4j:Message", "log4j_Message")
.replace("log4j:Throwable", "log4j_Throwable")
.getBytes());
outputStream.flush();
} catch (IOException ignore) { // won't happen
throw new Error(ignore);
}
else
printlnImpl(content, Level.guessLevel(content));
});
} catch (RejectedExecutionException e) {
return null;
}
printlnImpl(content, Level.guessLevel(content));
return null;
}

class Log4jHandlerImpl extends DefaultHandler {

private final SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");

String date = "", thread = "", logger = "";
Expand All @@ -168,7 +98,7 @@ public void startElement(String uri, String localName, String qName, Attributes
date = df.format(d);
try {
l = Level.valueOf(attributes.getValue("level"));
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
l = Level.INFO;
}
thread = attributes.getValue("thread");
Expand Down Expand Up @@ -197,7 +127,8 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String line = new String(ch, start, length);
if (line.trim().isEmpty()) return;
if (line.trim().isEmpty())
return;
if (readingMessage)
message.append(line).append(C.LINE_SEPARATOR);
else
Expand All @@ -208,7 +139,7 @@ public void println(String message, Level l) {
printlnImpl(message, l);
}
}

private void printlnImpl(String message, Level l) {
for (Pair<String, String> entry : forbiddenTokens)
message = message.replace(entry.key, entry.value);
Expand Down
4 changes: 2 additions & 2 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/StrUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public static boolean equalsOne(String base, String... a) {
return false;
}

public static boolean containsOne(List<String> base, List<String> match, Predicate<String> pred) {
public static boolean containsOne(Collection<String> base, String... match) {
for (String a : base)
for (String b : match)
if (pred.apply(a) && a.toLowerCase().contains(b.toLowerCase()))
if (a.toLowerCase().contains(b.toLowerCase()))
return true;
return false;
}
Expand Down
79 changes: 79 additions & 0 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/sys/ExitWaiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 huangyuhui <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util.sys;

import java.util.Collection;
import org.jackhuang.hmcl.api.HMCLApi;
import org.jackhuang.hmcl.api.IProcess;
import org.jackhuang.hmcl.api.event.process.JVMLaunchFailedEvent;
import org.jackhuang.hmcl.api.event.process.JavaProcessExitedAbnormallyEvent;
import org.jackhuang.hmcl.api.event.process.JavaProcessStoppedEvent;
import org.jackhuang.hmcl.util.CollectionUtils;
import org.jackhuang.hmcl.util.StrUtils;
import org.jackhuang.hmcl.util.log.Level;

/**
*
* @author huang
*/
public class ExitWaiter extends Thread {

private final ProcessMonitor monitor;
private final IProcess process;
private final Runnable callback;

/**
* Constructor
* @param monitor
* @param callback nullable, will be called when process exited.
*/
public ExitWaiter(ProcessMonitor monitor, Runnable callback) {
this.monitor = monitor;
this.process = monitor.getProcess();
this.callback = callback;

setName("exit-waitor");
}

@Override
public void run() {
try {
int exitCode = process.getRawProcess().waitFor();
monitor.errorThread.join();
monitor.inputThread.join();

Collection<String> errorLines = CollectionUtils.filter(process.getStdOutLines(), str -> Level.isError(Level.guessLevel(str)));

// LaunchWrapper will terminate the application with exit code 0, though this is error state.
if (exitCode != 0 || StrUtils.containsOne(errorLines, "Unable to launch"))
HMCLApi.EVENT_BUS.fireChannel(new JavaProcessExitedAbnormallyEvent(monitor, process));
else if (exitCode != 0 && StrUtils.containsOne(errorLines,
"Could not create the Java Virtual Machine.",
"Error occurred during initialization of VM",
"A fatal exception has occurred. Program will exit.",
"Unable to launch"))
HMCLApi.EVENT_BUS.fireChannel(new JVMLaunchFailedEvent(monitor, process));
else
HMCLApi.EVENT_BUS.fireChannel(new JavaProcessStoppedEvent(this, process));

if (callback != null)
callback.run();
} catch (InterruptedException ex) {
}
}
}
Loading

0 comments on commit 3721842

Please sign in to comment.