Move all loggers to be members of the class, not global objects

This commit is contained in:
Pim van Pelt
2021-09-12 16:08:35 +00:00
parent 9265e211e3
commit 7206d92f40
5 changed files with 57 additions and 79 deletions

View File

@ -7,24 +7,16 @@ from __future__ import (
)
# --------------------------------------------
import struct
import pprint
import logging
import agentx
class NullHandler(logging.Handler):
def emit(self, record):
pass
logger = logging.getLogger('agentx.pdu')
logger.addHandler(NullHandler())
# --------------------------------------------
import struct
import pprint
import agentx
class PDU(object):
def __init__(self, type=0):
self.type = type
@ -36,23 +28,25 @@ class PDU(object):
self.decode_buf = ''
self.state = {}
self.values = []
self.logger = logging.getLogger('agentx.pdu')
self.logger.addHandler(NullHandler())
def dump(self):
name = agentx.PDU_TYPE_NAME[self.type]
logger.debug('PDU DUMP: New PDU')
logger.debug(
self.logger.debug('PDU DUMP: New PDU')
self.logger.debug(
'PDU DUMP: Meta : [%s: %d %d %d]' %
(name, self.session_id, self.transaction_id, self.packet_id))
if 'payload_length' in self.state:
logger.debug('PDU DUMP: Length : %s' %
self.logger.debug('PDU DUMP: Length : %s' %
self.state['payload_length'])
if hasattr(self, 'response'):
logger.debug('PDU DUMP: Response : %s' % self.response)
self.logger.debug('PDU DUMP: Response : %s' % self.response)
if hasattr(self, 'values'):
logger.debug('PDU DUMP: Values : %s' %
self.logger.debug('PDU DUMP: Values : %s' %
pprint.pformat(self.values))
if hasattr(self, 'range_list'):
logger.debug('PDU DUMP: Range list: %s' %
self.logger.debug('PDU DUMP: Range list: %s' %
pprint.pformat(self.range_list))
# ====================================================
@ -108,7 +102,7 @@ class PDU(object):
# No data
pass
else:
logger.error('Unknown Type:' % type)
self.logger.error('Unknown Type:' % type)
return buf
def encode_header(self, pdu_type, payload_length=0, flags=0):
@ -181,8 +175,8 @@ class PDU(object):
oid = '.'.join(str(i) for i in sub_ids)
return oid, ret['include']
except Exception as e:
logger.exception('Invalid packing OID header')
logger.debug('%s' % pprint.pformat(self.decode_buf))
self.logger.exception('Invalid packing OID header')
self.logger.debug('%s' % pprint.pformat(self.decode_buf))
def decode_search_range(self):
start_oid, include = self.decode_oid()
@ -207,14 +201,14 @@ class PDU(object):
self.decode_buf = self.decode_buf[l + padding:]
return buf
except Exception as e:
logger.exception('Invalid packing octet header')
self.logger.exception('Invalid packing octet header')
def decode_value(self):
try:
vtype, _ = struct.unpack('!HH', self.decode_buf[:4])
self.decode_buf = self.decode_buf[4:]
except Exception as e:
logger.exception('Invalid packing value header')
self.logger.exception('Invalid packing value header')
oid, _ = self.decode_oid()
if vtype in [
agentx.TYPE_INTEGER, agentx.TYPE_COUNTER32,
@ -241,7 +235,7 @@ class PDU(object):
# No data
data = None
else:
logger.error('Unknown Type: %s' % vtype)
self.logger.error('Unknown Type: %s' % vtype)
return {'type': vtype, 'name': oid, 'data': data}
def decode_header(self):
@ -267,11 +261,11 @@ class PDU(object):
self.decode_buf = self.decode_buf[:ret['payload_length']]
if ret['flags'] & 0x08: # content present
context = self.decode_octet()
logger.debug('Context: %s' % context)
self.logger.debug('Context: %s' % context)
return ret
except Exception as e:
logger.exception('Invalid packing: %d' % len(self.decode_buf))
logger.debug('%s' % pprint.pformat(self.decode_buf))
self.logger.exception('Invalid packing: %d' % len(self.decode_buf))
self.logger.debug('%s' % pprint.pformat(self.decode_buf))
def decode(self, buf):
self.set_decode_buf(buf)
@ -310,4 +304,4 @@ class PDU(object):
else:
pdu_type_str = agentx.PDU_TYPE_NAME.get(
ret['pdu_type'], 'Unknown:' + str(ret['pdu_type']))
logger.error('Unsupported PDU type:' + pdu_type_str)
self.logger.error('Unsupported PDU type:' + pdu_type_str)