|
| 1 | +/* |
| 2 | + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy |
| 3 | + * Copyright (C) 2021-2025 RK_01/RaphiMC and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package net.raphimc.viaproxy.cli.command.impl; |
| 19 | + |
| 20 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 21 | +import gs.mclo.api.MclogsClient; |
| 22 | +import gs.mclo.api.response.UploadLogResponse; |
| 23 | +import net.raphimc.viaproxy.ViaProxy; |
| 24 | +import net.raphimc.viaproxy.cli.command.Command; |
| 25 | +import net.raphimc.viaproxy.cli.command.executor.CommandExecutor; |
| 26 | +import net.raphimc.viaproxy.util.logging.Logger; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileNotFoundException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | + |
| 34 | +public class UploadLogCommand extends Command { |
| 35 | + |
| 36 | + public UploadLogCommand() { |
| 37 | + super("uploadlog", "Upload the ViaProxy log", "uploadlog"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void register(final LiteralArgumentBuilder<CommandExecutor> builder) { |
| 42 | + builder.executes(context -> { |
| 43 | + final org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger(); |
| 44 | + final RollingRandomAccessFileAppender fileAppender = (RollingRandomAccessFileAppender) logger.getAppenders().get("LatestFile"); |
| 45 | + fileAppender.getManager().flush(); |
| 46 | + final File logFile = new File(fileAppender.getFileName()); |
| 47 | + |
| 48 | + try { |
| 49 | + final MclogsClient mclogsClient = new MclogsClient("ViaProxy", ViaProxy.VERSION); |
| 50 | + final UploadLogResponse apiResponse = mclogsClient.uploadLog(logFile.toPath()).get(); |
| 51 | + if (apiResponse.isSuccess()) { |
| 52 | + context.getSource().sendMessage("Uploaded log file to " + apiResponse.getUrl()); |
| 53 | + } else { |
| 54 | + context.getSource().sendMessage("The log file could not be uploaded: " + apiResponse.getError()); |
| 55 | + } |
| 56 | + } catch (ExecutionException e) { |
| 57 | + if (e.getCause() instanceof FileNotFoundException) { |
| 58 | + context.getSource().sendMessage("The log file could not be found"); |
| 59 | + } else { |
| 60 | + Logger.LOGGER.error("Failed to upload log file", e.getCause()); |
| 61 | + } |
| 62 | + } catch (Throwable e) { |
| 63 | + Logger.LOGGER.error("Failed to upload log file", e); |
| 64 | + } |
| 65 | + return 1; |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments