Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/com/boatticket/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down Expand Up @@ -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.
*
* <p>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
// ------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/boatticket/config/LoggingConfigurator.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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):
*
* <ul>
* <li>{@code console=false} (default, and what every shipped tenant config uses): no window, no
* file, zero overhead. This is what every client/end user always sees.
* <li>{@code console=true}: all java.util.logging output is written to a log file AND a separate
* terminal window is spawned that live-tails that file, so it looks and behaves just like the
* old built-in console window did — except now it's controlled by editing one line in a
* properties file and restarting the app, not by how the installer was packaged.
* </ul>
*/
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");
}
}
2 changes: 2 additions & 0 deletions src/main/tenants/honnavar/Config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ printer.name=
printer.left.margin.dots=140
printer.width.dots=580
printer.characters.per.line=53

console=False
3 changes: 3 additions & 0 deletions src/main/tenants/kasarkod/Config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ printer.characters.per.line=53
# UPDATE NETWORK TIMEOUT
# ----------------------------------------------------------------------
update.connection.timeout.seconds=30


console=False
Loading