[英]Python-Need to redirect the console output to a log file
I am executing multiple "make" files from my python script.我正在从我的 python 脚本执行多个“make”文件。 A sample script is as follows :
示例脚本如下:
print("Calling make file")
call(["make"])
and the output will be :输出将是:
Calling make file
调用make文件
Starting make
开始制作
cd src && make distclean
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)'
make[1]: 进入目录'/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.
但是被调用的 make 文件的输出没有被重定向。
Python: python 2.7, OS: CentOS Python:python 2.7,操作系统:CentOS
You need to redirect output of processes executed by python (in your case, make
).您需要重定向由 python 执行的进程的输出(在您的情况下,
make
)。 See documentation for subprocess module and specifically, the redirection parameters ( stdin=...
, stdout=...
, stderr=...
) of its methods.请参阅subprocess 模块的文档,特别是其方法的重定向参数(
stdin=...
、 stdout=...
、 stderr=...
)。 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.根据您的需要,您可以完全丢弃子
DEVNULL
输出( DEVNULL
),或将其收集到 python 进程( PIPE
)内的变量或将其发送到另一个流。
To capture regular output of the command we can use the subprocess.check_output()
function.要捕获命令的常规输出,我们可以使用
subprocess.check_output()
函数。 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.强制同步是因为该函数必须等待被调用子进程的输出,但是如果它失败(返回您想要捕获的错误消息),您将需要处理引发的
CalledProcessError
。 Fortunately the CalledProcessError
exception has an output
attribute that stored the error message.幸运的是
CalledProcessError
异常有一个存储错误消息的output
属性。
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.当 make 实例干净地退出时,
try
会给 log_info 输出,而当它没有时, except
捕获错误。 Either way you cant move on until the log_info
is written to the log
file, so there should be no issues with synchronization无论哪种方式,在
log_info
写入log
文件之前,您都无法继续前进,因此同步应该没有问题
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
我真的需要现在编译它,它附带的makefile不起作用我需要收集输出来调试它然后继续并以对您最有用或最快的方式完成它
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 .然后停下来,不要重新发明你正在寻找的轮子cmake 。 (and will log output for you as well)
(并且也会为您记录输出)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.