o
    +ke#                     @   s|   d Z ddlZddlZddlZddlZddlZddlZddlZdadddZ	ddd	Z
d
d ZdddZG dd dejZdS )a  logging facilities

The way to use this is as follows:

* each module declares its own logger, using:

    from .logger import create_logger
    logger = create_logger()

* then each module uses logger.info/warning/debug/etc according to the
  level it believes is appropriate:

    logger.debug('debugging info for developers or power users')
    logger.info('normal, informational output')
    logger.warning('warn about a non-fatal error or sth else')
    logger.error('a fatal error')

  ... and so on. see the `logging documentation
  <https://docs.python.org/3/howto/logging.html#when-to-use-logging>`_
  for more information

* console interaction happens on stderr, that includes interactive
  reporting functions like `help`, `info` and `list`

* ...except ``input()`` is special, because we can't control the
  stream it is using, unfortunately. we assume that it won't clutter
  stdout, because interaction would be broken then anyways

* what is output on INFO level is additionally controlled by commandline
  flags
    NFc                 C   s2   | d| d|j  d|  }tt }|| d S )N:z: )__name__create_loggerwarning)messagecategoryfilenamelinenofilelinemsglogger r   /usr/lib/python3.10/site-packages/borg/logger.py_log_warning/   s   r   BORG_LOGGING_CONFinfoc              
   C   s  d}|rt j||}|rbz>t j|}t|}tj| W d   n1 s)w   Y  da	t
t}t
d}	||	_|d| d tt_W dS  tya }
 z
t|
}W Y d}
~
nd}
~
ww t
d}t| }|rs|ssd}nd}|r{t|nt|}|| t
d}	||	_||	_t	r|jr|jd	   |j  || ||  da	t
t}|r|d
| d| d |d tt_|S )a  setup logging module according to the arguments provided

    if conf_fname is given (or the config file name can be determined via
    the env_var, if given): load this logging configuration.

    otherwise, set up a stream handler logger on stderr (by default, if no
    stream is provided).

    if is_serve == True, we configure a special log format as expected by
    the borg client log message interceptor.
    NTborgz'using logging configuration read from "" z/$LOG %(levelname)s %(name)s Remote: %(message)sz%(message)sr   zsetup_logging for "z" failed with "z".z,using builtin fallback logging configuration)osenvirongetpathabspathopenloggingconfigZ
fileConfig
configured	getLoggerr   jsondebugr   warningsshowwarning	ExceptionstrStreamHandlerJsonFormatter	FormattersetFormatter	formatterhandlerscloseclear
addHandlersetLevelupperr   )streamZ
conf_fnameZenv_varlevelZis_server    err_msgfr   Zborg_loggererrhandlerfmtr*   r   r   r   setup_logging8   sT   











r8   c                  C   sh   z(t  j} t | }|du s|jtkr%| j} t | }|du s|jtks|jW S  ty3   t Y S w )zfind the name of the first module calling this module

    if we cannot find it, we return the current module's name
    (__name__) instead.
    N)inspectcurrentframef_back	getmoduler   AttributeError)framemoduler   r   r   find_parent_modulew   s   


r@   c                    s   G  fddd  | S )a  lazily create a Logger object with the proper path, which is returned by
    find_parent_module() by default, or is provided via the commandline

    this is really a shortcut for:

        logger = logging.getLogger(__name__)

    we use it to avoid errors and provide a more standard API.

    We must create the logger lazily, because this is usually called from
    module level (and thus executed at import time - BEFORE setup_logging()
    was called). By doing it lazily we can do the setup first, we just have to
    be careful not to call any logger methods before the setup_logging() call.
    If you try, you'll get an exception.
    c                       sn   e Zd ZdddZedd Z fddZdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )z!create_logger.<locals>.LazyLoggerNc                 S   s   |pt  | _d | _d S N)r@   _LazyLogger__name_LazyLogger__real_logger)selfnamer   r   r   __init__   s   
z*create_logger.<locals>.LazyLogger.__init__c                 S   sP   | j d u r%tstdt| j| _ | jdr%| j jtjkr%| j 	d | j S )Nz8tried to call a logger before setup_logging() was calledzborg.debug.WARNING)
rC   r   r$   r   r   rB   
startswithr2   NOTSETr/   )rD   r   r   r   Z__logger   s   
z*create_logger.<locals>.LazyLogger.__loggerc                    s    | j d | S )N.)rB   )rD   suffix
LazyLoggerr   r   getChild      z*create_logger.<locals>.LazyLogger.getChildc                 _   s   | j j|i |S rA   )_LazyLogger__loggerr/   rD   argskwr   r   r   r/      rO   z*create_logger.<locals>.LazyLogger.setLevelc                 _   0   d|v r| d|di d< | jj|i |S Nmsgidextra)pop
setdefaultrP   logrQ   r   r   r   rZ         z%create_logger.<locals>.LazyLogger.logc                 _   rT   rU   )rX   rY   rP   	exceptionrQ   r   r   r   r\      r[   z+create_logger.<locals>.LazyLogger.exceptionc                 _   rT   rU   )rX   rY   rP   r!   rQ   r   r   r   r!      r[   z'create_logger.<locals>.LazyLogger.debugc                 _   rT   rU   )rX   rY   rP   r   rQ   r   r   r   r      r[   z&create_logger.<locals>.LazyLogger.infoc                 _   rT   rU   )rX   rY   rP   r   rQ   r   r   r   r      r[   z)create_logger.<locals>.LazyLogger.warningc                 _   rT   rU   )rX   rY   rP   errorrQ   r   r   r   r]      r[   z'create_logger.<locals>.LazyLogger.errorc                 _   rT   rU   )rX   rY   rP   criticalrQ   r   r   r   r^      r[   z*create_logger.<locals>.LazyLogger.criticalrA   )r   
__module____qualname__rF   propertyrP   rN   r/   rZ   r\   r!   r   r   r]   r^   r   rL   r   r   rM      s    

	rM   r   )rE   r   rL   r   r      s   8r   c                       s    e Zd ZdZ fddZ  ZS )r'   )	levelnamerE   r   rV   c                    sJ   t  | d|jddd}| jD ]}t||d }|r|||< qt|S )NZlog_messager   CRITICAL)typetimer   rb   )superformatcreatedRECORD_ATTRIBUTESgetattrr    dumps)rD   recorddataattrvalue	__class__r   r   rg      s   

zJsonFormatter.format)r   r_   r`   ri   rg   __classcell__r   r   rp   r   r'      s    r'   )NN)NNr   r   FFrA   )__doc__r9   r    r   Zlogging.configZlogging.handlersr   r"   r   r   r8   r@   r   r(   r'   r   r   r   r   <module>   s     

	?
K