From b6c6ae73204e80f2db6e133670249307ef069e5a Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Dec 2018 17:40:11 +0100 Subject: [PATCH] Improved logging --- src/de/mas/wiiu/jnus/DecryptionService.java | 2 +- src/de/mas/wiiu/jnus/NUSTitleLoaderWUD.java | 8 ++++---- .../wiiu/jnus/PipedInputStreamWithException.java | 4 +++- src/de/mas/wiiu/jnus/WUDService.java | 14 +++++++------- src/de/mas/wiiu/jnus/entities/fst/FSTEntry.java | 13 +++++++++---- src/de/mas/wiiu/jnus/utils/HashUtil.java | 10 +++++----- src/de/mas/wiiu/jnus/utils/Utils.java | 7 +++++++ .../jnus/utils/cryptography/NUSDecryption.java | 6 ++++-- 8 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/de/mas/wiiu/jnus/DecryptionService.java b/src/de/mas/wiiu/jnus/DecryptionService.java index 9e0cad2..a10463d 100644 --- a/src/de/mas/wiiu/jnus/DecryptionService.java +++ b/src/de/mas/wiiu/jnus/DecryptionService.java @@ -88,7 +88,7 @@ public final class DecryptionService { return; } - log.fine("Decrypting " + entry.getFilename()); + log.info("Decrypting " + entry.getFilename()); String targetFilePath = new StringBuilder().append(outputPath).append("/").append(entry.getFilename()).toString(); String fullPath = new StringBuilder().append(outputPath).toString(); diff --git a/src/de/mas/wiiu/jnus/NUSTitleLoaderWUD.java b/src/de/mas/wiiu/jnus/NUSTitleLoaderWUD.java index ff5a22d..5f4d9ba 100644 --- a/src/de/mas/wiiu/jnus/NUSTitleLoaderWUD.java +++ b/src/de/mas/wiiu/jnus/NUSTitleLoaderWUD.java @@ -44,7 +44,7 @@ public final class NUSTitleLoaderWUD extends NUSTitleLoader { public static List loadNUSTitle(String WUDPath, File key) throws Exception { byte[] data = Files.readAllBytes(key.toPath()); if (data == null) { - System.out.println("Failed to read the key file."); + log.warning("Failed to read the key file."); return new ArrayList<>(); } return loadNUSTitle(WUDPath, data); @@ -62,7 +62,7 @@ public final class NUSTitleLoaderWUD extends NUSTitleLoader { byte[] usedTitleKey = titleKey; File wudFile = new File(WUDPath); if (!wudFile.exists()) { - log.info(WUDPath + " does not exist."); + log.warning(WUDPath + " does not exist."); System.exit(1); } @@ -70,7 +70,7 @@ public final class NUSTitleLoaderWUD extends NUSTitleLoader { if (usedTitleKey == null && !forceNoKey) { File keyFile = new File(wudFile.getParentFile().getPath() + File.separator + Settings.WUD_KEY_FILENAME); if (!keyFile.exists()) { - log.info(keyFile.getAbsolutePath() + " does not exist and no title key was provided."); + log.warning(keyFile.getAbsolutePath() + " does not exist and no title key was provided."); return new ArrayList<>(); } usedTitleKey = Files.readAllBytes(keyFile.toPath()); @@ -78,7 +78,7 @@ public final class NUSTitleLoaderWUD extends NUSTitleLoader { WUDInfo wudInfo = WUDInfoParser.createAndLoad(image.getWUDDiscReader(), usedTitleKey); if (wudInfo == null) { - System.out.println("WTF. ERROR."); + log.warning("Failed to parse any WUDInfo"); return new ArrayList<>(); } diff --git a/src/de/mas/wiiu/jnus/PipedInputStreamWithException.java b/src/de/mas/wiiu/jnus/PipedInputStreamWithException.java index e2ac831..d4a7d12 100644 --- a/src/de/mas/wiiu/jnus/PipedInputStreamWithException.java +++ b/src/de/mas/wiiu/jnus/PipedInputStreamWithException.java @@ -20,7 +20,9 @@ import java.io.IOException; import java.io.PipedInputStream; import de.mas.wiiu.jnus.utils.Utils; +import lombok.extern.java.Log; +@Log public class PipedInputStreamWithException extends PipedInputStream implements InputStreamWithException { private Exception e = null; private boolean exceptionSet = false; @@ -63,7 +65,7 @@ public class PipedInputStreamWithException extends PipedInputStream implements I Utils.sleep(10); } if (tries > 100) { - // TODO: warning? + log.warning("Tried too often."); break; } } diff --git a/src/de/mas/wiiu/jnus/WUDService.java b/src/de/mas/wiiu/jnus/WUDService.java index f3e7e2c..71f0256 100644 --- a/src/de/mas/wiiu/jnus/WUDService.java +++ b/src/de/mas/wiiu/jnus/WUDService.java @@ -147,7 +147,7 @@ public final class WUDService { } } while (written < image.getWUDFileSize()); System.out.println(); - System.out.println("Sectors compressed."); + log.info("Sectors compressed."); log.info("Writing sector table"); fileOutput.seek(sectorTableStart); ByteBuffer buffer = ByteBuffer.allocate(sectorTablePlaceHolder.length); @@ -164,7 +164,7 @@ public final class WUDService { public static boolean compareWUDImage(WUDImage firstImage, WUDImage secondImage) throws IOException { if (firstImage.getWUDFileSize() != secondImage.getWUDFileSize()) { - log.info("Filesize is different"); + log.warning("Filesize is different"); return false; } InputStream in1 = firstImage.getWUDDiscReader().readEncryptedToInputStream(0, WUDImage.WUD_FILESIZE); @@ -182,13 +182,13 @@ public final class WUDService { int read1 = StreamUtils.getChunkFromStream(in1, blockBuffer1, overflow1, bufferSize); int read2 = StreamUtils.getChunkFromStream(in2, blockBuffer2, overflow2, bufferSize); if (read1 != read2) { - log.info("Verification error"); + log.warning("Verification error"); result = false; break; } if (!Arrays.equals(blockBuffer1, blockBuffer2)) { - log.info("Verification error"); + log.warning("Verification error"); result = false; break; } @@ -203,7 +203,7 @@ public final class WUDService { } } while (totalread < WUDImage.WUD_FILESIZE); System.out.println(); - System.out.println("Verfication done!"); + log.info("Verfication done!"); in1.close(); in2.close(); @@ -275,7 +275,7 @@ public final class WUDService { } } while (totalread < WUDImage.WUD_FILESIZE); System.out.println(); - System.out.println("Decompressing done!"); + log.info("Decompressing done!"); in.close(); out.close(); @@ -331,7 +331,7 @@ public final class WUDService { double readMB = totalread / 1024.0 / 1024.0; double percent = ((double) totalread / WUDImage.WUD_FILESIZE) * 100; - System.out.println(String.format("\rHashing: %.2fMB done (%.2f%%)", readMB, percent)); + log.info(String.format("\rHashing: %.2fMB done (%.2f%%)", readMB, percent)); HashResult result = new HashResult(sha1.digest(), md5.digest(), Utils.StringToByteArray(Long.toHexString(checksumEngine.getValue()))); diff --git a/src/de/mas/wiiu/jnus/entities/fst/FSTEntry.java b/src/de/mas/wiiu/jnus/entities/fst/FSTEntry.java index 1f3a0c1..800cf73 100644 --- a/src/de/mas/wiiu/jnus/entities/fst/FSTEntry.java +++ b/src/de/mas/wiiu/jnus/entities/fst/FSTEntry.java @@ -16,6 +16,7 @@ ****************************************************************************/ package de.mas.wiiu.jnus.entities.fst; +import java.io.PrintStream; import java.util.ArrayList; import java.util.List; @@ -151,14 +152,18 @@ public class FSTEntry { } public void printRecursive(int space) { + printRecursive(System.out,space); + } + + public void printRecursive(PrintStream out, int space) { for (int i = 0; i < space; i++) { - System.out.print(" "); + out.print(" "); } - System.out.print(getFilename()); + out.print(getFilename()); if (isNotInPackage()) { - System.out.print(" (not in package)"); + out.print(" (not in package)"); } - System.out.println(); + out.println(); for (FSTEntry child : getDirChildren(true)) { child.printRecursive(space + 5); } diff --git a/src/de/mas/wiiu/jnus/utils/HashUtil.java b/src/de/mas/wiiu/jnus/utils/HashUtil.java index 4f7d4f1..9335c64 100644 --- a/src/de/mas/wiiu/jnus/utils/HashUtil.java +++ b/src/de/mas/wiiu/jnus/utils/HashUtil.java @@ -204,7 +204,7 @@ public final class HashUtil { byte[] hash2 = HashUtil.hashSHA1(file2); boolean result = Arrays.equals(hash1, hash2); if (!result) { - log.info("Hash doesn't match for " + file1.getAbsolutePath() + "(" + Utils.ByteArrayToString(hash1) + ") and " + file2.getAbsolutePath() + "(" + log.warning("Hash doesn't match for " + file1.getAbsolutePath() + "(" + Utils.ByteArrayToString(hash1) + ") and " + file2.getAbsolutePath() + "(" + Utils.ByteArrayToString(hash2) + ")!"); } return result; @@ -246,7 +246,7 @@ public final class HashUtil { if (!Arrays.equals(expected_h1_hash, real_h1_hash)) { throw new CheckSumWrongException("h1 checksumfail", real_h1_hash, expected_h1_hash); } else { - log.finer("h1 checksum right!"); + log.finest("h1 checksum right!"); } } @@ -257,12 +257,12 @@ public final class HashUtil { if (!Arrays.equals(expected_h2_hash, real_h2_hash)) { throw new CheckSumWrongException("h2 checksumfail", real_h2_hash, expected_h2_hash); } else { - log.fine("h2 checksum right!"); + log.finest("h2 checksum right!"); } } if (h3Hashes == null) { - log.info("didn't check the h3, its missing."); + log.warning("didn't check the h3, its missing."); return; } if ((block % 4096) == 0) { @@ -272,7 +272,7 @@ public final class HashUtil { if (!Arrays.equals(expected_h3_hash, real_h3_hash)) { throw new CheckSumWrongException("h3 checksumfail", real_h3_hash, expected_h3_hash); } else { - log.fine("h3 checksum right!"); + log.finest("h3 checksum right!"); } } } diff --git a/src/de/mas/wiiu/jnus/utils/Utils.java b/src/de/mas/wiiu/jnus/utils/Utils.java index f5b814a..4adfb12 100644 --- a/src/de/mas/wiiu/jnus/utils/Utils.java +++ b/src/de/mas/wiiu/jnus/utils/Utils.java @@ -23,6 +23,9 @@ import java.io.InputStream; import java.math.BigInteger; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Arrays; +import java.util.logging.Level; +import java.util.logging.LogManager; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -176,6 +179,10 @@ public final class Utils { return 0L; } } + + public static void setGlobalLogLevel(Level level) { + Arrays.stream(LogManager.getLogManager().getLogger("").getHandlers()).forEach(h -> h.setLevel(level)); + } public static boolean checkFileExists(String path) { return new File(path).exists(); diff --git a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java index 2cc5acf..e461bec 100644 --- a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java +++ b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java @@ -30,7 +30,9 @@ import de.mas.wiiu.jnus.utils.CheckSumWrongException; import de.mas.wiiu.jnus.utils.HashUtil; import de.mas.wiiu.jnus.utils.StreamUtils; import de.mas.wiiu.jnus.utils.Utils; +import lombok.extern.java.Log; +@Log public class NUSDecryption extends AESDecryption { public NUSDecryption(byte[] AESKey, byte[] IV) { super(AESKey, IV); @@ -130,7 +132,7 @@ public class NUSDecryption extends AESDecryption { throw new CheckSumWrongException("hash checksum failed", calculated_hash1, expected_hash); } else { - // log.warning("Hash DOES match saves output stream."); + log.finest("Hash DOES match saves output stream."); } } @@ -184,7 +186,7 @@ public class NUSDecryption extends AESDecryption { soffset = 0; } } while (wrote < filesize && (inBlockBuffer == BLOCKSIZE)); - // System.out.println("Decryption okay"); + log.finest("Decryption okay"); outputStream.close(); inputStream.close(); }