chore(logging): Implemented basic logging

This commit is contained in:
Jakub Žitník 2025-03-23 13:54:51 +01:00
parent c1674d7866
commit b6c0e730d3
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
10 changed files with 86 additions and 5 deletions

View File

@ -124,9 +124,9 @@
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.17</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.18</version> <!-- latest at the time -->
</dependency>
</dependencies>

View File

@ -7,19 +7,23 @@ import cz.jzitnik.game.threads.ThreadProvider;
import cz.jzitnik.game.ui.*;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.tui.ScreenRenderer;
import lombok.extern.slf4j.Slf4j;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import java.io.IOException;
@Slf4j
public class Main {
public static void main(String[] args) {
try {
log.info("Setting up terminal");
Terminal terminal = TerminalBuilder.terminal();
terminal.enterRawMode();
if (!terminal.hasMouseSupport()) {
System.out.println("Error: This terminal does not support mouse.");
log.error("This terminal does not support mouse");
System.exit(1);
}
@ -56,6 +60,7 @@ public class Main {
Thread.sleep(1000);
}
log.info("Closing terminal");
terminal.trackMouse(Terminal.MouseTracking.Off);
terminal.close();
} catch (IOException | InterruptedException ignored) {

View File

@ -2,6 +2,9 @@ package cz.jzitnik.game;
import java.io.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class GameSaver {
public void save(Game game) {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("world.ser"))) {
@ -11,7 +14,9 @@ public class GameSaver {
}
}
// TODO: This will need rewrite
public static Game load() {
log.info("Loading game");
File file = new File("world.ser");
if (!file.isFile()) {
@ -19,6 +24,7 @@ public class GameSaver {
}
try {
log.info("Loading game from save file");
FileInputStream fileIn = new FileInputStream("world.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);

View File

@ -3,9 +3,11 @@ package cz.jzitnik.game;
import cz.jzitnik.game.sprites.*;
import cz.jzitnik.tui.Sprite;
import cz.jzitnik.tui.SpriteList;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
@Slf4j
public class SpriteLoader {
public enum SPRITES {
// BLOCKS
@ -364,6 +366,7 @@ public class SpriteLoader {
}
public static SpriteList<SPRITES> load() {
log.info("Loading sprites");
return new SpriteList<>(SPRITES.class, SPRITES_MAP);
}
}

View File

@ -6,18 +6,23 @@ import java.util.Set;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
@Slf4j
public class CustomLogicProvider {
private final List<CustomLogicInterface> logicList = new ArrayList<>();
public void update(Game game) {
for (CustomLogicInterface logicInterface : logicList) {
log.debug("Running logic {}.", logicInterface.getClass().getSimpleName());
logicInterface.nextIteration(game);
}
}
public CustomLogicProvider() {
log.info("Loading logic");
registerHandlers();
}
@ -30,6 +35,7 @@ public class CustomLogicProvider {
try {
CustomLogicInterface instance = (CustomLogicInterface) clazz.getDeclaredConstructor().newInstance();
logicList.add(instance);
log.info("Loaded custom logic with name {}", clazz.getSimpleName());
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -10,8 +10,11 @@ import cz.jzitnik.game.annotations.EntityLogic;
import cz.jzitnik.game.entities.Block;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
@Slf4j
public class EntityLogicProvider {
private static final int CHUNK_SIZE = 20;
public final HashMap<String, EntityLogicInterface> logicList = new HashMap<>();
@ -26,6 +29,7 @@ public class EntityLogicProvider {
}
public void update(Game game) {
log.debug("Updating all entities");
int[] playerLocation = game.getPlayerCords();
int playerX = playerLocation[0];
int playerY = playerLocation[1];
@ -56,11 +60,15 @@ public class EntityLogicProvider {
if (!logicList.containsKey(entityLogicMobDTO.getMob().getBlockId())) {
return;
}
var logic = logicList.get(entityLogicMobDTO.getMob().getBlockId());
logicList.get(entityLogicMobDTO.getMob().getBlockId()).nextIteration(entityLogicMobDTO);
log.debug("Updating entity {} using logic {} on coordinates x:{},y:{}", entityLogicMobDTO.getMob().getBlockId(),
logic.getClass().getSimpleName(), entityLogicMobDTO.getX(), entityLogicMobDTO.getY());
logic.nextIteration(entityLogicMobDTO);
}
public EntityLogicProvider() {
log.info("Loading entity logic");
registerHandlers();
}
@ -74,6 +82,7 @@ public class EntityLogicProvider {
EntityLogicInterface instance = (EntityLogicInterface) clazz.getDeclaredConstructor().newInstance();
EntityLogic annotation = clazz.getAnnotation(EntityLogic.class);
logicList.put(annotation.value(), instance);
log.info("Loaded logic for entity {}", annotation.value());
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -8,10 +8,12 @@ import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.ThreadRegistry;
import cz.jzitnik.game.entities.Player;
import cz.jzitnik.tui.ScreenRenderer;
import lombok.extern.slf4j.Slf4j;
import org.jline.terminal.Terminal;
import org.reflections.Reflections;
@Slf4j
public class ThreadProvider {
private final Game game;
private final ScreenRenderer screenRenderer;
@ -20,6 +22,7 @@ public class ThreadProvider {
private final List<Thread> list = new ArrayList<>();
public void start() {
log.info("Loading all the threads");
for (Thread thread : list) {
thread.start();
}

View File

@ -4,8 +4,12 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ResourceLoader {
public static String loadResource(String fileName) {
log.debug("Loading resource: {}", "textures/" + fileName);
try (InputStream inputStream = ResourceLoader.class.getClassLoader()
.getResourceAsStream("textures/" + fileName)) {
if (inputStream == null) {

View File

@ -11,12 +11,15 @@ import cz.jzitnik.game.ui.Healthbar;
import cz.jzitnik.tui.utils.SpriteCombiner;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jline.terminal.Terminal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Slf4j
public class ScreenRenderer {
private final SpriteList spriteList;
private final Terminal terminal;
@ -43,6 +46,7 @@ public class ScreenRenderer {
}
public synchronized void render(Game game) {
log.debug("Rendering frame");
var world = game.getWorld();
StringBuilder main = new StringBuilder();
main.append("\033[H\033[2J");
@ -154,6 +158,7 @@ public class ScreenRenderer {
}
}
log.debug("Frame rendered");
System.out.println(main);
}

View File

@ -0,0 +1,40 @@
<configuration>
<appender name="GENERAL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/general.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- File name pattern for rolled logs -->
<fileNamePattern>logs/general.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<!-- Max size before rotation -->
<maxFileSize>10MB</maxFileSize>
<!-- Max history (how many days to keep) -->
<maxHistory>30</maxHistory>
<!-- Total size cap -->
<totalSizeCap>500MB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERROR_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="GENERAL_FILE" />
<appender-ref ref="ERROR_CONSOLE" />
</root>
</configuration>