added comments to clarify things

This commit is contained in:
Preston 2025-10-28 17:18:58 -07:00
parent d7b7dee6d1
commit 8f5a466a59

View File

@ -6,47 +6,42 @@
#include "watchDog.h" #include "watchDog.h"
namespace fs = std::filesystem; //makes it easier to read namespace fs = std::filesystem; //makes it easier to read
void WatchDog::start() void WatchDog::start() {
{ try {
//checks if file exist // Try to get the last write time directly
if(!fs::exists(path))
{
//returns and sets parameters to false if file doesnt exist
watching = false;
hasInitialTime = false;
std::cout << "WatchDog: File does not exists: " << path << std::endl;
return;
}
//grabs intial write time
lastWriteTime = fs::last_write_time(path); lastWriteTime = fs::last_write_time(path);
// If successful, start watching
watching = true; watching = true;
hasInitialTime = true; hasInitialTime = true;
std::cout << "WatchDog: Started" << std::endl; std::cout << "WatchDog: Started monitoring " << path << std::endl;
}
catch (const fs::filesystem_error& e) {
// If the file doesn't exist or another filesystem error occurs
watching = false;
hasInitialTime = false;
std::cerr << "WatchDog: Cannot start. File error: " << e.what() << std::endl;
}
catch (const std::exception& e) {
// Catch any other unexpected errors
watching = false;
hasInitialTime = false;
std::cerr << "WatchDog: Unexpected error: " << e.what() << std::endl;
}
} }
void WatchDog::stop(){ void WatchDog::stop(){
watching = false; watching = false;
} }
//this is currently just telling you when the file is edited, the exact time its edited
bool WatchDog::checkFile() { bool WatchDog::checkFile() {
//If not watching returns false
if (!watching) return false; if (!watching) return false;
//Checking if file was deleted try {
if(!fs::exists(path)) // Try to get last write time
{
if (hasInitialTime) {
std::cout << "WatchDog: File was delete: " << path << std::endl;
hasInitialTime = false;
return true;
}
return false;
}
//Built in function with file system to check last write tim
fs::file_time_type currentWriteTime = fs::last_write_time(path); fs::file_time_type currentWriteTime = fs::last_write_time(path);
// File was just created // File was just created
if(!hasInitialTime) if (!hasInitialTime) {
{
lastWriteTime = currentWriteTime; lastWriteTime = currentWriteTime;
hasInitialTime = true; hasInitialTime = true;
std::cout << "WatchDog: File created: " << path << std::endl; std::cout << "WatchDog: File created: " << path << std::endl;
@ -54,14 +49,27 @@ bool WatchDog::checkFile(){
} }
// File modified // File modified
if (currentWriteTime != lastWriteTime) if (currentWriteTime != lastWriteTime) {
{
lastWriteTime = currentWriteTime; lastWriteTime = currentWriteTime;
std::cout << "WatchDog: File modifed at " std::cout << "WatchDog: File modified at "
<< timePointToString(lastWriteTime) << std::endl; << timePointToString(lastWriteTime) << std::endl;
return true; return true;
} }
} catch (const fs::filesystem_error& e) {
// File deleted, inaccessible, or path invalid
if (hasInitialTime) {
std::cout << "WatchDog: File deleted or inaccessible: " << path << std::endl;
hasInitialTime = false;
return true;
}
return false;
} catch (const std::exception& e) {
std::cerr << "WatchDog: Unexpected error checking file: " << e.what() << std::endl;
return false;
}
// No change // No change
return false; return false;
} }