chiark / gitweb /
wip debug
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Apr 2017 02:35:18 +0000 (03:35 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 2 Apr 2017 02:35:18 +0000 (03:35 +0100)
hippotat/__init__.py

index c31bdb786d37aeda5a65d6d57a810074dcf7e008..0b059e59e2384d0b20cf388ec17887198e66c8ae 100644 (file)
@@ -46,6 +46,7 @@ class DBG(twisted.python.constants.Names):
   HTTP_FULL = NamedConstant()
   CTRL_DUMP = NamedConstant()
   SLIP_FULL = NamedConstant()
+  DATA_COMPLETE = NamedConstant()
 
 _hex_codec = codecs.getencoder('hex_codec')
 
@@ -64,9 +65,13 @@ def log_debug(dflag, msg, idof=None, d=None):
   if idof is not None:
     msg = '[%#x] %s' % (id(idof), msg)
   if d is not None:
-    #d = d[0:64]
+    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
+    msg += ' ' + d + trunc
   log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg)
 
 @implementer(twisted.logger.ILogFilterPredicate)
@@ -74,15 +79,13 @@ class LogNotBoringTwisted:
   def __call__(self, event):
     yes = twisted.logger.PredicateResult.yes
     no  = twisted.logger.PredicateResult.no
-    return yes
     try:
       if event.get('log_level') != LogLevel.info:
         return yes
-      try:
-        dflag = event.get('dflag')
-      except KeyError:
-        dflag = DBG.TWISTED
-      return yes if (dflag in debug_set) else no
+      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
@@ -402,36 +405,59 @@ def common_startup():
   optparser.add_option('-c', '--config', dest='configfile',
                        default='/etc/hippotat/config')
 
-  def ds_by_detail(od,os,detail_level,op):
+  def dfs_less_detailed(dl):
+    return [df for df in DBG.iterconstants() if df <= dl]
+
+  def ds_default(od,os,dl,op):
     global debug_set
-    debug_set = set([df for df in DBG.iterconstants() if df <= detail_level])
+    debug_set = set(dfs_less_detailed(debug_def_detail))
 
-  def ds_one(mutator,df, od,os,value,op):
-    mutator(df)
+  def ds_select(od,os, spec, op):
+    last_df = next(DBG.iterconstants())
+    mutator = debug_set.add
 
-  optparser.add_option('-D', '--debug',
-                       default=debug_def_detail.name,
-                       type='choice',
-                       choices=[dl.name for dl in DBG.iterconstants()],
-                       action='callback',
-                       callback= ds_by_detail)
+    for it in spec.split(','):
+
+      if not len(it):
+        for df in dfs_less_detailed(last_df):
+          mutator(df)
+        continue
 
-  optparser.add_option('--no-debug',
+      if it.startswith('-'):
+        mutator = debug_set.discard
+        it = it[1:]
+      else:
+        mutator = debug_set.add
+        
+      try:
+        df = DBG.lookupByName(it)
+      except ValueError:
+        optparser.error('unknown debug flag %s in --debug-select' % it)
+      mutator(df)
+      last_df = df
+
+  optparser.add_option('-D', '--debug',
                        nargs=0,
                        action='callback',
-                       callback= partial(ds_by_detail,DBG.INIT))
-
-  for df in DBG.iterconstants():
-    optparser.add_option('--debug-'+df.name,
-                         action='callback',
-                         callback= partial(ds_one, debug_set.add, df))
-    optparser.add_option('--no-debug-'+df.name,
-                         action='callback',
-                         callback= partial(ds_one, debug_set.discard, df))
+                       help='enable default debug (to stdout)',
+                       callback= ds_default)
+
+  optparser.add_option('--debug-select',
+                       nargs=1,
+                       type='string',
+                       metavar='[-]DFLAG[,],...',
+                       help=
+'''enable (or with -, disable) each specified DFLAG;
+empty entry means do the same for all DFLAGS "more interesting"
+than the last (or, all DFLAGs)''',
+                       action='callback',
+                       callback= ds_select)
 
   (opts, args) = optparser.parse_args()
   if len(args): optparser.error('no non-option arguments please')
 
+  print(repr(debug_set), file=sys.stderr)
+
   re = regexp.compile('#.*')
   cfg.read_string(re.sub('', defcfg))
   cfg.read(opts.configfile)