1

Ok, so using this example, I'm logging my stdout to a file as well as sending it to the terminal.

But when I look at the log file, the backspaces are not processed, but printed along with the output.

Any way I could log the "final" state of stdout of a python script?

Community
  • 1
  • 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158

2 Answers2

1

Here is a solution that takes the basic class from the answer you linked and adds some regex handling for \r and \b:

import sys
import re

class Logger(object):
    def __init__(self, filename="Default.log"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")
        self.cr_pattern = re.compile("^.*\r", re.M)
        self.bs_pattern = re.compile(".\b")

    def write(self, message):
        self.terminal.write(message)
        message = self.bs_pattern.sub('', self.cr_pattern.sub('', message))
        self.log.write(message)

sys.stdout = Logger("yourlogfilename.txt")
print "Hello\rGoodbyee\b world!"

Example run:

$ python test.py
Goodbye world!
$ cat yourlogfilename.txt
Goodbye world!

Note that normally you should use raw string literals for your regular expressions, this is one of the rare cases where you shouldn't.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

I'm using the logging facility of Python and had the same problem that it transforms the correct end-of-line sequence "\x0D\x00\x0A\x00" to "\x0D\x0A\x00".

The program output which I want to log via Python is UTF-16 as you can see.

To avoid that Python does anything different from just writing the Bytes it receives from stdout to the logfile, I tried to add encoding="UTF-8" to logging.FileHandler().

The result is that '\x0D' isn't printed anymore and I get "\x0A\x00\x0A\x00" instead. That's a little bit better I guess.

On Linux it seems that there is no problem with that. I'm using the same script there but my program prints UTF-8 characters instead.

(There are two questions left here: Why does Python add another newline? And is it possible to log additional Information to the logfile? Putting an 'U' before strings or using "...".encode(UTF-16) doesn't work. But otherwise the logfile will contain strings of different encodings and becomes useless...)

Littm
  • 4,923
  • 4
  • 30
  • 38