I want to store the commands and their output to be stored in log.txt . So, i am assigning customer Logger class object to sys.stdout with write method overriden to store the output both in file as well as to print in console. The problem here is that when sys.stdout is overriden, ‘tab’ autocompletion of commands in ABC class ( hi,hello,shutdown ) is not working and up and down arrows are also not working for forward and backward history commands.
I expect that when ‘hel’ is typed in command above and then if tab is pressed, it should display ‘hello help’ in the next line. Pressing tab shouldn’t add 4 spaces to the command being typed. Pleas help with working solution in which auto completion of commands with tab,up/down arrow works and the commands and their output should store in a file ‘log.txt’. Please mind that i am running this program in red had linux 7 OS, not windows.
Thanks for your reply. But, output of commands is not getting written to file.
In above solution, we are passing Logger object as stdout to the cmd.py file. So, the content in cmd.py file( eg. output of help command, doc_string of any method) will only be written to log.txt. content in do_* methods ABC class is not written to log.txt. We want sys.stdout to be overriden with Logger object in current python file. Then only the content in do_* methods will be written to the log.txt file.
Tried the following code for tab auto completion using readline module. Still it’s not working.
I am suspecting that sys module and readline module are interfering with each other and this functionality is not working because of that. Please help to suggest a solution for this.
The abc.stdout is a Logger object and the command outputs (e.g. help string) will be put into the log file. If it is not preferable, just remove the argument.
sys.stdout = Logger()
This line should be removed as it overwrites the system stdout and will prevent the readline completer from working. So, you should use the custom output instead of print function:
This is working. Thanks.
One more thing…
Now, i want to redirect sys.stderr along with sys.stdout to the same file and console.
i tried the following class but it’s printing eveything twice.
The challenge here is how do we know ‘message’ is from sys.stdout or sys.stderr ?
If we use above class, everything will be printed twice in console. Is there any condition we can try
so that the write method will be similar to following.
if condition :
sys.console.write(message)
else:
sys.err_console.write(message)
Our intension here is to print message based on which method it was called from, either sys.stdout or sys.stderr.