Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set log level #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions pyapns/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@
FEEDBACK_SERVER_HOSTNAME = "feedback.push.apple.com"
FEEDBACK_SERVER_PORT = 2196

_log_level = "INFO"


app_ids = {} # {'app_id': APNSService()}


def set_log_level(level):
global _log_level
_log_level = level


def log_msg(*args, **kwargs):
log_msg(*args, logLevel=_log_level, **kwargs)


class StringIO(_StringIO):
"""Add context management protocol to StringIO
ie: http://bugs.python.org/issue1286
Expand All @@ -51,9 +64,9 @@ def read(self):
class APNSClientContextFactory(ClientContextFactory):
def __init__(self, ssl_cert_file):
if 'BEGIN CERTIFICATE' not in ssl_cert_file:
log.msg('APNSClientContextFactory ssl_cert_file=%s' % ssl_cert_file)
log_msg('APNSClientContextFactory ssl_cert_file=%s' % ssl_cert_file)
else:
log.msg('APNSClientContextFactory ssl_cert_file={FROM_STRING}')
log_msg('APNSClientContextFactory ssl_cert_file={FROM_STRING}')
self.ctx = SSL.Context(SSL.TLSv1_METHOD)
if 'BEGIN CERTIFICATE' in ssl_cert_file:
cer = crypto.load_certificate(crypto.FILETYPE_PEM, ssl_cert_file)
Expand All @@ -70,34 +83,34 @@ def getContext(self):

class APNSProtocol(Protocol):
def connectionMade(self):
log.msg('APNSProtocol connectionMade')
log_msg('APNSProtocol connectionMade')
self.factory.addClient(self)

def sendMessage(self, msg):
log.msg('APNSProtocol sendMessage msg=%s' % binascii.hexlify(msg))
log_msg('APNSProtocol sendMessage msg=%s' % binascii.hexlify(msg))
return self.transport.write(msg)

def connectionLost(self, reason):
log.msg('APNSProtocol connectionLost')
log_msg('APNSProtocol connectionLost')
self.factory.removeClient(self)


class APNSFeedbackHandler(LineReceiver):
MAX_LENGTH = 1024*1024

def connectionMade(self):
log.msg('feedbackHandler connectionMade')
log_msg('feedbackHandler connectionMade')

def rawDataReceived(self, data):
log.msg('feedbackHandler rawDataReceived %s' % binascii.hexlify(data))
log_msg('feedbackHandler rawDataReceived %s' % binascii.hexlify(data))
self.io.write(data)

def lineReceived(self, data):
log.msg('feedbackHandler lineReceived %s' % binascii.hexlify(data))
log_msg('feedbackHandler lineReceived %s' % binascii.hexlify(data))
self.io.write(data)

def connectionLost(self, reason):
log.msg('feedbackHandler connectionLost %s' % reason)
log_msg('feedbackHandler connectionLost %s' % reason)
self.deferred.callback(self.io.getvalue())
self.io.close()

Expand All @@ -117,14 +130,14 @@ def buildProtocol(self, addr):
return p

def startedConnecting(self, connector):
log.msg('APNSFeedbackClientFactory startedConnecting')
log_msg('APNSFeedbackClientFactory startedConnecting')

def clientConnectionLost(self, connector, reason):
log.msg('APNSFeedbackClientFactory clientConnectionLost reason=%s' % reason)
log_msg('APNSFeedbackClientFactory clientConnectionLost reason=%s' % reason)
ClientFactory.clientConnectionLost(self, connector, reason)

def clientConnectionFailed(self, connector, reason):
log.msg('APNSFeedbackClientFactory clientConnectionFailed reason=%s' % reason)
log_msg('APNSFeedbackClientFactory clientConnectionFailed reason=%s' % reason)
ClientFactory.clientConnectionLost(self, connector, reason)


Expand All @@ -146,7 +159,7 @@ def removeClient(self, p):
self.deferred.addErrback(log_errback('APNSClientFactory removeClient'))

def startedConnecting(self, connector):
log.msg('APNSClientFactory startedConnecting')
log_msg('APNSClientFactory startedConnecting')

def buildProtocol(self, addr):
self.resetDelay()
Expand All @@ -155,11 +168,11 @@ def buildProtocol(self, addr):
return p

def clientConnectionLost(self, connector, reason):
log.msg('APNSClientFactory clientConnectionLost reason=%s' % reason)
log_msg('APNSClientFactory clientConnectionLost reason=%s' % reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

def clientConnectionFailed(self, connector, reason):
log.msg('APNSClientFactory clientConnectionFailed reason=%s' % reason)
log_msg('APNSClientFactory clientConnectionFailed reason=%s' % reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)


Expand All @@ -173,7 +186,7 @@ class APNSService(service.Service):
feedbackProtocolFactory = APNSFeedbackClientFactory

def __init__(self, cert_path, environment, timeout=15):
log.msg('APNSService __init__')
log_msg('APNSService __init__')
self.factory = None
self.environment = environment
self.cert_path = cert_path
Expand All @@ -186,7 +199,7 @@ def getContextFactory(self):
def write(self, notifications):
"Connect to the APNS service and send notifications"
if not self.factory:
log.msg('APNSService write (connecting)')
log_msg('APNSService write (connecting)')
server, port = ((APNS_SERVER_SANDBOX_HOSTNAME
if self.environment == 'sandbox'
else APNS_SERVER_HOSTNAME), APNS_SERVER_PORT)
Expand Down Expand Up @@ -214,7 +227,7 @@ def cancel_timeout(r):

def read(self):
"Connect to the feedback service and read all data."
log.msg('APNSService read (connecting)')
log_msg('APNSService read (connecting)')
try:
server, port = ((FEEDBACK_SERVER_SANDBOX_HOSTNAME
if self.environment == 'sandbox'
Expand Down Expand Up @@ -270,7 +283,7 @@ def xmlrpc_provision(self, app_id, path_to_cert_or_cert, environment, timeout=15
'environments are `sandbox` and `production`' % (
environment,))
if not app_id in self.app_ids:
# log.msg('provisioning ' + app_id + ' environment ' + environment)
# log_msg('provisioning ' + app_id + ' environment ' + environment)
self.app_ids[app_id] = APNSService(path_to_cert_or_cert, environment, timeout)

def xmlrpc_notify(self, app_id, token_or_token_list, aps_dict_or_list):
Expand Down