I have an application which is written in C and logs my function calls by writing them to a file. Now I need to re-use these logged function calls in another C-application (which is also the reason for logging).
This application shou开发者_JS百科ld do - next to other things - exactly the things which were logged.
example: MYLOGFILE which was written by the first application
printf("Starting main process w/ pid %d\n", (int)getpid());
Display *dsp = XOpenDisplay( NULL );
Then the second application shall execute the function calls logged and taken from the file.
Any ideas how to realize that?
In essence, you want to generate C code and have that code executed, right? If this is is the case, you can check TCC that is available as a library and provides exactly this functionality.
Of course assuming your platform is among the supported ones.
That said I would rather avoid emitting C code and having another application reading it, compiling it and executing it. I would suggest you one of the following:
invent your own language and emit it in your logfile. Then write an interpreter for that language and make the other program read and interpret the log file.
Embed a scripting language (i.e. Lua) in your second program and emit your logfile in that language.
Which solution would work best depends on the complexity of the tasks you have to describe in your logfile.
As very last solution, assuming you want to emit C you can consider using system()
to invoke your compiler and than the compiled program. But I would really really discourage you to do so!
It sounds like you're building some kind of replication mechanism. Node A performs actions 1 through N, and then you want to replicate those same actions on Node B.
There are lots of options here, but they all fall into two categories:
Option 1: Write the log file in a format that can be consumed by an existing utility. If you were using JavaScript, or some other language that has an "eval" function, then this would be really easy. You'd just write JavaScript to the log file on Node A, then "eval" it from Node B.
The sample log you provided reflects this approach, although it will be a little more complicated for you because C doesn't have an "eval" function. Basically you'll have to ship your log file from Node A to B, compile it and link it with libraries or other C modules that implement the functions used by the C log, then run the resulting program. You can turn the log file into a complete C source file by including it in a template like this:
int main(int argc, char *argv[]) {
// Do your setup stuff here.
#include "/path/to/the/log/file"
// Clean up.
return 0
}
Option 2. Create a parser that parses and executes the log within the context of some other program.
In this case, since your log statements look like C, you'll essentially be writing a C interpreter.
精彩评论