0

I am trying to save the terminal output of a python script to a file and print the output to the terminal. Using the logging script below I was able to successfully save the python output, but my script calls GNU Radio as a class and this output is not saved currectly. I believe the problem is that GNU Radio is compiled C code and not captured in the stdout redirect. I have tried using Eli Bendersky's script as a redirect but I am not understanding how to implement this in Python 2.7. Ideally I would be able to add this C-level redirect to the Logger class and save the GNU Radio output in the same generated log file. Thanks for the help!

import sys

class Logger(object):
    def __init__(self, filename="Default.log"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")

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

sys.stdout = Logger("yourlogfilename.txt")
print "Hello world !" # this is should be saved in yourlogfilename.txt
gvega
  • 11
  • 4
  • Could you just not use 'tee' in your command line? – Max Feb 07 '19 at 18:49
  • running `$ python top_block.py | tee output.txt` results in the text file being created but it remains empty. Text continues to appear on the console. Even still ideally I can implement this redirect code in the script so it requires no special input on the user side. – gvega Feb 07 '19 at 18:58
  • 2
    Is it possible it's writing to stderr? ` 2>&1` might help to redirect stderr. Just, this problem is a LOT easier to solve on the output side. – Max Feb 07 '19 at 18:59
  • No that's not the issue sadly, I have tried redirecting stderr too and the output from GNU Radio is still not captured. – gvega Feb 07 '19 at 19:01
  • That is quite odd. Is gnuradio a library, or is it running as a subprocess? Does it have its own logging controls? I wonder if it's opening its own FD to the terminal. – Max Feb 07 '19 at 19:06
  • I am running GNU radio through a generated python file the program automatically creates. My file is similar to this [one](https://wiki.gnuradio.org/index.php/Guided_Tutorial_GNU_Radio_in_Python#3.1.1._GRC_Generated_Python_Files). But the script itself pulls from the gnuradio library. It definitely may have it's own logging controls, sadly the documentation is hit or miss. – gvega Feb 07 '19 at 19:13

0 Answers0