Improve wrapper

This commit is contained in:
Quinten 2023-05-06 11:31:33 +02:00 committed by GitHub
parent 51439f3e5a
commit bf1217839d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 29 deletions

View File

@ -8,7 +8,7 @@
#include <time.h>
#include <sys/stat.h>
void print_latest_log(const char* log_directory) {
void print_latest_log(const char* log_directory, off_t* last_pos, off_t* last_size) {
DIR *dir;
struct dirent *entry;
time_t latest_time = 0;
@ -39,14 +39,20 @@ void print_latest_log(const char* log_directory) {
return;
}
printf("\nLatest log file: %s\n", latest_file);
int fd = open(latest_file, O_RDONLY);
if (fd < 0) {
printf("Error opening file %s\n", latest_file);
return;
}
// Get the size of the file
off_t size = lseek(fd, 0, SEEK_END);
// Check if the file size has increased since the last read
if (size > *last_size) {
// Move the file pointer to the beginning of the file
lseek(fd, 0, SEEK_SET);
char buffer[4096];
int bytes_read;
int last_line_printed = 0; // Flag to check whether we have printed the last line
@ -75,8 +81,15 @@ void print_latest_log(const char* log_directory) {
printf("\n");
}
close(fd);
// Remember the last position and size that was read
*last_pos = lseek(fd, 0, SEEK_CUR);
*last_size = size;
}
close(fd);
}
int main(int argc, char** argv) {
if (argc < 2) {
@ -104,12 +117,15 @@ int main(int argc, char** argv) {
sleep(30);
off_t last_pos = 0;
off_t last_size = 0;
char* log_directory = "/home/container/SNM2020/Saved/Logs";
print_latest_log(log_directory);
print_latest_log(log_directory, &last_pos, &last_size);
while (1) {
sleep(1);
print_latest_log(log_directory);
print_latest_log(log_directory, &last_pos, &last_size);
}
kill(pid, SIGTERM);