2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2018-06-21 14:28:34 +00:00
|
|
|
#
|
|
|
|
# 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>.
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
import logging
|
2019-02-18 19:43:08 +00:00
|
|
|
from typing import Optional, Set, Type
|
2018-04-23 10:58:30 +00:00
|
|
|
|
|
|
|
from . import protobuf
|
|
|
|
|
|
|
|
OMITTED_MESSAGES = set() # type: Set[Type[protobuf.MessageType]]
|
|
|
|
|
2019-09-26 13:41:42 +00:00
|
|
|
DUMP_BYTES = 5
|
2020-03-05 16:38:31 +00:00
|
|
|
DUMP_PACKETS = 4
|
2019-09-26 13:41:42 +00:00
|
|
|
|
|
|
|
logging.addLevelName(DUMP_BYTES, "BYTES")
|
2020-03-05 16:38:31 +00:00
|
|
|
logging.addLevelName(DUMP_PACKETS, "PACKETS")
|
2018-04-23 10:58:30 +00:00
|
|
|
|
2019-09-26 14:48:16 +00:00
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
class PrettyProtobufFormatter(logging.Formatter):
|
2018-05-09 16:09:31 +00:00
|
|
|
def format(self, record: logging.LogRecord) -> str:
|
2018-04-23 10:58:30 +00:00
|
|
|
time = self.formatTime(record)
|
2018-08-13 16:21:24 +00:00
|
|
|
message = "[{time}] {source} {level}: {msg}".format(
|
|
|
|
time=time,
|
|
|
|
level=record.levelname.upper(),
|
2018-05-09 16:09:31 +00:00
|
|
|
source=record.name,
|
2018-08-13 16:21:24 +00:00
|
|
|
msg=super().format(record),
|
|
|
|
)
|
|
|
|
if hasattr(record, "protobuf"):
|
2018-04-23 10:58:30 +00:00
|
|
|
if type(record.protobuf) in OMITTED_MESSAGES:
|
|
|
|
message += " ({} bytes)".format(record.protobuf.ByteSize())
|
|
|
|
else:
|
2018-05-28 13:01:17 +00:00
|
|
|
message += "\n" + protobuf.format_message(record.protobuf)
|
2018-04-23 10:58:30 +00:00
|
|
|
return message
|
2018-05-09 16:09:31 +00:00
|
|
|
|
|
|
|
|
2019-09-26 13:41:42 +00:00
|
|
|
def enable_debug_output(verbosity: int = 1, handler: Optional[logging.Handler] = None):
|
2018-05-09 16:09:31 +00:00
|
|
|
if handler is None:
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
|
|
|
|
formatter = PrettyProtobufFormatter()
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
2019-09-26 13:41:42 +00:00
|
|
|
if verbosity > 0:
|
|
|
|
level = logging.DEBUG
|
|
|
|
if verbosity > 1:
|
|
|
|
level = DUMP_BYTES
|
2020-03-05 16:38:31 +00:00
|
|
|
if verbosity > 2:
|
|
|
|
level = DUMP_PACKETS
|
2019-09-26 13:41:42 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
logger = logging.getLogger("trezorlib")
|
2019-09-26 13:41:42 +00:00
|
|
|
logger.setLevel(level)
|
2018-05-09 16:09:31 +00:00
|
|
|
logger.addHandler(handler)
|