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 <time.h>
#include <sys/stat.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; DIR *dir;
struct dirent *entry; struct dirent *entry;
time_t latest_time = 0; time_t latest_time = 0;
@ -39,45 +39,58 @@ void print_latest_log(const char* log_directory) {
return; return;
} }
printf("\nLatest log file: %s\n", latest_file);
int fd = open(latest_file, O_RDONLY); int fd = open(latest_file, O_RDONLY);
if (fd < 0) { if (fd < 0) {
printf("Error opening file %s\n", latest_file); printf("Error opening file %s\n", latest_file);
return; return;
} }
char buffer[4096]; // Get the size of the file
int bytes_read; off_t size = lseek(fd, 0, SEEK_END);
int last_line_printed = 0; // Flag to check whether we have printed the last line
do { // Check if the file size has increased since the last read
bytes_read = read(fd, buffer, sizeof(buffer)); if (size > *last_size) {
if (bytes_read > 0) { // Move the file pointer to the beginning of the file
// Check if the last character is a newline lseek(fd, 0, SEEK_SET);
if (buffer[bytes_read - 1] == '\n') {
fwrite(buffer, 1, bytes_read, stdout); char buffer[4096];
fflush(stdout); int bytes_read;
} else { int last_line_printed = 0; // Flag to check whether we have printed the last line
// If the last character is not a newline, add one do {
char* temp = (char*) malloc(bytes_read + 1); bytes_read = read(fd, buffer, sizeof(buffer));
memcpy(temp, buffer, bytes_read); if (bytes_read > 0) {
temp[bytes_read] = '\n'; // Check if the last character is a newline
fwrite(temp, 1, bytes_read + 1, stdout); if (buffer[bytes_read - 1] == '\n') {
fflush(stdout); fwrite(buffer, 1, bytes_read, stdout);
free(temp); fflush(stdout);
} else {
// If the last character is not a newline, add one
char* temp = (char*) malloc(bytes_read + 1);
memcpy(temp, buffer, bytes_read);
temp[bytes_read] = '\n';
fwrite(temp, 1, bytes_read + 1, stdout);
fflush(stdout);
free(temp);
}
last_line_printed = (buffer[bytes_read - 1] == '\n');
} }
last_line_printed = (buffer[bytes_read - 1] == '\n'); } while (bytes_read > 0);
// If the last line was not printed, print it now
if (!last_line_printed) {
printf("\n");
} }
} while (bytes_read > 0);
// If the last line was not printed, print it now // Remember the last position and size that was read
if (!last_line_printed) { *last_pos = lseek(fd, 0, SEEK_CUR);
printf("\n"); *last_size = size;
} }
close(fd); close(fd);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc < 2) { if (argc < 2) {
printf("Usage: winewrapper wine_path wine_args exe_path exe_args\n"); printf("Usage: winewrapper wine_path wine_args exe_path exe_args\n");
@ -104,12 +117,15 @@ int main(int argc, char** argv) {
sleep(30); sleep(30);
off_t last_pos = 0;
off_t last_size = 0;
char* log_directory = "/home/container/SNM2020/Saved/Logs"; char* log_directory = "/home/container/SNM2020/Saved/Logs";
print_latest_log(log_directory); print_latest_log(log_directory, &last_pos, &last_size);
while (1) { while (1) {
sleep(1); sleep(1);
print_latest_log(log_directory); print_latest_log(log_directory, &last_pos, &last_size);
} }
kill(pid, SIGTERM); kill(pid, SIGTERM);