Skip to content

Commit

Permalink
Merge pull request #1094 from tahoe-lafs/3708.bytes-logging
Browse files Browse the repository at this point in the history
Remove bytes from Foolscap logging

Fixes ticket:3708
  • Loading branch information
itamarst committed Jul 9, 2021
2 parents 9df792c + af49dcb commit 8e28a9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Empty file added newsfragments/3708.minor
Empty file.
12 changes: 6 additions & 6 deletions src/allmydata/util/jsonbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def backslashreplace_py2(ex):
codecs.register_error("backslashreplace_tahoe_py2", backslashreplace_py2)


def _bytes_to_unicode(any_bytes, obj):
"""Create a function that recursively converts bytes to unicode.
def bytes_to_unicode(any_bytes, obj):
"""Convert bytes to unicode.
:param any_bytes: If True, also support non-UTF-8-encoded bytes.
:param obj: Object to de-byte-ify.
Expand Down Expand Up @@ -63,11 +63,11 @@ class UTF8BytesJSONEncoder(json.JSONEncoder):
"""
def encode(self, o, **kwargs):
return json.JSONEncoder.encode(
self, _bytes_to_unicode(False, o), **kwargs)
self, bytes_to_unicode(False, o), **kwargs)

def iterencode(self, o, **kwargs):
return json.JSONEncoder.iterencode(
self, _bytes_to_unicode(False, o), **kwargs)
self, bytes_to_unicode(False, o), **kwargs)


class AnyBytesJSONEncoder(json.JSONEncoder):
Expand All @@ -79,11 +79,11 @@ class AnyBytesJSONEncoder(json.JSONEncoder):
"""
def encode(self, o, **kwargs):
return json.JSONEncoder.encode(
self, _bytes_to_unicode(True, o), **kwargs)
self, bytes_to_unicode(True, o), **kwargs)

def iterencode(self, o, **kwargs):
return json.JSONEncoder.iterencode(
self, _bytes_to_unicode(True, o), **kwargs)
self, bytes_to_unicode(True, o), **kwargs)


def dumps(obj, *args, **kwargs):
Expand Down
18 changes: 15 additions & 3 deletions src/allmydata/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
from foolscap.logging import log
from twisted.python import log as tw_log

if PY2:
def bytes_to_unicode(ign, obj):
return obj
else:
# We want to convert bytes keys to Unicode, otherwise JSON serialization
# inside foolscap will fail (for details see
# https://github.com/warner/foolscap/issues/88)
from .jsonbytes import bytes_to_unicode


NOISY = log.NOISY # 10
OPERATIONAL = log.OPERATIONAL # 20
UNUSUAL = log.UNUSUAL # 23
Expand All @@ -28,7 +38,8 @@
BAD = log.BAD # 40


msg = log.msg
def msg(*args, **kwargs):
return log.msg(*args, **bytes_to_unicode(True, kwargs))

# If log.err() happens during a unit test, the unit test should fail. We
# accomplish this by sending it to twisted.log too. When a WEIRD/SCARY/BAD
Expand All @@ -39,7 +50,7 @@ def err(failure=None, _why=None, **kwargs):
tw_log.err(failure, _why, **kwargs)
if 'level' not in kwargs:
kwargs['level'] = log.UNUSUAL
return log.err(failure, _why, **kwargs)
return log.err(failure, _why, **bytes_to_unicode(True, kwargs))

class LogMixin(object):
""" I remember a msg id and a facility and pass them to log.msg() """
Expand All @@ -57,7 +68,8 @@ def log(self, msg, facility=None, parent=None, *args, **kwargs):
if pmsgid is None:
pmsgid = self._grandparentmsgid
kwargs = {ensure_str(k): v for (k, v) in kwargs.items()}
msgid = log.msg(msg, facility=facility, parent=pmsgid, *args, **kwargs)
msgid = log.msg(msg, facility=facility, parent=pmsgid, *args,
**bytes_to_unicode(True, kwargs))
if self._parentmsgid is None:
self._parentmsgid = msgid
return msgid
Expand Down

0 comments on commit 8e28a9d

Please sign in to comment.