Migrated configuration and logging to Envoy Common (#113)
* Moved client specific configuration to ClientConfig * Finished ClientConfig integration, checking init state in EnvoyLog * Migrated Config, ConfigItem and EnvoyLog to envoy-common * Updated envoy-common dependency to develop-SNAPSHOT
This commit is contained in:
		@@ -6,7 +6,7 @@ import java.util.Queue;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Stores elements in a queue to process them later.<br>
 | 
			
		||||
 
 | 
			
		||||
@@ -2,33 +2,38 @@ package envoy.client.data;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.security.NoSuchAlgorithmException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
 | 
			
		||||
import envoy.data.Config;
 | 
			
		||||
import envoy.data.ConfigItem;
 | 
			
		||||
import envoy.data.LoginCredentials;
 | 
			
		||||
import envoy.exception.EnvoyException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Manages all application settings that are set during application startup by
 | 
			
		||||
 * either loading them from the {@link Properties} file
 | 
			
		||||
 * {@code client.properties} or parsing them from the command line arguments of
 | 
			
		||||
 * the application.<br>
 | 
			
		||||
 * Implements a configuration specific to the Envoy Client with default values
 | 
			
		||||
 * and convenience methods.<br>
 | 
			
		||||
 * <br>
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
 * File: <strong>Config.java</strong><br>
 | 
			
		||||
 * Created: <strong>12 Oct 2019</strong><br>
 | 
			
		||||
 * File: <strong>ClientConfig.java</strong><br>
 | 
			
		||||
 * Created: <strong>01.03.2020</strong><br>
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kai S. K. Engelbart
 | 
			
		||||
 * @since Envoy v0.1-alpha
 | 
			
		||||
 * @since Envoy v0.1-beta
 | 
			
		||||
 */
 | 
			
		||||
public class Config {
 | 
			
		||||
public class ClientConfig extends Config {
 | 
			
		||||
 | 
			
		||||
	private Map<String, ConfigItem<?>> items = new HashMap<>();
 | 
			
		||||
	private static ClientConfig config;
 | 
			
		||||
 | 
			
		||||
	private static Config config;
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the singleton instance of the client config
 | 
			
		||||
	 * @since Envoy v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	public static ClientConfig getInstance() {
 | 
			
		||||
		if (config == null) config = new ClientConfig();
 | 
			
		||||
		return config;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private Config() {
 | 
			
		||||
	private ClientConfig() {
 | 
			
		||||
		items.put("server", new ConfigItem<>("server", "s", Function.identity(), null, true));
 | 
			
		||||
		items.put("port", new ConfigItem<>("port", "p", Integer::parseInt, null, true));
 | 
			
		||||
		items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), true));
 | 
			
		||||
@@ -40,72 +45,6 @@ public class Config {
 | 
			
		||||
		items.put("password", new ConfigItem<>("password", "pw", String::toCharArray));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the singleton instance of the {@link Config}
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public static Config getInstance() {
 | 
			
		||||
		if (config == null) config = new Config();
 | 
			
		||||
		return config;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Defaults to the {@code client.properties} file for information.
 | 
			
		||||
	 * This file contains information about
 | 
			
		||||
	 * the server and port, as well as the path to the local
 | 
			
		||||
	 * database and the synchronization timeout
 | 
			
		||||
	 *
 | 
			
		||||
	 * @throws EnvoyException if the {@code client.properties} file could not be
 | 
			
		||||
	 *                        loaded
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void load() throws EnvoyException {
 | 
			
		||||
		ClassLoader loader = getClass().getClassLoader();
 | 
			
		||||
		try {
 | 
			
		||||
			Properties properties = new Properties();
 | 
			
		||||
			properties.load(loader.getResourceAsStream("client.properties"));
 | 
			
		||||
			items.forEach((name, item) -> { if (properties.containsKey(name)) item.parse(properties.getProperty(name)); });
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			throw new EnvoyException("Failed to load client.properties", e);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Sets the server, port and localDB path via command line properties --server /
 | 
			
		||||
	 * -s, --port / -p and --localDB / -db.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param args the command line arguments to parse
 | 
			
		||||
	 * @throws EnvoyException if the command line arguments contain an unknown token
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void load(String[] args) throws EnvoyException {
 | 
			
		||||
		for (int i = 0; i < args.length; i++)
 | 
			
		||||
			for (ConfigItem<?> item : items.values())
 | 
			
		||||
				if (args[i].startsWith("--")) {
 | 
			
		||||
					if (args[i].length() == 2) throw new EnvoyException("Malformed command line argument at position " + i);
 | 
			
		||||
					final String commandLong = args[i].substring(2);
 | 
			
		||||
					if (item.getCommandLong().equals(commandLong)) {
 | 
			
		||||
						item.parse(args[++i]);
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
				} else if (args[i].startsWith("-")) {
 | 
			
		||||
					if (args[i].length() == 1) throw new EnvoyException("Malformed command line argument at position " + i);
 | 
			
		||||
					final String commandShort = args[i].substring(1);
 | 
			
		||||
					if (item.getCommandShort().equals(commandShort)) {
 | 
			
		||||
						item.parse(args[++i]);
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
				} else throw new EnvoyException("Malformed command line argument at position " + i);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return {@code true} if all mandatory config items are initialized
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public boolean isInitialized() {
 | 
			
		||||
		return items.values().stream().filter(ConfigItem::isMandatory).map(ConfigItem::get).noneMatch(Objects::isNull);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the host name of the Envoy server
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
@@ -178,4 +117,4 @@ public class Config {
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -1,92 +0,0 @@
 | 
			
		||||
package envoy.client.data;
 | 
			
		||||
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Contains a single {@link Config} value as well as the corresponding command
 | 
			
		||||
 * line arguments and its default value.<br>
 | 
			
		||||
 * <br>
 | 
			
		||||
 * Project: <strong>envoy-clientChess</strong><br>
 | 
			
		||||
 * File: <strong>ConfigItem.javaEvent.java</strong><br>
 | 
			
		||||
 * Created: <strong>21.12.2019</strong><br>
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kai S. K. Engelbart
 | 
			
		||||
 * @param <T> the type of the config item's value
 | 
			
		||||
 * @since Envoy v0.2-alpha
 | 
			
		||||
 */
 | 
			
		||||
public class ConfigItem<T> {
 | 
			
		||||
 | 
			
		||||
	private final String				commandLong, commandShort;
 | 
			
		||||
	private final Function<String, T>	parseFunction;
 | 
			
		||||
	private final boolean				mandatory;
 | 
			
		||||
 | 
			
		||||
	private T value;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Initializes a {@link ConfigItem}.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param commandLong   the long command line argument to set this value
 | 
			
		||||
	 * @param commandShort  the short command line argument to set this value
 | 
			
		||||
	 * @param parseFunction the {@code Function<String, T>} that parses the value
 | 
			
		||||
	 *                      from a string
 | 
			
		||||
	 * @param defaultValue  the optional default value to set before parsing
 | 
			
		||||
	 * @param mandatory     indicated that this config item must be initialized with
 | 
			
		||||
	 *                      a non-null value
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction, T defaultValue, boolean mandatory) {
 | 
			
		||||
		this.commandLong	= commandLong;
 | 
			
		||||
		this.commandShort	= commandShort;
 | 
			
		||||
		this.parseFunction	= parseFunction;
 | 
			
		||||
		this.mandatory		= mandatory;
 | 
			
		||||
		value				= defaultValue;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Initializes an optional {@link ConfigItem} without a default value.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param commandLong   the long command line argument to set this value
 | 
			
		||||
	 * @param commandShort  the short command line argument to set this value
 | 
			
		||||
	 * @param parseFunction the {@code Function<String, T>} that parses the value
 | 
			
		||||
	 *                      from a string
 | 
			
		||||
	 * @since Envoy v0.3-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction) {
 | 
			
		||||
		this(commandLong, commandShort, parseFunction, null, false);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Parses this {@ConfigItem}'s value from a string.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param input the string to parse from
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void parse(String input) { value = parseFunction.apply(input); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return The long command line argument to set the value of this
 | 
			
		||||
	 *         {@link ConfigItem}
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public String getCommandLong() { return commandLong; }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return The short command line argument to set the value of this
 | 
			
		||||
	 *         {@link ConfigItem}
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public String getCommandShort() { return commandShort; }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the value of this {@link ConfigItem}
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public T get() { return value; }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return {@code true} if this {@link ConfigItem} is mandatory for successful
 | 
			
		||||
	 *         application initialization
 | 
			
		||||
	 * @since Envoy v0.3-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public boolean isMandatory() { return mandatory; }
 | 
			
		||||
}
 | 
			
		||||
@@ -5,6 +5,7 @@ import java.io.IOException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
import envoy.data.ConfigItem;
 | 
			
		||||
import envoy.data.IdGenerator;
 | 
			
		||||
import envoy.util.SerializationUtils;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -33,12 +33,12 @@ public class Settings {
 | 
			
		||||
	/**
 | 
			
		||||
	 * Settings are stored in this file.
 | 
			
		||||
	 */
 | 
			
		||||
	private static final File settingsFile = new File(Config.getInstance().getHomeDirectory(), "settings.ser");
 | 
			
		||||
	private static final File settingsFile = new File(ClientConfig.getInstance().getHomeDirectory(), "settings.ser");
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * User-defined themes are stored inside this file.
 | 
			
		||||
	 */
 | 
			
		||||
	private static final File themeFile = new File(Config.getInstance().getHomeDirectory(), "themes.ser");
 | 
			
		||||
	private static final File themeFile = new File(ClientConfig.getInstance().getHomeDirectory(), "themes.ser");
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Singleton instance of this class.
 | 
			
		||||
 
 | 
			
		||||
@@ -10,13 +10,13 @@ import java.util.logging.Logger;
 | 
			
		||||
import javax.naming.TimeLimitExceededException;
 | 
			
		||||
 | 
			
		||||
import envoy.client.data.Cache;
 | 
			
		||||
import envoy.client.data.Config;
 | 
			
		||||
import envoy.client.data.ClientConfig;
 | 
			
		||||
import envoy.client.data.LocalDb;
 | 
			
		||||
import envoy.client.event.SendEvent;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.*;
 | 
			
		||||
import envoy.event.*;
 | 
			
		||||
import envoy.event.ContactOperationEvent.Operation;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
import envoy.util.SerializationUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -45,8 +45,8 @@ public class Client implements Closeable {
 | 
			
		||||
	private volatile boolean	rejected;
 | 
			
		||||
 | 
			
		||||
	// Configuration and logging
 | 
			
		||||
	private static final Config	config	= Config.getInstance();
 | 
			
		||||
	private static final Logger	logger	= EnvoyLog.getLogger(Client.class);
 | 
			
		||||
	private static final ClientConfig	config	= ClientConfig.getInstance();
 | 
			
		||||
	private static final Logger			logger	= EnvoyLog.getLogger(Client.class);
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Enters the online mode by acquiring a user ID from the server. As a
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,10 @@ package envoy.client.net;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.Message.MessageStatus;
 | 
			
		||||
import envoy.event.EventBus;
 | 
			
		||||
import envoy.event.MessageStatusChangeEvent;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,10 @@ import java.util.function.Consumer;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import envoy.client.event.MessageCreationEvent;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.Message;
 | 
			
		||||
import envoy.data.Message.MessageStatus;
 | 
			
		||||
import envoy.event.EventBus;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ import java.util.function.Consumer;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
import envoy.util.SerializationUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,9 @@ import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import envoy.client.data.LocalDb;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.Message;
 | 
			
		||||
import envoy.event.MessageStatusChangeEvent;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Implements methods to send {@link Message}s and
 | 
			
		||||
 
 | 
			
		||||
@@ -22,12 +22,12 @@ import envoy.client.net.WriteProxy;
 | 
			
		||||
import envoy.client.ui.list.ComponentList;
 | 
			
		||||
import envoy.client.ui.list.ComponentListModel;
 | 
			
		||||
import envoy.client.ui.settings.SettingsScreen;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.Message;
 | 
			
		||||
import envoy.data.Message.MessageStatus;
 | 
			
		||||
import envoy.data.MessageBuilder;
 | 
			
		||||
import envoy.data.User;
 | 
			
		||||
import envoy.event.*;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
 
 | 
			
		||||
@@ -14,13 +14,13 @@ import javax.swing.border.EmptyBorder;
 | 
			
		||||
import envoy.client.data.*;
 | 
			
		||||
import envoy.client.event.HandshakeSuccessfulEvent;
 | 
			
		||||
import envoy.client.net.Client;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.LoginCredentials;
 | 
			
		||||
import envoy.data.Message;
 | 
			
		||||
import envoy.data.User;
 | 
			
		||||
import envoy.event.EventBus;
 | 
			
		||||
import envoy.event.HandshakeRejectionEvent;
 | 
			
		||||
import envoy.exception.EnvoyException;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
@@ -59,9 +59,9 @@ public class LoginDialog extends JDialog {
 | 
			
		||||
	private final LocalDb			localDb;
 | 
			
		||||
	private final Cache<Message>	receivedMessageCache;
 | 
			
		||||
 | 
			
		||||
	private static final Config	config				= Config.getInstance();
 | 
			
		||||
	private static final Logger	logger				= EnvoyLog.getLogger(LoginDialog.class);
 | 
			
		||||
	private static final long	serialVersionUID	= 352021600833907468L;
 | 
			
		||||
	private static final ClientConfig	config				= ClientConfig.getInstance();
 | 
			
		||||
	private static final Logger			logger				= EnvoyLog.getLogger(LoginDialog.class);
 | 
			
		||||
	private static final long			serialVersionUID	= 352021600833907468L;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Displays a dialog enabling the user to enter their user name and password.
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import java.awt.EventQueue;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.Properties;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
@@ -14,9 +15,10 @@ import javax.swing.SwingUtilities;
 | 
			
		||||
import envoy.client.data.*;
 | 
			
		||||
import envoy.client.net.Client;
 | 
			
		||||
import envoy.client.net.WriteProxy;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.data.Config;
 | 
			
		||||
import envoy.data.Message;
 | 
			
		||||
import envoy.exception.EnvoyException;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Starts the Envoy client and prompts the user to enter their name.<br>
 | 
			
		||||
@@ -48,13 +50,15 @@ public class Startup {
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public static void main(String[] args) {
 | 
			
		||||
		Config config = Config.getInstance();
 | 
			
		||||
		ClientConfig config = ClientConfig.getInstance();
 | 
			
		||||
 | 
			
		||||
		SwingUtilities.invokeLater(() -> chatWindow = new ChatWindow());
 | 
			
		||||
 | 
			
		||||
		try {
 | 
			
		||||
			// Load the configuration from client.properties first
 | 
			
		||||
			config.load();
 | 
			
		||||
			Properties properties = new Properties();
 | 
			
		||||
			properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties"));
 | 
			
		||||
			config.load(properties);
 | 
			
		||||
 | 
			
		||||
			// Override configuration values with command line arguments
 | 
			
		||||
			if (args.length > 0) config.load(args);
 | 
			
		||||
@@ -62,13 +66,13 @@ public class Startup {
 | 
			
		||||
			// Check if all mandatory configuration values have been initialized
 | 
			
		||||
			if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized");
 | 
			
		||||
		} catch (Exception e) {
 | 
			
		||||
			JOptionPane
 | 
			
		||||
				.showMessageDialog(null, "Error loading configuration values:\n" + e.toString(), "Configuration error", JOptionPane.ERROR_MESSAGE);
 | 
			
		||||
			JOptionPane.showMessageDialog(null, "Error loading configuration values:\n" + e, "Configuration error", JOptionPane.ERROR_MESSAGE);
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
			System.exit(1);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Setup logger for the envoy package
 | 
			
		||||
		EnvoyLog.initialize(config);
 | 
			
		||||
		EnvoyLog.attach("envoy");
 | 
			
		||||
		EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
 | 
			
		||||
		EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
 | 
			
		||||
@@ -85,8 +89,7 @@ public class Startup {
 | 
			
		||||
			localDb = new PersistentLocalDb(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
 | 
			
		||||
		} catch (IOException e3) {
 | 
			
		||||
			logger.log(Level.SEVERE, "Could not initialize local database", e3);
 | 
			
		||||
			JOptionPane
 | 
			
		||||
				.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
 | 
			
		||||
			JOptionPane.showMessageDialog(null, "Could not initialize local database!\n" + e3, "Local database error", JOptionPane.ERROR_MESSAGE);
 | 
			
		||||
			System.exit(1);
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ import javax.swing.JTextPane;
 | 
			
		||||
import envoy.client.data.Settings;
 | 
			
		||||
import envoy.client.data.SettingsItem;
 | 
			
		||||
import envoy.client.ui.Theme;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Displays GUI components that allow general settings regarding the client.<br>
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,8 @@ import envoy.client.data.Settings;
 | 
			
		||||
import envoy.client.event.ThemeChangeEvent;
 | 
			
		||||
import envoy.client.ui.PrimaryButton;
 | 
			
		||||
import envoy.client.ui.Theme;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.event.EventBus;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class provides the GUI to change the user specific settings.<br>
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,8 @@ import envoy.client.data.Settings;
 | 
			
		||||
import envoy.client.event.ThemeChangeEvent;
 | 
			
		||||
import envoy.client.ui.Color;
 | 
			
		||||
import envoy.client.ui.Theme;
 | 
			
		||||
import envoy.client.util.EnvoyLog;
 | 
			
		||||
import envoy.event.EventBus;
 | 
			
		||||
import envoy.util.EnvoyLog;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Displays GUI components that allow changing the current {@Theme} and creating
 | 
			
		||||
 
 | 
			
		||||
@@ -1,107 +0,0 @@
 | 
			
		||||
package envoy.client.util;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.text.SimpleDateFormat;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.logging.*;
 | 
			
		||||
 | 
			
		||||
import envoy.client.data.Config;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Configures the {@link java.util.logging} API to output the log into the
 | 
			
		||||
 * console and a log file.<br>
 | 
			
		||||
 * <br>
 | 
			
		||||
 * Call the {@link EnvoyLog#attach(String)} method to configure a part of the
 | 
			
		||||
 * logger hierarchy.<br>
 | 
			
		||||
 * <br>
 | 
			
		||||
 * Project: <strong>envoy-client</strong><br>
 | 
			
		||||
 * File: <strong>EnvoyLogger.java</strong><br>
 | 
			
		||||
 * Created: <strong>14 Dec 2019</strong><br>
 | 
			
		||||
 *
 | 
			
		||||
 * @author Leon Hofmeister
 | 
			
		||||
 * @author Kai S. K. Engelbart
 | 
			
		||||
 * @since Envoy v0.2-alpha
 | 
			
		||||
 */
 | 
			
		||||
public class EnvoyLog {
 | 
			
		||||
 | 
			
		||||
	private static FileHandler		fileHandler;
 | 
			
		||||
	private static StreamHandler	consoleHandler;
 | 
			
		||||
 | 
			
		||||
	static {
 | 
			
		||||
 | 
			
		||||
		// Remove default console handler
 | 
			
		||||
		LogManager.getLogManager().reset();
 | 
			
		||||
 | 
			
		||||
		// Configure log file
 | 
			
		||||
		File logFile = new File(Config.getInstance().getHomeDirectory(),
 | 
			
		||||
				"log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log");
 | 
			
		||||
		logFile.getParentFile().mkdirs();
 | 
			
		||||
 | 
			
		||||
		// Configure formatting
 | 
			
		||||
		System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %n");
 | 
			
		||||
		SimpleFormatter formatter = new SimpleFormatter();
 | 
			
		||||
 | 
			
		||||
		try {
 | 
			
		||||
			fileHandler = new FileHandler(logFile.getAbsolutePath());
 | 
			
		||||
			fileHandler.setLevel(Config.getInstance().getFileLevelBarrier());
 | 
			
		||||
			fileHandler.setFormatter(formatter);
 | 
			
		||||
		} catch (SecurityException | IOException e) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
		consoleHandler = new StreamHandler(System.out, formatter);
 | 
			
		||||
		consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier());
 | 
			
		||||
		consoleHandler.setFormatter(formatter);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private EnvoyLog() {}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Configures all loggers that are contained within the hierarchy of a specific
 | 
			
		||||
	 * path to use the console and file handlers.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param path the path to the loggers to configure
 | 
			
		||||
	 * @since Envoy Client v0.1-beta
 | 
			
		||||
	 */
 | 
			
		||||
	public static void attach(String path) {
 | 
			
		||||
		// Get root logger
 | 
			
		||||
		Logger logger = Logger.getLogger(path);
 | 
			
		||||
 | 
			
		||||
		// Add handlers
 | 
			
		||||
		if (fileHandler != null) logger.addHandler(fileHandler);
 | 
			
		||||
		logger.addHandler(consoleHandler);
 | 
			
		||||
 | 
			
		||||
		// Delegate logger level filtering to the handlers
 | 
			
		||||
		logger.setLevel(Level.ALL);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Creates a logger for a specified class, which output will be displayed inside
 | 
			
		||||
	 * the console and written to the log file.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param logClass the class in which the logger is used
 | 
			
		||||
	 * @return the created logger
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public static Logger getLogger(Class<?> logClass) {
 | 
			
		||||
		return Logger.getLogger(logClass.getCanonicalName());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Defines the logger level required for a record to be written to the log file.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param fileLevelBarrier the log file level
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Defines the logger level required for a record to be written to the console.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param consoleLevelBarrier the console logger level
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public static void setConsoleLevelBarrier(Level consoleLevelBarrier) {
 | 
			
		||||
		if (consoleHandler != null) consoleHandler.setLevel(consoleLevelBarrier);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user