Skip to content

Commit

Permalink
Logging is a-very-nice
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaid committed Dec 1, 2019
1 parent e5c4bc8 commit 6aac9ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 39 deletions.
43 changes: 4 additions & 39 deletions LPHK.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,10 @@ def get_first_textfile_line(file_path):
VERSION = get_first_textfile_line(os.path.join(PATH, "VERSION"))

# Setup dual logging/printing
class Tee:
def __init__(self, file_path):
self.file_path = file_path
self.file = open(self.file_path, "w")
self.stdout_logger = self.TeeStdout(self.file)
self.stderr_logger = self.TeeStderr(self.file)
def __del__(self):
self.stdout_logger.__del__()
self.stderr_logger.__del__()
self.file.close()
class TeeStdout:
def __init__(self, file_in):
self.file = file_in
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
def write(self, data):
self.file.write(data)
self.file.flush()
self.stdout.write(data)
def flush(self):
self.file.flush()
class TeeStderr:
def __init__(self, file_in):
self.file = file_in
self.stderr = sys.stderr
sys.stderr = self
def __del__(self):
sys.stderr = self.stderr
def write(self, data):
self.file.write(data)
self.file.flush()
self.stderr.write(data)
def flush(self):
self.file.flush()

LOG_PATH = os.path.join(USER_PATH, LOG_TITLE)
LOG = Tee(LOG_PATH)

import logger
logger.start(LOG_PATH)

# Start printing output
def datetime_str():
Expand Down Expand Up @@ -135,7 +100,7 @@ def shutdown():
lp_events.timer.cancel()
lp.Close()
window.lp_connected = False
LOG.__del__()
logger.stop()
if window.restart:
if IS_EXE:
os.startfile(sys.argv[0])
Expand Down
65 changes: 65 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logger module by Ella J. (nimaid)
#
# This module provides basic logging of STDOUT and STDERR to a text file
# Usage:
#
# import logger
# logger.start("/path/to/my/log.txt")
# ...
# logger.stop()

import sys

log = None

class _Logger:
def __init__(self, file_path):
self.file_path = file_path
self.file = open(self.file_path, "w")
self.stdout_logger = self._LoggerStdout(self.file)
self.stderr_logger = self._LoggerStderr(self.file)
def __del__(self):
self.stdout_logger.__del__()
self.stderr_logger.__del__()
self.file.close()
class _LoggerStdout:
def __init__(self, file_in):
self.file = file_in
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
def write(self, data):
self.file.write(data)
self.file.flush()
self.stdout.write(data)
def flush(self):
self.file.flush()
class _LoggerStderr:
def __init__(self, file_in):
self.file = file_in
self.stderr = sys.stderr
sys.stderr = self
def __del__(self):
sys.stderr = self.stderr
def write(self, data):
self.file.write(data)
self.file.flush()
self.stderr.write(data)
def flush(self):
self.file.flush()

def start(file_path):
global log
if log != None:
raise Exception("A log is already running: " + log.file_path)
else:
log = _Logger(file_path)

def stop():
global log
if log == None:
raise Exception("No log is currently running.")
else:
log.__del__()
log = None

0 comments on commit 6aac9ee

Please sign in to comment.