Skip to content
Open
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
21 changes: 14 additions & 7 deletions robotbackgroundlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

__version__ = '1.3dev'


class BaseLogger(object):
"""Base class for custom loggers with same api as ``robot.api.logger``."""

Expand Down Expand Up @@ -80,12 +79,12 @@ def __init__(self):

def write(self, msg, level, html=False):
with self.lock:
thread = threading.currentThread().getName()
if thread in self.LOGGING_THREADS:
thread_name = current_thread_name()
if thread_name in self.LOGGING_THREADS:
logger.write(msg, level, html)
else:
message = BackgroundMessage(msg, level, html)
self._messages.setdefault(thread, []).append(message)
self._messages.setdefault(thread_name, []).append(message)

def log_background_messages(self, name=None):
"""Forwards messages logged on background to Robot Framework log.
Expand All @@ -96,17 +95,25 @@ def log_background_messages(self, name=None):

This method must be called from the main thread.
"""
thread = threading.currentThread().getName()
if thread not in self.LOGGING_THREADS:
thread_name = current_thread_name()
if thread_name not in self.LOGGING_THREADS:
raise RuntimeError(
"Logging background messages is only allowed from the main "
"thread. Current thread name: %s" % thread)
"thread. Current thread name: %s" % thread_name)
with self.lock:
if name:
self._log_messages_by_thread(name)
else:
self._log_all_messages()

def current_thread_name():
if hasattr(threading, 'current_thread'):
# Changed in version 2.6: Added current_thread() and name property.
return threading.current_thread().name
else:
# This gives warnings in more recent versions of Python
return threading.currentThread().getName()

def _log_messages_by_thread(self, name):
for message in self._messages.pop(name, []):
print(message.format())
Expand Down