File: logs.py

package info (click to toggle)
python-socketio-client 0.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 212 kB
  • sloc: python: 1,250; javascript: 142; makefile: 8
file content (48 lines) | stat: -rw-r--r-- 1,276 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
import time

from .symmetries import NullHandler


LOG = logging.getLogger('socketIO-client')
LOG.addHandler(NullHandler())


class LoggingMixin(object):

    def _log(self, level, msg, *attrs):
        LOG.log(level, '%s %s' % (self._log_name, msg), *attrs)

    def _debug(self, msg, *attrs):
        self._log(logging.DEBUG, msg, *attrs)

    def _info(self, msg, *attrs):
        self._log(logging.INFO, msg, *attrs)

    def _warn(self, msg, *attrs):
        self._log(logging.WARNING, msg, *attrs)

    def _yield_warning_screen(self, seconds=None):
        last_warning = None
        for elapsed_time in _yield_elapsed_time(seconds):
            try:
                yield elapsed_time
            except Exception as warning:
                warning = str(warning)
                if last_warning != warning:
                    last_warning = warning
                    self._warn(warning)
                time.sleep(1)


def _yield_elapsed_time(seconds=None):
    start_time = time.time()
    if seconds is None:
        while True:
            yield _get_elapsed_time(start_time)
    while _get_elapsed_time(start_time) < seconds:
        yield _get_elapsed_time(start_time)


def _get_elapsed_time(start_time):
    return time.time() - start_time