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.

346 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. # remove single \r that make long lines in text files
  170. # and other terminal commands
  171. msg = re.sub(r'\r(?!\n)|\x1B\[(K|.*?(m|h|l))', '', msg)
  172. if rec.levelno >= logging.INFO: # ??
  173. return msg
  174. rec.msg = msg
  175. rec.c1 = colors.PINK
  176. rec.c2 = colors.NORMAL
  177. return logging.Formatter.format(self, rec)
  178. log = None
  179. """global logger for Logs.debug, Logs.error, etc"""
  180. def debug(*k, **kw):
  181. """
  182. Wrap logging.debug, the output is filtered for performance reasons
  183. """
  184. if verbose:
  185. k = list(k)
  186. k[0] = k[0].replace('\n', ' ')
  187. global log
  188. log.debug(*k, **kw)
  189. def error(*k, **kw):
  190. """
  191. Wrap logging.errors, display the origin of the message when '-vv' is set
  192. """
  193. global log
  194. log.error(*k, **kw)
  195. if verbose > 2:
  196. st = traceback.extract_stack()
  197. if st:
  198. st = st[:-1]
  199. buf = []
  200. for filename, lineno, name, line in st:
  201. buf.append(' File "%s", line %d, in %s' % (filename, lineno, name))
  202. if line:
  203. buf.append(' %s' % line.strip())
  204. if buf: log.error("\n".join(buf))
  205. def warn(*k, **kw):
  206. """
  207. Wrap logging.warn
  208. """
  209. global log
  210. log.warn(*k, **kw)
  211. def info(*k, **kw):
  212. """
  213. Wrap logging.info
  214. """
  215. global log
  216. log.info(*k, **kw)
  217. def init_log():
  218. """
  219. Initialize the loggers globally
  220. """
  221. global log
  222. log = logging.getLogger('waflib')
  223. log.handlers = []
  224. log.filters = []
  225. hdlr = log_handler()
  226. hdlr.setFormatter(formatter())
  227. log.addHandler(hdlr)
  228. log.addFilter(log_filter())
  229. log.setLevel(logging.DEBUG)
  230. def make_logger(path, name):
  231. """
  232. Create a simple logger, which is often used to redirect the context command output::
  233. from waflib import Logs
  234. bld.logger = Logs.make_logger('test.log', 'build')
  235. bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False)
  236. # have the file closed immediately
  237. Logs.free_logger(bld.logger)
  238. # stop logging
  239. bld.logger = None
  240. The method finalize() of the command will try to free the logger, if any
  241. :param path: file name to write the log output to
  242. :type path: string
  243. :param name: logger name (loggers are reused)
  244. :type name: string
  245. """
  246. logger = logging.getLogger(name)
  247. hdlr = logging.FileHandler(path, 'w')
  248. formatter = logging.Formatter('%(message)s')
  249. hdlr.setFormatter(formatter)
  250. logger.addHandler(hdlr)
  251. logger.setLevel(logging.DEBUG)
  252. return logger
  253. def make_mem_logger(name, to_log, size=8192):
  254. """
  255. Create a memory logger to avoid writing concurrently to the main logger
  256. """
  257. from logging.handlers import MemoryHandler
  258. logger = logging.getLogger(name)
  259. hdlr = MemoryHandler(size, target=to_log)
  260. formatter = logging.Formatter('%(message)s')
  261. hdlr.setFormatter(formatter)
  262. logger.addHandler(hdlr)
  263. logger.memhandler = hdlr
  264. logger.setLevel(logging.DEBUG)
  265. return logger
  266. def free_logger(logger):
  267. """
  268. Free the resources held by the loggers created through make_logger or make_mem_logger.
  269. This is used for file cleanup and for handler removal (logger objects are re-used).
  270. """
  271. try:
  272. for x in logger.handlers:
  273. x.close()
  274. logger.removeHandler(x)
  275. except Exception:
  276. pass
  277. def pprint(col, msg, label='', sep='\n'):
  278. """
  279. Print messages in color immediately on stderr::
  280. from waflib import Logs
  281. Logs.pprint('RED', 'Something bad just happened')
  282. :param col: color name to use in :py:const:`Logs.colors_lst`
  283. :type col: string
  284. :param msg: message to display
  285. :type msg: string or a value that can be printed by %s
  286. :param label: a message to add after the colored output
  287. :type label: string
  288. :param sep: a string to append at the end (line separator)
  289. :type sep: string
  290. """
  291. info("%s%s%s %s" % (colors(col), msg, colors.NORMAL, label), extra={'terminator':sep})