Package qm :: Module trace
[hide private]
[frames] | no frames]

Source Code for Module qm.trace

  1  ######################################################################## 
  2  # 
  3  # File:   trace.py 
  4  # Author: Mark Mitchell 
  5  # Date:   01/25/2002 
  6  # 
  7  # Contents: 
  8  #   Tracer 
  9  # 
 10  # Copyright (c) 2002 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # Imports 
 18  ######################################################################## 
 19   
 20  import os 
 21  import sys 
 22   
 23  ######################################################################## 
 24  # Classes 
 25  ######################################################################## 
 26   
27 -class Tracer:
28 """A 'Tracer' outputs trace messages useful for debugging.""" 29 30 prefix = 'QM_THRESHOLD_' 31 """The prefix for an environment variable that indicates a 32 threshold for a particular trace category. The string following 33 the prefix gives the name of the trace category. If the 34 environment variable has no associated level, the associated level 35 is one. Otherwise, the associated level is given by the value of 36 the environment variable.""" 37
38 - def __init__(self, file=sys.stderr):
39 """Construct a new 'Tracer'. 40 41 'file' -- The file object to which output should be written.""" 42 43 self.__file = file 44 self.__thresholds = {} 45 46 # Take any environment variables that begin with QM_THRESHOLD_ 47 # as the initial values for thresholds. 48 keys = filter (lambda key: (key[:len(Tracer.prefix)] 49 == Tracer.prefix), 50 os.environ.keys()) 51 for key in keys: 52 level = os.environ[key] 53 if level: 54 level = int(level) 55 else: 56 level = 1 57 self.SetThreshold(key[len(Tracer.prefix):], level)
58 59
60 - def Write(self, message, category, level=0):
61 """Output a trace message. 62 63 'message' -- A string giving the contents of the message. The 64 message should begin with a capital letter and end with a 65 period. 66 67 'category' -- A string indicating the category to which this 68 message belongs. 69 70 'level' -- A non-negative integer indicating the level at 71 which the message should be output. 72 73 Every category has an associated threshold. If the level of 74 the 'message' is less than the threshold, the mesage will be 75 output.""" 76 77 if level < self.GetThreshold(category): 78 self.__file.write("[%s]: %s\n" % (category, message)) 79 self.__file.flush()
80 81
82 - def GetThreshold(self, category):
83 """Return the current threshold for 'category'. 84 85 'category' -- A string giving a trace category. 86 87 returns -- The threshold associated with 'category'. If no 88 threshold has been set, the threshold is considered to be 89 zero.""" 90 91 return self.__thresholds.get(category, 0)
92 93
94 - def SetThreshold(self, category, level):
95 """Set the threshold associated with 'category'. 96 97 'category' --A string giving a trace category. 98 99 'level' -- A non-negative integer indicating the threshold 100 level for 'category'.""" 101 102 self.__thresholds[category] = level
103