logger.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """ This is a logging utility module for other modules of the ddns library
  16. package.
  17. """
  18. import isc.log
  19. # The logger for this package
  20. logger = isc.log.Logger('libddns')
  21. class ClientFormatter:
  22. """A utility class to convert a client address to string.
  23. This class is constructed with a Python standard socket address tuple.
  24. If it's 2-element tuple, it's assumed to be an IPv4 socket address
  25. and will be converted to the form of '<addr>:<port>'.
  26. If it's 4-element tuple, it's assumed to be an IPv6 socket address.
  27. and will be converted to the form of '[<addr>]:<por>'.
  28. This class is designed to delay the conversion until it's explicitly
  29. requested, so the conversion doesn't happen if the corresponding log
  30. message is suppressed because of its log level (which is often the case
  31. for debug messages).
  32. """
  33. def __init__(self, addr):
  34. self.__addr = addr
  35. def __str__(self):
  36. if len(self.__addr) == 2:
  37. return self.__addr[0] + ':' + str(self.__addr[1])
  38. elif len(self.__addr) == 4:
  39. return '[' + self.__addr[0] + ']:' + str(self.__addr[1])
  40. return None
  41. class ZoneFormatter:
  42. """A utility class to convert zone name and class to string.
  43. This class is constructed with a name of a zone (isc.dns.Name object)
  44. and its RR class (isc.dns.RRClass object). Its text conversion method
  45. (__str__) converts them into a string in the form of
  46. '<zone name>/<zone class>' where the trailing dot of the zone name
  47. is omitted.
  48. If the given zone name on construction is None, it's assumed to be
  49. the zone isn't identified but needs to be somehow logged. The conversion
  50. method returns a special string to indicate this case.
  51. This class is designed to delay the conversion until it's explicitly
  52. requested, so the conversion doesn't happen if the corresponding log
  53. message is suppressed because of its log level (which is often the case
  54. for debug messages).
  55. """
  56. def __init__(self, zname, zclass):
  57. self.__zname = zname
  58. self.__zclass = zclass
  59. def __str__(self):
  60. if self.__zname is None:
  61. return '(zone unknown/not determined)'
  62. return self.__zname.to_text(True) + '/' + self.__zclass.to_text()
  63. class RRsetFormatter:
  64. """A utility class to convert rrsets to a short descriptive string.
  65. This class is constructed with an rrset (isc.dns.RRset object).
  66. Its text conversion method (__str__) converts it into a string
  67. with only the name, class and type of the rrset.
  68. This is used in logging so that the RRset can be identified, without
  69. being completely printed, which would result in an unnecessary
  70. multi-line message.
  71. This class is designed to delay the conversion until it's explicitly
  72. requested, so the conversion doesn't happen if the corresponding log
  73. message is suppressed because of its log level.
  74. """
  75. def __init__(self, rrset):
  76. self.__rrset = rrset
  77. def __str__(self):
  78. return self.__rrset.get_name().to_text() + " " +\
  79. self.__rrset.get_class().to_text() + " " +\
  80. self.__rrset.get_type().to_text()