
    Rǻi                        d dl mZ d dlZd dlmZ d dlm	Z
mZmZmZmZmZmZmZmZ d dlmZ ddlmZ  dd	gZ! G d
 de      Z" G d de      Z# G d d      Z$eedddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ%y)    )annotationsN)suppress)	CRITICALDEBUGERRORFilter	Formatter	getLoggerINFOStreamHandlerWARNING)stderr   )_typingWatcherwatchc                  "     e Zd ZdZ fdZ xZS )_ColourFormatterz'Colour formatter for pretty log output.c                    t         |   |      }|j                  t        k(  rd| dS |j                  t        k(  rd| dS |j                  t
        k(  rd| dS |j                  t        k(  rd| dS |j                  t        k(  rd| dS |S )Nz[31;1mz[0mz[33;1mz[33mz[37mz[36m)superformatlevelno	_CRITICAL_ERROR_WARNING_INFO_DEBUG)selfrecords	__class__s      I/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/neo4j/debug.pyr   z_ColourFormatter.format/   s    GN6">>Y&s'**^^v%s'**^^x'aS((^^u$aS((^^v%aS((H    )__name__
__module____qualname____doc__r   __classcell__r!   s   @r"   r   r   ,   s    1 r#   r   c                      e Zd ZdZd Zy)_TaskIdFilterz)Injecting async task id into log records.c                v    	 t        t        j                               |_        y# t        $ r
 d |_        Y yw xY w)NT)id_asynciocurrent_tasktaskRuntimeError)r   r   s     r"   filterz_TaskIdFilter.filterB   s<    	X2245FK   	FK	s   "% 88N)r$   r%   r&   r'   r2    r#   r"   r+   r+   ?   s
    3r#   r+   c                  x     e Zd ZdZeedddd	 	 	 	 	 	 	 	 	 	 	 	 	 d
 fdZddZd Z	 d	 	 	 	 	 ddZ	dd	Z
 xZS )r   a8  
    Log watcher for easier logging setup.

    Example::

        from neo4j.debug import Watcher

        with Watcher("neo4j"):
            # DEBUG logging to stderr enabled within this context
            ...  # do something

    .. note:: The Watcher class is not thread-safe. Having Watchers in multiple
        threads can lead to duplicate log messages as the context manager will
        enable logging for all threads.

    .. note::
        The exact logging format and messages are not part of the API contract
        and might change at any time without notice. They are meant for
        debugging purposes and human consumption only.

    :param logger_names: Names of loggers to watch.
    :param default_level: Default minimum log level to show.
        The level can be overridden by setting ``level`` when calling
        :meth:`.watch`.
    :param default_out: Default output stream for all loggers.
        The level can be overridden by setting ``out`` when calling
        :meth:`.watch`.
    :type default_out: stream or file-like object
    :param colour: Whether the log levels should be indicated with ANSI colour
        codes.
    :param thread_info: whether to include information about the current
        thread in the log message. Defaults to :data:`True`.
    :param task_info: whether to include information about the current
        async task in the log message. Defaults to :data:`True`.

    .. versionchanged:: 5.3

        * Added ``thread_info`` and ``task_info`` parameters.
        * Logging format around thread and task information changed.
    FTdefault_leveldefault_outcolourthread_info	task_infoc               &   t         
|           || _        | j                  D cg c]  }t        |       c}| _        || _        || _        i | _        || _        d}|rd|z   }|rd|z   }|sd|z   }|rt        nt        }	 |	|      | _        y c c}w )Nz%(asctime)s  %(message)sz[Task %(task)-15s] z[Thread %(thread)d] z[%(levelname)-8s] )r   __init___logger_names
_getLogger_loggers_default_level_default_out	_handlers
_task_infor   
_Formatter	formatter)r   r6   r7   r8   r9   r:   logger_namesnameformat_formatter_clsr!   s             r"   r<   zWatcher.__init__t   s     	)6:6H6HIdD)I+'46#,+g5G,w6G*W4G,2(
&w/ Js   Bc                &    | j                          | S )zEnable logging for all loggers.)r   )r   s    r"   	__enter__zWatcher.__enter__   s    

