;ò
µAc           @   s%  d  Z  d k Z d k Z d Z d Z d Z d Z d f  d „  ƒ  YZ d f  d	 „  ƒ  YZ e	 d
 j o¾d Z
 d e
 GHe e
 d d ƒZ d GHx( e d d ƒ D] Z e e d e ƒ q™ Wd GHd k Z d „  Z e e _ d GHx( e d d ƒ D] Z e e d e ƒ qé Wd GHd GHe e
 d d d ƒZ e e e g d d ƒZ e i e _ d GHx( e d d ƒ D] Z e e d e ƒ q]Wd GHd Z d GHe e ƒ d  GHe e d! ƒ d" GHe e d# ƒ d$ GHe d! ƒ d% GHe i e d! ƒ d& GHe d' h  d( d) <ƒ d* GHe e _ e d+ ƒ e i d+ ƒ n d S(,   s  
A module for convenient yet powerful file-object logging

BASIC USAGE

  from logger import Logger

  log = Logger(threshold=0)    # create the log object and give it
                               # a threshold of 0
  log.log(2, 'all done')       # send a log of priority 2 (not printed)
  log(0, 'error: bandits!')    # send a log of priority 0 (printed)
  log.write(0, stringvar)      # do a raw write on the file object

DESCRIPTION

  Each logging object is given a threshold.  Any messages that are
  then sent to that object are logged only if their priority meets or
  exceeds the threshold.  Lower numerical priority means that a
  message is more important.  For example: if a log object has
  threshold 2, then all messages of priority 2, 1, 0, -1, etc will be
  logged, while those of priority 3, 4, etc. will not.  I suggest the
  following scale:
  
     LOG PRIORITY    MEANING
              -1     failure - cannot be ignored
               0     important message - printed in default mode
               1     informational message - printed with -v
               2     debugging information

        THRESHOLD    MEANING
              -1     quiet mode (-q) only failures are printed
               0     normal operation
               1     verbose mode (-v)
               2     debug mode (-vv or -d)

  It can be extended farther in both directions, but that is rarely
  useful.  It can also be shifted in either direction.  This might be
  useful if you want o supply the threshold directly on the command
  line but have trouble passing in negative numbers.  In that case,
  add 1 to all thresholds and priorities listed above.

BASIC OPTIONS

  There are a couple of basic options that are commonly needed.  These
  are attribues of instances of class Logger.

  preprefix

    Text that will be printed at the start of each line of output (for
    log()ged, not write()en messages).  This might be your program's
    name, for example.

      log.preprefix = 'myprog'

    If preprefix is callable, then it will be called for each log and
    the returned value will be used.  This is useful for printing the
    current time.

      import time
      def printtime():
          return time.strftime('%m/%d/%y %H:%M:%S ',
                               time.localtime(time.time()))
      log.preprefix = printtime
  
  file_object

    This is the file object to which output is directed.  If it is None,
    then the logs are quietly dropped.

LOG CONTAINERS

  If you want a program to log to multiple destinations, it might be
  convenient to use log containers.  A log container is an object
  which hold several log objects.  When you log to a log container it
  passes the message on (with optional tests) to each of the log
  objects it contains.  For example:

    from logger import Logger, LogContainer

    system = Logger(threshold=1, file_object=logfile)
    debug  = Logger(threshold=5, file_object=sys.stdout)
    log = LogContainer([system, debug])

    log(3, 'sent to system and debug, but only debug will print it')
    log(0, 'very important, both will print it')

  In this mode, log containers are just shorthand for calling all
  contained objects with the same priority and message.

  When a log object is held in a container, it can still be used
  directly.  For example, you can still do

    debug(3, ['this will not be sent to the system log, even if its',
              ' threshold is set very high'])

  (Yes, you can send lists of strings and they will be formatted on
  different lines.  It is pretty smart.)

