We generally use one of the following methods to log the output messages of programs which print a lot of messages.
-
$ ./program >& message.log
-
$ ./program |& tee message.log
One obvious way to fix this is to put fflush(stdout) after every printf statement you have, this is OK for small programs but for very large programs its really tedious. I came up with the following technique to solve this problem. This solution is generic.
[main.c] int main(int argc, char **argv){ ForceLogMessages("message.log"); .... ... } void ForceLogMessages(const char *mesg_file){ int ret_setvbuf; /*close the stdout and re-open a text file*/ fclose(stdout); stdout = fopen(name, "w"); //make stdout unbuffered ret_setvbuf = setvbuf(stdout, (char *) NULL, _IOLBF, 0); if(ret_setvbuf){ fprintf(stderr, "UNABLE TO MAKE stdout UNBUFFERED %s", strerror(errno)); } assert(stdout); }
Now if we run the program and we can monitor the status with tail -f message.log since we have made stdout unbuffered we can see a message in message.log every time a printf is executed in the original program and you can know what exactly is happening.
No comments:
Post a Comment