Skip to content

Commit

Permalink
Use logger
Browse files Browse the repository at this point in the history
  • Loading branch information
merykitty committed Apr 12, 2021
1 parent e2a0d4c commit 014359d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/target/
/resources/
/data/
/SLPProcessor/
/MerySLP/
/slp-processor.iml
/.idea/
*.jfr
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/github/merykitty/meryslp/Info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.github.merykitty.meryslp;

import io.github.merykitty.meryslp.misc.EnvironmentResolver;

public class Info {
public static void main(String[] args) {

EnvironmentResolver.info(
"""
"""
);
}
}
16 changes: 9 additions & 7 deletions src/main/java/io/github/merykitty/meryslp/SLPDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;

import io.github.merykitty.meryslp.misc.EnvironmentResolver;
import io.github.merykitty.meryslp.misc.PrimitiveOptional;
Expand All @@ -20,6 +21,8 @@ public class SLPDecoder {
private static final Path DEFAULT_OUTPUT_FOLDER = Path.of("data/decoder-output");

public static void main(String[] args) throws IOException {
EnvironmentResolver.setLogLevel(Level.INFO);

var parser = ArgumentParsers.newFor("SLPDecoder").build()
.defaultHelp(true)
.description("Decode SLP files into human readable graphics images and meta data");
Expand All @@ -33,7 +36,6 @@ public static void main(String[] args) throws IOException {
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
System.out.println("Incorrect command line format");
return;
}
var configFile = EnvironmentResolver.homeDir().resolve(DEFAULT_CONFIG_FILE);
Expand All @@ -55,25 +57,25 @@ public static void main(String[] args) throws IOException {
processSLPFile(path, outputFolder, palettes);
});
} catch (RuntimeException e) {
System.out.println("Error: " + e.getCause().toString());
EnvironmentResolver.error("Error: " + e.getCause().toString());
} catch (Exception e) {
System.out.println("Error: " + e.toString());
EnvironmentResolver.error("Error: " + e.toString());
}
}

private static void processSLPFile(Path inputFile, Path outputFolder, PaletteContainer palettes) {
try {
var fileName = inputFile.getFileName();
System.out.println("File name: " + fileName.toString());
EnvironmentResolver.info("File name: " + fileName.toString());
long start = System.currentTimeMillis();
var file = SLPFiles.decode(inputFile, palettes);
long mid = System.currentTimeMillis();
System.out.println("Read slp file: " + (mid - start) + " ms");
EnvironmentResolver.info("Read slp file: " + (mid - start) + " ms");
file.exportGraphics(outputFolder.resolve(fileName));
long end = System.currentTimeMillis();
System.out.println("Print data: " + (end - mid) + " ms");
EnvironmentResolver.info("Print data: " + (end - mid) + " ms");
} catch (Exception e) {
System.out.println(e);
EnvironmentResolver.info(e);
}
}
}
16 changes: 9 additions & 7 deletions src/main/java/io/github/merykitty/meryslp/SLPEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;

