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
This commit is contained in:
abraunegg 2018-11-24 07:13:16 +11:00 committed by GitHub
parent c5ee62efd8
commit a26a69ce9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 55 deletions

View file

@ -180,7 +180,13 @@ onedrive --synchronize --single-directory '<dir_name>'
Example: If the full path is `~/OneDrive/mydir`, the command would be `onedrive --synchronize --single-directory 'mydir'` 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: In some cases it may be desirable to 'upload only' to OneDrive. To do this use the following command:
``` ```
onedrive --synchronize --upload-only onedrive --synchronize --upload-only
@ -193,9 +199,23 @@ onedrive --synchronize --verbose
``` ```
### Client Activity Log ### 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: 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: ### All available commands:
```text ```
Usage: onedrive [OPTION]... Usage: onedrive [OPTION]...
no option No Sync and exit 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. --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 --confdir Set the directory used to store the configuration files
--create-directory Create a directory on OneDrive - no sync will be performed. --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. --destination-directory Destination directory for renamed or move on OneDrive - no sync will be performed.
--debug-http Debug OneDrive HTTP communication. --debug-https Debug OneDrive HTTPS communication.
--disable-upload-validation Disable upload validation when uploading to OneDrive -d --download-only Only download remote changes
-d --download Only download remote changes --disable-upload-validation Disable upload validation when uploading to OneDrive
--local-first Synchronize from the local directory source first, before downloading changes from OneDrive. --enable-logging Enable client activity to a separate log file
--logout Logout the current user --local-first Synchronize from the local directory source first, before downloading changes from OneDrive.
-m --monitor Keep monitoring for local and remote changes --logout Logout the current user
--no-remote-delete Do not delete local file 'deletes' from OneDrive when using --upload-only -m --monitor Keep monitoring for local and remote changes
--print-token Print the access token, useful for debugging --no-remote-delete Do not delete local file 'deletes' from OneDrive when using --upload-only
--resync Forget the last saved state, perform a full sync --print-token Print the access token, useful for debugging
--remove-directory Remove a directory on OneDrive - no sync will be performed. --resync Forget the last saved state, perform a full sync
--single-directory Specify a single local directory within the OneDrive root to sync. --remove-directory Remove a directory on OneDrive - no sync will be performed.
--skip-symlinks Skip syncing of symlinks --single-directory Specify a single local directory within the OneDrive root to sync.
--source-directory Source directory to rename or move on OneDrive - no sync will be performed. --skip-symlinks Skip syncing of symlinks
--syncdir Set the directory used to sync the files that are synced --source-directory Source directory to rename or move on OneDrive - no sync will be performed.
--synchronize Perform a synchronization --syncdir Set the directory used to sync the files that are synced
--upload-only Only upload to OneDrive, do not sync changes from OneDrive locally --synchronize Perform a synchronization
-v --verbose Print more details, useful for debugging --upload-only Only upload to OneDrive, do not sync changes from OneDrive locally
--version Print the version and exit -v --verbose Print more details, useful for debugging
-h --help This help information. --version Print the version and exit
-h --help This help information.
``` ```
### File naming ### File naming

View file

