U
    Vb	                     @   s   d Z dZdZddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ dZG d	d
 d
Zdd Zedd Zdd Zdd Zdd ZdS )zCopyright 2021, 3LizzGPL version 3zinfo@3liz.org    N)contextmanager)QgisQgsMessageLog)to_boolLizmapc                   @   sT   e Zd ZeedddZeedddZeedddZeedd	d
Z	dS )Loggermessagec                 C   s   t t| ttj d S N)r   
logMessagestrPLUGINr   Infor    r   0/var/qgis-server/plugins/lizmap/server/logger.pyinfo   s    zLogger.infoc                 C   s   t t| ttj d S r
   )r   r   r   r   r   Warningr   r   r   r   warning   s    zLogger.warningc                 C   s   t t| ttj d S r
   )r   r   r   r   r   Criticalr   r   r   r   critical   s    zLogger.criticalec                 C   s   t dj| t d dS )z Log a Python exception. z#Critical exception:
{e}
{traceback})r   	tracebackN)r   r   formatr   
format_excr   r   r   r   log_exception"   s    zLogger.log_exceptionN)
__name__
__module____qualname__staticmethodr   r   r   r   BaseExceptionr   r   r   r   r   r      s   r   c                    s    fdd}|S )z$ Decorator to catch all exceptions. c               
      sZ   z | | W nF t k
rT } z(ttdr:t|  t| W 5 d }~X Y nX d S )NCI	Exceptionr   osgetenvr   r   )argskwargsr   funcr   r   inner_function/   s    
z)exception_handler.<locals>.inner_functionr   )r)   r*   r   r(   r   exception_handler-   s    r+   c               
   c   sV   z
dV  W nF t k
rP }  z(ttdr6t|   t|  W 5 d} ~ X Y nX dS )z3 Define a trap context for catching all exceptions Nr!   r"   r   r   r   r   trap:   s    

r,   c                    s   t   fdd}|S )z Decorator to log function. c                     s   t  j  | |}|S r
   )r   r   r   )r&   r'   valuer(   r   r   log_function_coreH   s    
z'log_function.<locals>.log_function_core	functoolswraps)r)   r.   r   r(   r   log_functionF   s    r2   c                    s   t   fdd}|S )z# Decorator to make some profiling. c                     s<   t   } | |}t   }td jt|| d |S )Nz{} ran in {}s   )timer   r   r   r   round)r&   r'   startresultendr(   r   r   wrapperT   s
    
zprofiling.<locals>.wrapperr/   r)   r9   r   r(   r   	profilingQ   s    r;   c                    s   t   fdd}|S )z. Decorator to log the output of the function. c                     s(    | |}t d j|t|  |S )Nz {} output is {} for parameter {})r   r   r   r   r   )r&   r'   r7   r(   r   r   r9   b   s    
z!log_output_value.<locals>.wrapperr/   r:   r   r(   r   log_output_value_   s    r<   )__copyright____license__	__email__r0   r$   r4   r   
contextlibr   	qgis.corer   r   lizmap.server.toolsr   r   r   r+   r,   r2   r;   r<   r   r   r   r   <module>   s"   
