Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Improve logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CiroZDP committed Mar 20, 2024
1 parent fbce161 commit e5aa3c6
Show file tree
Hide file tree
Showing 31 changed files with 199 additions and 44 deletions.
4 changes: 2 additions & 2 deletions opcraft/logs/game.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[17:20:33] [main/INFO]: Selected language: English (United States)
[17:20:33] [main/INFO]: OpenCraft 24r04 started!
[18:51:10] [main/INFO]: Selected language: Dullutiano (Dulutia)
[18:51:10] [main/INFO]: OpenCraft 24r05 started!
10 changes: 10 additions & 0 deletions opcraft/logs/internal.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[net.opencraft.sound.Sound] Ignored exception:
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at net.opencraft.sound.Sound.play(Sound.java:83)
at net.opencraft.sound.SoundManager.update(SoundManager.java:90)
at net.opencraft.renderer.scenes.Scene.setCurrent(Scene.java:56)
at net.opencraft.client.Game.init(Game.java:51)
at net.opencraft.client.Game.run(Game.java:59)
at java.base/java.lang.Thread.run(Thread.java:842)

6 changes: 4 additions & 2 deletions opcraft/logs/renderdragon.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[17:20:32] [renderThread/INFO]: Buffered rendering not configured, creating new framework.
[17:20:33] [renderThread/INFO]: Render system initialized!
[18:51:10] [renderThread/INFO]: Buffered rendering not configured, creating new framework.
[18:51:10] [renderThread/INFO]: Render system initialized!
[18:51:10] [renderThread/INFO]: Rendered scene opencraft.scene:load
[18:51:15] [renderThread/INFO]: Rendered scene opencraft.scene:menu
1 change: 1 addition & 0 deletions opcraft/logs/soundmanager.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[18:51:10] [soundManager/INFO]: Sound manager started!
21 changes: 8 additions & 13 deletions src/main/java/net/opencraft/client/Game.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package net.opencraft.client;

import static net.opencraft.LoggerConfig.LOG_FORMAT;
import static net.opencraft.LoggerConfig.handle;
import static net.opencraft.renderer.display.DisplayManager.destroyDisplay;
import static net.opencraft.logging.LoggerConfig.*;
import static net.opencraft.renderer.display.DisplayManager.*;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Logger;

import net.opencraft.LoggerConfig;
import net.opencraft.config.GameConfig;
import net.opencraft.config.GameExperiments;
import net.opencraft.data.packs.DefaultPack;
import net.opencraft.data.packs.Pack;
import net.opencraft.language.Languages;
import net.opencraft.logging.LoggerConfig;
import net.opencraft.renderer.RenderDragon;
import net.opencraft.renderer.Screen;
import net.opencraft.renderer.scenes.Scene;
import net.opencraft.sound.SoundManager;

