Added event logs. Cloese #244

This commit is contained in:
huangyuhui 2018-03-01 00:27:07 +08:00
parent f83777e1d2
commit 1bcef2bab0
18 changed files with 219 additions and 17 deletions

View File

@ -202,8 +202,10 @@ public final class MainPage extends StackPane implements DecoratorPage {
children.add(buildNode(repository, version, GameVersion.minecraftVersion(repository.getVersionJar(version.getId())).orElse("Unknown"))); children.add(buildNode(repository, version, GameVersion.minecraftVersion(repository.getVersionJar(version.getId())).orElse("Unknown")));
} }
JFXUtilities.runInFX(() -> { JFXUtilities.runInFX(() -> {
contentPane.getChildren().setAll(masonryPane); if (profile == repository.getProfile()) {
FXUtils.resetChildren(masonryPane, children); contentPane.getChildren().setAll(masonryPane);
FXUtils.resetChildren(masonryPane, children);
}
}); });
} }

View File

@ -82,7 +82,7 @@ public final class GameAssetDownloadTask extends Task {
AssetObject assetObject = entry.getValue(); AssetObject assetObject = entry.getValue();
String url = dependencyManager.getDownloadProvider().getAssetBaseURL() + assetObject.getLocation(); String url = dependencyManager.getDownloadProvider().getAssetBaseURL() + assetObject.getLocation();
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile())) { if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile())) {
Logging.LOG.log(Level.SEVERE, "Unable to create new file {0}, because parent directory cannot be created", file); Logging.LOG.log(Level.SEVERE, "Unable to create new file " + file + ", because parent directory cannot be created");
continue; continue;
} }
if (file.isDirectory()) if (file.isDirectory())

View File

