|
| 1 | +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine |
| 2 | + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine |
| 3 | +
|
| 4 | + This module provides a framework for the use of DNS Service Discovery |
| 5 | + using IP multicast. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + USA |
| 21 | +""" |
| 22 | + |
| 23 | +import logging |
| 24 | +import sys |
| 25 | +from typing import Any, Dict, Union, cast |
| 26 | + |
| 27 | +log = logging.getLogger(__name__.split('.')[0]) |
| 28 | +log.addHandler(logging.NullHandler()) |
| 29 | + |
| 30 | +if log.level == logging.NOTSET: |
| 31 | + log.setLevel(logging.WARN) |
| 32 | + |
| 33 | + |
| 34 | +class QuietLogger: |
| 35 | + _seen_logs = {} # type: Dict[str, Union[int, tuple]] |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def log_exception_warning(cls, *logger_data: Any) -> None: |
| 39 | + exc_info = sys.exc_info() |
| 40 | + exc_str = str(exc_info[1]) |
| 41 | + if exc_str not in cls._seen_logs: |
| 42 | + # log at warning level the first time this is seen |
| 43 | + cls._seen_logs[exc_str] = exc_info |
| 44 | + logger = log.warning |
| 45 | + else: |
| 46 | + logger = log.debug |
| 47 | + logger(*(logger_data or ['Exception occurred']), exc_info=True) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def log_warning_once(cls, *args: Any) -> None: |
| 51 | + msg_str = args[0] |
| 52 | + if msg_str not in cls._seen_logs: |
| 53 | + cls._seen_logs[msg_str] = 0 |
| 54 | + logger = log.warning |
| 55 | + else: |
| 56 | + logger = log.debug |
| 57 | + cls._seen_logs[msg_str] = cast(int, cls._seen_logs[msg_str]) + 1 |
| 58 | + logger(*args) |
0 commit comments