Intercept C function call from Python
Question: Is there a way to intercept a C function call on a binary (e.g: write to a
file) in Python?

I need to execute the binary on Linux from python and capture the output that
it writes to the log file. The log file name is unpredictable.


Answer: The way to go is to create a named pipe. If `logfile` is the name of your
logfile:

    
    
    import os
    import subprocess
    
    LOGFILE = 'somefile.log'
    
    if not os.path.exists(LOGFILE):
        os.mkfifo(LOGFILE)
    proc = subprocess.Popen([YOUR_PROGRAM])
    with open(LOGFILE) as log:
        data = log.read()  # process your data
    
    proc.wait()
    

