
    
i$                     ^    d Z ddlZddlmZ  G d dej                  Z G d d          ZdS )z pySim: Logging
    N)stylec                   $     e Zd Z fdZd Z xZS )_PySimLogHandlerc                 V    t                                                       || _        d S N)super__init__log_callback)selfr
   	__class__s     7/home/jenkins/workspace/simtester-sanitize/pySim/log.pyr	   z_PySimLogHandler.__init__   s'    (    c                 \    |                      |          }|                     ||           d S r   )formatr
   )r   recordformatted_messages      r   emitz_PySimLogHandler.emit"   s1     KK//+V44444r   )__name__
__module____qualname__r	   r   __classcell__)r   s   @r   r   r      sG        ) ) ) ) )5 5 5 5 5 5 5r   r   c                   L   e Zd ZdZdZdez   Z ej        e          Z ej        e          Z	dZ
i ZdZej                            ej                   d Zedi fdefd            Zedd	efd
            Zeej        fdefd            Zed             Zedefd            ZdS )PySimLoggera  
    Static class to centralize the log output of PySim applications. This class can be used to print log messages from
    any pySim module. Configuration of the log behaviour (see setup and set_ methods) is entirely optional. In case no
    print callback is set (see setup method), the logger will pass the log messages directly to print() without applying
    any formatting to the original log message.
    z%(levelname)s: %(message)sz$%(module)s.%(lineno)d -- %(name)s - NFc                      t          d          )Nz static class, do not instantiate)RuntimeError)r   s    r   r	   zPySimLogger.__init__>   s    =>>>r   colorsc                 6    | t           _        |t           _        dS )a  
        Set a print callback function and color scheme. This function call is optional. In case this method is not
        called, default settings apply.
        Args:
            print_callback : A callback function that accepts the resulting log string as input. The callback should
                             have the following format: print_callback(message:str)
            colors : An optional dict through which certain log levels can be assigned a color.
                     (e.g. {logging.WARN: YELLOW})
        N)r   print_callbackr   )r   r   s     r   setupzPySimLogger.setupA   s     &4"#r   verbosec                     | t           _        dS )z
        Enable/disable verbose logging. (has no effect in case no print callback is set, see method setup)
        Args:
            verbose: verbosity (True = verbose logging, False = normal logging)
        N)r   r    )r    s    r   set_verbosezPySimLogger.set_verboseO   s     &r   levelc                 D    t           j                            |            dS )z
        Set the logging level.
        Args:
            level: Logging level, valis log leves are: DEBUG, INFO, WARNING, ERROR and CRITICAL
        N)loggingrootsetLevel)r#   s    r   	set_levelzPySimLogger.set_levelX   s      	e$$$$$r   c                    t           j        st          |j                   d S t           j        r+t
          j                            t           j        |          }n*t
          j                            t           j	        |          }t           j
                            |j                  }|r+t                               t          ||                     d S t                               |           d S )N)fg)r   r   printmessager    r%   	Formatterr   _PySimLogger__formatter_verbose_PySimLogger__formatterr   getlevelnor   )r,   r   r   colors       r   _log_callbackzPySimLogger._log_callbacka   s    ) 	> &.!!!!! " ^$+$5$<$<[=\^d$e$e!!$+$5$<$<[=TV\$]$]!&**6>::E >**51B+O+O+OPPPPP**+<=====r   log_facilityc                     t          j        |           }t          t          j                  }|                    |           |S )z
        Set up and return a new python logger object
        Args:
            log_facility : Name of log facility (e.g. "MAIN", "RUNTIME"...)
        )r
   )r%   	getLoggerr   r   r3   
addHandler)r4   loggerhandlers      r   r0   zPySimLogger.gets   s@     "<00"0IJJJ'"""r   )F)r   r   r   __doc__
LOG_FMTSTRLOG_FMTSTR_VERBOSEr%   r-   r/   r.   r   r   r    r&   r'   DEBUGr	   staticmethoddictr   boolr"   intr(   r3   strr0    r   r   r   r   &   sb         .J?*L#'#J//K+'+,>?? N F GL'-(((? ? ? #2 $ $D $ $ $ \$ ' 'D ' ' ' \' %m % % % % % \% > > \>" 	# 	 	 	 \	 	 	r   r   )r:   r%   cmd2r   Handlerr   r   rC   r   r   <module>rF      s    .       5 5 5 5 5w 5 5 5W W W W W W W W W Wr   