
L~Mc           @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z d d l Z d d l m	 Z	 d d l
 m Z m Z m Z d d- d     YZ d	 e	 f d
     YZ e j e i d d 6d d 6 d   Z d   Z d a g  a g  a d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d d  Z e Z d d. d     YZ  d d/ d     YZ! y e" Wn> e# k
 re!   Z" e" j$ Z$ e" j% Z% e" j& Z& e" j' Z' n Xd   Z( d   Z) d d0 d      YZ* d! e+ f d"     YZ, d# d1 d$     YZ- y t. Wn e# k
 rd a. n Xd%   Z/ d& d'  Z0 d( d2 d)     YZ1 d*   Z2 y t3 Wn) e# k
 re- d  a3 e- d&  Z4 n Xd+ d3 d,     YZ5 d d l6 Z6 d d l7 Z7 e6 j8 e!  y t9 Wn$ e# k
 re5   a9 t9 j:   n Xd S(4   s%   
Logging and metrics infrastructure.
i(   t   divisionN(   t   datetime(   t	   Interface(   t   utilt   contextt   reflectt   ILogContextc           B   s   e  Z d  Z RS(   s   
    Actually, this interface is just a synoym for the dictionary interface,
    but it serves as a key for the default information in a log.

    I do not inherit from Interface because the world is a cruel place.
    (   t   __name__t
   __module__t   __doc__(    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR      s   t   ILogObserverc           B   s   e  Z d  Z d   Z RS(   s   
    An observer which can do something with log events.

    Given that most log observers are actually bound methods, it's okay to not
    explicitly declare provision of this interface.
    c         C   s   d S(   si  
        Log an event.

        @type eventDict: C{dict} with C{str} keys.
        @param eventDict: A dictionary with arbitrary keys.  However, these
            keys are often available:
              - C{message}: A C{tuple} of C{str} containing messages to be
                logged.
              - C{system}: A C{str} which indicates the "system" which is
                generating this event.
              - C{isError}: A C{bool} indicating whether this event represents
                an error.
              - C{failure}: A L{failure.Failure} instance
              - C{why}: Used as header of the traceback in case of errors.
              - C{format}: A string format used in place of C{message} to
                customize the event.  The intent is for the observer to format
                a message by doing something like C{format % eventDict}.
        N(    (   t	   eventDict(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   __call__(   s    (   R   R   R	   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR
   !   s   i    t   isErrort   -t   systemc         O   s?   t  j t  j   } | j |   t  j i | t 6| | |  S(   N(   R   t   getR   t   copyt   updatet   call(   t   ctxt   funct   argst   kwt   newCtx(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   callWithContextB   s    c         O   s   y |  j    } Wn- t k
 r&   n d } t d |  n Xy t i | d 6| | |  SWn' t k
 rt   n t d |  n Xd S(   s   
    Utility method which wraps a function in a try:/except:, logs a failure if
    one occurrs, and uses the system's logPrefix.
    s   (buggy logPrefix method)R   N(   t	   logPrefixt   KeyboardInterruptt   errR   (   t   loggerR   R   R   t   lp(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   callWithLoggerG   s    c           C   s#   t  j d d t d d d a d S(   s   
    DEPRECATED in Twisted 2.5.

    Support function for testing frameworks.

    Start keeping errors in a buffer which can be retrieved (and emptied) with
    flushErrors.
    s6   log.startKeepingErrors is deprecated since Twisted 2.5t   categoryt
   stackleveli   i   N(   t   warningst   warnt   DeprecationWarningt   _keepErrors(    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   startKeepingErrors`   s    	c          G   s#   t  j d d t d d t |    S(   s  
    DEPRECATED in Twisted 2.5.  See L{TestCase.flushLoggedErrors}.

    Support function for testing frameworks.

    Return a list of errors that occurred since the last call to flushErrors().
    (This will return None unless startKeepingErrors has been called.)
    s   log.flushErrors is deprecated since Twisted 2.5. If you need to flush errors from within a unittest, use TestCase.flushLoggedErrors instead.R    R!   i   (   R"   R#   R$   t   _flushErrors(   t
   errorTypes(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   flushErrorso   s    
c          G   sl   t  } g  a  |  rh xS | D]H } d } x& |  D] } | j |  r, d } q, q, W| r t |  q q Wn  | S(   s)   
    PRIVATE. DEPRECATED. DON'T USE.
    i   i    (   t   _keptErrorst   checkR   (   R(   t   kt   erkt   shouldReLogt   errT(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR'      s    c          G   s'   t  j d d t d d t |    d S(   s   
    DEPRECATED
    s0   log.ignoreErrors is deprecated since Twisted 2.5R    R!   i   N(   R"   R#   R$   t   _ignore(   t   types(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   ignoreErrors   s    c          G   s"   x |  D] } t  j |  q Wd S(   s)   
    PRIVATE. DEPRECATED. DON'T USE.
    N(   t   _ignoreErrorst   append(   R1   t   type(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR0      s    c           C   s$   t  j d d t d d t   d S(   s   
    DEPRECATED
    s0   log.clearIgnores is deprecated since Twisted 2.5R    R!   i   N(   R"   R#   R$   t   _clearIgnores(    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   clearIgnores   s    c           C   s
   g  a  d S(   s)   
    PRIVATE. DEPRECATED. DON'T USE.
    N(   R3   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR6      s    c         K   s  |  d k r t j   }  n  t |  t j  r t r t r d } x3 t D]+ } |  j |  } | rk d } PqF d } qF W| r t j |   q q t j |   n  t	 d |  d | d d |  nV t |  t
  r t	 d t j |   d | d d |  n t	 t |   d | d d | d S(   s  
    Write a failure to the log.

    The C{_stuff} and C{_why} parameters use an underscore prefix to lessen
    the chance of colliding with a keyword argument the application wishes
    to pass.  It is intended that they be supplied with arguments passed
    positionally, not by keyword.

    @param _stuff: The failure to log.  If C{_stuff} is C{None} a new
        L{Failure} will be created from the current exception state.  If
        C{_stuff} is an C{Exception} instance it will be wrapped in a
        L{Failure}.
    @type _stuff: C{NoneType}, C{Exception}, or L{Failure}.

    @param _why: The source of this failure.  This will be logged along with
        C{_stuff} and should describe the context in which the failure
        occurred.
    @type _why: C{str}
    i    i   t   failuret   whyR   N(   t   NoneR8   t   Failuret
   isinstanceR%   R3   R+   R*   R4   t   msgt	   Exceptiont   repr(   t   _stufft   _whyR   t   keepR   t   r(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR      s&    
(t   Loggerc           B   s   e  Z d  Z d   Z RS(   sM   
    This represents a class which may 'own' a log. Used by subclassing.
    c         C   s   d S(   s   
        Override this method to insert custom logging behavior.  Its
        return value will be inserted in front of every line.  It may
        be called more times than the number of output lines.
        R   (    (   t   self(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR      s    (   R   R   R	   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRD      s   t   LogPublisherc           B   sJ   e  Z d  Z d g Z d   Z d   Z d   Z d   Z d d d  Z	 RS(   s5   
    Class for singleton log message publishing.
    R=   c         C   s   g  |  _  d  S(   N(   t	   observers(   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   __init__   s    c         C   s&   t  |  s t  |  j j |  d S(   s   
        Add a new observer.

        @type other: Provider of L{ILogObserver}
        @param other: A callable object that will be called with each new log
            message (a dict).
        N(   t   callablet   AssertionErrorRG   R4   (   RE   t   other(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   addObserver   s    c         C   s   |  j  j |  d S(   s%   
        Remove an observer.
        N(   RG   t   remove(   RE   RK   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   removeObserver   s    c         O   s   t  j t  p i  j   } | j |  | | d <t j   | d <x t t |  j  d d d  D] } y |  j | |  Wqb t	 k
 r   qb t
 k
 r   qb |  j | } d   |  j | <t t j   d | f  | |  j | <qb Xqb Wd S(   sJ  
        Log a new message.

        For example::

        >>> log.msg('Hello, world.')

        In particular, you MUST avoid the forms::

        >>> log.msg(u'Hello, world.')
        >>> log.msg('Hello ', 'world.')

        These forms work (sometimes) by accident and will be disabled
        entirely in the future.
        t   messaget   timei   ic         S   s   d  S(   N(   R:   (   t   event(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   <lambda>$  s    s   Log observer %s failed.N(   R   R   R   R   R   RP   t   xranget   lenRG   R   t   UnicodeEncodeErrorR   R8   R;   (   RE   RO   R   t   actualEventDictt   it   observer(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR=     s"    
&c         C   s   | d	 k r@ |  j d | d t j |  d | d | d d  nA t j d
 k  rh t | | | | |  n t | | | | | |  d	 S(   s  
        Twisted-enabled wrapper around L{warnings.showwarning}.

        If C{file} is C{None}, the default behaviour is to emit the warning to
        the log system, otherwise the original L{warnings.showwarning} Python
        function is called.
        t   warningR    t   filenamet   linenot   formats2   %(filename)s:%(lineno)s: %(category)s: %(warning)si   i   N(   i   i   (   R:   R=   R   t   qualt   syst   version_infot   _oldshowwarning(   RE   RO   R    RZ   R[   t   filet   line(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   showwarning*  s    	
N(
   R   R   R	   t   synchronizedRH   RL   RN   R=   R:   Rc   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRF      s   					$c         C   sn   y |  | } WnY t  k
 r$   nF y d |  | f } Wqj y d |  f } Wqf d } qf Xqj Xn X| S(   s   
    Try to format the string C{fmtString} using C{fmtDict} arguments,
    swallowing all errors to always return a string.
    sD   Invalid format string or unformattable object in log message: %r, %ss=   UNFORMATTABLE OBJECT WRITTEN TO LOG with fmt %r, MESSAGE LOSTsJ   PATHOLOGICAL ERROR IN BOTH FORMAT STRING AND MESSAGE DETAILS, MESSAGE LOST(   R   (   t	   fmtStringt   fmtDictt   text(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   _safeFormatJ  s    c         C   s   |  d } | sy |  d rP d |  k rP |  j  d  p8 d d |  d j   } q d |  k rr t |  d |   } q d Sn d	 j t t j |   } | S(
   s  
    Extract text from an event dict passed to a log observer. If it cannot
    handle the dict, it returns None.

    The possible keys of eventDict are:
     - C{message}: by default, it holds the final text. It's required, but can
       be empty if either C{isError} or C{format} is provided (the first
       having the priority).
     - C{isError}: boolean indicating the nature of the event.
     - C{failure}: L{failure.Failure} instance, required if the event is an
       error.
     - C{why}: if defined, used as header of the traceback in case of errors.
     - C{format}: string format used in place of C{message} to customize
       the event. It uses all keys present in C{eventDict} to format
       the text.
    Other keys will be used when applying the C{format}, or ignored.
    RO   R   R8   R9   s   Unhandled Errors   
R\   Nt    (   R   t   getTracebackRh   t   joint   mapR   t   safe_str(   R   t   edmRg   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   textFromEventDictd  s    
*t   FileLogObserverc           B   sJ   e  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   s   
    Log observer that writes to a file-like object.

    @type timeFormat: C{str} or C{NoneType}
    @ivar timeFormat: If not C{None}, the format string passed to strftime().
    c         C   s   | j  |  _  | j |  _ d  S(   N(   t   writet   flush(   RE   t   f(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRH     s    c         C   s.   t  j |  t  j |  } | j d | j S(   s*  
        Return the current local timezone offset from UTC.

        @type when: C{int}
        @param when: POSIX (ie, UTC) timestamp for which to find the offset.

        @rtype: C{int}
        @return: The number of seconds offset from UTC.  West is positive,
        east is negative.
        i<   i   i  iQ (   R   t   utcfromtimestampt   fromtimestampt   dayst   seconds(   RE   t   whent   offset(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   getTimezoneOffset  s    c      
   C   s   |  j  d k	 r+ t j |  j  t j |   S|  j |  } t j | |  } t t	 | d d   } t t	 | d d   } | d k  r d } n d } d | j
 | j | j | j | j | j | | | f	 S(   s  
        Format the given UTC value as a string representing that time in the
        local timezone.

        By default it's formatted as a ISO8601-like string (ISO8601 date and
        ISO8601 time separated by a space). It can be customized using the
        C{timeFormat} attribute, which will be used as input for the underlying
        C{time.strftime} call.

        @type when: C{int}
        @param when: POSIX (ie, UTC) timestamp for which to find the offset.

        @rtype: C{str}
        i<   i    R   t   +s%   %d-%02d-%02d %02d:%02d:%02d%s%02d%02dN(   t
   timeFormatR:   RP   t   strftimet	   localtimeRz   R   Rt   t   abst   intt   yeart   montht   dayt   hourt   minutet   second(   RE   Rx   t   tzOffsett   tzHourt   tzMint   tzSign(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt
   formatTime  s    	c         C   s   t  |  } | d  k r d  S|  j | d  } i | d d 6| j d d  d 6} t d |  } t j |  j | d |  t j |  j  d  S(   NRP   R   s   
s   
	Rg   s   [%(system)s] %(text)s
Ri   (	   Ro   R:   R   t   replaceRh   R   t   untilConcludesRq   Rr   (   RE   R   Rg   t   timeStrRf   t   msgStr(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   emit  s    $c         C   s   t  |  j  d S(   s-   
        Start observing log events.
        N(   RL   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   start  s    c         C   s   t  |  j  d S(   s,   
        Stop observing log events.
        N(   RN   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   stop  s    N(   R   R   R	   R:   R|   RH   Rz   R   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRp     s   					t   PythonLoggingObserverc           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   sL  
    Output twisted messages to Python standard library L{logging} module.

    WARNING: specific logging configurations (example: network) can lead to
    a blocking system. Nothing is done here to prevent that, so be sure to not
    use this: code within Twisted, such as twisted.web, assumes that logging
    does not block.
    t   twistedc         C   s   t  j |  |  _ d S(   si   
        @param loggerName: identifier used for getting logger.
        @type loggerName: C{str}
        N(   t   loggingt	   getLoggerR   (   RE   t
   loggerName(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRH     s    c         C   sk   d | k r | d } n | d r/ t  j } n	 t  j } t |  } | d k rT d S|  j j | |  d S(   s%  
        Receive a twisted log entry, format it and bridge it to python.

        By default the logging level used is info; log.err produces error
        level, and you can customize the level by using the C{logLevel} key::

        >>> log.msg('debugging', logLevel=logging.DEBUG)

        t   logLevelR   N(   R   t   ERRORt   INFORo   R:   R   t   log(   RE   R   t   levelRg   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    

	c         C   s   t  |  j  d S(   s-   
        Start observing log events.
        N(   RL   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    c         C   s   t  |  j  d S(   s,   
        Stop observing log events.
        N(   RN   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    (   R   R   R	   RH   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s
   		t   StdioOnnaStickc           B   s   e  Z d  Z d Z d Z d Z d Z d d  Z d   Z d   Z	 d   Z
 d   Z e Z e Z e Z e Z d	   Z d
   Z RS(   s.   
    Class that pretends to be stout/err.
    i    t   wbs   <stdio (log)>c         C   s   | |  _  d |  _ d  S(   Nt    (   R   t   buf(   RE   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRH     s    	c         C   s   d  S(   N(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   close  s    c         C   s   d S(   Ni(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   fileno  s    c         C   s   d  S(   N(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRr     s    c         C   s   t  d   d  S(   Ns   can't read from the log!(   t   IOError(   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   read"  s    c         C   s^   |  j  | j d  } | d |  _  | d d !} x' | D] } t | d d d |  j q7 Wd  S(   Ns   
ii    t   printedi   R   (   R   t   splitR=   R   (   RE   t   datat   dt   messagesRO   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRq   *  s
    c         C   s.   x' | D] } t  | d d d |  j q Wd  S(   NR   i   R   (   R=   R   (   RE   t   linesRb   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt
   writelines1  s    (   R   R   R	   t   closedt	   softspacet   modet   nameRH   R   R   Rr   R   t   readlinet	   readlinest   seekt   tellRq   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    					c         O   s6   t  |  t  r d St |   } t | j | |  | S(   s   
    Initialize logging to a specified file.

    @return: A L{FileLogObserver} if a new observer is added, None otherwise.
    N(   R<   R   Rp   t   startLoggingWithObserverR   (   Ra   t   aR   t   flo(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   startLogging<  s
    i   c         C   sg   t  s t j a  t t _ n  t r4 t j   d a n  t |   t d  | rc t t	 _
 t t	 _ n  d S(   s   
    Initialize logging to a specified observer. If setStdout is true
    (defaults to yes), also redirect sys.stdout and sys.stderr
    to the specified file.
    s   Log opened.N(   R`   R"   Rc   t   defaultObserverR   R:   RL   R=   t   logfileR^   t   stdoutt   logerrt   stderr(   RX   t	   setStdout(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR   J  s    	
	

	t   NullFilec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   i    c         C   s   d  S(   N(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR   `  s    c         C   s   d  S(   N(    (   RE   t   bytes(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRq   a  s    c         C   s   d  S(   N(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyRr   b  s    c         C   s   d  S(   N(    (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR   c  s    (   R   R   R   R   Rq   Rr   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR   ^  s
   			c           C   s   t    a d S(   s   
    Throw away all logs.
    N(   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   discardLogsf  s    t   DefaultObserverc           B   s2   e  Z d  Z e j Z d   Z d   Z d   Z RS(   s   
    Default observer.

    Will ignore all non-error messages and send error messages to sys.stderr.
    Will be removed when startLogging() is called for the first time.
    c         C   s   | d r d | k r@ | j  d  p( d d | d j   } n0 d j g  | d D] } t |  ^ qQ  d } |  j j |  |  j j   n  d  S(   NR   R8   R9   s   Unhandled Errors   
Ri   RO   (   R   Rj   Rk   t   strR   Rq   Rr   (   RE   R   Rg   t   m(    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   _emit  s    
*0c         C   s   t  |  j  d  S(   N(   RL   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    c         C   s   t  |  j  d  S(   N(   RN   R   (   RE   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR     s    (   R   R   R	   R^   R   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyR   v  s
   			(    (    (    (    (    (    (    (;   R	   t
   __future__R    R^   RP   R"   R   R   t   zope.interfaceR   t   twisted.pythonR   R   R   R   R
   t
   setDefaultR   R   R%   R*   R3   R&   R)   R'   R2   R0   R7   R6   R:   R   t   deferrRD   RF   t   theLogPublishert	   NameErrorRL   RN   R=   Rc   Rh   Ro   Rp   t   objectR   R   R`   R   R   R   R   R   R   R   t
   threadableR8   t   synchronizeR   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/python/log.pyt   <module>   s~   
	
									+U						!S3+
				