_debug.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2009-2011 Wander Lairson Costa
  2. #
  3. # The following terms apply to all files associated
  4. # with the software unless explicitly disclaimed in individual files.
  5. #
  6. # The authors hereby grant permission to use, copy, modify, distribute,
  7. # and license this software and its documentation for any purpose, provided
  8. # that existing copyright notices are retained in all copies and that this
  9. # notice is included verbatim in any distributions. No written agreement,
  10. # license, or royalty fee is required for any of the authorized uses.
  11. # Modifications to this software may be copyrighted by their authors
  12. # and need not follow the licensing terms described here, provided that
  13. # the new terms are clearly indicated on the first page of each file where
  14. # they apply.
  15. #
  16. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  17. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  18. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  19. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  20. # POSSIBILITY OF SUCH DAMAGE.
  21. #
  22. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
  25. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  26. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  27. # MODIFICATIONS.
  28. __author__ = 'Wander Lairson Costa'
  29. __all__ = ['methodtrace', 'functiontrace']
  30. import logging
  31. import usb._interop as _interop
  32. def _trace_function_call(logger, fname, *args, **named_args):
  33. logger.debug(
  34. # TODO: check if 'f' is a method or a free function
  35. fname + '(' + \
  36. ', '.join((str(val) for val in args)) + \
  37. ', '.join((name + '=' + str(val) for name, val in named_args.items())) + ')'
  38. )
  39. # decorator for methods calls tracing
  40. def methodtrace(logger):
  41. def decorator_logging(f):
  42. def do_trace(*args, **named_args):
  43. # this if is just a optimization to avoid unecessary string formatting
  44. if logging.DEBUG >= logger.getEffectiveLevel():
  45. fn = type(args[0]).__name__ + '.' + f.__name__
  46. _trace_function_call(logger, fn, *args[1:], **named_args)
  47. return f(*args, **named_args)
  48. _interop._update_wrapper(do_trace, f)
  49. return do_trace
  50. return decorator_logging
  51. # decorator for methods calls tracing
  52. def functiontrace(logger):
  53. def decorator_logging(f):
  54. def do_trace(*args, **named_args):
  55. # this if is just a optimization to avoid unecessary string formatting
  56. if logging.DEBUG >= logger.getEffectiveLevel():
  57. _trace_function_call(logger, f.__name__, *args, **named_args)
  58. return f(*args, **named_args)
  59. _interop._update_wrapper(do_trace, f)
  60. return do_trace
  61. return decorator_logging