2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2018 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>.
|
|
|
|
|
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]]
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def enable_debug_output(handler: Optional[logging.Handler] = None):
|
|
|
|
if handler is None:
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
|
|
|
|
formatter = PrettyProtobufFormatter()
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
logger = logging.getLogger("trezorlib")
|
2018-05-09 16:09:31 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(handler)
|