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,63 +6,71 @@
#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)) lastWriteTime = fs::last_write_time(path);
{
//returns and sets parameters to false if file doesnt exist // If successful, start watching
watching = true;
hasInitialTime = true;
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; watching = false;
hasInitialTime = false; hasInitialTime = false;
std::cout << "WatchDog: File does not exists: " << path << std::endl; std::cerr << "WatchDog: Cannot start. File error: " << e.what() << std::endl;
return; }
catch (const std::exception& e) {
// Catch any other unexpected errors
watching = false;
hasInitialTime = false;
std::cerr << "WatchDog: Unexpected error: " << e.what() << std::endl;
} }
//grabs intial write time
lastWriteTime = fs::last_write_time(path);
watching = true;
hasInitialTime = true;
std::cout << "WatchDog: Started" << std::endl;
} }
void WatchDog::stop(){ void WatchDog::stop(){
watching = false; watching = false;
} }
bool WatchDog::checkFile(){ //this is currently just telling you when the file is edited, the exact time its edited
//If not watching returns false bool WatchDog::checkFile() {
if (!watching) return false; if (!watching) return false;
//Checking if file was deleted try {
if(!fs::exists(path)) // Try to get last write time
{ fs::file_time_type currentWriteTime = fs::last_write_time(path);
// File was just created
if (!hasInitialTime) {
lastWriteTime = currentWriteTime;
hasInitialTime = true;
std::cout << "WatchDog: File created: " << path << std::endl;
return true;
}
// File modified
if (currentWriteTime != lastWriteTime) {
lastWriteTime = currentWriteTime;
std::cout << "WatchDog: File modified at "
<< timePointToString(lastWriteTime) << std::endl;
return true;
}
} catch (const fs::filesystem_error& e) {
// File deleted, inaccessible, or path invalid
if (hasInitialTime) { if (hasInitialTime) {
std::cout << "WatchDog: File was delete: " << path << std::endl; std::cout << "WatchDog: File deleted or inaccessible: " << path << std::endl;
hasInitialTime = false; hasInitialTime = false;
return true; return true;
} }
return false; return false;
}
//Built in function with file system to check last write tim
fs::file_time_type currentWriteTime = fs::last_write_time(path);
//File was just created } catch (const std::exception& e) {
if(!hasInitialTime) std::cerr << "WatchDog: Unexpected error checking file: " << e.what() << std::endl;
{ return false;
lastWriteTime = currentWriteTime;
hasInitialTime = true;
std::cout << "WatchDog: File created: " << path << std::endl;
return true;
} }
//File modified // No change
if (currentWriteTime != lastWriteTime)
{
lastWriteTime = currentWriteTime;
std::cout << "WatchDog: File modifed at "
<< timePointToString(lastWriteTime) << std::endl;
return true;
}
//No change
return false; return false;
} }