2016-08-04 23:35:58 +02:00
|
|
|
import std.stdio;
|
2018-03-14 05:43:40 +01:00
|
|
|
import std.file;
|
|
|
|
import std.datetime;
|
2018-04-25 04:11:34 +02:00
|
|
|
import std.process;
|
2018-04-07 09:06:57 +02:00
|
|
|
import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen;
|
|
|
|
import std.algorithm : splitter;
|
|
|
|
|
|
|
|
// shared string variable for username
|
|
|
|
string username;
|
2018-05-16 11:19:43 +02:00
|
|
|
string logFilePath;
|
2018-04-07 09:06:57 +02:00
|
|
|
static this() {
|
|
|
|
username = getUserName();
|
2018-05-16 11:19:43 +02:00
|
|
|
logFilePath = "/var/log/onedrive/";
|
2018-04-07 09:06:57 +02:00
|
|
|
}
|
2016-08-04 23:35:58 +02:00
|
|
|
|
|
|
|
// enable verbose logging
|
|
|
|
bool verbose;
|
|
|
|
|
2018-05-16 11:19:43 +02:00
|
|
|
void init()
|
|
|
|
{
|
|
|
|
if (!exists(logFilePath)){
|
|
|
|
// logfile path does not exist
|
|
|
|
try {
|
|
|
|
mkdirRecurse(logFilePath);
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
void log(T...)(T args)
|
|
|
|
{
|
2017-12-28 15:21:41 +01:00
|
|
|
writeln(args);
|
2018-03-14 05:43:40 +01:00
|
|
|
// Write to log file
|
2018-04-07 09:06:57 +02:00
|
|
|
logfileWriteLine(args);
|
2016-08-04 23:35:58 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:21:53 +02:00
|
|
|
void fileOnly(T...)(T args)
|
|
|
|
{
|
|
|
|
// Write to log file only
|
|
|
|
logfileWriteLine(args);
|
|
|
|
}
|
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
void vlog(T...)(T args)
|
|
|
|
{
|
2018-03-14 05:43:40 +01:00
|
|
|
if (verbose) {
|
|
|
|
writeln(args);
|
|
|
|
// Write to log file
|
2018-04-07 09:06:57 +02:00
|
|
|
logfileWriteLine(args);
|
2018-03-14 05:43:40 +01:00
|
|
|
}
|
2016-08-04 23:35:58 +02:00
|
|
|
}
|
2017-12-28 15:21:41 +01:00
|
|
|
|
|
|
|
void error(T...)(T args)
|
|
|
|
{
|
|
|
|
stderr.writeln(args);
|
2018-03-14 05:43:40 +01:00
|
|
|
// Write to log file
|
2018-04-07 09:06:57 +02:00
|
|
|
logfileWriteLine(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void logfileWriteLine(T...)(T args)
|
|
|
|
{
|
|
|
|
// Write to log file
|
2018-05-16 11:19:43 +02:00
|
|
|
string logFileName = .logFilePath ~ .username ~ ".onedrive.log";
|
2018-03-14 05:43:40 +01:00
|
|
|
auto currentTime = Clock.currTime();
|
|
|
|
auto timeString = currentTime.toString();
|
2018-04-25 04:11:34 +02:00
|
|
|
File logFile;
|
|
|
|
|
|
|
|
// Resolve: std.exception.ErrnoException@std/stdio.d(423): Cannot open file `/var/log/onedrive/xxxxx.onedrive.log' in mode `a' (Permission denied)
|
|
|
|
try {
|
|
|
|
logFile = File(logFileName, "a");
|
|
|
|
}
|
|
|
|
catch (std.exception.ErrnoException e) {
|
|
|
|
// We cannot open the log file in /var/log/onedrive 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");
|
|
|
|
string logFileNameAlternate = homePath ~ "/onedrive.log";
|
|
|
|
logFile = File(logFileNameAlternate, "a");
|
|
|
|
}
|
|
|
|
// Write to the log file
|
2018-03-14 05:43:40 +01:00
|
|
|
logFile.writeln(timeString, " ", args);
|
|
|
|
logFile.close();
|
2018-04-07 09:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private string getUserName()
|
|
|
|
{
|
|
|
|
auto pw = getpwuid(getuid);
|
|
|
|
auto uinfo = pw.pw_gecos[0 .. strlen(pw.pw_gecos)].splitter(',');
|
|
|
|
if (!uinfo.empty && uinfo.front.length){
|
|
|
|
return uinfo.front.idup;
|
|
|
|
} else {
|
|
|
|
// Unknown user?
|
|
|
|
return "unknown";
|
|
|
|
}
|
2017-12-28 15:21:41 +01:00
|
|
|
}
|