Package epydoc :: Module log
[hide private]
[frames] | no frames]

Source Code for Module epydoc.log

  1  # epydoc -- Logging 
  2  # 
  3  # Copyright (C) 2005 Edward Loper 
  4  # Author: Edward Loper <edloper@loper.org> 
  5  # URL: <http://epydoc.sf.net> 
  6  # 
  7  # $Id: log.py 1704 2008-02-01 19:15:34Z edloper $ 
  8   
  9  """ 
 10  Functions used to report messages and progress updates to the user. 
 11  These functions are delegated to zero or more registered L{Logger} 
 12  objects, which are responsible for actually presenting the information 
 13  to the user.  Different interfaces are free to create and register 
 14  their own C{Logger}s, allowing them to present this information in the 
 15  manner that is best suited to each interface. 
 16   
 17  @note: I considered using the standard C{logging} package to provide 
 18  this functionality.  However, I found that it would be too difficult 
 19  to get that package to provide the behavior I want (esp. with respect 
 20  to progress displays; but also with respect to message blocks). 
 21   
 22  @group Message Severity Levels: DEBUG, INFO, WARNING, ERROR, FATAL 
 23  """ 
 24  __docformat__ = 'epytext en' 
 25   
 26  import sys, os 
 27   
 28  DEBUG = 10 
 29  INFO = 20 
 30  DOCSTRING_WARNING = 25 
 31  WARNING = 30 
 32  ERROR = 40 
 33  FATAL = 40 
 34   
 35  ###################################################################### 
 36  # Logger Base Class 
 37  ###################################################################### 
38 -class Logger:
39 """ 40 An abstract base class that defines the interface for X{loggers}, 41 which are used by epydoc to report information back to the user. 42 Loggers are responsible for tracking two types of information: 43 44 - Messages, such as warnings and errors. 45 - Progress on the current task. 46 47 This abstract class allows the command-line interface and the 48 graphical interface to each present this information to the user 49 in the way that's most natural for each interface. To set up a 50 logger, create a subclass of C{Logger} that overrides all methods, 51 and register it using L{register_logger}. 52 """ 53 #//////////////////////////////////////////////////////////// 54 # Messages 55 #//////////////////////////////////////////////////////////// 56
57 - def log(self, level, message):
58 """ 59 Display a message. 60 61 @param message: The message string to display. C{message} may 62 contain newlines, but does not need to end in a newline. 63 @param level: An integer value indicating the severity of the 64 message. 65 """
66
67 - def close(self):
68 """ 69 Perform any tasks needed to close this logger. This should 70 be safe to call multiple times. 71 """
72 73 #//////////////////////////////////////////////////////////// 74 # Message blocks 75 #//////////////////////////////////////////////////////////// 76
77 - def start_block(self, header):
78 """ 79 Start a new message block. Any calls to L{info()}, 80 L{warning()}, or L{error()} that occur between a call to 81 C{start_block} and a corresponding call to C{end_block} will 82 be grouped together, and displayed with a common header. 83 C{start_block} can be called multiple times (to form nested 84 blocks), but every call to C{start_block} I{must} be balanced 85 by a call to C{end_block}. 86 """
87
88 - def end_block(self):
89 """ 90 End a warning block. See L{start_block} for details. 91 """
92 93 #//////////////////////////////////////////////////////////// 94 # Progress bar 95 #//////////////////////////////////////////////////////////// 96
97 - def start_progress(self, header=None):
98 """ 99 Begin displaying progress for a new task. C{header} is a 100 description of the task for which progress is being reported. 101 Each call to C{start_progress} must be followed by a call to 102 C{end_progress} (with no intervening calls to 103 C{start_progress}). 104 """
105
106 - def end_progress(self):
107 """ 108 Finish off the display of progress for the current task. See 109 L{start_progress} for more information. 110 """
111
112 - def progress(self, percent, message=''):
113 """ 114 Update the progress display. 115 116 @param percent: A float from 0.0 to 1.0, indicating how much 117 progress has been made. 118 @param message: A message indicating the most recent action 119 that contributed towards that progress. 120 """
121
122 -class SimpleLogger(Logger):
123 - def __init__(self, threshold=WARNING):
124 self.threshold = threshold
125 - def log(self, level, message):
126 if level >= self.threshold: print message
127 128 ###################################################################### 129 # Logger Registry 130 ###################################################################### 131 132 _loggers = [] 133 """ 134 The list of registered logging functions. 135 """ 136
137 -def register_logger(logger):
138 """ 139 Register a logger. Each call to one of the logging functions 140 defined by this module will be delegated to each registered 141 logger. 142 """ 143 _loggers.append(logger)
144
145 -def remove_logger(logger, close_logger=True):
146 if close_logger: logger.close() 147 _loggers.remove(logger)
148 149 ###################################################################### 150 # Logging Functions 151 ###################################################################### 152 # The following methods all just delegate to the corresponding 153 # methods in the Logger class (above) for each registered logger. 154
155 -def fatal(*messages):
156 """Display the given fatal message.""" 157 message = ' '.join(['%s' % (m,) for m in messages]) 158 for logger in _loggers: logger.log(FATAL, message)
159
160 -def error(*messages):
161 """Display the given error message.""" 162 message = ' '.join(['%s' % (m,) for m in messages]) 163 for logger in _loggers: logger.log(ERROR, message)
164
165 -def warning(*messages):
166 """Display the given warning message.""" 167 message = ' '.join(['%s' % (m,) for m in messages]) 168 for logger in _loggers: logger.log(WARNING, message)
169
170 -def docstring_warning(*messages):
171 """Display the given docstring warning message.""" 172 message = ' '.join(['%s' % (m,) for m in messages]) 173 for logger in _loggers: logger.log(DOCSTRING_WARNING, message)
174
175 -def info(*messages):
176 """Display the given informational message.""" 177 message = ' '.join(['%s' % (m,) for m in messages]) 178 for logger in _loggers: logger.log(INFO, message)
179
180 -def debug(*messages):
181 """Display the given debugging message.""" 182 message = ' '.join(['%s' % (m,) for m in messages]) 183 for logger in _loggers: logger.log(DEBUG, message)
184
185 -def start_block(header):
186 for logger in _loggers: logger.start_block(header)
187 start_block.__doc__ = Logger.start_block.__doc__ 188
189 -def end_block():
190 for logger in _loggers: logger.end_block()
191 end_block.__doc__ = Logger.end_block.__doc__ 192
193 -def start_progress(header=None):
194 for logger in _loggers: logger.start_progress(header)
195 start_progress.__doc__ = Logger.start_progress.__doc__ 196
197 -def end_progress():
198 for logger in _loggers: logger.end_progress()
199 end_progress.__doc__ = Logger.end_progress.__doc__ 200
201 -def progress(percent, message=''):
202 for logger in _loggers: logger.progress(percent, '%s' % message)
203 progress.__doc__ = Logger.progress.__doc__ 204
205 -def close():
206 for logger in _loggers: logger.close()
207