views:

109

answers:

2

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 should 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?

+1  A: 

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:

  1. 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.

  2. 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!

Remo.D
A: 

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.

Willis Blackburn