Skip to content

Commit

Permalink
Add decorator logger for pydebug (#13)
Browse files Browse the repository at this point in the history
* Add decorator logger

* Use decorator to pass the same messages to the debug log as is passed
  using the debug function to standard out.
* This will let users use python standard logging.

* Respect pydebug api and make output logging safe

* Do not add newline by default
  • Loading branch information
MartinHjelmare authored Apr 10, 2018
1 parent 1b4e250 commit abff1b9
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion leicacam/cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
from __future__ import print_function

import os
import functools
import logging
import platform
import socket
from collections import OrderedDict
from time import sleep, time

import pydebug

_LOGGER = logging.getLogger(__name__)


def logger(function):
"""Decorate passed in function and log message to module logger."""
@functools.wraps(function)
def wrapper(*args, **kwargs):
"""Wrap function."""
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '') # do not add newline by default
out = sep.join([repr(x) for x in args])
out = out + end
_LOGGER.debug(out)
return function(*args, **kwargs)
return wrapper


# debug with `DEBUG=leicacam python script.py`
if platform.system() == 'Windows':
# monkeypatch
@logger
def debug(msg):
"""Debug on Windows."""
try:
Expand All @@ -21,7 +41,7 @@ def debug(msg):
except KeyError:
pass
else:
debug = pydebug.debug('leicacam') # pylint: disable=invalid-name
debug = logger(pydebug.debug('leicacam')) # pylint: disable=invalid-name


class CAM(object):
Expand Down

0 comments on commit abff1b9

Please sign in to comment.