Skip to content

Commit d96a22f

Browse files
committed
pass IO tests; succeed in saving log file; ready to upgrade
1 parent f73e902 commit d96a22f

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/main/java/cn/rocket/assaignmark/LocalURL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* 程序中存放资源地址的工具类
1010
*
1111
* @author Rocket
12-
* @version 1.0.8
12+
* @version 1.1.8
1313
* @since 0.9.8
1414
*/
1515
public final class LocalURL {

src/main/java/cn/rocket/assaignmark/core/MarkTable.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ protected MarkTable(String wbPath, AMEventHandler handler, String outputPath,
113113
interrupt(true);
114114
}
115115

116+
117+
//TODO pom.xml, README.md版本更新
116118
/**
117119
* 构造一个分数表实例,如果你想单独使用它的话。
118120
* <p>

src/main/java/cn/rocket/assaignmark/gui/Controller.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.nio.file.Files;
3333
import java.nio.file.Path;
34-
import java.nio.file.Paths;
34+
import java.text.SimpleDateFormat;
3535
import java.util.Date;
3636
import java.util.Properties;
3737

@@ -192,9 +192,7 @@ public void startM() {
192192
new AMFactory(
193193
atField.getText(), mtField.getText(), new GUIEventHandler(this), outField.getText()
194194
).work();
195-
} catch (RuntimeException e) {
196-
if (!(e.getCause() instanceof InterruptedException))
197-
throw e;
195+
} catch (Exception ignored) {
198196
}
199197
}, "Assigning Task Thread");
200198
// 在运行任务时按关闭会先终止任务线程
@@ -243,24 +241,35 @@ public void handle(AMEvent event, String msg) {
243241
alert.show();
244242

245243
} else if (index <= AMEvent.getLastEvent().getIndex()) {
246-
String message = Processor.MSG_ARR[event.ordinal()];
244+
String message = Processor.MSG_ARR[event.ordinal()]; // 事件信息
247245
boolean unexpected = false;
248246
if (msg != null) {
249247
message += "\n";
250-
if (msg.startsWith(AMEvent.ERR_FAILED_TO_CLOSE.toString()) || event == AMEvent.ERR_UNEXPECTED) {
248+
boolean fail;
249+
if ((fail = msg.startsWith(AMEvent.ERR_FAILED_TO_CLOSE.toString())) || event == AMEvent.ERR_UNEXPECTED) {
251250
unexpected = true;
252-
message += msg.substring(0, msg.indexOf('\n',
253-
AMEvent.ERR_FAILED_TO_CLOSE.toString().length() + 2)); // ERR_FAILED_TO_CLOSE\n_
254-
String name = new Date().toString();
255-
Path path = Paths.get(LocalURL.JAR_PARENT_PATH, "/", name, ".txt");
256-
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
257-
writer.write(msg);
251+
message += msg.substring(0, msg.indexOf('\n', fail ? // 截断事件栈帧
252+
AMEvent.ERR_FAILED_TO_CLOSE.toString().length() + 2 : 0)); // if fail -> ERR_FAILED_TO_CLOSE\n_
253+
String name = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss").format(new Date()); // 生成文件名
254+
Path path = null;
255+
try {
256+
path = new File(LocalURL.JAR_PARENT_PATH + name + ".txt").getCanonicalFile().toPath();
258257
} catch (IOException e) {
259-
LogManager.getRootLogger().error("Can't write error log out!");
258+
LogManager.getRootLogger().error("Failed to get file name to save: " +
259+
e.getLocalizedMessage());
260260
}
261-
//TODO IOException 测试
262-
message += "\n发生意料之外的异常,已保存到jar路径下的文件中," +
263-
"按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue";
261+
if (path != null) {
262+
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
263+
writer.write(msg);
264+
} catch (IOException e) {
265+
LogManager.getRootLogger().error("Can't write error log out: " +
266+
e.getLocalizedMessage());
267+
}
268+
message += "\n发生意料之外的异常,已保存到jar路径下的文件中," +
269+
"按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue";
270+
} else
271+
message += "\n发生意料之外的异常," +
272+
"按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue";
264273
} else
265274
message += msg;
266275
}
@@ -274,7 +283,7 @@ public void handle(AMEvent event, String msg) {
274283
alert.close();
275284
};
276285

277-
alert.setEventHandler(handler, null);
286+
alert.setEventHandler(handler, null); // handler默认为null!
278287
alert.show();
279288
}
280289
});

0 commit comments

Comments
 (0)