PHP 8.5.2
Preview: _buffer.py Size: 1.49 KB
/lib/python3/dist-packages/twisted/logger/_buffer.py

# -*- test-case-name: twisted.logger.test.test_buffer -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Log observer that maintains a buffer.
"""

from collections import deque
from typing import Deque, Optional

from zope.interface import implementer

from ._interfaces import ILogObserver, LogEvent

_DEFAULT_BUFFER_MAXIMUM = 64 * 1024


@implementer(ILogObserver)
class LimitedHistoryLogObserver:
    """
    L{ILogObserver} that stores events in a buffer of a fixed size::

        >>> from twisted.logger import LimitedHistoryLogObserver
        >>> history = LimitedHistoryLogObserver(5)
        >>> for n in range(10): history({'n': n})
        ...
        >>> repeats = []
        >>> history.replayTo(repeats.append)
        >>> len(repeats)
        5
        >>> repeats
        [{'n': 5}, {'n': 6}, {'n': 7}, {'n': 8}, {'n': 9}]
        >>>
    """

    def __init__(self, size: Optional[int] = _DEFAULT_BUFFER_MAXIMUM) -> None:
        """
        @param size: The maximum number of events to buffer.  If L{None}, the
            buffer is unbounded.
        """
        self._buffer: Deque[LogEvent] = deque(maxlen=size)

    def __call__(self, event: LogEvent) -> None:
        self._buffer.append(event)

    def replayTo(self, otherObserver: ILogObserver) -> None:
        """
        Re-play the buffered events to another log observer.

        @param otherObserver: An observer to replay events to.
        """
        for event in self._buffer:
            otherObserver(event)

Directory Contents

Dirs: 2 × Files: 17

Name Size Perms Modified Actions
test DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
- drwxr-xr-x 2026-01-08 12:56:23
Edit Download
1.49 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
624 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.28 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.71 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.16 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.29 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.44 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.21 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.12 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.75 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.17 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.29 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).