r#   c                $    | j                          yz Disable logging for all loggers.N)stop)r   exc_typeexc_valexc_tbs       r"   __exit__zWatcher.__exit__   s    		r#   c                   || j                   }|| j                  }| j                          t        |      }|j	                  | j
                         |j                  |       | j                  r|j                  t                      | j                  D ]Q  }|| j                  |j                  <   |j                  |       |j                         |kD  sA|j                  |       S y)a:  
        Enable logging for all loggers.

        :param level: Minimum log level to show.
            If :data:`None`, the ``default_level`` is used.
        :param out: Output stream for all loggers.
            If :data:`None`, the ``default_out`` is used.
        :type out: stream or file-like object
        N)r@   rA   rN   _StreamHandlersetFormatterrE   setLevelrC   	addFilterr+   r?   rB   rG   
addHandlergetEffectiveLevel)r   levelouthandlerloggers        r"   r   zWatcher.watch   s     =''E;##C		 %T^^,??mo.mm 	'F*1DNN6;;'g&'')E1&		'r#   c                    | j                   D ]N  }t        t              5  |j                  | j                  j                  |j                               ddd       P y# 1 sw Y   [xY wrM   )r?   	_suppressKeyErrorremoveHandlerrB   poprG   )r   r]   s     r"   rN   zWatcher.stop   s_    mm 	FF8$ F$$T^^%7%7%DEF F	FF Fs   5AA(	)rF   
str | Noner6   intr7   	_t.TextIOr8   boolr9   rf   r:   rf   returnNone)rg   r   )NN)rZ   z
int | Noner[   z_t.TextIO | Nonerg   rh   )rg   rh   )r$   r%   r&   r'   r   _stderrr<   rK   rR   r   rN   r(   r)   s   @r"   r   r   J   s    'X $!( 0!0 0 	0
 0 0 0 
06

 AE''-='	'8Fr#   FT)rZ   r[   r8   r9   r:   c           	     D    t        || ||||d}|j                          |S )aC  
    Quick wrapper for using  :class:`.Watcher`.

    Create a Watcher with the given configuration, enable watching and return
    it.

    Example::

        from neo4j.debug import watch

        watch("neo4j")
        # from now on, DEBUG logging to stderr is enabled in the driver

    .. note::
        The exact logging format and messages are not part of the API contract
        and might change at any time without notice. They are meant for
        debugging purposes and human consumption only.

    :param logger_names: Names of loggers to watch.
    :param level: see ``default_level`` of :class:`.Watcher`.
    :param out: see ``default_out`` of :class:`.Watcher`.
    :param colour: see ``colour`` of :class:`.Watcher`.
    :param thread_info: see ``thread_info`` of :class:`.Watcher`.
    :param task_info: see ``task_info`` of :class:`.Watcher`.

    :returns: Watcher instance
    :rtype: :class:`.Watcher`

    .. versionchanged:: 5.3

        * Added ``thread_info`` and ``task_info`` parameters.
        * Logging format around thread and task information changed.
    r5   )r   r   )rZ   r[   r8   r9   r:   rF   watchers          r"   r   r      s1    R 	G MMONr#   )rF   rc   rZ   rd   r[   re   r8   rf   r9   rf   r:   rf   rg   r   )&
__future__r   _asyncior.   
contextlibr   r_   loggingr   r   r   r   r   r   r   _Filterr	   rD   r
   r>   r   r   r   rT   r   r   sysr   ri    r   _t__all__r   r+   r   r   r3   r#   r"   <module>rv      s   " (  ,
 
 
 "  z &G nF nFf 222 
2 	2
 2 2 2r#   