public class Game implements Runnable {

public static final String NAME = "OpenCraft";
public static final String VERSION = "24r04";
public static final String VERSION = "24r05";
public static final String TITLE = NAME + ((char) 0x20) + VERSION;

public static final int NANOSECONDS = 1000000000;
Expand All @@ -34,7 +33,6 @@ public class Game implements Runnable {
private static Pack selected_pack = DefaultPack.getDefaultPack();

private boolean running = false;
private Screen screen;

static {
LoggerConfig.clearLogDir();
Expand All @@ -46,21 +44,20 @@ public class Game implements Runnable {

public void init() {
RenderDragon.init();
System.out.println();

this.screen = RenderDragon.getScreen();
if (GameExperiments.SKIP_LOAD_SCENE)
Scene.setCurrent(Scene.MENU_SCENE);
else
Scene.setCurrent(Scene.LOAD_SCENE);

System.out.println();
running = true;
}

@Override
public void run() {
init();
logger.info(String.format("Selected language: %s", getLanguage().getDisplayName(getLanguage())));
logger.info(String.format("Selected language: %s", Languages.getDisplayName(getLanguage())));

logger.info(Game.TITLE + " started!");

Expand All @@ -87,8 +84,6 @@ public void run() {
}

public void render() {
BufferedImage img = this.screen.getImage();
Scene.renderCurrent(img);
RenderDragon.update();
}

Expand Down Expand Up @@ -149,7 +144,7 @@ public static boolean isDefaultPackSelected() {
}

public static BufferedImage screenshot() {
return getInstance().screen.screenshot();
return RenderDragon.getScreen().screenshot();
}

public static void main(String[] args) throws IOException {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/opencraft/config/GameConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.util.Locale;

import net.opencraft.data.packs.Pack;
import net.opencraft.language.Languages;

public class GameConfig {

public static Locale LANGUAGE = Locale.getDefault();
public static Locale LANGUAGE = Languages.get("du-DU");
public static byte TICK_RATE = 60;
public static boolean UNICODE = false;
public static Pack PACK_SELECTED = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/opencraft/config/GameExperiments.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ private GameExperiments() {
* This experiment skip completly the load screen.<br>
* Recommended for developers!
* */
public static final boolean SKIP_LOAD_SCENE = true;
public static final boolean SKIP_LOAD_SCENE = false;

}
5 changes: 5 additions & 0 deletions src/main/java/net/opencraft/data/packs/DefaultPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.FileInputStream;
import java.io.InputStream;

import net.opencraft.logging.InternalLogger;

public final class DefaultPack extends Pack {

private static final DefaultPack defaultPack = new DefaultPack();
Expand All @@ -21,6 +23,9 @@ public InputStream getResource(String resource) {
try {
in = new FileInputStream(resource);
} catch (Exception ignored) {
InternalLogger.out.printf("[%s] Ignored exception:\n", getClass().getName());
ignored.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}

return in;
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/net/opencraft/data/packs/ResourcePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import net.opencraft.logging.InternalLogger;

public class ResourcePack extends Pack {

private ZipFile zfile;
Expand All @@ -27,7 +29,11 @@ public static ResourcePack get(final String filename) {

try {
pack = new ResourcePack(filename);
} catch (Exception ignored) {}
} catch (Exception ignored) {
InternalLogger.out.printf("[%s] Ignored exception:\n", ResourcePack.class.getName());
ignored.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}

return pack;
}
Expand All @@ -38,7 +44,11 @@ public InputStream getResource(String resource) {

try {
stream = zfile.getInputStream(zfile.getEntry(resource));
} catch (Exception ignored) {}
} catch (Exception ignored) {
InternalLogger.out.printf("[%s] Ignored exception:\n", getClass().getName());
ignored.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}

return stream;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/opencraft/language/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ public class Languages {
public static Locale get(String languageTag) {
return Locale.forLanguageTag(languageTag);
}

public static String getDisplayName(Locale locale) {
String name = locale.getDisplayName(locale);

if (locale.equals(Languages.get("du-DU")))
name = "Dullutiano (Dulutia)";

return name;
}

}
23 changes: 21 additions & 2 deletions src/main/java/net/opencraft/language/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Translator {
private static final Map<String, String> GALICIAN = new HashMap<>();
private static final Map<String, String> SPANISH = new HashMap<>();
private static final Map<String, String> FRENCH = new HashMap<>();
private static final Map<String, String> ITALIAN = new HashMap<>();
private static final Map<String, String> DULLUTIANO = new HashMap<>();

static {

Expand Down Expand Up @@ -40,16 +42,33 @@ public class Translator {
FRENCH.put("opencraft.button:settings", "Paramètres");
FRENCH.put("opencraft.button:quit", "Quitter le jeu");

// Italian translation
ITALIAN.put("opencraft.button:singleplayer", "Giocatore singolo");
ITALIAN.put("opencraft.button:multiplayer", "Multigiocatore");
ITALIAN.put("opencraft.button:settings", "Impostazioni");
ITALIAN.put("opencraft.button:quit", "Abbandonare");

// Dullutian translation
DULLUTIANO.put("opencraft.button:singleplayer", "Unh jocattore");
DULLUTIANO.put("opencraft.button:multiplayer", "Jocatoreus");
DULLUTIANO.put("opencraft.button:settings", "Configuraçionne");
DULLUTIANO.put("opencraft.button:quit", "Schiatta jogo");


}

public static String translate(String property, Locale language) {

if (language.getDisplayName().equalsIgnoreCase("Spanish"))
if (language.getDisplayLanguage().equalsIgnoreCase("Spanish"))
return SPANISH.getOrDefault(property, property);
else if (language.getDisplayName().equalsIgnoreCase("Galician"))
else if (language.getDisplayLanguage().equalsIgnoreCase("Galician"))
return GALICIAN.getOrDefault(property, property);
else if (language.getDisplayLanguage().equalsIgnoreCase("French"))
return FRENCH.getOrDefault(property, property);
else if (language.getDisplayLanguage().equalsIgnoreCase("Italian"))
return ITALIAN.getOrDefault(property, property);
else if (language.equals(Languages.get("du-DU")))
return DULLUTIANO.getOrDefault(property, property);
else
return ENGLISH.getOrDefault(property, property);

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/opencraft/logging/InternalLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.opencraft.logging;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class InternalLogger {

private static FileOutputStream fos;
public static PrintStream out;

static {
try {
fos = new FileOutputStream(LoggerConfig.getLogDir() + "/internal.log");
out = new PrintStream(fos);
} catch (Exception ignored) {
out = new PrintStream(OutputStream.nullOutputStream());
}
}

private InternalLogger() {
}



}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.opencraft;
package net.opencraft.logging;

import java.io.File;
import java.io.IOException;
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/net/opencraft/renderer/RenderDragon.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.opencraft.renderer;

import static net.opencraft.LoggerConfig.LOG_FORMAT;
import static net.opencraft.LoggerConfig.handle;
import static net.opencraft.logging.LoggerConfig.LOG_FORMAT;
import static net.opencraft.logging.LoggerConfig.handle;
import static net.opencraft.renderer.display.DisplayManager.createDisplay;
import static net.opencraft.renderer.display.DisplayManager.getDisplay;
import static net.opencraft.renderer.display.DisplayManager.getDisplayHeight;
Expand All @@ -15,8 +15,11 @@
import java.util.logging.Logger;

import net.opencraft.renderer.display.Display;
import net.opencraft.renderer.scenes.Scene;
import net.opencraft.renderer.scenes.SceneListener;
import net.opencraft.util.Resource;

public class RenderDragon extends Canvas {
public class RenderDragon extends Canvas implements SceneListener {

private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger("renderThread");
Expand All @@ -29,7 +32,6 @@ public class RenderDragon extends Canvas {
static {
// Set logging format
System.setProperty("java.util.logging.SimpleFormatter.format", LOG_FORMAT);

handle(logger, "/renderdragon.log");
}

Expand All @@ -41,6 +43,7 @@ public static void init() {
return;

instance = new RenderDragon();
Scene.addListener(instance);
initializated = true;

createDisplay();
Expand All @@ -54,6 +57,7 @@ public static void init() {
}

public static void update() {
Scene.renderCurrent(screen.getImage());
instance.render();
updateDisplay();
}
Expand All @@ -80,4 +84,14 @@ public static boolean isInitializated() {
return initializated;
}


@Override
public void onSceneChanged(Resource scene1, Resource scene2) {
logger.info(String.format("Rendered scene %s", scene2));

}
@Override
public void onSceneUpdated(Resource scene_res) {
}

}
5 changes: 5 additions & 0 deletions src/main/java/net/opencraft/renderer/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import javax.imageio.ImageIO;

import net.opencraft.logging.InternalLogger;

public class Texture {

public final Optional<Image> img;
Expand All @@ -30,6 +32,9 @@ public static Texture of(InputStream in) {
try {
img = ImageIO.read(in);
} catch (Exception ignored) {
InternalLogger.out.printf("[%s] Ignored exception:\n", Texture.class.getName());
ignored.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}

return Texture.of(img);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/opencraft/renderer/scenes/LoadScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.awt.image.BufferedImage;

import net.opencraft.config.GameExperiments;
import net.opencraft.logging.InternalLogger;
import net.opencraft.renderer.display.Display;
import net.opencraft.sound.Sound;
import net.opencraft.util.Assets;
Expand Down Expand Up @@ -72,6 +73,9 @@ public void changeScreen(long time) {
try {
Thread.sleep(time);
} catch (Exception ignored) {
InternalLogger.out.printf("[%s] Ignored exception:\n", getClass().getName());
ignored.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}
}

Expand Down
Loading

0 comments on commit e5aa3c6

Please sign in to comment.