diff --git a/src/main/java/com/boatticket/config/ConfigManager.java b/src/main/java/com/boatticket/config/ConfigManager.java index 2910006..786139c 100644 --- a/src/main/java/com/boatticket/config/ConfigManager.java +++ b/src/main/java/com/boatticket/config/ConfigManager.java @@ -67,6 +67,10 @@ public class ConfigManager { // causes PDFs to be generated at a real deployed counter. private static final boolean DEFAULT_DEV_MODE = false; + // Keep this false so a broken/missing config never accidentally pops + // open a console window at a real deployed counter. + private static final boolean DEFAULT_CONSOLE_ENABLED = false; + private static Properties cachedProperties; private ConfigManager() {} @@ -175,6 +179,28 @@ public static boolean isDevMode() { return Boolean.parseBoolean(value.trim()); } + // ------------------------------------------------------------------ + // Console toggle + // ------------------------------------------------------------------ + + /** + * True: a visible log console window is opened at startup, live-showing everything the app logs. + * + *

False (default, and what every shipped tenant config uses): no console window is ever shown, + * regardless of {@link #isDevMode()} — the two flags are independent. The packaged app is always + * a GUI-subsystem executable with no native console, so this is what actually controls whether + * {@link LoggingConfigurator} spawns its own visible log window. + */ + public static boolean isConsoleEnabled() { + String value = getProperties().getProperty("console"); + + if (value == null || value.isBlank()) { + return DEFAULT_CONSOLE_ENABLED; + } + + return Boolean.parseBoolean(value.trim()); + } + // ------------------------------------------------------------------ // Printer // ------------------------------------------------------------------ @@ -285,6 +311,8 @@ private static Properties load() { System.out.println( "[ConfigManager] dev.mode = " + props.getProperty("dev.mode") + + ", console = " + + props.getProperty("console") + ", printer.name = " + props.getProperty("printer.name")); } catch (IOException e) { @@ -345,6 +373,7 @@ private static void writeHardcodedDefaults(Path configFilePath) throws IOExcepti "update.connection.timeout.seconds", String.valueOf(DEFAULT_UPDATE_CONNECTION_TIMEOUT_SECONDS)); defaults.setProperty("dev.mode", String.valueOf(DEFAULT_DEV_MODE)); + defaults.setProperty("console", String.valueOf(DEFAULT_CONSOLE_ENABLED)); defaults.setProperty("printer.name", ""); defaults.setProperty( "printer.left.margin.dots", String.valueOf(DEFAULT_PRINTER_LEFT_MARGIN_DOTS)); diff --git a/src/main/java/com/boatticket/config/LoggingConfigurator.java b/src/main/java/com/boatticket/config/LoggingConfigurator.java new file mode 100644 index 0000000..da27258 --- /dev/null +++ b/src/main/java/com/boatticket/config/LoggingConfigurator.java @@ -0,0 +1,106 @@ +package com.boatticket.config; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +/** + * Controls, entirely through {@link ConfigManager#isConsoleEnabled()} (backed by each tenant's + * external {@code Config.properties}), whether a visible log console appears at runtime. + * + *

The packaged app is a GUI-subsystem executable (no console window is ever created by Windows + * itself — see {@code publish-release.yaml}, {@code --win-console} was intentionally removed), so + * whether a console-like window shows up can no longer be decided by any OS/packaging setting. This + * class fills that gap at the Java level instead, using its own independent {@code console} + * property (separate from {@code dev.mode}, which only controls the reference-PDF feature): + * + *

+ */ +public final class LoggingConfigurator { + + private static final String LOG_FILE_NAME = "app.log"; + private static volatile boolean configured = false; + + private LoggingConfigurator() {} + + /** Call once, as early as possible in application startup (before anything else logs). */ + public static synchronized void configure() { + if (configured) { + return; + } + + configured = true; + + if (!ConfigManager.isConsoleEnabled()) { + return; + } + + try { + Path logDir = Path.of(ConfigManager.getAppDirectory()); + Files.createDirectories(logDir); + + Path logFile = logDir.resolve(LOG_FILE_NAME); + + // append=true, single rotating-free file kept simple on purpose — + // this is a developer convenience, not a production log pipeline. + FileHandler fileHandler = new FileHandler(logFile.toString(), 5_000_000, 1, true); + fileHandler.setFormatter(new SimpleFormatter()); + fileHandler.setLevel(Level.ALL); + + Logger rootLogger = Logger.getLogger(""); + rootLogger.addHandler(fileHandler); + rootLogger.setLevel(Level.ALL); + + rootLogger.info(() -> "[CONSOLE] Logging to file: " + logFile.toAbsolutePath()); + + openLiveLogWindow(logFile); + } catch (IOException exception) { + // Never let a logging setup failure block the app from starting. + System.err.println( + "[LoggingConfigurator] Could not set up dev-mode logging: " + exception.getMessage()); + } + } + + /** + * Spawns a separate, visible terminal window that live-tails the log file using PowerShell's + * {@code Get-Content -Wait}, so it behaves just like the old built-in console did. Windows-only, + * matching the rest of this Windows-only application; silently does nothing on any other OS. + */ + private static void openLiveLogWindow(Path logFile) { + if (!isWindows()) { + return; + } + + try { + Path script = Files.createTempFile("BoatBookingSystem-devlog-", ".cmd"); + String command = + "@echo off\r\n" + + "title Boat Booking System - Console (console=true)\r\n" + + "powershell -NoExit -Command \"Get-Content -Path '" + + logFile.toAbsolutePath() + + "' -Wait -Tail 200\"\r\n"; + Files.writeString(script, command, StandardCharsets.US_ASCII); + + new ProcessBuilder("cmd.exe", "/c", "start", "\"\"", script.toString()).start(); + } catch (IOException exception) { + System.err.println( + "[LoggingConfigurator] Could not open the dev log window: " + exception.getMessage()); + } + } + + private static boolean isWindows() { + return System.getProperty("os.name", "").toLowerCase().contains("win"); + } +} diff --git a/src/main/tenants/honnavar/Config.properties b/src/main/tenants/honnavar/Config.properties index 8003842..896f54d 100644 --- a/src/main/tenants/honnavar/Config.properties +++ b/src/main/tenants/honnavar/Config.properties @@ -75,3 +75,5 @@ printer.name= printer.left.margin.dots=140 printer.width.dots=580 printer.characters.per.line=53 + +console=False diff --git a/src/main/tenants/kasarkod/Config.properties b/src/main/tenants/kasarkod/Config.properties index 96f5ba7..887641a 100644 --- a/src/main/tenants/kasarkod/Config.properties +++ b/src/main/tenants/kasarkod/Config.properties @@ -42,3 +42,6 @@ printer.characters.per.line=53 # UPDATE NETWORK TIMEOUT # ---------------------------------------------------------------------- update.connection.timeout.seconds=30 + + +console=False