U
    Ӈgg                     @   sN   d dl Z d dlZd dlZeeZG dd dZdddeeeddd	Z	dS )
    Nc                   @   s:   e Zd ZdZdddeeedddZdd	 Zd
d ZdS )Timeda  
    A context manager which measures and optionally logs context run time.

    :param msg: A message that describes the thing that is being measured
    :param threshold: Threshold, in seconds. When the context exceeds this
        threshold, a log will be made.
    :param log_mode: Control whether to log. Defaults to "threshold". Possible
        values include:
        "always" - Always log 'msg', even when 'threshold' is not reached.
        "threshold" - Log when context time exceeds 'threshold'.
        "skip" - Do not log. Context time and message are stored in the
            'output' and 'delta' attributes, respectively. Used to manually
            coalesce with other logs at the call site.

    usage:

        this call:
        ```
        with Timed("Configuring the network"):
            run_configure()
        ```

        might produce this log:
        ```
            Configuring the network took 0.100 seconds
        ```
    {Gz?	thresholdr   log_modemsgr   r   c                C   s(   || _ || _|| _d| _d| _d| _d S )N g        )r   r   r   outputstartdelta)selfr   r   r    r   7/usr/lib/python3/dist-packages/cloudinit/performance.py__init__%   s    zTimed.__init__c                 C   s   t  | _| S )N)time	monotonicr   )r   r   r   r   	__enter__3   s    
zTimed.__enter__c                 C   s   t  | j | _d| jdd}d| jkr<td| j| nZd| jkrJd S d| jkr| j| jkrtd| j| | j d| | _	nt
d	| j d
d S )Nztook z.3fz secondsalwaysz%s %sskipr    zInvalid Timed log_mode value: 'z'.)r   r   r   r   r   LOGdebugr   r   r
   
ValueError)r   exc_typeZexc_valexc_tbsuffixr   r   r   __exit__7   s    


zTimed.__exit__N)	__name__
__module____qualname____doc__strfloatr   r   r   r   r   r   r   r      s    r   r   r   r   r   c                   s    fdd}|S )a  
    A decorator which measures and optionally logs context run time.

    :param msg: A message that describes the thing that is being measured
    :param threshold: Threshold, in seconds. When the context exceeds this
        threshold, a log will be made.
    :param log_mode: Control whether to log. Defaults to "threshold". Possible
        values include:
        "always" - Always log 'msg', even when 'threshold' is not reached.
        "threshold" - Log when context time exceeds 'threshold'.

    usage:

        this call:
        ```
        @timed("Configuring the network")
        def run_configure():
            ...
        ```

        might produce this log:
        ```
            Configuring the network took 0.100 seconds
        ```
    c                    s    t   fdd}|S )Nc               
      s0   t d  | |W  5 Q R  S Q R X d S )Nr   )r   )argskwargs)funcr   r   r   r   r   	decoratord   s    z)timed.<locals>.wrapper.<locals>.decorator)	functoolswraps)r&   r'   r   r   r   )r&   r   wrapperc   s    ztimed.<locals>.wrapperr   )r   r   r   r+   r   r*   r   timedH   s    r,   )
r(   Zloggingr   Z	getLoggerr   r   r   r"   r#   r,   r   r   r   r   <module>   s
   
@