I am working on a project where there is a log file that is constantly being written. I am writing a C++ code for monitoring this log file in one thread (maybe using popen?) and communicating back with the main thread.
The main thread will have bunch of tests running, and the logs are basically the method to verify whether certain tests succeeded or not.
Every time certain keywords come up during the monitoring of the log, the monitoring thread will send that line to the main thread, which will then use that information to do something.
I guess the big thing is that I need to have both the monitoring/processing logs and the main testing functions running simultaneously; and I am aware that this can be done by threads.
int main () {
char cmd[1024] = "tail -f log.txt";
FILE* stream = popen(cmd, "r");
if (!stream) printf ("ERROR");
char buffer[1024];
while (fgets(buffer, 1024, stream) != NULL) {
printf("MSG: %s",buffer);
}
pclose(stream);
printf("DONE!");
}
Currently I am doing this without the multithread, so my program won't do anything while it is in the while loop processing tail -f.
I want to do other things while this is running on a different thread. All this is going to do is process the logs and send certain logs to the listeners for them to handle in the main thread.
Can anyone guide me through the design of this..?
Aucun commentaire:
Enregistrer un commentaire