Tejas
Tejas

Reputation: 57

Python-Need to redirect the console output to a log file

I am executing multiple "make" files from my python script. A sample script is as follows :

print("Calling make file")

call(["make"])

and the output will be :

Calling make file

Starting make

cd src && make distclean

make[1]: Entering directory '/home/tejas/WLANrepo/src/3rdparty/redis-stable/src' rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html (cd ../deps && make distclean)'

I want the entire output to be redirected to a log file. I tried :

class Logger(object):

    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("Buildexec_logfile.log", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

but this will redirect only those statements that are under "print". But the output of the make file that gets called is not getting redirected.

Python: python 2.7, OS: CentOS

Upvotes: 4

Views: 2612

Answers (2)

Ajay
Ajay

Reputation: 407

To capture regular output of the command we can use the subprocess.check_output() function. The synchronization is forced because the function must wait for the output of the called sub-process, however if it fails (returns the error message you want to capture) you will need to handle the CalledProcessError that is raised. Fortunately the CalledProcessError exception has an output attribute that stored the error message.

import subprocess
with open('logfile.log','w') as log:
    for makefile in multiple_makefiles:
        log.write("calling {}".format(makefile)
        try:
            log_info = subprocess.check_output(["make",makefile],stderr=STDOUT)
        except subprocess.CalledProcessError as e:
            log_info = e.output
        log.write(log_info)

The try will give log_info the output when the make instance exits cleanly and the except catches the error when it does not. Either way you cant move on until the log_info is written to the log file, so there should be no issues with synchronization

I would also add that if you are writing this script for utility reasons like:

Dang. I really need this compiled now and the makefiles that shipped with it aren't working I need to gather the output to debug this Then go ahead and get it finished in whatever way is most useful or the quickest for you

BUT

If you are really doing something more like:

I need a setup script to automate building my software, and I will ship it with the software for the foreseeable future

Then stop, don't re-invent the wheel you are looking for cmake. (and will log output for you as well)

Upvotes: 0

user3159253
user3159253

Reputation: 17455

You need to redirect output of processes executed by python (in your case, make). See documentation for subprocess module and specifically, the redirection parameters (stdin=..., stdout=..., stderr=...) of its methods. Depending on your needs you may completely discard subprocess output (DEVNULL), or collect it to a variable inside python process (PIPE) or send it to another stream.

Upvotes: 2

Related Questions