001// SPDX-License-Identifier: GPL-3.0-or-later
002
003package es.uvigo.esei.sing.textproc.logging;
004
005import java.util.concurrent.atomic.AtomicReference;
006import java.util.logging.Logger;
007
008import lombok.AccessLevel;
009import lombok.NoArgsConstructor;
010import lombok.NonNull;
011
012/**
013 * Entry point to the TextProc logging functionalities.
014 *
015 * @author Alejandro González García
016 * @implNote The implementation of this class is thread-safe.
017 */
018@NoArgsConstructor(access = AccessLevel.PRIVATE)
019public final class TextProcLogging {
020        private static final AtomicReference<Logger> LOGGER = new AtomicReference<>();
021
022        /**
023         * Initializes logging for the given application name. Subsequent calls to
024         * {@link #getLogger()} will return a logger with the application name specified
025         * here. This method only has any effects the first time it is invoked.
026         *
027         * @param appName The application name for the logger.
028         * @throws IllegalArgumentException If {@code appName} is {@code null}.
029         */
030        public static void initialize(@NonNull final String appName) {
031                LOGGER.compareAndSet(null, Logger.getLogger(appName));
032        }
033
034        /**
035         * Returns the logger previously initialized with {@link #initialize(String)}.
036         * This method always returns the same object.
037         *
038         * @return The described logger. It never is {@code null}.
039         * @throws IllegalStateException If the logger wasn't initialized yet.
040         */
041        public static Logger getLogger() {
042                final Logger logger = LOGGER.get();
043
044                if (logger == null) {
045                        throw new IllegalStateException("Logging hasn't been initialized");
046                }
047
048                return logger;
049        }
050}