From a26a69ce9f781149a35263a9812abaea8099371f Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sat, 24 Nov 2018 07:13:16 +1100 Subject: [PATCH] Change logging to a separate logfile is no longer the default (#239) * Change '--download' to '--download-only' to align with '--upload-only' * Enable logging to a separate file only if the '--enable-logging' flag is passed through at client run time * Implement configuration option for logfile location, if logging is enabled --- README.md | 77 ++++++++++++++++++++++++++++++++------------------ src/config.d | 4 ++- src/log.d | 50 ++++++++++++++++++-------------- src/main.d | 18 ++++++++---- src/onedrive.d | 1 + 5 files changed, 95 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 34b95490..392f376f 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,13 @@ onedrive --synchronize --single-directory '' Example: If the full path is `~/OneDrive/mydir`, the command would be `onedrive --synchronize --single-directory 'mydir'` -### Performing a 'one-way' sync +### Performing a 'one-way' download sync +In some cases it may be desirable to 'download only' from OneDrive. To do this use the following command: +``` +onedrive --synchronize --download-only +``` + +### Performing a 'one-way' upload sync In some cases it may be desirable to 'upload only' to OneDrive. To do this use the following command: ``` onedrive --synchronize --upload-only @@ -193,9 +199,23 @@ onedrive --synchronize --verbose ``` ### Client Activity Log -When running onedrive all actions are logged to `/var/log/onedrive/` +When running onedrive all actions can be logged to a separate log file. This can be enabled by using the `--enable-logging` flag. By default, log files will be written to `/var/log/onedrive/` -All logfiles will be in the format of `%username%.onedrive.log` +**Note:** You will need to ensure your user has the applicable permissions to write to this directory or the following warning will be printed: +``` +Unable to access /var/log/onedrive/ +Please manually create '/var/log/onedrive/' and set appropriate permissions to allow write access +The requested client activity log will instead be located in the users home directory +``` + +All logfiles will be in the format of `%username%.onedrive.log`, where `%username%` represents the user who ran the client. + +**Note:** +To use a different log directory rather than the default above, add the following as a configuration option to `~/.config/onedrive/config`: +``` +log_dir = "/path/to/location/" +``` +Trailing slash required An example of the log file is below: ``` @@ -391,33 +411,34 @@ If you encounter any bugs you can report them here on Github. Before filing an i - ... ### All available commands: -```text +``` Usage: onedrive [OPTION]... -no option No Sync and exit - --check-for-nomount Check for the presence of .nosync in the syncdir root. If found, do not perform sync. - --confdir Set the directory used to store the configuration files - --create-directory Create a directory on OneDrive - no sync will be performed. - --destination-directory Destination directory for renamed or move on OneDrive - no sync will be performed. - --debug-http Debug OneDrive HTTP communication. ---disable-upload-validation Disable upload validation when uploading to OneDrive --d --download Only download remote changes - --local-first Synchronize from the local directory source first, before downloading changes from OneDrive. - --logout Logout the current user --m --monitor Keep monitoring for local and remote changes - --no-remote-delete Do not delete local file 'deletes' from OneDrive when using --upload-only - --print-token Print the access token, useful for debugging - --resync Forget the last saved state, perform a full sync - --remove-directory Remove a directory on OneDrive - no sync will be performed. - --single-directory Specify a single local directory within the OneDrive root to sync. - --skip-symlinks Skip syncing of symlinks - --source-directory Source directory to rename or move on OneDrive - no sync will be performed. - --syncdir Set the directory used to sync the files that are synced - --synchronize Perform a synchronization - --upload-only Only upload to OneDrive, do not sync changes from OneDrive locally --v --verbose Print more details, useful for debugging - --version Print the version and exit --h --help This help information. +no option No sync and exit + --check-for-nomount Check for the presence of .nosync in the syncdir root. If found, do not perform sync. + --confdir Set the directory used to store the configuration files + --create-directory Create a directory on OneDrive - no sync will be performed. + --destination-directory Destination directory for renamed or move on OneDrive - no sync will be performed. + --debug-https Debug OneDrive HTTPS communication. +-d --download-only Only download remote changes + --disable-upload-validation Disable upload validation when uploading to OneDrive + --enable-logging Enable client activity to a separate log file + --local-first Synchronize from the local directory source first, before downloading changes from OneDrive. + --logout Logout the current user +-m --monitor Keep monitoring for local and remote changes + --no-remote-delete Do not delete local file 'deletes' from OneDrive when using --upload-only + --print-token Print the access token, useful for debugging + --resync Forget the last saved state, perform a full sync + --remove-directory Remove a directory on OneDrive - no sync will be performed. + --single-directory Specify a single local directory within the OneDrive root to sync. + --skip-symlinks Skip syncing of symlinks + --source-directory Source directory to rename or move on OneDrive - no sync will be performed. + --syncdir Set the directory used to sync the files that are synced + --synchronize Perform a synchronization + --upload-only Only upload to OneDrive, do not sync changes from OneDrive locally +-v --verbose Print more details, useful for debugging + --version Print the version and exit +-h --help This help information. ``` ### File naming diff --git a/src/config.d b/src/config.d index e14a23f5..6d8b3e92 100644 --- a/src/config.d +++ b/src/config.d @@ -37,7 +37,9 @@ final class Config // Configure the monitor mode loop - the number of seconds by which // each sync operation is undertaken when idle under monitor mode setValue("monitor_interval", "45"); - + // Configure the default logging directory to be /var/log/onedrive/ + setValue("log_dir", "/var/log/onedrive/"); + if (!load(userConfigFilePath)) { log.vlog("No config file found, using defaults"); } diff --git a/src/log.d b/src/log.d index fdd0701a..a10b2096 100644 --- a/src/log.d +++ b/src/log.d @@ -5,19 +5,20 @@ import std.process; import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen; import std.algorithm : splitter; +// enable verbose logging +bool verbose; +bool writeLogFile = false; + // shared string variable for username string username; string logFilePath; -static this() { - username = getUserName(); - logFilePath = "/var/log/onedrive/"; -} -// enable verbose logging -bool verbose; - -void init() +void init(string logDir) { + writeLogFile = true; + username = getUserName(); + logFilePath = logDir; + if (!exists(logFilePath)){ // logfile path does not exist try { @@ -25,41 +26,48 @@ void init() } catch (std.file.FileException e) { // we got an error .. - writeln("\nUnable to create /var/log/onedrive/ "); - writeln("Please manually create /var/log/onedrive/ and set appropriate permissions to allow write access"); - writeln("The client activity log will be located in the users home directory\n"); + writeln("\nUnable to access ", logFilePath); + writeln("Please manually create '",logFilePath, "' and set appropriate permissions to allow write access"); + writeln("The requested client activity log will instead be located in the users home directory\n"); } } - } void log(T...)(T args) { writeln(args); - // Write to log file - logfileWriteLine(args); + if(writeLogFile){ + // Write to log file + logfileWriteLine(args); + } } void fileOnly(T...)(T args) { - // Write to log file only - logfileWriteLine(args); + if(writeLogFile){ + // Write to log file + logfileWriteLine(args); + } } void vlog(T...)(T args) { if (verbose) { writeln(args); - // Write to log file - logfileWriteLine(args); + if(writeLogFile){ + // Write to log file + logfileWriteLine(args); + } } } void error(T...)(T args) { stderr.writeln(args); - // Write to log file - logfileWriteLine(args); + if(writeLogFile){ + // Write to log file + logfileWriteLine(args); + } } private void logfileWriteLine(T...)(T args) @@ -75,7 +83,7 @@ private void logfileWriteLine(T...)(T args) logFile = File(logFileName, "a"); } catch (std.exception.ErrnoException e) { - // We cannot open the log file in /var/log/onedrive for writing + // We cannot open the log file in logFilePath location for writing // The user is not part of the standard 'users' group (GID 100) // Change logfile to ~/onedrive.log putting the log file in the users home directory string homePath = environment.get("HOME"); diff --git a/src/main.d b/src/main.d index 040a3fa6..e3ff8675 100644 --- a/src/main.d +++ b/src/main.d @@ -83,6 +83,8 @@ int main(string[] args) bool noRemoteDelete; // Are we able to reach the OneDrive Service bool online = false; + // Do we enable a log file + bool enableLogFile = false; // Does the user want to disable upload validation - https://github.com/abraunegg/onedrive/issues/205 // SharePoint will associate some metadata from the library the file is uploaded to directly in the file - thus change file size & checksums bool disableUploadValidation = false; @@ -97,8 +99,9 @@ int main(string[] args) "create-directory", "Create a directory on OneDrive - no sync will be performed.", &createDirectory, "destination-directory", "Destination directory for renamed or move on OneDrive - no sync will be performed.", &destinationDirectory, "debug-https", "Debug OneDrive HTTPS communication.", &debugHttp, + "download-only|d", "Only download remote changes", &downloadOnly, "disable-upload-validation", "Disable upload validation when uploading to OneDrive", &disableUploadValidation, - "download|d", "Only download remote changes", &downloadOnly, + "enable-logging", "Enable client activity to a separate log file", &enableLogFile, "local-first", "Synchronize from the local directory source first, before downloading changes from OneDrive.", &localFirst, "logout", "Logout the current user", &logout, "monitor|m", "Keep monitoring for local and remote changes", &monitor, @@ -137,9 +140,6 @@ int main(string[] args) return EXIT_SUCCESS; } - // Configure Logging - log.init(); - // load configuration log.vlog("Loading config ..."); configDirName = configDirName.expandTilde().absolutePath(); @@ -148,6 +148,14 @@ int main(string[] args) auto cfg = new config.Config(configDirName); cfg.init(); + // Configure logging if enabled + if (enableLogFile){ + // Read in a user defined log directory or use the default + string logDir = cfg.getValue("log_dir"); + log.vlog("Using logfile dir: ", logDir); + log.init(logDir); + } + // command line parameters override the config if (syncDirName) cfg.setValue("sync_dir", syncDirName.expandTilde().absolutePath()); if (skipSymlinks) cfg.setValue("skip_symlinks", "true"); @@ -219,7 +227,7 @@ int main(string[] args) // Did the user specify a 'different' sync dir by passing a value in? if (syncDirName){ // was there a ~ in the passed in state? it will not work via init.d / systemd - if (canFind(cfg.getValue("sync_dir"),"~") ) { + if (canFind(cfg.getValue("sync_dir"),"~")) { // A ~ was found syncDir = homePath ~ "/OneDrive"; } else { diff --git a/src/onedrive.d b/src/onedrive.d index b00467a5..13c80a11 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -342,6 +342,7 @@ final class OneDriveApi if (!skipToken) addAccessTokenHeader(); // HACK: requestUploadStatus auto response = perform(); checkHttpCode(response); + // OneDrive API Response Debugging if (.debugResponse){ writeln("OneDrive API Response: ", response); }