Beispiel #1
0
def redirectOutput():
    # If I write code such as sys.stdout = open('stdout.txt', 'w'),
    # I have to flush the file after writing to it (using "print" for example).
    # The following class saves the need to flush the file each time.
    class FlushedFile:
        """ Provides an output file that is immediately flushed after write """
        def __init__(self, filepath):
            try:
                self.terminal = sys.stdout
                self.file = open(filepath, 'w')
            except Exception:
                self.file = None

        def write(self, str):
            if self.file == None:
                return
            self.file.write(str)
            self.file.flush()

        def __getattr__(self, attr):
            try:
                self.file.flush()
                return getattr(self.terminal, attr)
            except Exception as err:
                print("launchy_util, FlashedFile.__getattr__,", err)

    # Redirect stdout and stderr
    sys.stdout = FlushedFile(os.path.join(launchy.getAppTempPath(), 'py_stdout.log'))
    sys.stderr = FlushedFile(os.path.join(launchy.getAppTempPath(), 'py_stderr.log'))
    print("launchy_util, redirect output finished")
Beispiel #2
0
def init_logging():
    try:
        import launchy
        log_file_name = os.path.join(launchy.getAppTempPath(), 'launchypy.log')
    except:
        import pathlib
        log_file_name = os.path.join(
            pathlib.Path(__file__).parent.resolve(), 'launchypy.log')
    finally:
        print('launchy_util::init_logging, log file name:', log_file_name)
        log.basicConfig(
            filename=log_file_name,
            filemode='w',
            format='%(asctime)s.%(msecs)03d [%(levelname).1s] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S',
            level=log.DEBUG)
        log.info('launchy_util::init_logging, log init')