2023-08-27 01:35:51 +02:00
|
|
|
// What is this module called?
|
|
|
|
module log;
|
|
|
|
|
|
|
|
// What does this module require to function?
|
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-12-05 20:19:00 +01:00
|
|
|
import std.conv;
|
2020-05-20 03:37:11 +02:00
|
|
|
import core.memory;
|
2023-08-27 01:35:51 +02:00
|
|
|
import core.sys.posix.pwd;
|
|
|
|
import core.sys.posix.unistd;
|
|
|
|
import core.stdc.string : strlen;
|
2018-04-07 09:06:57 +02:00
|
|
|
import std.algorithm : splitter;
|
2023-08-27 01:35:51 +02:00
|
|
|
|
2018-12-05 20:19:00 +01:00
|
|
|
version(Notifications) {
|
|
|
|
import dnotify;
|
|
|
|
}
|
2018-04-07 09:06:57 +02:00
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
// module variables
|
|
|
|
// verbose logging count
|
2019-04-11 04:26:20 +02:00
|
|
|
long verbose;
|
2023-08-27 01:35:51 +02:00
|
|
|
// do we write a log file? ... this should be a config falue
|
2018-11-23 21:13:16 +01:00
|
|
|
bool writeLogFile = false;
|
2023-08-27 01:35:51 +02:00
|
|
|
// did the log file write fail?
|
2022-01-02 00:22:23 +01:00
|
|
|
bool logFileWriteFailFlag = false;
|
2018-12-05 20:19:00 +01:00
|
|
|
private bool doNotifications;
|
|
|
|
|
2018-04-07 09:06:57 +02:00
|
|
|
// shared string variable for username
|
|
|
|
string username;
|
2018-05-16 11:19:43 +02:00
|
|
|
string logFilePath;
|
2016-08-04 23:35:58 +02:00
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void init(string logDir) {
|
2018-11-23 21:13:16 +01:00
|
|
|
writeLogFile = true;
|
|
|
|
username = getUserName();
|
|
|
|
logFilePath = logDir;
|
|
|
|
|
2018-05-16 11:19:43 +02:00
|
|
|
if (!exists(logFilePath)){
|
|
|
|
// logfile path does not exist
|
|
|
|
try {
|
|
|
|
mkdirRecurse(logFilePath);
|
|
|
|
}
|
|
|
|
catch (std.file.FileException e) {
|
|
|
|
// we got an error ..
|
2018-11-23 21:13:16 +01:00
|
|
|
writeln("\nUnable to access ", logFilePath);
|
|
|
|
writeln("Please manually create '",logFilePath, "' and set appropriate permissions to allow write access");
|
2022-03-07 04:02:50 +01:00
|
|
|
writeln("The requested client activity log will instead be located in your users home directory");
|
2018-05-16 11:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void setNotifications(bool value) {
|
2019-01-30 09:34:07 +01:00
|
|
|
version(Notifications) {
|
|
|
|
// if we try to enable notifications, check for server availability
|
|
|
|
// and disable in case dbus server is not reachable
|
|
|
|
if (value) {
|
|
|
|
auto serverAvailable = dnotify.check_availability();
|
|
|
|
if (!serverAvailable) {
|
|
|
|
log("Notification (dbus) server not available, disabling");
|
|
|
|
value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 20:19:00 +01:00
|
|
|
doNotifications = value;
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void log(T...)(T args) {
|
2017-12-28 15:21:41 +01:00
|
|
|
writeln(args);
|
2018-11-23 21:13:16 +01:00
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
logfileWriteLine(args);
|
|
|
|
}
|
2016-08-04 23:35:58 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void logAndNotify(T...)(T args) {
|
2018-12-05 20:19:00 +01:00
|
|
|
notify(args);
|
|
|
|
log(args);
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void fileOnly(T...)(T args) {
|
2018-11-23 21:13:16 +01:00
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
logfileWriteLine(args);
|
|
|
|
}
|
2018-05-03 08:21:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void vlog(T...)(T args) {
|
2018-12-07 19:01:22 +01:00
|
|
|
if (verbose >= 1) {
|
2018-03-14 05:43:40 +01:00
|
|
|
writeln(args);
|
2018-11-23 21:13:16 +01:00
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
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
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void vdebug(T...)(T args) {
|
2018-12-07 19:01:22 +01:00
|
|
|
if (verbose >= 2) {
|
|
|
|
writeln("[DEBUG] ", args);
|
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
logfileWriteLine("[DEBUG] ", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void vdebugNewLine(T...)(T args) {
|
2019-11-11 10:27:34 +01:00
|
|
|
if (verbose >= 2) {
|
|
|
|
writeln("\n[DEBUG] ", args);
|
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
logfileWriteLine("\n[DEBUG] ", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void error(T...)(T args) {
|
2017-12-28 15:21:41 +01:00
|
|
|
stderr.writeln(args);
|
2018-11-23 21:13:16 +01:00
|
|
|
if(writeLogFile){
|
|
|
|
// Write to log file
|
|
|
|
logfileWriteLine(args);
|
|
|
|
}
|
2018-04-07 09:06:57 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void errorAndNotify(T...)(T args) {
|
2018-12-05 20:19:00 +01:00
|
|
|
notify(args);
|
|
|
|
error(args);
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void notify(T...)(T args) {
|
2018-12-05 20:19:00 +01:00
|
|
|
version(Notifications) {
|
|
|
|
if (doNotifications) {
|
|
|
|
string result;
|
|
|
|
foreach (index, arg; args) {
|
|
|
|
result ~= to!string(arg);
|
|
|
|
if (index != args.length - 1)
|
|
|
|
result ~= " ";
|
|
|
|
}
|
|
|
|
auto n = new Notification("OneDrive", result, "IGNORED");
|
|
|
|
try {
|
|
|
|
n.show();
|
2019-11-21 08:59:45 +01:00
|
|
|
// Sent message to notification daemon
|
|
|
|
if (verbose >= 2) {
|
|
|
|
writeln("[DEBUG] Sent notification to notification service. If notification is not displayed, check dbus or notification-daemon for errors");
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:19:00 +01:00
|
|
|
} catch (Throwable e) {
|
|
|
|
vlog("Got exception from showing notification: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
private void logfileWriteLine(T...)(T args) {
|
2021-06-07 00:26:36 +02:00
|
|
|
static import std.exception;
|
2018-04-07 09:06:57 +02:00
|
|
|
// 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) {
|
2018-11-23 21:13:16 +01:00
|
|
|
// We cannot open the log file in logFilePath location for writing
|
2018-04-25 04:11:34 +02:00
|
|
|
// 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
|
2022-01-02 00:22:23 +01:00
|
|
|
|
|
|
|
if (!logFileWriteFailFlag) {
|
|
|
|
// write out error message that we cant log to the requested file
|
|
|
|
writeln("\nUnable to write activity log to ", logFileName);
|
|
|
|
writeln("Please set appropriate permissions to allow write access to the logging directory for your user account");
|
|
|
|
writeln("The requested client activity log will instead be located in your users home directory\n");
|
|
|
|
|
|
|
|
// set the flag so we dont keep printing this error message
|
|
|
|
logFileWriteFailFlag = true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 04:11:34 +02:00
|
|
|
string homePath = environment.get("HOME");
|
|
|
|
string logFileNameAlternate = homePath ~ "/onedrive.log";
|
|
|
|
logFile = File(logFileNameAlternate, "a");
|
|
|
|
}
|
|
|
|
// Write to the log file
|
2020-06-04 22:00:42 +02:00
|
|
|
logFile.writeln(timeString, "\t", args);
|
2018-03-14 05:43:40 +01:00
|
|
|
logFile.close();
|
2018-04-07 09:06:57 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
private string getUserName() {
|
2018-04-07 09:06:57 +02:00
|
|
|
auto pw = getpwuid(getuid);
|
2020-02-02 23:10:57 +01:00
|
|
|
|
|
|
|
// get required details
|
|
|
|
auto runtime_pw_name = pw.pw_name[0 .. strlen(pw.pw_name)].splitter(',');
|
|
|
|
auto runtime_pw_uid = pw.pw_uid;
|
|
|
|
auto runtime_pw_gid = pw.pw_gid;
|
|
|
|
|
|
|
|
// user identifiers from process
|
|
|
|
vdebug("Process ID: ", pw);
|
|
|
|
vdebug("User UID: ", runtime_pw_uid);
|
|
|
|
vdebug("User GID: ", runtime_pw_gid);
|
|
|
|
|
|
|
|
// What should be returned as username?
|
|
|
|
if (!runtime_pw_name.empty && runtime_pw_name.front.length){
|
|
|
|
// user resolved
|
|
|
|
vdebug("User Name: ", runtime_pw_name.front.idup);
|
|
|
|
return runtime_pw_name.front.idup;
|
2018-04-07 09:06:57 +02:00
|
|
|
} else {
|
|
|
|
// Unknown user?
|
2020-02-02 23:10:57 +01:00
|
|
|
vdebug("User Name: unknown");
|
2018-04-07 09:06:57 +02:00
|
|
|
return "unknown";
|
|
|
|
}
|
2018-12-05 20:19:00 +01:00
|
|
|
}
|
2020-05-20 03:37:11 +02:00
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void displayMemoryUsagePreGC() {
|
2020-05-20 03:37:11 +02:00
|
|
|
// Display memory usage
|
|
|
|
writeln("\nMemory Usage pre GC (bytes)");
|
|
|
|
writeln("--------------------");
|
2023-08-27 01:35:51 +02:00
|
|
|
writeln("memory usedSize = ", GC.stats.usedSize);
|
|
|
|
writeln("memory freeSize = ", GC.stats.freeSize);
|
|
|
|
writeln("memory allocatedInCurrentThread = ", GC.stats.allocatedInCurrentThread, "\n");
|
2020-05-20 03:37:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 01:35:51 +02:00
|
|
|
void displayMemoryUsagePostGC() {
|
2020-05-20 03:37:11 +02:00
|
|
|
// Display memory usage
|
|
|
|
writeln("\nMemory Usage post GC (bytes)");
|
|
|
|
writeln("--------------------");
|
2023-08-27 01:35:51 +02:00
|
|
|
writeln("memory usedSize = ", GC.stats.usedSize);
|
|
|
|
writeln("memory freeSize = ", GC.stats.freeSize);
|
|
|
|
writeln("memory allocatedInCurrentThread = ", GC.stats.allocatedInCurrentThread, "\n");
|
2020-05-20 03:37:11 +02:00
|
|
|
}
|