diff --git a/core/src/main/java/com/gitee/drinkjava2/frog/Env.java b/core/src/main/java/com/gitee/drinkjava2/frog/Env.java index 901dcc659edb01cb2ab60cbb9fb01785c5b939e0..d71cbce65aebf279a853b8e8320907af58c2623b 100644 --- a/core/src/main/java/com/gitee/drinkjava2/frog/Env.java +++ b/core/src/main/java/com/gitee/drinkjava2/frog/Env.java @@ -14,6 +14,7 @@ import com.gitee.drinkjava2.frog.egg.FrogEggTool; import com.gitee.drinkjava2.frog.objects.EnvObject; import com.gitee.drinkjava2.frog.objects.Food; import com.gitee.drinkjava2.frog.objects.Material; +import com.gitee.drinkjava2.frog.util.Logger; import com.gitee.drinkjava2.frog.util.RandomUtils; /** @@ -91,11 +92,11 @@ public class Env extends JPanel { public static List frog_eggs = new ArrayList<>(); // 这里存放新建或从磁盘载入上轮下的蛋,每个蛋可能生成几个青蛙, - public static EnvObject[] things = new EnvObject[]{ };// 所有外界物体,如食物、字母测试工具都放在这个things里面 + public static EnvObject[] things = new EnvObject[]{ };// 所有外界物体,如食物、字母测试工具都放在这个things里面 static { - System.out.println("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8 - System.out.println("脑图快捷键: T:顶视 F:前视 L:左视 R:右视 X:斜视 方向键:剖视 空格:暂停 鼠标:缩放旋转平移"); + Logger.info("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8 + Logger.info("脑图快捷键: T:顶视 F:前视 L:左视 R:右视 X:斜视 方向键:剖视 空格:暂停 鼠标:缩放旋转平移"); if (DELETE_FROG_EGGS) FrogEggTool.deleteEggs(); } diff --git a/core/src/main/java/com/gitee/drinkjava2/frog/egg/FrogEggTool.java b/core/src/main/java/com/gitee/drinkjava2/frog/egg/FrogEggTool.java index 99f06ac60cc21da4d4ea254d483479d652f266c2..16ce8ab5822c7f9f71378d3e247b383367d7e886 100644 --- a/core/src/main/java/com/gitee/drinkjava2/frog/egg/FrogEggTool.java +++ b/core/src/main/java/com/gitee/drinkjava2/frog/egg/FrogEggTool.java @@ -24,6 +24,7 @@ import com.gitee.drinkjava2.frog.Application; import com.gitee.drinkjava2.frog.Env; import com.gitee.drinkjava2.frog.Frog; import com.gitee.drinkjava2.frog.util.LocalFileUtils; +import com.gitee.drinkjava2.frog.util.Logger; /** * FrogEggTool save/load frog eggs to file @@ -49,18 +50,16 @@ public class FrogEggTool { Env.frog_eggs.clear(); for (int i = 0; i < Env.FROG_EGG_QTY; i++) Env.frog_eggs.add(new Egg(Env.frogs.get(i))); - System.out.print("Fist frog energy=" + first.energy); - System.out.print(", Last frog energy=" + last.energy); + Logger.info("Fist frog energy={}, Last frog energy={}", first.energy, last.energy); if (Env.SAVE_EGGS_FILE) { FileOutputStream fo = new FileOutputStream(Application.CLASSPATH + "frog_eggs.ser"); ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(Env.frog_eggs); so.close(); - System.out.print(". Saved " + Env.frog_eggs.size() + " eggs to file '" + Application.CLASSPATH + "frog_eggs.ser'"); + Logger.info(". Saved {} eggs to file '{}frog_eggs.ser'", Env.frog_eggs.size(), Application.CLASSPATH); } - System.out.println(); } catch (IOException e) { - System.out.println(e); + Logger.error(e); } } @@ -78,7 +77,7 @@ public class FrogEggTool { } public static void deleteEggs() { - System.out.println("Delete exist egg file: '" + Application.CLASSPATH + "frog_eggs.ser'"); + Logger.info("Delete exist egg file: '{}frog_eggs.ser'", Application.CLASSPATH); LocalFileUtils.deleteFile(Application.CLASSPATH + "frog_eggs.ser"); } @@ -92,7 +91,7 @@ public class FrogEggTool { FileInputStream eggsFile = new FileInputStream(Application.CLASSPATH + "frog_eggs.ser"); ObjectInputStream eggsInputStream = new ObjectInputStream(eggsFile); Env.frog_eggs = (List) eggsInputStream.readObject(); - System.out.println("Loaded " + Env.frog_eggs.size() + " eggs from file '" + Application.CLASSPATH + Logger.info("Loaded " + Env.frog_eggs.size() + " eggs from file '" + Application.CLASSPATH + "frog_eggs.ser" + "'.\n"); eggsInputStream.close(); } catch (Exception e) { @@ -105,7 +104,7 @@ public class FrogEggTool { //TODO Env.frog_eggs.add(egg); } - System.out.println("Fail to load frog egg file '" + Application.CLASSPATH + "frog_eggs.ser" + "', created " + Logger.info("Fail to load frog egg file '" + Application.CLASSPATH + "frog_eggs.ser" + "', created " + Env.frog_eggs.size() + " eggs to do test."); } diff --git a/core/src/main/java/com/gitee/drinkjava2/frog/util/Logger.java b/core/src/main/java/com/gitee/drinkjava2/frog/util/Logger.java new file mode 100644 index 0000000000000000000000000000000000000000..b38a87522c185a08a075a9cb389851564cde4b6d --- /dev/null +++ b/core/src/main/java/com/gitee/drinkjava2/frog/util/Logger.java @@ -0,0 +1,174 @@ +package com.gitee.drinkjava2.frog.util; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * @Description: 简版控制台日志打印 + * @Date: 2021/12/07 + */ +public class Logger { + private static final String LEV_EL = "debug"; + private static final int LEVEL_INT; + private static final BlockingQueue LOG_LIST = new ArrayBlockingQueue<>(256); + private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"); + private static final OutputStream OUTPUT_STREAM = System.out; + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + private static final String DELIM_STR = "{}"; + private static final String TAB = "\tat"; + private static final Map LEVEL_MAP = new HashMap<>(); + + static { + LEVEL_MAP.put("DEBUG", 1); + LEVEL_MAP.put("INFO", 2); + LEVEL_MAP.put("WARN", 2); + LEVEL_MAP.put("ERROR", 2); + LEVEL_INT = LEVEL_MAP.get(LEV_EL.toUpperCase()); + new Thread(() -> { + while (true) { + try { + outPutConsole(LOG_LIST.take()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }).start(); + } + + public static void debug(String msg) { + printLog(LEVEL_MAP.get("DEBUG"), msg); + } + + public static void debug(String msg, Object params) { + printLog(LEVEL_MAP.get("DEBUG"), msg, params); + } + + public static void debug(String msg, Object... params) { + printLog(LEVEL_MAP.get("DEBUG"), msg, params); + } + + public static void info(String msg) { + printLog(LEVEL_MAP.get("INFO"), msg); + } + + public static void info(String msg, Object params) { + printLog(LEVEL_MAP.get("INFO"), msg, params); + } + + public static void info(String msg, Object... params) { + printLog(LEVEL_MAP.get("INFO"), msg, params); + } + + public static void warn(String msg) { + printLog(LEVEL_MAP.get("WARN"), msg); + } + + public static void warn(String msg, Object params) { + printLog(LEVEL_MAP.get("WARN"), msg, params); + } + + public static void warn(String msg, Object... params) { + printLog(LEVEL_MAP.get("WARN"), msg, params); + } + + public static void error(String msg) { + printLog(LEVEL_MAP.get("ERROR"), msg); + } + + public static void error(String msg, Object params) { + printLog(LEVEL_MAP.get("ERROR"), msg, params); + } + + public static void error(String msg, Object... params) { + printLog(LEVEL_MAP.get("ERROR"), msg, params); + } + + public static void error(Object param) { + printLog(LEVEL_MAP.get("ERROR"), "", param); + } + + private static void printLog(int levelInt, String msg, Object... params) { + try { + if (levelInt >= LEVEL_INT) { + LOG_LIST.put(generateMsg(getLevelStr(levelInt), msg, params)); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private static String generateMsg(String levelStr, String msg, Object... params) { + StackTraceElement stack = Thread.currentThread().getStackTrace()[4]; + String s = "{} [{}][{}#{} {}] - " + msg + LINE_SEPARATOR; + Object[] args = new Object[5 + params.length]; + args[0] = FORMAT.format(System.currentTimeMillis()); + args[1] = levelStr; + args[2] = stack.getClassName(); + args[3] = stack.getMethodName(); + args[4] = stack.getLineNumber(); + + Throwable throwable = null; + if (params.length > 0) { + final Object lastEntry = params[params.length - 1]; + if (lastEntry instanceof Throwable) { + throwable = (Throwable) lastEntry; + System.arraycopy(params, 0, args, 5, params.length - 1); + } else { + System.arraycopy(params, 0, args, 5, params.length); + } + } + return formatMsg(s, throwable, args); + } + + private static String formatMsg(String msg, Throwable throwable, Object... params) { + StringBuilder sb = new StringBuilder(); + int s; + int i = 0; + for (Object o : params) { + s = msg.indexOf(DELIM_STR, i); + if (s > -1) { + sb.append(msg, i, s).append(o); + i = s + 2; + } + } + sb.append(msg, i, msg.length()); + if (null != throwable) { + sb.append(throwable).append(LINE_SEPARATOR); + StackTraceElement[] stack = throwable.getStackTrace(); + for (StackTraceElement element : stack) { + sb.append(TAB).append(element).append(LINE_SEPARATOR); + } + } + return sb.toString(); + } + + private static void outPutConsole(String msg) { + try { + OUTPUT_STREAM.write(msg.getBytes(StandardCharsets.UTF_8)); + OUTPUT_STREAM.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static String getLevelStr(int levelInt) { + switch (levelInt) { + case 1: + return "DEBUG"; + case 2: + return "INFO"; + case 3: + return "WARN"; + case 4: + return "ERROR"; + default: + throw new IllegalStateException("Level " + levelInt + " is unknown."); + } + } +}