logutils.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from inspect import getcallargs
  16. from functools import wraps
  17. import logging
  18. import inspect
  19. import time
  20. _TIME_FUNC_ID = 0
  21. def _log_debug_as_f(f, msg, msg_args):
  22. name = f.__module__
  23. logger = logging.getLogger(name)
  24. if logger.isEnabledFor(logging.DEBUG):
  25. lineno = f.func_code.co_firstlineno
  26. pathname = f.func_code.co_filename
  27. record = logging.LogRecord(
  28. name=name,
  29. level=logging.DEBUG,
  30. pathname=pathname,
  31. lineno=lineno,
  32. msg=msg,
  33. args=msg_args,
  34. exc_info=None
  35. )
  36. logger.handle(record)
  37. def log_function(f):
  38. """ Function decorator that logs every call to that function.
  39. """
  40. func_name = f.__name__
  41. @wraps(f)
  42. def wrapped(*args, **kwargs):
  43. name = f.__module__
  44. logger = logging.getLogger(name)
  45. level = logging.DEBUG
  46. if logger.isEnabledFor(level):
  47. bound_args = getcallargs(f, *args, **kwargs)
  48. def format(value):
  49. r = str(value)
  50. if len(r) > 50:
  51. r = r[:50] + "..."
  52. return r
  53. func_args = [
  54. "%s=%s" % (k, format(v)) for k, v in bound_args.items()
  55. ]
  56. msg_args = {
  57. "func_name": func_name,
  58. "args": ", ".join(func_args)
  59. }
  60. _log_debug_as_f(
  61. f,
  62. "Invoked '%(func_name)s' with args: %(args)s",
  63. msg_args
  64. )
  65. return f(*args, **kwargs)
  66. wrapped.__name__ = func_name
  67. return wrapped
  68. def time_function(f):
  69. func_name = f.__name__
  70. @wraps(f)
  71. def wrapped(*args, **kwargs):
  72. global _TIME_FUNC_ID
  73. id = _TIME_FUNC_ID
  74. _TIME_FUNC_ID += 1
  75. start = time.clock() * 1000
  76. try:
  77. _log_debug_as_f(
  78. f,
  79. "[FUNC START] {%s-%d}",
  80. (func_name, id),
  81. )
  82. r = f(*args, **kwargs)
  83. finally:
  84. end = time.clock() * 1000
  85. _log_debug_as_f(
  86. f,
  87. "[FUNC END] {%s-%d} %f",
  88. (func_name, id, end - start,),
  89. )
  90. return r
  91. return wrapped
  92. def trace_function(f):
  93. func_name = f.__name__
  94. linenum = f.func_code.co_firstlineno
  95. pathname = f.func_code.co_filename
  96. @wraps(f)
  97. def wrapped(*args, **kwargs):
  98. name = f.__module__
  99. logger = logging.getLogger(name)
  100. level = logging.DEBUG
  101. s = inspect.currentframe().f_back
  102. to_print = [
  103. "\t%s:%s %s. Args: args=%s, kwargs=%s" % (
  104. pathname, linenum, func_name, args, kwargs
  105. )
  106. ]
  107. while s:
  108. if True or s.f_globals["__name__"].startswith("synapse"):
  109. filename, lineno, function, _, _ = inspect.getframeinfo(s)
  110. args_string = inspect.formatargvalues(*inspect.getargvalues(s))
  111. to_print.append(
  112. "\t%s:%d %s. Args: %s" % (
  113. filename, lineno, function, args_string
  114. )
  115. )
  116. s = s.f_back
  117. msg = "\nTraceback for %s:\n" % (func_name,) + "\n".join(to_print)
  118. record = logging.LogRecord(
  119. name=name,
  120. level=level,
  121. pathname=pathname,
  122. lineno=lineno,
  123. msg=msg,
  124. args=None,
  125. exc_info=None
  126. )
  127. logger.handle(record)
  128. return f(*args, **kwargs)
  129. wrapped.__name__ = func_name
  130. return wrapped
  131. def get_previous_frames():
  132. s = inspect.currentframe().f_back.f_back
  133. to_return = []
  134. while s:
  135. if s.f_globals["__name__"].startswith("synapse"):
  136. filename, lineno, function, _, _ = inspect.getframeinfo(s)
  137. args_string = inspect.formatargvalues(*inspect.getargvalues(s))
  138. to_return.append("{{ %s:%d %s - Args: %s }}" % (
  139. filename, lineno, function, args_string
  140. ))
  141. s = s.f_back
  142. return ", ". join(to_return)
  143. def get_previous_frame(ignore=[]):
  144. s = inspect.currentframe().f_back.f_back
  145. while s:
  146. if s.f_globals["__name__"].startswith("synapse"):
  147. if not any(s.f_globals["__name__"].startswith(ig) for ig in ignore):
  148. filename, lineno, function, _, _ = inspect.getframeinfo(s)
  149. args_string = inspect.formatargvalues(*inspect.getargvalues(s))
  150. return "{{ %s:%d %s - Args: %s }}" % (
  151. filename, lineno, function, args_string
  152. )
  153. s = s.f_back
  154. return None