@ -17,6 +17,8 @@
*/ */
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.ToStringBuilder;
import java.util.Objects; import java.util.Objects;
/** /**
@ -28,13 +30,13 @@ public class Event {
/** /**
* The object on which the Event initially occurred. * The object on which the Event initially occurred.
*/ */
protected transient Object source; protected transient Object source;
/** /**
* Constructs a prototypical Event. * Constructs a prototypical Event.
* *
* @param source The object on which the Event initially occurred. * @param source The object on which the Event initially occurred.
* @throws NullPointerException if source is null. * @throws NullPointerException if source is null.
*/ */
public Event(Object source) { public Event(Object source) {
Objects.requireNonNull(source); Objects.requireNonNull(source);
@ -54,10 +56,10 @@ public class Event {
/** /**
* Returns a String representation of this Event. * Returns a String representation of this Event.
* *
* @return A a String representation of this Event. * @return A a String representation of this Event.
*/ */
public String toString() { public String toString() {
return getClass().getName() + "[source=" + source + "]"; return new ToStringBuilder(this).append("source", source).toString();
} }
private boolean canceled; private boolean canceled;
@ -72,7 +74,6 @@ public class Event {
} }
/** /**
*
* @param canceled new value * @param canceled new value
* @throws UnsupportedOperationException if trying to cancel a non-cancelable event. * @throws UnsupportedOperationException if trying to cancel a non-cancelable event.
*/ */

View File

@ -17,6 +17,8 @@
*/ */
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.Logging;
import java.util.HashMap; import java.util.HashMap;
/** /**
@ -36,6 +38,8 @@ public final class EventBus {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Event.Result fireEvent(Event obj) { public Event.Result fireEvent(Event obj) {
Logging.LOG.info(obj + " gets fired");
return channel((Class<Event>) obj.getClass()).fireEvent(obj); return channel((Class<Event>) obj.getClass()).fireEvent(obj);
} }

View File

@ -17,6 +17,8 @@
*/ */
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.ToStringBuilder;
import java.io.File; import java.io.File;
/** /**
@ -49,4 +51,13 @@ public final class GameJsonParseFailedEvent extends Event {
public String getVersion() { public String getVersion() {
return version; return version;
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("jsonFile", jsonFile)
.append("version", version)
.toString();
}
} }

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.ManagedProcess; import org.jackhuang.hmcl.util.ManagedProcess;
import org.jackhuang.hmcl.util.ToStringBuilder;
/** /**
* This event gets fired when we launch the JVM and it got crashed. * This event gets fired when we launch the JVM and it got crashed.
@ -44,4 +45,12 @@ public class JVMLaunchFailedEvent extends Event {
public ManagedProcess getProcess() { public ManagedProcess getProcess() {
return process; return process;
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("process", process)
.toString();
}
} }

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.util.ToStringBuilder;
/** /**
* This event gets fired when a minecraft version has been loaded. * This event gets fired when a minecraft version has been loaded.
@ -48,4 +49,12 @@ public final class LoadedOneVersionEvent extends Event {
public boolean hasResult() { public boolean hasResult() {
return true; return true;
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("version", version)
.toString();
}
} }

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.ManagedProcess; import org.jackhuang.hmcl.util.ManagedProcess;
import org.jackhuang.hmcl.util.ToStringBuilder;
/** /**
* This event gets fired when a JavaProcess exited abnormally and the exit code is not zero. * This event gets fired when a JavaProcess exited abnormally and the exit code is not zero.
@ -44,4 +45,12 @@ public final class ProcessExitedAbnormallyEvent extends Event {
public ManagedProcess getProcess() { public ManagedProcess getProcess() {
return process; return process;
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("process", process)
.toString();
}
} }

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.event; package org.jackhuang.hmcl.event;
import org.jackhuang.hmcl.util.ManagedProcess; import org.jackhuang.hmcl.util.ManagedProcess;
import org.jackhuang.hmcl.util.ToStringBuilder;
/** /**
* This event gets fired when minecraft process exited successfully and the exit code is 0. * This event gets fired when minecraft process exited successfully and the exit code is 0.
@ -44,4 +45,12 @@ public class ProcessStoppedEvent extends Event {
public ManagedProcess getProcess() { public ManagedProcess getProcess() {
return process; return process;
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("process", process)
.toString();
}
} }

View File

@ -35,4 +35,8 @@ public final class RefreshingVersionsEvent extends Event {
super(source); super(source);
} }
@Override
public boolean hasResult() {
return true;
}
} }

View File

@ -0,0 +1,59 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* 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.event;
import org.jackhuang.hmcl.util.ToStringBuilder;
/**
* This event gets fired when a minecraft version is being removed.
* <br>
* This event is fired on the {@link org.jackhuang.hmcl.event.EventBus#EVENT_BUS}
*
* @author huangyuhui
*/
public class RemoveVersionEvent extends Event {
private final String version;
/**
*
* @param source {@link org.jackhuang.hmcl.game.GameRepository}
* @param version the version id.
*/
public RemoveVersionEvent(Object source, String version) {
super(source);
this.version = version;
}
public String getVersion() {
return version;
}
@Override
public boolean hasResult() {
return true;
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("version", version)
.toString();
}
}

View File

@ -0,0 +1,65 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* 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.event;
import org.jackhuang.hmcl.util.ToStringBuilder;
/**
* This event gets fired when a minecraft version is being removed.
* <br>
* This event is fired on the {@link org.jackhuang.hmcl.event.EventBus#EVENT_BUS}
*
* @author huangyuhui
*/
public class RenameVersionEvent extends Event {
private final String from, to;
/**
*
* @param source {@link org.jackhuang.hmcl.game.GameRepository}
* @param from the version id.
*/
public RenameVersionEvent(Object source, String from, String to) {
super(source);
this.from = from;
this.to = to;
}
public String getFromVersion() {
return from;
}
public String getToVersion() {
return to;
}
@Override
public boolean hasResult() {
return true;
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("source", source)
.append("from", from)
.append("to", to)
.toString();
}
}

View File

@ -24,6 +24,7 @@ import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.Constants; import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.FileUtils; import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.ToStringBuilder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -116,6 +117,9 @@ public class DefaultGameRepository implements GameRepository {
@Override @Override
public boolean renameVersion(String from, String to) { public boolean renameVersion(String from, String to) {
if (EventBus.EVENT_BUS.fireEvent(new RenameVersionEvent(this, from, to)) == Event.Result.DENY)
return false;
try { try {
Version fromVersion = getVersion(from); Version fromVersion = getVersion(from);
File fromDir = getVersionRoot(from); File fromDir = getVersionRoot(from);
@ -145,6 +149,8 @@ public class DefaultGameRepository implements GameRepository {
public boolean removeVersionFromDisk(String id) { public boolean removeVersionFromDisk(String id) {
if (!versions.containsKey(id)) if (!versions.containsKey(id))
return true; return true;
if (EventBus.EVENT_BUS.fireEvent(new RemoveVersionEvent(this, id)) == Event.Result.DENY)
return false;
File file = getVersionRoot(id); File file = getVersionRoot(id);
if (!file.exists()) if (!file.exists())
return true; return true;
@ -201,7 +207,7 @@ public class DefaultGameRepository implements GameRepository {
try { try {
FileUtils.writeText(json, Constants.GSON.toJson(version)); FileUtils.writeText(json, Constants.GSON.toJson(version));
} catch (Exception e) { } catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Ignoring version {0} because wrong id {1} is set and cannot correct it.", new Object[]{id, version.getId()}); Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.");
continue; continue;
} }
} }
@ -217,7 +223,7 @@ public class DefaultGameRepository implements GameRepository {
EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY) EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY)
versions.put(version.getId(), version); versions.put(version.getId(), version);
} catch (VersionNotFoundException e) { } catch (VersionNotFoundException e) {
Logging.LOG.log(Level.WARNING, "Ignoring version {0} because it inherits from a nonexistent version.", version.getId()); Logging.LOG.log(Level.WARNING, "Ignoring version " + version.getId() + " because it inherits from a nonexistent version.");
} }
} }
@ -227,7 +233,9 @@ public class DefaultGameRepository implements GameRepository {
@Override @Override
public void refreshVersions() { public void refreshVersions() {
EventBus.EVENT_BUS.fireEvent(new RefreshingVersionsEvent(this)); if (EventBus.EVENT_BUS.fireEvent(new RefreshingVersionsEvent(this)) == Event.Result.DENY)
return;
Schedulers.newThread().schedule(() -> { Schedulers.newThread().schedule(() -> {
refreshVersionsImpl(); refreshVersionsImpl();
EventBus.EVENT_BUS.fireEvent(new RefreshedVersionsEvent(this)); EventBus.EVENT_BUS.fireEvent(new RefreshedVersionsEvent(this));
@ -337,4 +345,11 @@ public class DefaultGameRepository implements GameRepository {
return getModpackConfiguration(version).exists(); return getModpackConfiguration(version).exists();
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("versions", versions == null ? null : versions.keySet())
.append("baseDirectory", baseDirectory)
.toString();
}
} }

View File

@ -156,7 +156,7 @@ public class Version implements Comparable<Version>, Validation {
// To maximize the compatibility. // To maximize the compatibility.
if (!resolvedSoFar.add(id)) { if (!resolvedSoFar.add(id)) {
Logging.LOG.log(Level.WARNING, "Found circular dependency versions: {0}", resolvedSoFar); Logging.LOG.log(Level.WARNING, "Found circular dependency versions: " + resolvedSoFar);
return this; return this;
} }
@ -233,6 +233,11 @@ public class Version implements Comparable<Version>, Validation {
return id.compareTo(o.id); return id.compareTo(o.id);
} }
@Override
public String toString() {
return new ToStringBuilder(this).append("id", id).toString();
}
@Override @Override
public void validate() throws JsonParseException { public void validate() throws JsonParseException {
if (StringUtils.isBlank(id)) if (StringUtils.isBlank(id))

View File

@ -119,7 +119,7 @@ public class FileDownloadTask extends Task {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
URL currentURL = url; URL currentURL = url;
Logging.LOG.log(Level.FINER, "Downloading {0} to {1}", new Object[] { currentURL, file }); Logging.LOG.log(Level.FINER, "Downloading " + currentURL + " to " + file);
Exception exception = null; Exception exception = null;
for (int repeat = 0; repeat < retry; repeat++) { for (int repeat = 0; repeat < retry; repeat++) {

View File

@ -84,7 +84,7 @@ public final class GetTask extends TaskResult<String> {
Exception exception = null; Exception exception = null;
for (int time = 0; time < retry; ++time) { for (int time = 0; time < retry; ++time) {
if (time > 0) if (time > 0)
Logging.LOG.log(Level.WARNING, "Failed to download, repeat times: {0}", time); Logging.LOG.log(Level.WARNING, "Failed to download, repeat times: " + time);
try { try {
updateProgress(0); updateProgress(0);
HttpURLConnection conn = NetworkUtils.createConnection(url, proxy); HttpURLConnection conn = NetworkUtils.createConnection(url, proxy);

View File

@ -134,7 +134,7 @@ public final class TaskExecutor {
task.setState(Task.TaskState.READY); task.setState(Task.TaskState.READY);
if (task.getSignificance().shouldLog()) if (task.getSignificance().shouldLog())
Logging.LOG.log(Level.FINE, "Executing task: {0}", task.getName()); Logging.LOG.log(Level.FINE, "Executing task: " + task.getName());
taskListeners.forEach(it -> it.onReady(task)); taskListeners.forEach(it -> it.onReady(task));
@ -176,7 +176,7 @@ public final class TaskExecutor {
flag = true; flag = true;
if (task.getSignificance().shouldLog()) { if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINER, "Task finished: {0}", task.getName()); Logging.LOG.log(Level.FINER, "Task finished: " + task.getName());
} }
task.onDone().fireEvent(new TaskEvent(this, task, false)); task.onDone().fireEvent(new TaskEvent(this, task, false));

View File

@ -63,7 +63,7 @@ public final class Logging {
String date = format.format(new Date(record.getMillis())); String date = format.format(new Date(record.getMillis()));
String log = String.format("[%s] [%s.%s/%s] %s%n", String log = String.format("[%s] [%s.%s/%s] %s%n",
date, record.getSourceClassName(), record.getSourceMethodName(), date, record.getSourceClassName(), record.getSourceMethodName(),
record.getLevel().getName(), MessageFormat.format(record.getMessage(), record.getParameters()) record.getLevel().getName(), record.getMessage()
); );
ByteArrayOutputStream builder = new ByteArrayOutputStream(); ByteArrayOutputStream builder = new ByteArrayOutputStream();
if (record.getThrown() != null) if (record.getThrown() != null)