msg.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'msg' implementation
  5. Message handling infrastructure for bitbake
  6. """
  7. # Copyright (C) 2006 Richard Purdie
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. import sys
  22. import collections
  23. import bb
  24. import bb.event
  25. debug_level = collections.defaultdict(lambda: 0)
  26. verbose = False
  27. def _NamedTuple(name, fields):
  28. Tuple = collections.namedtuple(name, " ".join(fields))
  29. return Tuple(*range(len(fields)))
  30. domain = _NamedTuple("Domain", (
  31. "Default",
  32. "Build",
  33. "Cache",
  34. "Collection",
  35. "Data",
  36. "Depends",
  37. "Fetcher",
  38. "Parsing",
  39. "PersistData",
  40. "Provider",
  41. "RunQueue",
  42. "TaskData",
  43. "Util"))
  44. class MsgBase(bb.event.Event):
  45. """Base class for messages"""
  46. def __init__(self, msg):
  47. self._message = msg
  48. bb.event.Event.__init__(self)
  49. class MsgDebug(MsgBase):
  50. """Debug Message"""
  51. class MsgNote(MsgBase):
  52. """Note Message"""
  53. class MsgWarn(MsgBase):
  54. """Warning Message"""
  55. class MsgError(MsgBase):
  56. """Error Message"""
  57. class MsgFatal(MsgBase):
  58. """Fatal Message"""
  59. class MsgPlain(MsgBase):
  60. """General output"""
  61. #
  62. # Message control functions
  63. #
  64. def set_debug_level(level):
  65. for d in domain:
  66. debug_level[d] = level
  67. debug_level[domain.Default] = level
  68. def get_debug_level(msgdomain = domain.Default):
  69. return debug_level[msgdomain]
  70. def set_verbose(level):
  71. verbose = level
  72. def set_debug_domains(strdomains):
  73. for domainstr in strdomains:
  74. for d in domain:
  75. if domain._fields[d] == domainstr:
  76. debug_level[d] += 1
  77. break
  78. else:
  79. warn(None, "Logging domain %s is not valid, ignoring" % domainstr)
  80. #
  81. # Message handling functions
  82. #
  83. def debug(level, msgdomain, msg, fn = None):
  84. if not msgdomain:
  85. msgdomain = domain.Default
  86. if debug_level[msgdomain] >= level:
  87. bb.event.fire(MsgDebug(msg), None)
  88. if not bb.event._ui_handlers:
  89. print('DEBUG: %s' % (msg))
  90. def note(level, msgdomain, msg, fn = None):
  91. if not msgdomain:
  92. msgdomain = domain.Default
  93. if level == 1 or verbose or debug_level[msgdomain] >= 1:
  94. bb.event.fire(MsgNote(msg), None)
  95. if not bb.event._ui_handlers:
  96. print('NOTE: %s' % (msg))
  97. def warn(msgdomain, msg, fn = None):
  98. bb.event.fire(MsgWarn(msg), None)
  99. if not bb.event._ui_handlers:
  100. print('WARNING: %s' % (msg))
  101. def error(msgdomain, msg, fn = None):
  102. bb.event.fire(MsgError(msg), None)
  103. print('ERROR: %s' % (msg))
  104. def fatal(msgdomain, msg, fn = None):
  105. bb.event.fire(MsgFatal(msg), None)
  106. print('FATAL: %s' % (msg))
  107. sys.exit(1)
  108. def plain(msg, fn = None):
  109. bb.event.fire(MsgPlain(msg), None)
  110. if not bb.event._ui_handlers:
  111. print(msg)