public class SLPEncoder {
private static final Path DEFAULT_CONFIG_FILE = Path.of("resources/aoe1-config.json");
Expand All @@ -19,6 +20,8 @@ public class SLPEncoder {
private static final Path DEFAULT_OUTPUT_FOLDER = Path.of("data/encoder-output");

public static void main(String[] args) throws IOException {
EnvironmentResolver.setLogLevel(Level.INFO);

var parser = ArgumentParsers.newFor("SLPDecoder").build()
.defaultHelp(true)
.description("Encode human readable graphics images and meta data to SLP files");
Expand All @@ -32,7 +35,6 @@ public static void main(String[] args) throws IOException {
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
System.out.println("Incorrect command line format");
return;
}

Expand All @@ -53,25 +55,25 @@ public static void main(String[] args) throws IOException {
files.filter(Files::isDirectory)
.forEach(path -> encodeSLP(path, outputFolder, palettes));
} catch (RuntimeException e) {
System.out.println("Error: " + e.getCause().toString());
EnvironmentResolver.error("Error: " + e.getCause().toString());
} catch (Exception e) {
System.out.println("Error: " + e.toString());
EnvironmentResolver.error("Error: " + e.toString());
}
}

private static void encodeSLP(Path inputFile, Path outputFolder, PaletteContainer palettes) {
try {
var fileName = inputFile.getFileName();
System.out.println("File name: " + fileName.toString());
EnvironmentResolver.info("File name: " + fileName.toString());
long start = System.currentTimeMillis();
var file = SLPFiles.importGraphics(inputFile, palettes);
long mid = System.currentTimeMillis();
System.out.println("Import graphics: " + (mid - start) + " ms");
EnvironmentResolver.info("Import graphics: " + (mid - start) + " ms");
SLPFiles.encode(outputFolder.resolve(fileName), palettes, file, true);
long end = System.currentTimeMillis();
System.out.println("Encode slp file: " + (end - mid) + " ms");
EnvironmentResolver.info("Encode slp file: " + (end - mid) + " ms");
} catch (Exception e) {
System.out.println(e.toString());
EnvironmentResolver.info(e);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/github/merykitty/meryslp/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;

import io.github.merykitty.meryslp.common.SLPFiles;
import io.github.merykitty.meryslp.image.PaletteContainer;
Expand All @@ -18,6 +19,7 @@ public class Test {
private static final Path ENCODE_OUTPUT_FOLDER = HOME_DIR.resolve("data/encoder-output");

public static void main(String[] args) throws IOException {
EnvironmentResolver.setLogLevel(Level.ALL);
// var palettes = new PaletteContainer(CONFIG_FILE, PALETTE_FOLDER);
// var files = Files.list(DECODE_INPUT_FOLDER);
// files.filter(Files::isRegularFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
import java.lang.invoke.MethodType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EnvironmentResolver {
private static final long INIT_MAX_PATH_LENGTH = 256;
private static final long CHAR_SIZE = 2;

private static final String OS_NAME;
private static final Path HOME_DIR;
private static final Logger LOGGER = Logger.getGlobal();

static {
try {
OS_NAME = System.getProperty("os.name").toLowerCase();
LOGGER.addHandler(new ConsoleHandler());
if (OS_NAME.contains("linux")) {
var execPath = Files.readSymbolicLink(Path.of("/proc/self/exe"));
var execName = execPath.getFileName().toString();
Expand Down Expand Up @@ -83,11 +88,31 @@ public class EnvironmentResolver {
}
}

public static void setLogLevel(Level level) {
LOGGER.setLevel(level);
}

public static Path homeDir() {
return HOME_DIR;
}

public static String osName() {
return OS_NAME;
}

public static void debug(Object message) {
LOGGER.fine(message.toString());
}

public static void info(Object message) {
LOGGER.info(message.toString());
}

public static void warning(Object message) {
LOGGER.warning(message.toString());
}

public static void error(Object message) {
LOGGER.severe(message.toString());
}
}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module io.github.merykitty.meryslp {
requires java.logging;
requires jdk.unsupported;
requires jdk.incubator.foreign;
requires org.json;
Expand Down
Binary file added src/main/resources/unix-x64/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/main/resources/windows-x64/decoder.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
java-options=-Dforeign.restricted=permit -XX:+UseParallelGC -XX:+UnlockExperimentalVMOptions -XX:+TrustFinalNonStaticFields
module=io.github.merykitty.meryslp/io.github.merykitty.meryslp.SLPDecoder
icon=./resources/icon.png
icon=./resources/icon.ico
win-console=true
4 changes: 2 additions & 2 deletions src/main/resources/windows-x64/encoder.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
java-options=-Dforeign.restricted=permit -XX:+UseParallelGC -XX:+UnlockExperimentalVMOptions -XX:+TrustFinalNonStaticFields
module=io.github.merykitty.slpprocessor/io.github.merykitty.slpprocessor.SLPEncoder
icon=./resources/icon.png
module=io.github.merykitty.meryslp/io.github.merykitty.meryslp.SLPEncoder
icon=./resources/icon.ico
win-console=true
Binary file added src/main/resources/windows-x64/icon.ico
Binary file not shown.

0 comments on commit 014359d

Please sign in to comment.