Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogClientTab: Use stand-alone logging class #489

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/cfclient/ui/tabs/LogClientTab.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@
"/ui/tabs/logClientTab.ui")[0]


class LogClientTab(Tab, log_client_tab_class, logging.StreamHandler):
class LogHandler(logging.StreamHandler):
def __init__(self, signal):
logging.StreamHandler.__init__(self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a scary way of doing it. I think recommended in Python3 would be super().init(), but in this particular case you should be able to just remove this line, since the ctor doesn't require any arguments.

self._signal = signal

def emit(self, record):
fmt = '%(levelname)s:%(name)s:%(message)s'
formatter = logging.Formatter(fmt)
#
# Calling .emit() on the signal will make the callback
# we registered in LogClientTab run.
#
self._signal.emit(formatter.format(record))


class LogClientTab(Tab, log_client_tab_class):
"""
A tab for showing client logging information, such
as USB Gamepad connections or scan feedback.
Expand All @@ -55,7 +70,6 @@ class LogClientTab(Tab, log_client_tab_class, logging.StreamHandler):

def __init__(self, tabWidget, helper, *args):
super(LogClientTab, self).__init__(*args)
logging.StreamHandler.__init__(self)
self.setupUi(self)

self.tabName = "Log Client"
Expand All @@ -65,22 +79,9 @@ def __init__(self, tabWidget, helper, *args):

self._update.connect(self.printText)
self._clearButton.clicked.connect(self.clear)
#
# We inherit from logging.StreamHandler and add ourself to the root
# logger. This means that our implementation of emit() will be called
# when a new LogRecord object is available on the stream.
#
cflogger = logging.getLogger(None)
cflogger.addHandler(self)

def emit(self, record):
fmt = '%(levelname)s:%(name)s:%(message)s'
formatter = logging.Formatter(fmt)
#
# Calling .emit() on the _update signal will make the callback
# we registered in __init__ run.
#
self._update.emit(formatter.format(record))
cflogger = logging.getLogger(None)
cflogger.addHandler(LogHandler(self._update))

def printText(self, text):
logger.debug("[%s]", text)
Expand Down