diff --git a/src/main/java/com/sheepit/client/Configuration.java b/src/main/java/com/sheepit/client/Configuration.java index 79ad6c4..29007f3 100644 --- a/src/main/java/com/sheepit/client/Configuration.java +++ b/src/main/java/com/sheepit/client/Configuration.java @@ -52,6 +52,7 @@ import lombok.Data; } // accept job for ... private String configFilePath; + private String logDirectory; private File workingDirectory; private File sharedDownloadsDirectory; private File storageDirectory; // for permanent storage (binary archive) @@ -83,6 +84,7 @@ import lombok.Data; public Configuration(File cache_dir_, String login_, String password_) { this.configFilePath = null; + this.logDirectory = null; this.login = login_; this.password = password_; this.proxy = null; @@ -122,6 +124,7 @@ import lombok.Data; return c + "version: " + getJarVersion() + n + c + "configFilePath: " + configFilePath + n + + c + "logDir: " + (logDirectory == null ? "NULL" : logDirectory) + n + c + "workingDirectory: " + workingDirectory + n + c + "sharedDownloadsDirectory: " + sharedDownloadsDirectory + n + c + "storageDirectory: " + storageDirectory + n + diff --git a/src/main/java/com/sheepit/client/Log.java b/src/main/java/com/sheepit/client/Log.java index 9ab900b..ed6148f 100644 --- a/src/main/java/com/sheepit/client/Log.java +++ b/src/main/java/com/sheepit/client/Log.java @@ -19,6 +19,11 @@ package com.sheepit.client; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -36,8 +41,11 @@ public class Log { private boolean printStdOut; - private Log(boolean print_) { + private String logFile; + + private Log(boolean print_, String logDirectory_) { this.printStdOut = print_; + this.logFile = logDirectory_ + File.separator + "sheepit.log"; this.lastCheckPoint = 0; this.checkpoints.put(this.lastCheckPoint, new ArrayList()); this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss"); @@ -81,6 +89,15 @@ public class Log { if (this.printStdOut) { System.out.println(line); } + + if (this.logFile != null && this.logFile.isEmpty() == false) { + try { + Files.write(Paths.get(this.logFile), (line + "\n").getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE); + } + catch (IOException e) { + // :( catch-22, we can't really write the exception on the log file + } + } } } catch (Exception e) { @@ -109,11 +126,14 @@ public class Log { public static synchronized Log getInstance(Configuration config) { if (instance == null) { + String path = null; boolean print = false; if (config != null) { print = config.isPrintLog(); + path = config.getLogDirectory() == null ? null : config.getLogDirectory(); } - instance = new Log(print); + + instance = new Log(print, path); } return instance; } diff --git a/src/main/java/com/sheepit/client/SettingsLoader.java b/src/main/java/com/sheepit/client/SettingsLoader.java index d3486e8..1a97d50 100644 --- a/src/main/java/com/sheepit/client/SettingsLoader.java +++ b/src/main/java/com/sheepit/client/SettingsLoader.java @@ -64,6 +64,7 @@ public class SettingsLoader { HEADLESS("headless"), UI("ui"), THEME("theme"), + LOG_DIR("log-dir"), DISABLE_LARGE_DOWNLOADS("disable-large-downloads"); String propertyName; @@ -98,6 +99,7 @@ public class SettingsLoader { public static final String ARG_EXTRAS = "-extras"; public static final String ARG_UI = "-ui"; public static final String ARG_CONFIG = "-config"; + public static final String ARG_LOG_DIRECTORY = "-logdir"; public static final String ARG_VERSION = "--version"; public static final String ARG_SHOW_GPU = "--show-gpu"; public static final String ARG_NO_SYSTRAY = "--no-systray"; @@ -114,7 +116,7 @@ public class SettingsLoader { private Option login; private Option password; - + private Option logDir; private Option proxy; private Option hostname; private Option computeMethod; @@ -165,7 +167,6 @@ public class SettingsLoader { theme = setValue(theme_, theme, ARG_THEME); disableLargeDownloads = setValue(disableLargeDownloads_.toString(), disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS); - if (cores_ > 0) { cores = setValue(cores_.toString(), cores, ARG_CORES); } @@ -285,6 +286,7 @@ public class SettingsLoader { setProperty(prop, configFileProp, PropertyNames.UI, ui); setProperty(prop, configFileProp, PropertyNames.THEME, theme); setProperty(prop, configFileProp, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads); + setProperty(prop, configFileProp, PropertyNames.LOG_DIR, logDir); prop.store(output, null); } catch (IOException io) { @@ -389,6 +391,8 @@ public class SettingsLoader { disableLargeDownloads = loadConfigOption(prop, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS); + logDir = loadConfigOption(prop, PropertyNames.LOG_DIR, logDir, ARG_LOG_DIRECTORY); + if (prop.containsKey(PropertyNames.PRIORITY.propertyName)) { int prio = Integer.parseInt(prop.getProperty(PropertyNames.PRIORITY.propertyName)); if (priority == null) { @@ -534,6 +538,10 @@ public class SettingsLoader { if (config.isAutoSignIn() == false && autoSignIn != null) { config.setAutoSignIn(Boolean.parseBoolean(autoSignIn.getValue())); } + + if (config.getLogDirectory() != null && logDir != null) { + config.setLogDirectory(logDir.getValue()); + } } private void initWithDefaults() { @@ -556,11 +564,12 @@ public class SettingsLoader { this.theme = null; this.cores = new Option<>(String.valueOf(defaultConfigValues.getNbCores()), ARG_CORES); this.disableLargeDownloads = new Option<>(String.valueOf(defaultConfigValues.isDisableLargeDownloads()), ARG_DISABLE_LARGE_DOWNLOADS); + this.logDir = null; } @Override public String toString() { return String.format( - "SettingsLoader [path=%s, login=%s, password=%s, computeMethod=%s, gpu=%s, cacheDir=%s, sharedZip=%s, theme=%s, priority=%d, autosign=%s, usetray=%s, headless=%s, disableLargeDownloads=%s]", - path, login, password, computeMethod, gpu, cacheDir, sharedZip, theme, priority, autoSignIn, useSysTray, headless, disableLargeDownloads); + "SettingsLoader [path=%s, login=%s, password=%s, computeMethod=%s, gpu=%s, cacheDir=%s, sharedZip=%s, theme=%s, priority=%d, autosign=%s, usetray=%s, headless=%s, disableLargeDownloads=%s, logDir=%s]", + path, login, password, computeMethod, gpu, cacheDir, sharedZip, theme, priority, autoSignIn, useSysTray, headless, disableLargeDownloads, logDir); } } diff --git a/src/main/java/com/sheepit/client/standalone/Worker.java b/src/main/java/com/sheepit/client/standalone/Worker.java index e723eba..507c158 100644 --- a/src/main/java/com/sheepit/client/standalone/Worker.java +++ b/src/main/java/com/sheepit/client/standalone/Worker.java @@ -95,6 +95,8 @@ public class Worker { @Option(name = SettingsLoader.ARG_CONFIG, usage = "Specify the configuration file", required = false) private String config_file = null; + @Option(name = SettingsLoader.ARG_LOG_DIRECTORY, usage = "Specify the log directory", required = false) private String log_dir = null; + @Option(name = SettingsLoader.ARG_VERSION, usage = "Display application version", required = false, handler = VersionParameterHandler.class) private VersionParameterHandler versionHandler; @Option(name = SettingsLoader.ARG_SHOW_GPU, usage = "Print available GPU devices and exit", required = false, handler = ListGpuParameterHandler.class) private ListGpuParameterHandler listGpuParameterHandler; @@ -457,6 +459,10 @@ public class Worker { settingsLoader.markLaunchSettings(List.of(args)); } + // config file and log file should be next to each other + String configFilePath = config.getConfigFilePath() != null ? config.getConfigFilePath() : OS.getOS().getDefaultConfigFilePath(); + config.setLogDirectory(log_dir != null ? log_dir : (new File (configFilePath).getParent())); + Log.getInstance(config).debug("client version " + Configuration.jarVersion); // Hostname change will overwrite the existing one (default or read from configuration file) but changes will be lost when the client closes