_io.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- test-case-name: twisted.logger.test.test_io -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. File-like object that logs.
  6. """
  7. import sys
  8. from ._levels import LogLevel
  9. class LoggingFile(object):
  10. """
  11. File-like object that turns C{write()} calls into logging events.
  12. Note that because event formats are C{unicode}, C{bytes} received via
  13. C{write()} are converted to C{unicode}, which is the opposite of what
  14. C{file} does.
  15. @ivar softspace: File-like L{'softspace' attribute <file.softspace>}; zero
  16. or one.
  17. @type softspace: L{int}
  18. """
  19. softspace = 0
  20. def __init__(self, logger, level=LogLevel.info, encoding=None):
  21. """
  22. @param logger: the logger to log through.
  23. @param level: the log level to emit events with.
  24. @param encoding: The encoding to expect when receiving bytes via
  25. C{write()}. If L{None}, use C{sys.getdefaultencoding()}.
  26. @type encoding: L{str}
  27. @param log: The logger to send events to.
  28. @type log: L{Logger}
  29. """
  30. self.level = level
  31. self.log = logger
  32. if encoding is None:
  33. self._encoding = sys.getdefaultencoding()
  34. else:
  35. self._encoding = encoding
  36. self._buffer = ""
  37. self._closed = False
  38. @property
  39. def closed(self):
  40. """
  41. Read-only property. Is the file closed?
  42. @return: true if closed, otherwise false.
  43. @rtype: L{bool}
  44. """
  45. return self._closed
  46. @property
  47. def encoding(self):
  48. """
  49. Read-only property. File encoding.
  50. @return: an encoding.
  51. @rtype: L{str}
  52. """
  53. return self._encoding
  54. @property
  55. def mode(self):
  56. """
  57. Read-only property. File mode.
  58. @return: "w"
  59. @rtype: L{str}
  60. """
  61. return "w"
  62. @property
  63. def newlines(self):
  64. """
  65. Read-only property. Types of newlines encountered.
  66. @return: L{None}
  67. @rtype: L{None}
  68. """
  69. return None
  70. @property
  71. def name(self):
  72. """
  73. The name of this file; a repr-style string giving information about its
  74. namespace.
  75. @return: A file name.
  76. @rtype: L{str}
  77. """
  78. return (
  79. "<{0} {1}#{2}>".format(
  80. self.__class__.__name__,
  81. self.log.namespace,
  82. self.level.name,
  83. )
  84. )
  85. def close(self):
  86. """
  87. Close this file so it can no longer be written to.
  88. """
  89. self._closed = True
  90. def flush(self):
  91. """
  92. No-op; this file does not buffer.
  93. """
  94. pass
  95. def fileno(self):
  96. """
  97. Returns an invalid file descriptor, since this is not backed by an FD.
  98. @return: C{-1}
  99. @rtype: L{int}
  100. """
  101. return -1
  102. def isatty(self):
  103. """
  104. A L{LoggingFile} is not a TTY.
  105. @return: C{False}
  106. @rtype: L{bool}
  107. """
  108. return False
  109. def write(self, string):
  110. """
  111. Log the given message.
  112. @param string: Data to write.
  113. @type string: L{bytes} in this file's preferred encoding or L{unicode}
  114. """
  115. if self._closed:
  116. raise ValueError("I/O operation on closed file")
  117. if isinstance(string, bytes):
  118. string = string.decode(self._encoding)
  119. lines = (self._buffer + string).split("\n")
  120. self._buffer = lines[-1]
  121. lines = lines[0:-1]
  122. for line in lines:
  123. self.log.emit(self.level, format=u"{log_io}", log_io=line)
  124. def writelines(self, lines):
  125. """
  126. Log each of the given lines as a separate message.
  127. @param lines: Data to write.
  128. @type lines: iterable of L{unicode} or L{bytes} in this file's
  129. declared encoding
  130. """
  131. for line in lines:
  132. self.write(line)
  133. def _unsupported(self, *args):
  134. """
  135. Template for unsupported operations.
  136. @param args: Arguments.
  137. @type args: tuple of L{object}
  138. """
  139. raise IOError("unsupported operation")
  140. read = _unsupported
  141. next = _unsupported
  142. readline = _unsupported
  143. readlines = _unsupported
  144. xreadlines = _unsupported
  145. seek = _unsupported
  146. tell = _unsupported
  147. truncate = _unsupported