Skip to content

Commit

Permalink
update Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Apr 22, 2023
1 parent df56b70 commit 6046369
Show file tree
Hide file tree
Showing 20 changed files with 257 additions and 75 deletions.
17 changes: 17 additions & 0 deletions src/main/java/moe/dituon/petpet/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package moe.dituon.petpet;

import moe.dituon.petpet.server.WebServer;
import moe.dituon.petpet.share.BaseLogger;
import moe.dituon.petpet.share.BasePetService;
import moe.dituon.petpet.websocket.gocq.GoCQPetpet;

import java.util.Arrays;
Expand All @@ -9,6 +11,21 @@
public class Main {
public static void main(String[] args) {
System.setProperty("sun.java2d.opengl", "true");
BaseLogger.getInstance().info("\u001B[95m\n\n" +
" ▄▀▄ ▄▀▄\n" +
" ▄█░░▀▀▀▀▀░░█▄\n" +
" ▄▄ █░░░░░░░░░░░█ ▄▄\n" +
" █▄▄█ █░░▀░░┬░░▀░░█ █▄▄█\n" +
"█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█\n" +
"█ _ _ █\n" +
"█ _ __ ___ | |_ _ __ ___ | |_ █\n" +
"█ | '_ \\ / _ \\| __|| '_ \\ / _ \\| __| █\n" +
"█ | |_) || __/| |_ | |_) || __/| |_ █\n" +
"█ | .__/ \\___| \\__|| .__/ \\___| \\__| █\n" +
"█ |_| |_| █\n" +
"█ █\n" +
"█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█ v" +
BasePetService.VERSION + "\n");
if (args.length == 0) {
WebServer server = new WebServer();
return;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/moe/dituon/petpet/mirai/MiraiPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void readConfigByPluginAutoSave() {
@Override
public void readData(File dir){
if (dir.listFiles() == null) {
System.out.println(autoUpdate ?
MiraiPetpet.LOGGER.info(autoUpdate ?
"o((>ω< ))o 你这头懒猪, 没有下载petpet模板!\n\\^o^/ 还好我冰雪聪明, 帮你自动更新了⭐" :
"(゚Д゚*)ノ 没有petpet模板! 你自己手动更新吧x\n(☆-v-) 笨蛋! 让你不开自动更新⭐");
return;
Expand Down Expand Up @@ -114,15 +114,13 @@ public void sendImage(Group group, Member from, Member to, boolean random) {
/**
* 用key发送图片(无otherText)
*/
@Deprecated
public void sendImage(Group group, Member from, Member to, String key) {
sendImage(group, from, to, key, null);
}

/**
* 用key发送图片,指定otherText
*/
@Deprecated
public void sendImage(Group group, Member from, Member to, String key, String otherText) {
TextExtraData textExtraData = new TextExtraData(
from.getNameCard().isEmpty() ? from.getNick() : from.getNameCard(),
Expand Down Expand Up @@ -151,8 +149,7 @@ public void sendImage(Group group, String key,
).getFirst(), group)
);
} catch (Exception ex) {
System.out.println("发送图片时出错:" + ex.getMessage());
ex.printStackTrace();
MiraiPetpet.LOGGER.error("发送图片时出错: " + ex.getMessage(), ex);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/moe/dituon/petpet/mirai/MiraiPetpet.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package moe.dituon.petpet.mirai;

import kotlin.Pair;
import moe.dituon.petpet.plugin.*;
import moe.dituon.petpet.plugin.Cooler;
import moe.dituon.petpet.plugin.DataUpdater;
import moe.dituon.petpet.plugin.PluginRequestParser;
import moe.dituon.petpet.share.BaseConfigFactory;
import moe.dituon.petpet.share.BaseLogger;
import moe.dituon.petpet.share.BasePetService;
import moe.dituon.petpet.share.TextExtraData;
import net.mamoe.mirai.Bot;
Expand All @@ -25,6 +28,7 @@

public final class MiraiPetpet extends JavaPlugin {
public static final MiraiPetpet INSTANCE = new MiraiPetpet();
public static BaseLogger LOGGER = BaseLogger.getInstance();
private static List<Long> disabledGroup;
public static MiraiPetService service;
public static File dataFolder;
Expand All @@ -42,17 +46,19 @@ private MiraiPetpet() {
.name("PetPet")
.author("Dituon")
.build());
BaseLogger.getInstance().setLogger(new BaseLogger.MiraiUtilLogger(getLogger()));
service = new MiraiPetService();
}

@Override
public void onEnable() {
System.setProperty("sun.java2d.opengl", "true");
// BaseLogger.getInstance().setLogger(new BaseLogger.MiraiUtilLogger(getLogger()));
try {
reloadPluginConfig(MiraiPluginConfig.INSTANCE);
service.readConfigByPluginAutoSave();
} catch (NoClassDefFoundError ignored) {
getLogger().error("Mirai 2.11.0 提供了新的 JavaAutoSaveConfig 方法, 请更新Mirai版本至 2.11.0 (不是2.11.0-M1)\n使用旧版本将无法配置config");
LOGGER.warning("Mirai 2.11.0 提供了新的 JavaAutoSaveConfig 方法, 请更新Mirai版本至 2.11.0 (不是2.11.0-M1)\n使用旧版本将无法配置config");
}

dataFolder = getDataFolder();
Expand All @@ -65,7 +71,7 @@ public void onEnable() {
}).start();
disabledGroup = service.disabledGroups;

getLogger().info("\n\n" +
LOGGER.info("\u001B[95m\n\n" +
" ██████╗ ███████╗████████╗██████╗ ███████╗████████╗\n" +
" ██╔══██╗██╔════╝╚══██╔══╝██╔══██╗██╔════╝╚══██╔══╝\n" +
" ██████╔╝█████╗ ██║ ██████╔╝█████╗ ██║ \n" +
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/moe/dituon/petpet/plugin/DataUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public void autoUpdate() {
}

public void updateData() {
System.out.println("开始更新PetData");
PluginPetService.LOGGER.info("开始更新PetData");
UpdateIndex index = UpdateIndex.parse(
Objects.requireNonNull(getUrlText(repositoryUrl + "/index.json")));
List<String> newPetList = index.getDataList();
for (String pet : newPetList) {
if (MiraiPetpet.service.getDataMap().containsKey(pet)
|| MiraiPetpet.service.disabledKey.contains(pet)) continue;
if (!saveAs(pet, "data.json")) {
System.out.println("无法从远程仓库下载PetData: " + getRepositoryFileUrl(pet, "data.json"));
PluginPetService.LOGGER.warning("无法从远程仓库下载PetData: " + getRepositoryFileUrl(pet, "data.json"));
break;
}
short i = 0;
while (saveAs(pet, i + ".png")) i++;
System.out.println("PetData/" + pet + "下载成功 (length:" + i + ')');
PluginPetService.LOGGER.info("PetData/" + pet + "下载成功 (length:" + i + ')');
}

String fontsPath = REPO_DATA_PATH + BasePetService.FONTS_FOLDER;
Expand All @@ -55,26 +55,28 @@ public void updateData() {
for (String font : index.getFontList()) {
if (localFonts.contains(font)) continue;
if (!saveAs(BasePetService.FONTS_FOLDER, font)) {
System.out.println("无法从远程仓库下载PetFont: " + getRepositoryFileUrl(BasePetService.FONTS_FOLDER, font));
PluginPetService.LOGGER.warning("无法从远程仓库下载PetFont: " + getRepositoryFileUrl(BasePetService.FONTS_FOLDER, font));
return;
}
System.out.println("PetFont/" + font + "下载成功");
PluginPetService.LOGGER.info("PetFont/" + font + "下载成功");
}

System.out.println("PetData更新完毕, 正在重新加载");
PluginPetService.LOGGER.info("PetData更新完毕, 正在重载");
MiraiPetpet.service.readData(MiraiPetpet.dataFolder);
}

public boolean checkUpdate() {
UpdateIndex update = UpdateIndex.parse(
Objects.requireNonNull(getUrlText(MiraiPetpet.service.repositoryUrl + "/index.json")));
if (BasePetService.VERSION != update.getVersion())
System.out.println("PetpetPlugin可更新到最新版本: " + update.getVersion() +
PluginPetService.LOGGER.info("PetpetPlugin可更新到最新版本: " + update.getVersion() +
" (当前版本 " + BasePetService.VERSION + ") 要养成经常更新的好习惯哦 (*/ω\*)");
for (String pet : update.getDataList()) {
if (MiraiPetpet.service.getDataMap().containsKey(pet)
|| MiraiPetpet.service.disabledKey.contains(pet)) continue;
System.out.println("发现新增PetData");
if (
MiraiPetpet.service.getDataMap().containsKey(pet)
|| MiraiPetpet.service.disabledKey.contains(pet)
) continue;
PluginPetService.LOGGER.info("发现新增PetData");
return false;
}
return true;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/moe/dituon/petpet/plugin/PluginPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Objects;

public class PluginPetService extends BasePetService {

public String command = "pet";
public String commandHead = "";
public boolean respondReply = true;
Expand Down Expand Up @@ -39,19 +38,19 @@ public void readPluginServiceConfig(PluginServiceConfig config) {
readBaseServiceConfig(config.toBaseServiceConfig());

if (super.quality < 1 || super.quality >= 49) {
System.out.println(
MessageFormat.format("Petpet Plugin 的GIF质量参数范围为 1-49 (1为最佳), 你提供的质量参数为{0}, 已自动更改为默认值5", quality)
LOGGER.warning(
MessageFormat.format("Petpet Plugin 的GIF质量参数范围为 1-49 (1为最佳), 提供的质量参数为{0}, 已自动更改为默认值5", quality)
);
super.quality = 5;
}

System.out.println("Petpet GifMakerThreadPoolSize: " + super.getGifEncoderThreadPoolSize());
LOGGER.info("Petpet GifMakerThreadPoolSize: " + super.getGifEncoderThreadPoolSize());

for (String path : config.getDisabled()) {
disabledKey.add(path.replace("\"", ""));
}

System.out.println("ヾ(≧▽≦*)o Petpet 初始化成功,使用 " + command + " 以获取keyList!");
LOGGER.info("ヾ(≧▽≦*)o Petpet 初始化成功,使用 " + command + " 以获取keyList!");
}

public void readData(File dir) {
Expand All @@ -68,7 +67,7 @@ public void readData(File dir) {
}
});

System.out.println("Petpet 加载完毕 (共 " + dataMap.size() + " 素材,随机表列包含 " +
LOGGER.info("Petpet 加载完毕 (共 " + dataMap.size() + " 素材,随机表列包含 " +
randomableList.size() + " 素材,已禁用 " + disabledKey.size() + ")");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/moe/dituon/petpet/server/FormDataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public FormDataParser(ServerPetService service, HttpExchange httpExchange) throw

try {
String key = map.get("key").value;
System.out.println(key);
ServerPetService.LOGGER.info("DEBUG[FORM DATA KEY]: " + key);
assert key != null;

super.imagePair = service.generateImage(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/moe/dituon/petpet/server/PetHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void handle(HttpExchange httpExchange) {
return;
}

System.out.println("DEBUG[GET URL]: " + requestParam);
ServerPetService.LOGGER.info("DEBUG[GET QUERY]: " + requestParam);
parser = new GETParser(service, requestParam);
finish(httpExchange, parser);
return;
Expand Down Expand Up @@ -58,7 +58,7 @@ public void handle(HttpExchange httpExchange) {
}

String content = requestBodyContent.toString();
System.out.println("DEBUG[POST BODY]: " + content);
ServerPetService.LOGGER.info(requestBodyContent.insert(0, "DEBUG[POST BODY]: ").toString());
parser = new POSTParser(service, content);
finish(httpExchange, parser);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PreviewHttpHandler implements HttpHandler {
}
File destDir = new File(PREVIEW_ROOT);
if(!(destDir.exists()&& destDir.isDirectory())) {
if (!destDir.mkdirs()) System.out.println("can not mkdir " + PREVIEW_ROOT);
if (!destDir.mkdirs()) ServerPetService.LOGGER.info("can not mkdir " + PREVIEW_ROOT);
}
PreviewConfigDTO config = PreviewConfigDTO.decodeFromString(service.getFileStr(configFile));

Expand Down Expand Up @@ -65,7 +65,7 @@ public class PreviewHttpHandler implements HttpHandler {
k, avatars, texts, null
);
var snowTime = System.currentTimeMillis();
System.out.println(k + ' ' + (snowTime - stime) + "ms");
ServerPetService.LOGGER.info(k + ' ' + (snowTime - stime) + "ms");
File targetFile = new File(basePath + result.getSecond());
try {
Files.copy(result.getFirst(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
Expand All @@ -74,7 +74,7 @@ public class PreviewHttpHandler implements HttpHandler {
}
});
var nowTime = System.currentTimeMillis();
System.out.println("generate " + service.getDataMap().size() + " image in " + (nowTime - time) + "ms");
ServerPetService.LOGGER.info("Generate " + service.getDataMap().size() + " images in " + (nowTime - time) + "ms");
mime = "image/gif";
} catch (Exception ex) {
throw new RuntimeException(ex);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/moe/dituon/petpet/server/ServerPetService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package moe.dituon.petpet.server;

import moe.dituon.petpet.share.BaseLogger;
import moe.dituon.petpet.share.BasePetService;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class ServerPetService extends BasePetService {
public static final BaseLogger LOGGER = BaseLogger.getInstance();

public static final int DEFAULT_PORT = 2333;
public static final String DEFAULT_DATA_PATH = "data/xmmt.dituon.petpet/";
public static final String CONFIG_NAME = "server-config.json";
Expand All @@ -24,7 +27,7 @@ public void readConfig(ServerServiceConfig config) {
usePreview = config.getPreview();

readBaseServiceConfig(config.toBaseServiceConfig());
System.out.println("GifMakerThreadPoolSize: " + super.getGifEncoderThreadPoolSize());
LOGGER.info("GifMakerThreadPoolSize: " + super.getGifEncoderThreadPoolSize());
}

public void readConfig() {
Expand All @@ -47,7 +50,7 @@ public void readConfig() {
public void readData(File[] files) {
super.readData(files);
indexJson = PetDataDTO.stringify(super.dataMap);
System.out.println(keyListString);
LOGGER.info("\n---Key List---\n" + keyListString + "\n--------------\n");
}

public String getIndexJson() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/moe/dituon/petpet/server/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void init() {

try {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(service.port), 0);
System.out.println("PetpetWebServer started in port " + service.port);
ServerPetService.LOGGER.info("PetpetWebServer started in port " + service.port);

httpServer.createContext("/petpet", new PetHttpHandler(service));
httpServer.createContext("/preview", new PreviewHttpHandler(service));
Expand All @@ -38,17 +38,17 @@ private void init() {
InputStream html = new ByteArrayInputStream(stream.readAllBytes());
httpServer.createContext("/", exchange -> PetHttpHandler.handleResponse(exchange, html, "text/html"));

System.out.println("WebUI-URL: http://127.0.0.1:" + service.port + '/');
if (!service.usePreview) System.out.println("Warn: WebUI preview is disabled");
ServerPetService.LOGGER.info("WebUI-URL: http://127.0.0.1:" + service.port + '/');
if (!service.usePreview) ServerPetService.LOGGER.warning("WebUI preview is disabled");
} catch (Exception e){
System.out.println("Petpet WebUI is disabled");
ServerPetService.LOGGER.info("Petpet WebUI is disabled");
}

httpServer.setExecutor(Executors.newFixedThreadPool(service.webServerThreadPoolSize));
httpServer.start();

apiUrl = ("http://127.0.0.1:" + service.port + "/petpet").intern();
System.out.println("API-URL: " + apiUrl);
ServerPetService.LOGGER.info("API-URL: " + apiUrl);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/moe/dituon/petpet/share/AvatarModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static class DeformData {
int[][] anchor;

public static DeformData fromGifPos(JsonArray posElements) {
// System.out.println("DeformData fromPos by: " + posElements.toString());
// BasePetService.LOGGER.info("DeformData fromPos by: " + posElements.toString());

DeformData deformData = new DeformData();
deformData.deformPos = new Point2D[posElements.size()][POS_SIZE];
Expand Down
Loading

0 comments on commit 6046369

Please sign in to comment.