I recenlty desired a bit of python code that would allow me to output to the console and a logfile with the same print statement. After googleing I found this website which offered an excellent solution. However, I would like to be able to flush the output buffer after each write in order to see it in the log file. How would I go about adding it into this class?
I have tried to following setups...
class output_file(object):
  def __init__(self, stdout, filename):
    silentremove(filename)
    self.stdout = stdout
    self.logfile = open(filename, "a")
def write(self, text):
    self.stdout.write(text)
    self.logfile.write(text)
    sys.stdout.flush()
  def flush(self):
    sys.stdout.flush()
  def close(self):
    self.stdout.close()
    self.logfile.close()
This was had a cyclic error which resulted in the flush function calling itself.
class output_file(object):
  def __init__(self, stdout, filename):
    silentremove(filename)
    self.stdout = stdout
    self.logfile = open(filename, "a")
  def write(self, text):
    self.stdout.write(text)
    self.logfile.write(text)
    self.stdout.flush()
  def flush(self):
    sys.stdout.flush()
  def close(self):
    self.stdout.close()
    self.logfile.close()
This didn't flush it at all.
 
    