"Fossies" - the Fresh Open Source Software Archive

Member "getmail-5.16/getmailcore/logging.py" (31 Oct 2021, 3589 Bytes) of package /linux/misc/getmail-5.16.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "logging.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.15_vs_5.16.

    1 #!/usr/bin/env python2
    2 '''Logging support for getmail.
    3 
    4 The new standard Python libary module logging didn't cut it for me; it doesn't
    5 seem capable of handling some very simple requirements like logging messages of
    6 a certain level to one fd, and other messages of higher levels to a different fd
    7 (i.e. info to stdout, warnings to stderr).
    8 '''
    9 
   10 __all__ = [
   11     'Logger',
   12 ]
   13 
   14 import sys
   15 import os.path
   16 import traceback
   17 
   18 from getmailcore.constants import *
   19 
   20 #######################################
   21 class _Logger(object):
   22     '''Class for logging.  Do not instantiate directly; use Logger() instead,
   23     to keep this a singleton.
   24     '''
   25     def __init__(self):
   26         '''Create a logger.'''
   27         self.handlers = []
   28         self.newline = False
   29 
   30     def __call__(self):
   31         return self
   32 
   33     def addhandler(self, stream, minlevel, maxlevel=CRITICAL):
   34         '''Add a handler for logged messages.
   35 
   36         Logged messages of at least level <minlevel> (and at most level
   37         <maxlevel>, default CRITICAL) will be output to <stream>.
   38 
   39         If no handlers are specified, messages of all levels will be output to
   40         stdout.
   41         '''
   42         self.handlers.append({'minlevel' : minlevel, 'stream' : stream,
   43                               'newline' : True, 'maxlevel' : maxlevel})
   44 
   45     def clearhandlers(self):
   46         '''Clear the list of handlers.
   47 
   48         There should be a way to remove only one handler from a list.  But that
   49         would require an easy way for the caller to distinguish between them.
   50         '''
   51         self.handlers = []
   52 
   53     def log(self, msglevel, msgtxt):
   54         '''Log a message of level <msglevel> containing text <msgtxt>.'''
   55         for handler in self.handlers:
   56             if msglevel < handler['minlevel'] or msglevel > handler['maxlevel']:
   57                 continue
   58             if not handler['newline'] and msglevel == DEBUG:
   59                 handler['stream'].write('\n')
   60             handler['stream'].write(msgtxt)
   61             handler['stream'].flush()
   62             if msgtxt.endswith('\n'):
   63                 handler['newline'] = True
   64             else:
   65                 handler['newline'] = False
   66         if not self.handlers:
   67             if not self.newline and msglevel == DEBUG:
   68                 sys.stdout.write('\n')
   69             sys.stdout.write(msgtxt)
   70             sys.stdout.flush()
   71             if msgtxt.endswith('\n'):
   72                 self.newline = True
   73             else:
   74                 self.newline = False
   75 
   76     def trace(self, msg='trace\n'):
   77         '''Log a message with level TRACE.
   78 
   79         The message will be prefixed with filename, line number, and function
   80         name of the calling code.
   81         '''
   82         trace = traceback.extract_stack()[-2]
   83         msg = '%s [%s:%i] %s' % (trace[FUNCNAME] + '()',
   84             os.path.basename(trace[FILENAME]),
   85             trace[LINENO],
   86             msg
   87         )
   88         self.log(TRACE, msg)
   89 
   90     def debug(self, msg):
   91         '''Log a message with level DEBUG.'''
   92         self.log(DEBUG, msg)
   93 
   94     def moreinfo(self, msg):
   95         '''Log a message with level MOREINFO.'''
   96         self.log(MOREINFO, msg)
   97 
   98     def info(self, msg):
   99         '''Log a message with level INFO.'''
  100         self.log(INFO, msg)
  101 
  102     def warning(self, msg):
  103         '''Log a message with level WARNING.'''
  104         self.log(WARNING, msg)
  105 
  106     def error(self, msg):
  107         '''Log a message with level ERROR.'''
  108         self.log(ERROR, msg)
  109 
  110     def critical(self, msg):
  111         '''Log a message with level CRITICAL.'''
  112         self.log(CRITICAL, msg)
  113 
  114     # aliases
  115     warn = warning
  116 
  117 Logger = _Logger()
  118