@ -37,6 +37,8 @@ final class Config
// Configure the monitor mode loop - the number of seconds by which // Configure the monitor mode loop - the number of seconds by which
// each sync operation is undertaken when idle under monitor mode // each sync operation is undertaken when idle under monitor mode
setValue("monitor_interval", "45"); setValue("monitor_interval", "45");
// Configure the default logging directory to be /var/log/onedrive/
setValue("log_dir", "/var/log/onedrive/");
if (!load(userConfigFilePath)) { if (!load(userConfigFilePath)) {
log.vlog("No config file found, using defaults"); log.vlog("No config file found, using defaults");

View file

@ -5,19 +5,20 @@ import std.process;
import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen; import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen;
import std.algorithm : splitter; import std.algorithm : splitter;
// enable verbose logging
bool verbose;
bool writeLogFile = false;
// shared string variable for username // shared string variable for username
string username; string username;
string logFilePath; string logFilePath;
static this() {
username = getUserName();
logFilePath = "/var/log/onedrive/";
}
// enable verbose logging void init(string logDir)
bool verbose;
void init()
{ {
writeLogFile = true;
username = getUserName();
logFilePath = logDir;
if (!exists(logFilePath)){ if (!exists(logFilePath)){
// logfile path does not exist // logfile path does not exist
try { try {
@ -25,41 +26,48 @@ void init()
} }
catch (std.file.FileException e) { catch (std.file.FileException e) {
// we got an error .. // we got an error ..
writeln("\nUnable to create /var/log/onedrive/ "); writeln("\nUnable to access ", logFilePath);
writeln("Please manually create /var/log/onedrive/ and set appropriate permissions to allow write access"); writeln("Please manually create '",logFilePath, "' and set appropriate permissions to allow write access");
writeln("The client activity log will be located in the users home directory\n"); writeln("The requested client activity log will instead be located in the users home directory\n");
} }
} }
} }
void log(T...)(T args) void log(T...)(T args)
{ {
writeln(args); writeln(args);
// Write to log file if(writeLogFile){
logfileWriteLine(args); // Write to log file
logfileWriteLine(args);
}
} }
void fileOnly(T...)(T args) void fileOnly(T...)(T args)
{ {
// Write to log file only if(writeLogFile){
logfileWriteLine(args); // Write to log file
logfileWriteLine(args);
}
} }
void vlog(T...)(T args) void vlog(T...)(T args)
{ {
if (verbose) { if (verbose) {
writeln(args); writeln(args);
// Write to log file if(writeLogFile){
logfileWriteLine(args); // Write to log file
logfileWriteLine(args);
}
} }
} }
void error(T...)(T args) void error(T...)(T args)
{ {
stderr.writeln(args); stderr.writeln(args);
// Write to log file if(writeLogFile){
logfileWriteLine(args); // Write to log file
logfileWriteLine(args);
}
} }
private void logfileWriteLine(T...)(T args) private void logfileWriteLine(T...)(T args)
@ -75,7 +83,7 @@ private void logfileWriteLine(T...)(T args)
logFile = File(logFileName, "a"); logFile = File(logFileName, "a");
} }
catch (std.exception.ErrnoException e) { 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) // 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 // Change logfile to ~/onedrive.log putting the log file in the users home directory
string homePath = environment.get("HOME"); string homePath = environment.get("HOME");

View file

@ -83,6 +83,8 @@ int main(string[] args)
bool noRemoteDelete; bool noRemoteDelete;
// Are we able to reach the OneDrive Service // Are we able to reach the OneDrive Service
bool online = false; 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 // 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 // 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; bool disableUploadValidation = false;
@ -97,8 +99,9 @@ int main(string[] args)
"create-directory", "Create a directory on OneDrive - no sync will be performed.", &createDirectory, "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, "destination-directory", "Destination directory for renamed or move on OneDrive - no sync will be performed.", &destinationDirectory,
"debug-https", "Debug OneDrive HTTPS communication.", &debugHttp, "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, "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, "local-first", "Synchronize from the local directory source first, before downloading changes from OneDrive.", &localFirst,
"logout", "Logout the current user", &logout, "logout", "Logout the current user", &logout,
"monitor|m", "Keep monitoring for local and remote changes", &monitor, "monitor|m", "Keep monitoring for local and remote changes", &monitor,
@ -137,9 +140,6 @@ int main(string[] args)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
// Configure Logging
log.init();
// load configuration // load configuration
log.vlog("Loading config ..."); log.vlog("Loading config ...");
configDirName = configDirName.expandTilde().absolutePath(); configDirName = configDirName.expandTilde().absolutePath();
@ -148,6 +148,14 @@ int main(string[] args)
auto cfg = new config.Config(configDirName); auto cfg = new config.Config(configDirName);
cfg.init(); 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 // command line parameters override the config
if (syncDirName) cfg.setValue("sync_dir", syncDirName.expandTilde().absolutePath()); if (syncDirName) cfg.setValue("sync_dir", syncDirName.expandTilde().absolutePath());
if (skipSymlinks) cfg.setValue("skip_symlinks", "true"); 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? // Did the user specify a 'different' sync dir by passing a value in?
if (syncDirName){ if (syncDirName){
// was there a ~ in the passed in state? it will not work via init.d / systemd // 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 // A ~ was found
syncDir = homePath ~ "/OneDrive"; syncDir = homePath ~ "/OneDrive";
} else { } else {

View file

@ -342,6 +342,7 @@ final class OneDriveApi
if (!skipToken) addAccessTokenHeader(); // HACK: requestUploadStatus if (!skipToken) addAccessTokenHeader(); // HACK: requestUploadStatus
auto response = perform(); auto response = perform();
checkHttpCode(response); checkHttpCode(response);
// OneDrive API Response Debugging
if (.debugResponse){ if (.debugResponse){
writeln("OneDrive API Response: ", response); writeln("OneDrive API Response: ", response);
} }