Kodi plugin to to play various online streams (mostly Latvian)

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. """
  3. kodiswift.log
  4. --------------
  5. This module contains the kodiswift logger as well as a convenience
  6. method for creating new loggers.
  7. :copyright: (c) 2012 by Jonathan Beluch
  8. :license: GPLv3, see LICENSE for more details.
  9. """
  10. from __future__ import absolute_import
  11. import logging
  12. from kodiswift import CLI_MODE
  13. __all__ = ['setup_log', 'GLOBAL_LOG_LEVEL', 'log']
  14. # TODO: Add logging to a file as well when on CLI with lowest threshold
  15. # possible
  16. # fh = logging.FileHandler('log_filename.txt')
  17. # fh.setLevel(logging.DEBUG)
  18. # fh.setFormatter(formatter)
  19. # log.addHandler(fh)
  20. # TODO: Allow a global flag to set logging level when dealing with Kodi
  21. # TODO: Add -q and -v flags to CLI to quiet or enable more verbose logging
  22. class XBMCFilter(object):
  23. """A logging filter that streams to STDOUT or to the xbmc log if
  24. running inside Kodi.
  25. """
  26. python_to_xbmc = {
  27. 'DEBUG': 'LOGDEBUG',
  28. 'INFO': 'LOGNOTICE',
  29. 'WARNING': 'LOGWARNING',
  30. 'ERROR': 'LOGERROR',
  31. 'CRITICAL': 'LOGSEVERE',
  32. }
  33. xbmc_levels = {
  34. 'LOGDEBUG': 0,
  35. 'LOGINFO': 1,
  36. 'LOGNOTICE': 2,
  37. 'LOGWARNING': 3,
  38. 'LOGERROR': 4,
  39. 'LOGSEVERE': 5,
  40. 'LOGFATAL': 6,
  41. 'LOGNONE': 7,
  42. }
  43. def __init__(self, prefix):
  44. self.prefix = prefix
  45. def filter(self, record):
  46. """Returns True for all records if running in the CLI, else returns
  47. True.
  48. When running inside Kodi it calls the xbmc.log() method and prevents
  49. the message from being double printed to STDOUT.
  50. """
  51. # When running in Kodi, any logged statements will be double printed
  52. # since we are calling xbmc.log() explicitly. Therefore we return False
  53. # so every log message is filtered out and not printed again.
  54. if CLI_MODE:
  55. return True
  56. else:
  57. # Must not be imported until here because of import order issues
  58. # when running in CLI
  59. from kodiswift import xbmc
  60. xbmc_level = XBMCFilter.xbmc_levels.get(
  61. XBMCFilter.python_to_xbmc.get(record.levelname))
  62. xbmc.log('%s%s' % (self.prefix, record.getMessage()), xbmc_level)
  63. return False
  64. if CLI_MODE:
  65. GLOBAL_LOG_LEVEL = logging.INFO
  66. else:
  67. GLOBAL_LOG_LEVEL = logging.DEBUG
  68. def setup_log(name):
  69. """Returns a logging instance for the provided name. The returned
  70. object is an instance of logging.Logger. Logged messages will be
  71. printed to stderr when running in the CLI, or forwarded to Kodi's
  72. log when running in Kodi mode.
  73. """
  74. _log = logging.getLogger(name)
  75. _log.setLevel(GLOBAL_LOG_LEVEL)
  76. handler = logging.StreamHandler()
  77. formatter = logging.Formatter(
  78. '%(asctime)s - %(levelname)s - [%(name)s] %(message)s')
  79. handler.setFormatter(formatter)
  80. _log.addHandler(handler)
  81. _log.addFilter(XBMCFilter('[%s] ' % name))
  82. return _log
  83. # The kodiswift log
  84. # Plugin writers should use plugin.log instead.
  85. log = setup_log('kodiswift')