Created a logging configuration that supports various log levels, multiple output streams, and is inherently thread-safe. Changed some print calls to logger calls in bitmessagemain
This commit is contained in:
parent
414c781cfc
commit
f91a3c78c3
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
||||||
**pyc
|
**pyc
|
||||||
**dat
|
**dat
|
||||||
**.DS_Store
|
**.DS_Store
|
||||||
|
*.log
|
||||||
src/build
|
src/build
|
||||||
src/dist
|
src/dist
|
||||||
|
|
|
@ -47,6 +47,8 @@ import json
|
||||||
from subprocess import call # used when the API must execute an outside program
|
from subprocess import call # used when the API must execute an outside program
|
||||||
import singleton
|
import singleton
|
||||||
import proofofwork
|
import proofofwork
|
||||||
|
# Logging
|
||||||
|
from debug import logger
|
||||||
|
|
||||||
# For each stream to which we connect, several outgoingSynSender threads
|
# For each stream to which we connect, several outgoingSynSender threads
|
||||||
# will exist and will collectively create 8 connections with peers.
|
# will exist and will collectively create 8 connections with peers.
|
||||||
|
@ -95,9 +97,10 @@ class outgoingSynSender(threading.Thread):
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
sock.settimeout(20)
|
sock.settimeout(20)
|
||||||
if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and verbose >= 2:
|
if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and verbose >= 2:
|
||||||
shared.printLock.acquire()
|
#shared.printLock.acquire()
|
||||||
print 'Trying an outgoing connection to', HOST, ':', PORT
|
#print 'Trying an outgoing connection to', HOST, ':', PORT
|
||||||
shared.printLock.release()
|
logger.info('Trying an outgoing connection to %s:%s' % (HOST, PORT))
|
||||||
|
#shared.printLock.release()
|
||||||
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a':
|
elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a':
|
||||||
if verbose >= 2:
|
if verbose >= 2:
|
||||||
|
@ -672,13 +675,9 @@ class receiveDataThread(threading.Thread):
|
||||||
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \
|
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \
|
||||||
(time.time() - self.messageProcessingStartTime)
|
(time.time() - self.messageProcessingStartTime)
|
||||||
if sleepTime > 0:
|
if sleepTime > 0:
|
||||||
shared.printLock.acquire()
|
logger.info('Timing attack mitigation: Sleeping for %s seconds' % str(sleepTime))
|
||||||
print 'Timing attack mitigation: Sleeping for', sleepTime, 'seconds.'
|
|
||||||
shared.printLock.release()
|
|
||||||
time.sleep(sleepTime)
|
time.sleep(sleepTime)
|
||||||
shared.printLock.acquire()
|
logger.info('Total message processing time: %ss' % str(time.time() - self.messageProcessingStartTime))
|
||||||
print 'Total message processing time:', time.time() - self.messageProcessingStartTime, 'seconds.'
|
|
||||||
shared.printLock.release()
|
|
||||||
|
|
||||||
# A broadcast message has a valid time and POW and requires processing.
|
# A broadcast message has a valid time and POW and requires processing.
|
||||||
# The recbroadcast function calls this one.
|
# The recbroadcast function calls this one.
|
||||||
|
@ -715,9 +714,8 @@ class receiveDataThread(threading.Thread):
|
||||||
sendersHash = data[readPosition:readPosition + 20]
|
sendersHash = data[readPosition:readPosition + 20]
|
||||||
if sendersHash not in shared.broadcastSendersForWhichImWatching:
|
if sendersHash not in shared.broadcastSendersForWhichImWatching:
|
||||||
# Display timing data
|
# Display timing data
|
||||||
shared.printLock.acquire()
|
logger.info('Time spent deciding that we are not interested in this v1 broadcast: %s' %
|
||||||
print 'Time spent deciding that we are not interested in this v1 broadcast:', time.time() - self.messageProcessingStartTime
|
str(time.time() - self.messageProcessingStartTime))
|
||||||
shared.printLock.release()
|
|
||||||
return
|
return
|
||||||
# At this point, this message claims to be from sendersHash and
|
# At this point, this message claims to be from sendersHash and
|
||||||
# we are interested in it. We still have to hash the public key
|
# we are interested in it. We still have to hash the public key
|
||||||
|
@ -749,11 +747,11 @@ class receiveDataThread(threading.Thread):
|
||||||
signature = data[readPosition:readPosition + signatureLength]
|
signature = data[readPosition:readPosition + signatureLength]
|
||||||
try:
|
try:
|
||||||
if not highlevelcrypto.verify(data[12:readPositionAtBottomOfMessage], signature, sendersPubSigningKey.encode('hex')):
|
if not highlevelcrypto.verify(data[12:readPositionAtBottomOfMessage], signature, sendersPubSigningKey.encode('hex')):
|
||||||
print 'ECDSA verify failed'
|
logger.info('ECDSA verify failed')
|
||||||
return
|
return
|
||||||
print 'ECDSA verify passed'
|
logger.info('ECDSA verify passed')
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print 'ECDSA verify failed', err
|
logger.debug('ECDSA verify failed: %s' % str(err))
|
||||||
return
|
return
|
||||||
# verify passed
|
# verify passed
|
||||||
|
|
||||||
|
@ -1059,13 +1057,9 @@ class receiveDataThread(threading.Thread):
|
||||||
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \
|
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \
|
||||||
(time.time() - self.messageProcessingStartTime)
|
(time.time() - self.messageProcessingStartTime)
|
||||||
if sleepTime > 0:
|
if sleepTime > 0:
|
||||||
shared.printLock.acquire()
|
logger.info('Timing attack mitigation: Sleeping for %s seconds' % str(sleepTime))
|
||||||
print 'Timing attack mitigation: Sleeping for', sleepTime, 'seconds.'
|
|
||||||
shared.printLock.release()
|
|
||||||
time.sleep(sleepTime)
|
time.sleep(sleepTime)
|
||||||
shared.printLock.acquire()
|
logger.info('Total message processing time: %ss' % str(time.time() - self.messageProcessingStartTime))
|
||||||
print 'Total message processing time:', time.time() - self.messageProcessingStartTime, 'seconds.'
|
|
||||||
shared.printLock.release()
|
|
||||||
|
|
||||||
# A msg message has a valid time and POW and requires processing. The
|
# A msg message has a valid time and POW and requires processing. The
|
||||||
# recmsg function calls this one.
|
# recmsg function calls this one.
|
||||||
|
@ -1089,10 +1083,8 @@ class receiveDataThread(threading.Thread):
|
||||||
strftime(shared.config.get('bitmessagesettings', 'timeformat'), localtime(int(time.time()))), 'utf-8')))))
|
strftime(shared.config.get('bitmessagesettings', 'timeformat'), localtime(int(time.time()))), 'utf-8')))))
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
shared.printLock.acquire()
|
logger.info('This was NOT on acknowledgement bound for me.')
|
||||||
print 'This was NOT an acknowledgement bound for me.'
|
# logger.info("Ack data for which I'm watching: %s" % ackdataForWhichImWatching)
|
||||||
# print 'ackdataForWhichImWatching', ackdataForWhichImWatching
|
|
||||||
shared.printLock.release()
|
|
||||||
|
|
||||||
# This is not an acknowledgement bound for me. See if it is a message
|
# This is not an acknowledgement bound for me. See if it is a message
|
||||||
# bound for me by trying to decrypt it with my private keys.
|
# bound for me by trying to decrypt it with my private keys.
|
||||||
|
@ -1109,9 +1101,8 @@ class receiveDataThread(threading.Thread):
|
||||||
# print 'cryptorObject.decrypt Exception:', err
|
# print 'cryptorObject.decrypt Exception:', err
|
||||||
if not initialDecryptionSuccessful:
|
if not initialDecryptionSuccessful:
|
||||||
# This is not a message bound for me.
|
# This is not a message bound for me.
|
||||||
shared.printLock.acquire()
|
logger.info('Length of time program spent failing to decrypt this message %s seconds' %
|
||||||
print 'Length of time program spent failing to decrypt this message:', time.time() - self.messageProcessingStartTime, 'seconds.'
|
str(time.time() - self.messageProcessingStartTime))
|
||||||
shared.printLock.release()
|
|
||||||
else:
|
else:
|
||||||
# This is a message bound for me.
|
# This is a message bound for me.
|
||||||
toAddress = shared.myAddressesByHash[
|
toAddress = shared.myAddressesByHash[
|
||||||
|
|
45
src/debug.py
Normal file
45
src/debug.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#!/usr/bin/env python2.7
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import logging.config
|
||||||
|
|
||||||
|
log_level = 'DEBUG' # Get from a config file at some point?
|
||||||
|
|
||||||
|
logging.config.dictConfig({
|
||||||
|
'version': 1,
|
||||||
|
'formatters': {
|
||||||
|
'default': {
|
||||||
|
'format': '%(asctime)s - %(levelname)s - %(message)s',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'handlers': {
|
||||||
|
'console': {
|
||||||
|
'class': 'logging.StreamHandler',
|
||||||
|
'formatter': 'default',
|
||||||
|
'level': log_level,
|
||||||
|
'stream': 'ext://sys.stdout'
|
||||||
|
},
|
||||||
|
'file': {
|
||||||
|
'class': 'logging.handlers.RotatingFileHandler',
|
||||||
|
'formatter': 'default',
|
||||||
|
'level': log_level,
|
||||||
|
'filename': 'bm.log',
|
||||||
|
'maxBytes': 1024,
|
||||||
|
'backupCount': 0,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'loggers': {
|
||||||
|
'console_only': {
|
||||||
|
'handlers': ['console']
|
||||||
|
},
|
||||||
|
'both': {
|
||||||
|
'handlers': ['console', 'file']
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'root': {
|
||||||
|
'level': log_level,
|
||||||
|
'handlers': ['console']
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
logger = logging.getLogger('console_only')
|
Reference in New Issue
Block a user