abraunegg-onedrive/src/log.d
abraunegg f1aa190a91 Add uploadOnly flag and add username to logfile name
* Add specific uploadOnly flag so that the onedrive client only uploads
data from the local directory and does not download changes from
OneDrive
* Change the logfile name to include the username running the onedrive
client
2018-04-07 17:06:57 +10:00

60 lines
1.2 KiB
D

import std.stdio;
import std.file;
import std.datetime;
import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen;
import std.algorithm : splitter;
// shared string variable for username
string username;
static this() {
username = getUserName();
}
// enable verbose logging
bool verbose;
void log(T...)(T args)
{
writeln(args);
// Write to log file
logfileWriteLine(args);
}
void vlog(T...)(T args)
{
if (verbose) {
writeln(args);
// Write to log file
logfileWriteLine(args);
}
}
void error(T...)(T args)
{
stderr.writeln(args);
// Write to log file
logfileWriteLine(args);
}
private void logfileWriteLine(T...)(T args)
{
// Write to log file
string logFileName = "/var/log/onedrive/" ~ .username ~ ".onedrive.log";
auto currentTime = Clock.currTime();
auto timeString = currentTime.toString();
File logFile = File(logFileName, "a");
logFile.writeln(timeString, " ", args);
logFile.close();
}
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";
}
}