+class DBG(twisted.python.constants.Names):
+ INIT = NamedConstant()
+ ROUTE = NamedConstant()
+ DROP = NamedConstant()
+ FLOW = NamedConstant()
+ HTTP = NamedConstant()
+ TWISTED = NamedConstant()
+ QUEUE = NamedConstant()
+ HTTP_CTRL = NamedConstant()
+ QUEUE_CTRL = NamedConstant()
+ HTTP_FULL = NamedConstant()
+ CTRL_DUMP = NamedConstant()
+ SLIP_FULL = NamedConstant()
+ DATA_COMPLETE = NamedConstant()
+
+_hex_codec = codecs.getencoder('hex_codec')
+
+#---------- logging ----------
+
+org_stderr = sys.stderr
+
+log = twisted.logger.Logger()
+
+debug_set = set()
+debug_def_detail = DBG.HTTP
+
+def log_debug(dflag, msg, idof=None, d=None):
+ if dflag not in debug_set: return
+ #print('---------------->',repr((dflag, msg, idof, d)), file=sys.stderr)
+ if idof is not None:
+ msg = '[%#x] %s' % (id(idof), msg)
+ if d is not None:
+ trunc = ''
+ if not DBG.DATA_COMPLETE in debug_set:
+ if len(d) > 64:
+ d = d[0:64]
+ trunc = '...'
+ d = _hex_codec(d)[0].decode('ascii')
+ msg += ' ' + d + trunc
+ log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg)
+
+@implementer(twisted.logger.ILogFilterPredicate)
+class LogNotBoringTwisted:
+ def __call__(self, event):
+ yes = twisted.logger.PredicateResult.yes
+ no = twisted.logger.PredicateResult.no
+ try:
+ if event.get('log_level') != LogLevel.info:
+ return yes
+ dflag = event.get('dflag')
+ if dflag in debug_set: return yes
+ if dflag is None and DBG.TWISTED in debug_set: return yes
+ return no
+ except Exception:
+ print(traceback.format_exc(), file=org_stderr)
+ return yes
+
+#---------- default config ----------
+