mlog.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2013-2014 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import sys, os, platform, io
  12. """This is (mostly) a standalone module used to write logging
  13. information about Meson runs. Some output goes to screen,
  14. some to logging dir and some goes to both."""
  15. if platform.system().lower() == 'windows':
  16. colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('ANSICON')
  17. else:
  18. colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('TERM') != 'dumb'
  19. log_dir = None
  20. log_file = None
  21. log_fname = 'meson-log.txt'
  22. def initialize(logdir):
  23. global log_dir, log_file
  24. log_dir = logdir
  25. log_file = open(os.path.join(logdir, log_fname), 'w', encoding='utf8')
  26. def shutdown():
  27. global log_file
  28. if log_file is not None:
  29. exception_around_goer = log_file
  30. log_file = None
  31. exception_around_goer.close()
  32. class AnsiDecorator:
  33. plain_code = "\033[0m"
  34. def __init__(self, text, code):
  35. self.text = text
  36. self.code = code
  37. def get_text(self, with_codes):
  38. if with_codes:
  39. return self.code + self.text + AnsiDecorator.plain_code
  40. return self.text
  41. def bold(text):
  42. return AnsiDecorator(text, "\033[1m")
  43. def red(text):
  44. return AnsiDecorator(text, "\033[1;31m")
  45. def green(text):
  46. return AnsiDecorator(text, "\033[1;32m")
  47. def yellow(text):
  48. return AnsiDecorator(text, "\033[1;33m")
  49. def cyan(text):
  50. return AnsiDecorator(text, "\033[1;36m")
  51. def process_markup(args, keep):
  52. arr = []
  53. for arg in args:
  54. if isinstance(arg, str):
  55. arr.append(arg)
  56. elif isinstance(arg, AnsiDecorator):
  57. arr.append(arg.get_text(keep))
  58. else:
  59. arr.append(str(arg))
  60. return arr
  61. def force_print(*args, **kwargs):
  62. # _Something_ is going to get printed.
  63. try:
  64. print(*args, **kwargs)
  65. except UnicodeEncodeError:
  66. iostr = io.StringIO()
  67. kwargs['file'] = iostr
  68. print(*args, **kwargs)
  69. cleaned = iostr.getvalue().encode('ascii', 'replace').decode('ascii')
  70. print(cleaned)
  71. def debug(*args, **kwargs):
  72. arr = process_markup(args, False)
  73. if log_file is not None:
  74. print(*arr, file=log_file, **kwargs) # Log file never gets ANSI codes.
  75. log_file.flush()
  76. def log(*args, **kwargs):
  77. arr = process_markup(args, False)
  78. if log_file is not None:
  79. print(*arr, file=log_file, **kwargs) # Log file never gets ANSI codes.
  80. log_file.flush()
  81. if colorize_console:
  82. arr = process_markup(args, True)
  83. force_print(*arr, **kwargs)
  84. def warning(*args, **kwargs):
  85. log(yellow('WARNING:'), *args, **kwargs)
  86. # Format a list for logging purposes as a string. It separates
  87. # all but the last item with commas, and the last with 'and'.
  88. def format_list(list):
  89. l = len(list)
  90. if l > 2:
  91. return ' and '.join([', '.join(list[:-1]), list[-1]])
  92. elif l == 2:
  93. return ' and '.join(list)
  94. elif l == 1:
  95. return list[0]
  96. else:
  97. return ''