added comments to clarify things
This commit is contained in:
parent
d7b7dee6d1
commit
8f5a466a59
@ -6,63 +6,71 @@
|
||||
#include "watchDog.h"
|
||||
|
||||
namespace fs = std::filesystem; //makes it easier to read
|
||||
void WatchDog::start()
|
||||
{
|
||||
//checks if file exist
|
||||
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
|
||||
void WatchDog::start() {
|
||||
try {
|
||||
// Try to get the last write time directly
|
||||
lastWriteTime = fs::last_write_time(path);
|
||||
|
||||
// If successful, start watching
|
||||
watching = 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(){
|
||||
watching = false;
|
||||
}
|
||||
bool WatchDog::checkFile(){
|
||||
//If not watching returns false
|
||||
//this is currently just telling you when the file is edited, the exact time its edited
|
||||
bool WatchDog::checkFile() {
|
||||
if (!watching) return false;
|
||||
|
||||
//Checking if file was deleted
|
||||
if(!fs::exists(path))
|
||||
{
|
||||
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
|
||||
try {
|
||||
// Try to get last write time
|
||||
fs::file_time_type currentWriteTime = fs::last_write_time(path);
|
||||
|
||||
//File was just created
|
||||
if(!hasInitialTime)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
// File modified
|
||||
if (currentWriteTime != lastWriteTime) {
|
||||
lastWriteTime = currentWriteTime;
|
||||
std::cout << "WatchDog: File modifed at "
|
||||
std::cout << "WatchDog: File modified at "
|
||||
<< timePointToString(lastWriteTime) << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
//No change
|
||||
} 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
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user