jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
8.3KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2010 (ita)
  4. """
  5. logging, colors, terminal width and pretty-print
  6. """
  7. import os, re, traceback, sys
  8. from waflib import Utils, ansiterm
  9. if not os.environ.get('NOSYNC', False):
  10. # synchronized output is nearly mandatory to prevent garbled output
  11. if sys.stdout.isatty() and id(sys.stdout) == id(sys.__stdout__):
  12. sys.stdout = ansiterm.AnsiTerm(sys.stdout)
  13. if sys.stderr.isatty() and id(sys.stderr) == id(sys.__stderr__):
  14. sys.stderr = ansiterm.AnsiTerm(sys.stderr)
  15. # import the logging module after since it holds a reference on sys.stderr
  16. # in case someone uses the root logger
  17. import logging
  18. LOG_FORMAT = os.environ.get('WAF_LOG_FORMAT', '%(asctime)s %(c1)s%(zone)s%(c2)s %(message)s')
  19. HOUR_FORMAT = os.environ.get('WAF_HOUR_FORMAT', '%H:%M:%S')
  20. zones = ''
  21. verbose = 0
  22. colors_lst = {
  23. 'USE' : True,
  24. 'BOLD' :'\x1b[01;1m',
  25. 'RED' :'\x1b[01;31m',
  26. 'GREEN' :'\x1b[32m',
  27. 'YELLOW':'\x1b[33m',
  28. 'PINK' :'\x1b[35m',
  29. 'BLUE' :'\x1b[01;34m',
  30. 'CYAN' :'\x1b[36m',
  31. 'GREY' :'\x1b[37m',
  32. 'NORMAL':'\x1b[0m',
  33. 'cursor_on' :'\x1b[?25h',
  34. 'cursor_off' :'\x1b[?25l',
  35. }
  36. indicator = '\r\x1b[K%s%s%s'
  37. try:
  38. unicode
  39. except NameError:
  40. unicode = None
  41. def enable_colors(use):
  42. if use == 1:
  43. if not (sys.stderr.isatty() or sys.stdout.isatty()):
  44. use = 0
  45. if Utils.is_win32 and os.name != 'java':
  46. term = os.environ.get('TERM', '') # has ansiterm
  47. else:
  48. term = os.environ.get('TERM', 'dumb')
  49. if term in ('dumb', 'emacs'):
  50. use = 0
  51. if use >= 1:
  52. os.environ['TERM'] = 'vt100'
  53. colors_lst['USE'] = use
  54. # If console packages are available, replace the dummy function with a real
  55. # implementation
  56. try:
  57. get_term_cols = ansiterm.get_term_cols
  58. except AttributeError:
  59. def get_term_cols():
  60. return 80
  61. get_term_cols.__doc__ = """
  62. Get the console width in characters.
  63. :return: the number of characters per line
  64. :rtype: int
  65. """
  66. def get_color(cl):
  67. if not colors_lst['USE']: return ''
  68. return colors_lst.get(cl, '')
  69. class color_dict(object):
  70. """attribute-based color access, eg: colors.PINK"""
  71. def __getattr__(self, a):
  72. return get_color(a)
  73. def __call__(self, a):
  74. return get_color(a)
  75. colors = color_dict()
  76. re_log = re.compile(r'(\w+): (.*)', re.M)
  77. class log_filter(logging.Filter):
  78. """
  79. The waf logs are of the form 'name: message', and can be filtered by 'waf --zones=name'.
  80. For example, the following::
  81. from waflib import Logs
  82. Logs.debug('test: here is a message')
  83. Will be displayed only when executing::
  84. $ waf --zones=test
  85. """
  86. def __init__(self, name=None):
  87. pass
  88. def filter(self, rec):
  89. """
  90. filter a record, adding the colors automatically
  91. * error: red
  92. * warning: yellow
  93. :param rec: message to record
  94. """
  95. rec.zone = rec.module
  96. if rec.levelno >= logging.INFO:
  97. return True
  98. m = re_log.match(rec.msg)
  99. if m:
  100. rec.zone = m.group(1)
  101. rec.msg = m.group(2)
  102. if zones:
  103. return getattr(rec, 'zone', '') in zones or '*' in zones
  104. elif not verbose > 2:
  105. return False
  106. return True
  107. class log_handler(logging.StreamHandler):
  108. """Dispatches messages to stderr/stdout depending on the severity level"""
  109. def emit(self, record):
  110. # default implementation
  111. try:
  112. try:
  113. self.stream = record.stream
  114. except AttributeError:
  115. if record.levelno >= logging.WARNING:
  116. record.stream = self.stream = sys.stderr
  117. else:
  118. record.stream = self.stream = sys.stdout
  119. self.emit_override(record)
  120. self.flush()
  121. except (KeyboardInterrupt, SystemExit):
  122. raise
  123. except: # from the python library -_-
  124. self.handleError(record)
  125. def emit_override(self, record, **kw):
  126. self.terminator = getattr(record, 'terminator', '\n')
  127. stream = self.stream
  128. if unicode:
  129. # python2
  130. msg = self.formatter.format(record)
  131. fs = '%s' + self.terminator
  132. try:
  133. if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)):
  134. fs = fs.decode(stream.encoding)
  135. try:
  136. stream.write(fs % msg)
  137. except UnicodeEncodeError:
  138. stream.write((fs % msg).encode(stream.encoding))
  139. else:
  140. stream.write(fs % msg)
  141. except UnicodeError:
  142. stream.write((fs % msg).encode("UTF-8"))
  143. else:
  144. logging.StreamHandler.emit(self, record)
  145. class formatter(logging.Formatter):
  146. """Simple log formatter which handles colors"""
  147. def __init__(self):
  148. logging.Formatter.__init__(self, LOG_FORMAT, HOUR_FORMAT)
  149. def format(self, rec):
  150. """Messages in warning, error or info mode are displayed in color by default"""
  151. try:
  152. msg = rec.msg.decode('utf-8')
  153. except Exception:
  154. msg = rec.msg
  155. use = colors_lst['USE']
  156. if (use == 1 and rec.stream.isatty()) or use == 2:
  157. c1 = getattr(rec, 'c1', None)
  158. if c1 is None:
  159. c1 = ''
  160. if rec.levelno >= logging.ERROR:
  161. c1 = colors.RED
  162. elif rec.levelno >= logging.WARNING:
  163. c1 = colors.YELLOW
  164. elif rec.levelno >= logging.INFO:
  165. c1 = colors.GREEN
  166. c2 = getattr(rec, 'c2', colors.NORMAL)
  167. msg = '%s%s%s' % (c1, msg, c2)
  168. else:
  169. msg = msg.replace('\r', '\n')
  170. msg = re.sub(r'\x1B\[(K|.*?(m|h|l))', '', msg)
  171. if rec.levelno >= logging.INFO: # ??
  172. return msg
  173. rec.msg = msg
  174. rec.c1 = colors.PINK
  175. rec.c2 = colors.NORMAL
  176. return logging.Formatter.format(self, rec)
  177. log = None
  178. """global logger for Logs.debug, Logs.error, etc"""
  179. def debug(*k, **kw):
  180. """
  181. Wrap logging.debug, the output is filtered for performance reasons
  182. """
  183. if verbose:
  184. k = list(k)
  185. k[0] = k[0].replace('\n', ' ')
  186. global log
  187. log.debug(*k, **kw)
  188. def error(*k, **kw):
  189. """
  190. Wrap logging.errors, display the origin of the message when '-vv' is set
  191. """
  192. global log
  193. log.error(*k, **kw)
  194. if verbose > 2:
  195. st = traceback.extract_stack()
  196. if st:
  197. st = st[:-1]
  198. buf = []
  199. for filename, lineno, name, line in st:
  200. buf.append(' File "%s", line %d, in %s' % (filename, lineno, name))
  201. if line:
  202. buf.append(' %s' % line.strip())
  203. if buf: log.error("\n".join(buf))
  204. def warn(*k, **kw):
  205. """
  206. Wrap logging.warn
  207. """
  208. global log
  209. log.warn(*k, **kw)
  210. def info(*k, **kw):
  211. """
  212. Wrap logging.info
  213. """
  214. global log
  215. log.info(*k, **kw)
  216. def init_log():
  217. """
  218. Initialize the loggers globally
  219. """
  220. global log
  221. log = logging.getLogger('waflib')
  222. log.handlers = []
  223. log.filters = []
  224. hdlr = log_handler()
  225. hdlr.setFormatter(formatter())
  226. log.addHandler(hdlr)
  227. log.addFilter(log_filter())
  228. log.setLevel(logging.DEBUG)
  229. def make_logger(path, name):
  230. """
  231. Create a simple logger, which is often used to redirect the context command output::
  232. from waflib import Logs
  233. bld.logger = Logs.make_logger('test.log', 'build')
  234. bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False)
  235. # have the file closed immediately
  236. Logs.free_logger(bld.logger)
  237. # stop logging
  238. bld.logger = None
  239. The method finalize() of the command will try to free the logger, if any
  240. :param path: file name to write the log output to
  241. :type path: string
  242. :param name: logger name (loggers are reused)
  243. :type name: string
  244. """
  245. logger = logging.getLogger(name)
  246. hdlr = logging.FileHandler(path, 'w')
  247. formatter = logging.Formatter('%(message)s')
  248. hdlr.setFormatter(formatter)
  249. logger.addHandler(hdlr)
  250. logger.setLevel(logging.DEBUG)
  251. return logger
  252. def make_mem_logger(name, to_log, size=8192):
  253. """
  254. Create a memory logger to avoid writing concurrently to the main logger
  255. """
  256. from logging.handlers import MemoryHandler
  257. logger = logging.getLogger(name)
  258. hdlr = MemoryHandler(size, target=to_log)
  259. formatter = logging.Formatter('%(message)s')
  260. hdlr.setFormatter(formatter)
  261. logger.addHandler(hdlr)
  262. logger.memhandler = hdlr
  263. logger.setLevel(logging.DEBUG)
  264. return logger
  265. def free_logger(logger):
  266. """
  267. Free the resources held by the loggers created through make_logger or make_mem_logger.
  268. This is used for file cleanup and for handler removal (logger objects are re-used).
  269. """
  270. try:
  271. for x in logger.handlers:
  272. x.close()
  273. logger.removeHandler(x)
  274. except Exception:
  275. pass
  276. def pprint(col, msg, label='', sep='\n'):
  277. """
  278. Print messages in color immediately on stderr::
  279. from waflib import Logs
  280. Logs.pprint('RED', 'Something bad just happened')
  281. :param col: color name to use in :py:const:`Logs.colors_lst`
  282. :type col: string
  283. :param msg: message to display
  284. :type msg: string or a value that can be printed by %s
  285. :param label: a message to add after the colored output
  286. :type label: string
  287. :param sep: a string to append at the end (line separator)
  288. :type sep: string
  289. """
  290. info("%s%s%s %s" % (colors(col), msg, colors.NORMAL, label), extra={'terminator':sep})