package org.xbib.logging.os;

/**
 * Utilities for getting information about the current process.
 */
public final class Process {
    private Process() {
    }

    /**
     * Get the name of this process. If the process name is not known, then "&lt;unknown&gt;" is returned.
     * The process name may be overridden by setting the {@code xbib.process.name} property.
     *
     * @return the process name (not {@code null})
     */
    public static String getProcessName() {
        return computeProcessName();
    }

    private static String computeProcessName() {
        final ProcessHandle processHandle = ProcessHandle.current();
        String processName = System.getProperty("xbib.process.name");
        if (processName == null) {
            processName = processHandle.info().command().orElse(null);
        }
        if (processName == null) {
            processName = "<unknown>";
        }
        return processName;
    }
}
