mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-18 13:38:12 +00:00
9a330f3475
Protobuf encoding now happens in TrezorClient, and transports get encoded blobs to (chunkify and) send. This is a better design because transports don't need to know about protobuf. It also lays groundwork for sending raw bytes feature (#116) This commit also removes all vestiges of ProtocolV2 which was never used and will probably need to be redesigned from the ground up anyway. The code is still ready for protocol flexibility.
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
# This file is part of the Trezor project.
|
|
#
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
#
|
|
# This library is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
# as published by the Free Software Foundation.
|
|
#
|
|
# This library is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Lesser General Public License for more details.
|
|
#
|
|
# You should have received a copy of the License along with this library.
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
import logging
|
|
from typing import Optional, Set, Type
|
|
|
|
from . import protobuf
|
|
|
|
OMITTED_MESSAGES = set() # type: Set[Type[protobuf.MessageType]]
|
|
|
|
DUMP_BYTES = 5
|
|
DUMP_PACKETS = 4
|
|
|
|
logging.addLevelName(DUMP_BYTES, "BYTES")
|
|
logging.addLevelName(DUMP_PACKETS, "PACKETS")
|
|
|
|
|
|
class PrettyProtobufFormatter(logging.Formatter):
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
time = self.formatTime(record)
|
|
message = "[{time}] {source} {level}: {msg}".format(
|
|
time=time,
|
|
level=record.levelname.upper(),
|
|
source=record.name,
|
|
msg=super().format(record),
|
|
)
|
|
if hasattr(record, "protobuf"):
|
|
if type(record.protobuf) in OMITTED_MESSAGES:
|
|
message += " ({} bytes)".format(record.protobuf.ByteSize())
|
|
else:
|
|
message += "\n" + protobuf.format_message(record.protobuf)
|
|
return message
|
|
|
|
|
|
def enable_debug_output(verbosity: int = 1, handler: Optional[logging.Handler] = None):
|
|
if handler is None:
|
|
handler = logging.StreamHandler()
|
|
|
|
formatter = PrettyProtobufFormatter()
|
|
handler.setFormatter(formatter)
|
|
|
|
if verbosity > 0:
|
|
level = logging.DEBUG
|
|
if verbosity > 1:
|
|
level = DUMP_BYTES
|
|
if verbosity > 2:
|
|
level = DUMP_PACKETS
|
|
|
|
logger = logging.getLogger("trezorlib")
|
|
logger.setLevel(level)
|
|
logger.addHandler(handler)
|