Skip to content

Commit

Permalink
more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Sep 3, 2021
1 parent a117a6d commit 7bc4a41
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/h2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Objects for controlling the configuration of the HTTP/2 stack.
"""

import sys


class _BooleanConfigOption:
"""
Expand All @@ -27,7 +29,7 @@ def __set__(self, instance, value):

class DummyLogger:
"""
An Logger object that does not actual logging, hence a DummyLogger.
A Logger object that does not actual logging, hence a DummyLogger.
For the class the log operation is merely a no-op. The intent is to avoid
conditionals being sprinkled throughout the h2 code for calls to
Expand All @@ -49,6 +51,29 @@ def trace(self, *vargs, **kwargs):
pass


class OutputLogger:
"""
A Logger object that prints to stderr or any other file-like object.
This class is provided for convinience and not part of the stable API.
:param file: A file-like object passed to the print function.
Defaults to ``sys.stderr``.
:param trace: Enables trace-level output. Defaults to ``False``.
"""
def __init__(self, file=sys.stderr, trace=False):
super().__init__()
self.file = file
self.trace = trace

def debug(self, fmtstr, *args):
print(f"h2 (debug): {fmtstr % args}", file=self.file)

def trace(self, fmtstr, *args):
if self.trace:
print(f"h2 (trace): {fmtstr % args}", file=self.file)


class H2Configuration:
"""
An object that controls the way a single HTTP/2 connection behaves.
Expand Down
1 change: 1 addition & 0 deletions src/h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ def _receive_frame(self, frame):
.. versionchanged:: 2.0.0
Removed from the public API.
"""
self.config.logger.trace("Received frame: %s", repr(frame))
try:
# I don't love using __class__ here, maybe reconsider it.
frames, events = self._frame_dispatch_table[frame.__class__](frame)
Expand Down
2 changes: 1 addition & 1 deletion src/h2/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def validate_headers(headers, hdr_validation_flags):
# checking remains somewhat expensive, and attempts should be made wherever
# possible to reduce the time spent doing them.
#
# For example, we avoid tuple upacking in loops because it represents a
# For example, we avoid tuple unpacking in loops because it represents a
# fixed cost that we don't want to spend, instead indexing into the header
# tuples.
headers = _reject_uppercase_header_fields(
Expand Down

0 comments on commit 7bc4a41

Please sign in to comment.