Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

5.0 KB
Raw Blame
package opensuite.core;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Verbose diagnostic logging, enabled with the {@code --debug} command-line
 * flag. When on, every log line is written to the console and appended to a
 * dated file under {@code ~/.opensuite/logs/}, and uncaught exceptions are
 * appended there too, so a crash or a failed launch leaves a full trace to
 * work from instead of vanishing.
 *
 * Uncaught exceptions are captured on the console even without {@code --debug}:
 * {@link #installUncaughtHandler} is called unconditionally at startup.
 *
 * When off, this is nearly free: {@link #log} returns immediately.
 */
public final class Debug {

    private static volatile boolean enabled;
    private static PrintStream file;
    private static Path logPath;

    private static final DateTimeFormatter LINE_TS = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
    private static final DateTimeFormatter FILE_TS = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");

    private Debug() {}

    public static boolean enabled() { return enabled; }

    /** The file the current session is logging to, or null if not enabled. */
    public static Path logFile() { return logPath; }

    /**
     * Capture every uncaught exception on every thread (the Swing event
     * thread routes here too), so a failure is never a bare stack trace with
     * no context. Installed unconditionally at startup; with {@code --debug}
     * the trace is also appended to the session log file.
     */
    public static void installUncaughtHandler() {
        Thread.setDefaultUncaughtExceptionHandler(
                (t, ex) -> error("uncaught exception in thread '" + t.getName() + "'", ex));
    }

    /** Turn on debug logging: open the dated log file. */
    public static synchronized void enable() {
        if (enabled) return;
        enabled = true;
        try {
            Path dir = Path.of(System.getProperty("user.home"), ".opensuite", "logs");
            Files.createDirectories(dir);
            logPath = dir.resolve("opensuite-" + LocalDateTime.now().format(FILE_TS) + ".log");
            file = new PrintStream(new FileOutputStream(logPath.toFile(), true), true, "UTF-8");
        } catch (Exception e) {
            System.err.println("[debug] could not open log file: " + e);
        }

        log("debug logging enabled");
        if (logPath != null) log("log file: " + logPath);
        log("Open Suite " + opensuite.ui.Main.VERSION
                + " | java " + System.getProperty("java.version")
                + " | " + System.getProperty("os.name") + " " + System.getProperty("os.version")
                + " | scale uiScale=" + System.getProperty("sun.java2d.uiScale", "?")
                + " scaled=" + System.getProperty("opensuite.scaled", "?"));
    }

    /** Write a timestamped line to the console and the log file (no-op unless enabled). */
    public static void log(String msg) {
        if (!enabled) return;
        String line = "[" + LocalDateTime.now().format(LINE_TS) + "] " + msg;
        System.err.println(line);
        if (file != null) file.println(line);
    }

    /** Log a message and a full stack trace. Always prints, so crashes are never silent. */
    public static void error(String msg, Throwable ex) {
        String line = "[" + LocalDateTime.now().format(LINE_TS) + "] ERROR: " + msg;
        System.err.println(line);
        if (ex != null) ex.printStackTrace(System.err);
        if (file != null) {
            file.println(line);
            if (ex != null) ex.printStackTrace(file);
        } else if (keepErrors) {
            keep(line, ex);
        }
    }

    private static volatile boolean keepErrors;

    /** Have errors kept on disk when no debug log is open. The program asks for this as it starts; its tests do not. */
    public static void keepErrorsOnDisk() { keepErrors = true; }

    private static final long ERRORS_MAX = 512 * 1024;

    /**
     * Without {@code --debug} there is no session log, and a copy started from
     * a desktop icon has no console either. Errors are then kept in one small
     * file, {@code ~/.opensuite/logs/errors.log}, started afresh when it grows
     * large, so that a failure can still be looked into afterwards.
     */
    private static synchronized void keep(String line, Throwable ex) {
        try {
            Path dir = Path.of(System.getProperty("user.home"), ".opensuite", "logs");
            Files.createDirectories(dir);
            Path f = dir.resolve("errors.log");
            boolean fresh = !Files.exists(f) || Files.size(f) > ERRORS_MAX;
            try (PrintStream out = new PrintStream(new FileOutputStream(f.toFile(), !fresh), true, "UTF-8")) {
                out.println(LocalDateTime.now().toLocalDate() + " " + line);
                if (ex != null) ex.printStackTrace(out);
            }
        } catch (Exception ignored) { /* the console still has it */ }
    }
}