Feat: add log file on disk
This commit is contained in:
@@ -52,6 +52,7 @@ import lombok.Data;
|
|||||||
} // accept job for ...
|
} // accept job for ...
|
||||||
|
|
||||||
private String configFilePath;
|
private String configFilePath;
|
||||||
|
private String logDirectory;
|
||||||
private File workingDirectory;
|
private File workingDirectory;
|
||||||
private File sharedDownloadsDirectory;
|
private File sharedDownloadsDirectory;
|
||||||
private File storageDirectory; // for permanent storage (binary archive)
|
private File storageDirectory; // for permanent storage (binary archive)
|
||||||
@@ -83,6 +84,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
public Configuration(File cache_dir_, String login_, String password_) {
|
public Configuration(File cache_dir_, String login_, String password_) {
|
||||||
this.configFilePath = null;
|
this.configFilePath = null;
|
||||||
|
this.logDirectory = null;
|
||||||
this.login = login_;
|
this.login = login_;
|
||||||
this.password = password_;
|
this.password = password_;
|
||||||
this.proxy = null;
|
this.proxy = null;
|
||||||
@@ -122,6 +124,7 @@ import lombok.Data;
|
|||||||
return
|
return
|
||||||
c + "version: " + getJarVersion() + n +
|
c + "version: " + getJarVersion() + n +
|
||||||
c + "configFilePath: " + configFilePath + n +
|
c + "configFilePath: " + configFilePath + n +
|
||||||
|
c + "logDir: " + (logDirectory == null ? "NULL" : logDirectory) + n +
|
||||||
c + "workingDirectory: " + workingDirectory + n +
|
c + "workingDirectory: " + workingDirectory + n +
|
||||||
c + "sharedDownloadsDirectory: " + sharedDownloadsDirectory + n +
|
c + "sharedDownloadsDirectory: " + sharedDownloadsDirectory + n +
|
||||||
c + "storageDirectory: " + storageDirectory + n +
|
c + "storageDirectory: " + storageDirectory + n +
|
||||||
|
|||||||
@@ -19,6 +19,11 @@
|
|||||||
|
|
||||||
package com.sheepit.client;
|
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.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -36,8 +41,11 @@ public class Log {
|
|||||||
|
|
||||||
private boolean printStdOut;
|
private boolean printStdOut;
|
||||||
|
|
||||||
private Log(boolean print_) {
|
private String logFile;
|
||||||
|
|
||||||
|
private Log(boolean print_, String logDirectory_) {
|
||||||
this.printStdOut = print_;
|
this.printStdOut = print_;
|
||||||
|
this.logFile = logDirectory_ + File.separator + "sheepit.log";
|
||||||
this.lastCheckPoint = 0;
|
this.lastCheckPoint = 0;
|
||||||
this.checkpoints.put(this.lastCheckPoint, new ArrayList<String>());
|
this.checkpoints.put(this.lastCheckPoint, new ArrayList<String>());
|
||||||
this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
|
this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
|
||||||
@@ -81,6 +89,15 @@ public class Log {
|
|||||||
if (this.printStdOut) {
|
if (this.printStdOut) {
|
||||||
System.out.println(line);
|
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) {
|
catch (Exception e) {
|
||||||
@@ -109,11 +126,14 @@ public class Log {
|
|||||||
|
|
||||||
public static synchronized Log getInstance(Configuration config) {
|
public static synchronized Log getInstance(Configuration config) {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
|
String path = null;
|
||||||
boolean print = false;
|
boolean print = false;
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
print = config.isPrintLog();
|
print = config.isPrintLog();
|
||||||
|
path = config.getLogDirectory() == null ? null : config.getLogDirectory();
|
||||||
}
|
}
|
||||||
instance = new Log(print);
|
|
||||||
|
instance = new Log(print, path);
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ public class SettingsLoader {
|
|||||||
HEADLESS("headless"),
|
HEADLESS("headless"),
|
||||||
UI("ui"),
|
UI("ui"),
|
||||||
THEME("theme"),
|
THEME("theme"),
|
||||||
|
LOG_DIR("log-dir"),
|
||||||
DISABLE_LARGE_DOWNLOADS("disable-large-downloads");
|
DISABLE_LARGE_DOWNLOADS("disable-large-downloads");
|
||||||
|
|
||||||
String propertyName;
|
String propertyName;
|
||||||
@@ -98,6 +99,7 @@ public class SettingsLoader {
|
|||||||
public static final String ARG_EXTRAS = "-extras";
|
public static final String ARG_EXTRAS = "-extras";
|
||||||
public static final String ARG_UI = "-ui";
|
public static final String ARG_UI = "-ui";
|
||||||
public static final String ARG_CONFIG = "-config";
|
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_VERSION = "--version";
|
||||||
public static final String ARG_SHOW_GPU = "--show-gpu";
|
public static final String ARG_SHOW_GPU = "--show-gpu";
|
||||||
public static final String ARG_NO_SYSTRAY = "--no-systray";
|
public static final String ARG_NO_SYSTRAY = "--no-systray";
|
||||||
@@ -114,7 +116,7 @@ public class SettingsLoader {
|
|||||||
private Option<String> login;
|
private Option<String> login;
|
||||||
|
|
||||||
private Option<String> password;
|
private Option<String> password;
|
||||||
|
private Option<String> logDir;
|
||||||
private Option<String> proxy;
|
private Option<String> proxy;
|
||||||
private Option<String> hostname;
|
private Option<String> hostname;
|
||||||
private Option<String> computeMethod;
|
private Option<String> computeMethod;
|
||||||
@@ -165,7 +167,6 @@ public class SettingsLoader {
|
|||||||
theme = setValue(theme_, theme, ARG_THEME);
|
theme = setValue(theme_, theme, ARG_THEME);
|
||||||
disableLargeDownloads = setValue(disableLargeDownloads_.toString(), disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
|
disableLargeDownloads = setValue(disableLargeDownloads_.toString(), disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
|
||||||
|
|
||||||
|
|
||||||
if (cores_ > 0) {
|
if (cores_ > 0) {
|
||||||
cores = setValue(cores_.toString(), cores, ARG_CORES);
|
cores = setValue(cores_.toString(), cores, ARG_CORES);
|
||||||
}
|
}
|
||||||
@@ -285,6 +286,7 @@ public class SettingsLoader {
|
|||||||
setProperty(prop, configFileProp, PropertyNames.UI, ui);
|
setProperty(prop, configFileProp, PropertyNames.UI, ui);
|
||||||
setProperty(prop, configFileProp, PropertyNames.THEME, theme);
|
setProperty(prop, configFileProp, PropertyNames.THEME, theme);
|
||||||
setProperty(prop, configFileProp, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads);
|
setProperty(prop, configFileProp, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads);
|
||||||
|
setProperty(prop, configFileProp, PropertyNames.LOG_DIR, logDir);
|
||||||
prop.store(output, null);
|
prop.store(output, null);
|
||||||
}
|
}
|
||||||
catch (IOException io) {
|
catch (IOException io) {
|
||||||
@@ -389,6 +391,8 @@ public class SettingsLoader {
|
|||||||
|
|
||||||
disableLargeDownloads = loadConfigOption(prop, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
|
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)) {
|
if (prop.containsKey(PropertyNames.PRIORITY.propertyName)) {
|
||||||
int prio = Integer.parseInt(prop.getProperty(PropertyNames.PRIORITY.propertyName));
|
int prio = Integer.parseInt(prop.getProperty(PropertyNames.PRIORITY.propertyName));
|
||||||
if (priority == null) {
|
if (priority == null) {
|
||||||
@@ -534,6 +538,10 @@ public class SettingsLoader {
|
|||||||
if (config.isAutoSignIn() == false && autoSignIn != null) {
|
if (config.isAutoSignIn() == false && autoSignIn != null) {
|
||||||
config.setAutoSignIn(Boolean.parseBoolean(autoSignIn.getValue()));
|
config.setAutoSignIn(Boolean.parseBoolean(autoSignIn.getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.getLogDirectory() != null && logDir != null) {
|
||||||
|
config.setLogDirectory(logDir.getValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initWithDefaults() {
|
private void initWithDefaults() {
|
||||||
@@ -556,11 +564,12 @@ public class SettingsLoader {
|
|||||||
this.theme = null;
|
this.theme = null;
|
||||||
this.cores = new Option<>(String.valueOf(defaultConfigValues.getNbCores()), ARG_CORES);
|
this.cores = new Option<>(String.valueOf(defaultConfigValues.getNbCores()), ARG_CORES);
|
||||||
this.disableLargeDownloads = new Option<>(String.valueOf(defaultConfigValues.isDisableLargeDownloads()), ARG_DISABLE_LARGE_DOWNLOADS);
|
this.disableLargeDownloads = new Option<>(String.valueOf(defaultConfigValues.isDisableLargeDownloads()), ARG_DISABLE_LARGE_DOWNLOADS);
|
||||||
|
this.logDir = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return String.format(
|
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]",
|
"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);
|
path, login, password, computeMethod, gpu, cacheDir, sharedZip, theme, priority, autoSignIn, useSysTray, headless, disableLargeDownloads, logDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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_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_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;
|
@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));
|
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);
|
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
|
// Hostname change will overwrite the existing one (default or read from configuration file) but changes will be lost when the client closes
|
||||||
|
|||||||
Reference in New Issue
Block a user