Skip to content

Commit

Permalink
core: use $DEBUG variable to enable debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
geertj committed Jun 13, 2017
1 parent dee5835 commit 0701121
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/gruvi/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
from .ssl import SslTransport
from .sslcompat import create_default_context
from .address import getaddrinfo, saddr
from .util import EnvBool

__all__ = ['create_connection', 'create_server', 'Endpoint', 'Client', 'Server']

DEBUG = EnvBool.new('DEBUG')


@switchpoint
def create_connection(protocol_factory, address, ssl=False, server_hostname=None,
Expand Down Expand Up @@ -340,7 +343,7 @@ def handle_connection(self, client, ssl):
transport = Transport(client)
transport._log = self._log
transport._server = self
if __debug__:
if DEBUG:
self._log.debug('new connection on {}', saddr(client.getsockname()))
if hasattr(client, 'getpeername'):
self._log.debug('remote peer is {}', saddr(client.getpeername()))
Expand Down
24 changes: 24 additions & 0 deletions lib/gruvi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import absolute_import, print_function

import os
import sys
import re
import functools
Expand All @@ -17,6 +18,29 @@
__all__ = []


class EnvBool(object):
"""A boolean-like object that takes its value from the environment variable
*name*."""

_zero_values = ('0', 'n', 'f', 'false')

def __init__(self, name):
self._name = name

@classmethod
def new(cls, name):
# Optimize away $DEBUG when interpreter is running with -O
if name == 'DEBUG' and not __debug__:
return False
return cls(name)

def __nonzero__(self):
value = os.environ.get(self._name, '0')
return value.lower() not in self._zero_values

__bool__ = __nonzero__


class AbsentType(object):
"""A type that represents the absence of a value.
Expand Down

0 comments on commit 0701121

Please sign in to comment.