o
    x[hg  ã                   @   sR   d dl Z d dlZd dlZe e¡ZG dd„ dƒZdddœdededefd	d
„Z	dS )é    Nc                   @   s>   e Zd ZdZdddœdededef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
        ```
    ç{®Gáz„?Ú	threshold©r   Úlog_modeÚmsgr   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|¡ d S d| jkr&d S d| jkrF| j| jkrDt d| j|¡ | j› d|› �| _	d S d S t
d	| j› d
�ƒ‚)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_typeÚ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   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                     s@   t ˆˆˆd�� ˆ | i |¤ŽW  d   ƒ S 1 sw   Y  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(   Úloggingr   Ú	getLoggerr   r   r   r"   r#   r,   r   r   r   r   Ú<module>   s    
"@