1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# -*- coding: utf-8 -*- ## Copyright 2019 Trevor van Hoof and Jan Pijpers. ## Licensed under the Apache License, Version 2.0 ## Downloaded from https://janpijpers.com or https://gumroad.com/janpijpers ## See the license file attached or on https://www.janpijpers.com/script-licenses/ ''' Name: interceptStdOutAndStdErr Description: intercept the std out and write it to a log file. it could cause a bunch of errors if any file operations are called on an std pipe though XD need to implement the std pipe functions in the class but you get the idea i hope. ''' import sys import tempfile import uuid import time import os ## Make a temp output folder to keep track of the logs. TEMP_DIR = os.path.join(tempfile.gettempdir(), "StdOut") if not os.path.exists(TEMP_DIR): os.makedirs(TEMP_DIR) class stdOverwrite( object ): ''' Should also add a get attr for any unimplemented std pipe functions like all the file functions ''' def __init__( self, stdPipe, pipeName= "_output" ): self._terminal = stdPipe unique_filename = str(uuid.uuid4()) logPath = os.path.join(TEMP_DIR, unique_filename+"_{}.log".format(pipeName)) self.log = open( logPath , "w") print "Saving: ", pipeName, " to: ", logPath def write( self, message ): self._terminal.write( message ) self.log.write(message) self.log.flush() def close( self ): self._terminal.close() self.log.close() def flush(self): self._terminal.flush() sys.stdout = stdOverwrite(sys.stdout, "stdOut") sys.stderr = stdOverwrite(sys.stderr, "stdErr") print "漢字カタカナ" raise Exception("ble") ## this should all have been written to the log files in the temp folder |