ADVANCED

  There are a number of options available for both classes.  These are
  documented below, in the respective classes and methods.  Here is a
  list of some of the things you can do:

    * make the prefix contain a string which gets repeated for more
      important logs.  (prefix)
    * directly test if a log object WOULD log, so you can do
      complicated stuff, like efficient for loops. (test)
    * make the priority, threshold arbitrary objects, with a
      home-rolled test to see if it should log. (test)
    * give log containers a "master threshold" and define arbitrary
      behavior based on it.  Examples include:
      - only pass on messages of sufficient priority (ragardless of
        the thresholds of the log ojects).
      - only pass on messages to objects whose thresholds are
        (numerically) lower than the master threshold.

SEE ALSO

  Take a look at the examples at the end of this file in the test &
  demo section.

COMMENTS

  I welcome comments, questions, bug reports and requests... I'm very
  lonely. :)
Ns*   Michael D. Stenner <mstenner@phy.duke.edu>s   0.6s
   2002/08/13s4   http://www.dulug.duke.edu/~mstenner/software/logger/s   Loggerc           B   sh   t  Z d  Z d e i d d d d d d „ Z d „  Z d „  Z e d „ Z	 e	 Z
 e d	 „ Z d
 „  Z RS(   sÛ  A class for file-object logging
    
    USAGE:
      from logger import Logger

      log_obj = Logger(THRESHOLD)     # create the instance

      log_obj.log(3, 'message')       # log a message with priority 3
      log_obj(3, 'message')           # same thing
      log_obj(3, ['message'])         # same thing

      log_obj.test(3)                 # boolean - would a message of
                                      # this priority be printed?

      # a raw write call after the priority test, for writing
      # arbitrary text -- (this will not be followed by \n)
      log_obj.write(3, 'thing\nto\nwrite')  

      # generate the prefix used for priority 3
      pr = log_obj.gen_prefix(3)

      # see the examples in the test section for more

    ATTRIBUTES:
      (all of these are settable as keyword args to __init__)

      ATTRIBUTES   DEFAULT      DESCRIPTION
      ----------------------------------------------------------
      threshold    = 0          how verbose the program should be
      file_object  = sys.stderr file object to which output goes
      prefix       = ''         prefix string - repeated for more
                                important logs
      prefix_depth = 5          times prefix is repeated for logs
                                of priority 0.  Basically, set this
                                one larger than your highest log
                                priority.
      preprefix    = ''         string printed before the prefix
                                if callable, returned string will
                                be used (useful for printing time)
      postprefix   = ''         string printed after the prefix
      default      = 1          default priority to log at

    i    s    i   i   c         C   sC   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   s	   thresholds   selfs   file_objects   prefixs   prefix_depths	   preprefixs
   postprefixs   default(   s   selfs	   thresholds   file_objects   prefixs   prefix_depths	   preprefixs
   postprefixs   default(    (    s   /usr/share/yum/logger.pys   __init__Ç   s    						c         C   s   t  |  i ƒ t  | ƒ j Sd S(   s4  
        Return true if a log of the given priority would be printed.
        
        This can be overridden to do any test you like.  Specifically,
        log and threshold need not be integers.  They be arbitrary
        objects.  You need only override this method, possibly
        gen_prefix.
        N(   s   ints   selfs	   thresholds   priority(   s   selfs   priority(    (    s   /usr/share/yum/logger.pys   test×   s    	 c         C   s—   t  |  i ƒ o |  i ƒ  } n
 |  i } |  i oU |  i t | ƒ } | d j  o
 d } n x( t d | ƒ D] } | |  i } qm Wn | |  i
 Sd S(   s*  
        Return the full prefix (including pre and post) for the
        given priority.

        If you use prefix and use a more complicated priority and
        verbosity (non-numerical), then you should either give the
        chosen object a __int__ method, or override this function.
        i   i    N(   s   callables   selfs	   preprefixs   prefixs   prefix_depths   ints   prioritys   depths   ranges   is
   postprefix(   s   selfs   prioritys   prefixs   is   depth(    (    s   /usr/share/yum/logger.pys
   gen_prefixä   s    	  	
 
 c         C   s	  |  i | | ƒ \ } } |  i | ƒ oÝ |  i t j o d Sn t	 | ƒ t	 d ƒ j o2 t
 i | d ƒ } | d d j o | d =qÉ n? t	 | ƒ t	 g  ƒ j o t t
 i | ƒ } n t | ƒ g } |  i | ƒ } x* | D] } |  i i | | d ƒ qß Wn d S(   su   
        Print a log message.  This prepends the prefix to each line
        and does some basic formatting.
        Ns    s   
iÿÿÿÿ(   s   selfs   _use_defaults   prioritys   messages   ps   ms   tests   file_objects   Nones   types   strings   splits   mlists   maps   rstrips   strs
   gen_prefixs   prefixs   lines   write(   s   selfs   prioritys   messages   mlists   ms   ps   prefixs   line(    (    s   /usr/share/yum/logger.pys   logú   s         c         C   sX   |  i | | ƒ \ } } |  i | ƒ o, |  i t j o d Sn |  i i	 | ƒ n d S(   s™   
        Print a log message.  In this case, 'message' must be a string
        as it will be passed directly to the file object's write method.
        N(
   s   selfs   _use_defaults   prioritys   messages   ps   ms   tests   file_objects   Nones   write(   s   selfs   prioritys   messages   ms   p(    (    s   /usr/share/yum/logger.pys   write  s      c         C   s,   | t j o |  i | f Sn | | f Sd S(   s0   Substitute default priority if none was providedN(   s   messages   Nones   selfs   defaults   priority(   s   selfs   prioritys   message(    (    s   /usr/share/yum/logger.pys   _use_default  s      (   s   __name__s
   __module__s   __doc__s   syss   stderrs   __init__s   tests
   gen_prefixs   Nones   logs   __call__s   writes   _use_default(    (    (    s   /usr/share/yum/logger.pys   Loggerš   s   + !		
s   LogContainerc           B   sk   t  Z d  Z g  e d d „ Z d „  Z e d „ Z e Z e d „ Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(
   sA  A class for consolidating calls to multiple Logger objects

    USAGE:
      from logger import Logger, LogContainer

      log_1 = Logger(threshold=1, file_object=sys.stdout)
      log_2 = Logger(threshold=2, preprefix='LOG2')

      log = LogContainer([log_1, log_2])

      log(1, 'message')               # printed by log_1 and log_2
      log(2, 'message')               # only printed by log_2

    ATTRIBUTES:
      (all of these are settable as keyword args to __init__)

      ATTRIBUTES   DEFAULT      DESCRIPTION
      ----------------------------------------------------------
      list         = []         list of Logger objects
      threshold    = None       meaning depends on test - by default
                                threshold has no effect
      default      = 1          default priority to log at

    i   c         C   s   | |  _  | |  _ | |  _ d  S(   N(   s   lists   selfs	   thresholds   default(   s   selfs   lists	   thresholds   default(    (    s   /usr/share/yum/logger.pys   __init__:  s    		c         C   s   |  i i | ƒ d S(   s"   Add a log object to the container.N(   s   selfs   lists   appends   log_obj(   s   selfs   log_obj(    (    s   /usr/share/yum/logger.pys   add?  s     c         C   s`   |  i | | ƒ \ } } xA |  i D]6 } |  i | | |  i	 | ƒ o | i
 | | ƒ q" q" Wd S(   s_   Log a message to all contained log objects, depending on
        the results of test()
        N(   s   selfs   _use_defaults   prioritys   messages   ps   ms   lists   log_objs   tests	   thresholds   log(   s   selfs   prioritys   messages   log_objs   ms   p(    (    s   /usr/share/yum/logger.pys   logC  s     
 c         C   s`   |  i | | ƒ \ } } xA |  i D]6 } |  i | | |  i	 | ƒ o | i
 | | ƒ q" q" Wd  S(   N(   s   selfs   _use_defaults   prioritys   messages   ps   ms   lists   log_objs   tests	   thresholds   write(   s   selfs   prioritys   messages   log_objs   ms   p(    (    s   /usr/share/yum/logger.pys   writeN  s
    
 c         C   s,   | t j o |  i | f Sn | | f Sd S(   s0   Substitute default priority if none was providedN(   s   messages   Nones   selfs   defaults   priority(   s   selfs   prioritys   message(    (    s   /usr/share/yum/logger.pys   _use_defaultT  s      c         C   s   d Sd S(   sU  Test which log objects should be passed a given message.

        This method is used to determine if a given message (and
        priority) should be passed on to a given log_obj.  The
        container's threshold is also provided.

        This method always returns 1, and is the default, meaning that
        all messages will get passed to all objects.  It is intended
        to be overridden if you want more complex behavior.  To
        override with your own function, just do something like:

          def hell_no(p, m, t, object): return 0
          container.test = hell_no
        i   N(    (   s   selfs   prioritys   messages	   thresholds   log_obj(    (    s   /usr/share/yum/logger.pys   testY  s     c         C   s   | | j Sd S(   sÞ   Only pass on messages with sufficient priority compared to
        the master threshold.

          container = LogContainer([system, debug], threshold = 2)
          container.test = container.test_limit_priority
        N(   s   prioritys	   threshold(   s   selfs   prioritys   messages	   thresholds   log_obj(    (    s   /usr/share/yum/logger.pys   test_limit_priorityj  s     c         C   s   | i | j Sd S(   sõ   Only pass on messages to log objects whose threshold is
        (numerically) lower than the master threshold.

          container = LogContainer([system, debug], threshold = 2)
          container.test = container.test_limit_threshold
        N(   s   log_objs	   threshold(   s   selfs   prioritys   messages	   thresholds   log_obj(    (    s   /usr/share/yum/logger.pys   test_limit_thresholds  s     (   s   __name__s
   __module__s   __doc__s   Nones   __init__s   adds   logs   __call__s   writes   _use_defaults   tests   test_limit_prioritys   test_limit_threshold(    (    (    s   /usr/share/yum/logger.pys   LogContainer  s    						s   __main__i   s   THRESHOLD = %ss	   preprefixs   TEST  s    Lets log a few things!iþÿÿÿi
   s   log priority %ss,   
 Now make it print the time for each log...c           C   s#   t  i d t  i t  i  ƒ  ƒ ƒ Sd  S(   Ns   %m/%d/%y %H:%M:%S (   s   times   strftimes	   localtime(    (    (    s   /usr/share/yum/logger.pys	   printtimeˆ  s    s    and log a few more thingss<   
 now create another with a different prefix and priority...s    and put them in a container...i   s   LOG2 s	   thresholdi    s    and log a bit mores:   
 OK, enough of the container... lets play with formattings   abcd
efgh
ijkls   
 no trailing newlines   
 with trailing newlines   
s   
 two trailing newliness   

s   
 log JUST a newlines/   
 use the write method, with a trailing newlines   
 print some complex objecti   s   keys   values=   
 now set the file object to None (nothing should be printed)s   THIS SHOULD NOT BE PRINTED(   s   __doc__s   syss   strings   AUTHORs   VERSIONs   DATEs   URLs   Loggers   LogContainers   __name__s	   thresholds   logs   ranges   is   times	   printtimes	   preprefixs   log2s   conts   test_limit_prioritys   tests   stuffs   writes   Nones   file_object(   s	   printtimes   log2s   strings   AUTHORs   URLs   is   conts   times   stuffs   syss	   thresholds   VERSIONs   LogContainers   DATEs   Loggers   log(    (    s   /usr/share/yum/logger.pys   ?   sf   		…]	  			    

	
