o
    r¨Êhÿ3  ã                   @   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mZ ddlmZ ddlm	Z	 ddlm
Z
 G dd„ dƒZG d	d
„ d
ƒZdd„ Zeƒ Zdd„ ZG dd„ dƒZG dd„ deƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZG dd„ deƒZd+dd „Zd!Zefd"d#„Zd$d%„ Zd&d'„ Zd,d)d*„ZdS )-zÐ
Logger implementation loosely modeled on PEP 282.  We don't use the
PEP 282 logger implementation in the stdlib ('logging') because it's
idiosyncratic and a bit slow for our purposes (we don't use threads).
é    N©Úsyslog)Úlong)Úis_text_stream)Ú	as_stringc                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	ÚLevelsByNameé2   é(   é   é   é
   é   é   N)
Ú__name__Ú
__module__Ú__qualname__ÚCRITÚERROÚWARNÚINFOÚDEBGÚTRACÚBLAT© r   r   úD/var/www/html/env/lib/python3.10/site-packages/supervisor/loggers.pyr      s    r   c                   @   s6   e Zd ZejZejZejZ	ej
ZejZejZejZdS )ÚLevelsByDescriptionN)r   r   r   r   r   Úcriticalr   Úerrorr   Úwarnr   Úinfor   Údebugr   Útracer   Úblatherr   r   r   r   r      s    
r   c                  C   s.   i } t j ¡ D ]\}}| d¡s|| |< q| S )NÚ_)r   Ú__dict__ÚitemsÚ
startswith)ÚbynumberÚnameÚnumberr   r   r   Ú_levelNumbers'   s   
€r*   c                 C   s   t t| d ƒ}|S ©N)Úgetattrr   )ÚdescriptionÚnumr   r   r   ÚgetLevelNumByDescription0   s   r/   c                   @   sP   e Zd ZdZejZddd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ ZdS )ÚHandlerú%(message)sNc                 C   s   || _ d| _d S ©NF)ÚstreamÚclosed)Úselfr3   r   r   r   Ú__init__8   ó   
zHandler.__init__c                 C   ó
   || _ d S r+   )Úfmt)r5   r9   r   r   r   Ú	setFormat<   ó   
zHandler.setFormatc                 C   r8   r+   ©Úlevel)r5   r=   r   r   r   ÚsetLevel?   r;   zHandler.setLevelc              
   C   sJ   z| j  ¡  W d S  ty$ } z|jd tjkr‚ W Y d }~d S d }~ww ©Nr   )r3   ÚflushÚIOErrorÚargsÚerrnoÚEPIPE©r5   Úwhyr   r   r   r@   B   s   ÿ€þzHandler.flushc                 C   sX   | j s*t| jdƒr z| j ¡ }W n	 ty   Y nw |dk r d S | j ¡  d| _ d S d S )NÚfilenor   T)r4   Úhasattrr3   rG   rA   Úclose)r5   Úfdr   r   r   rI   J   s   ý

ôzHandler.closec                 C   s´   zO| j dkot|jtƒo|j p|jdd ik}t| jƒ }|r#|j}n| j | ¡  }|r1| d¡}z| j 	|¡ W n t
yH   | j 	|¡ Y nw |  ¡  W d S    |  ¡  Y d S )Nr1   Úexc_infozutf-8)r9   Ú
isinstanceÚmsgÚbytesÚkwr   r3   ÚasdictÚencodeÚwriteÚUnicodeErrorr@   ÚhandleError)r5   ÚrecordÚbinaryÚbinary_streamrM   r   r   r   ÚemitY   s(   

ÿþ
ûzHandler.emitc                 C   s.   t  ¡ }t |d |d |d d t j¡ ~d S )Nr   é   é   )ÚsysrK   Ú	tracebackÚprint_exceptionÚstderr)r5   Úeir   r   r   rT   q   s    zHandler.handleErrorr+   )r   r   r   r9   r   r   r=   r6   r:   r>   r@   rI   rX   rT   r   r   r   r   r0   4   s    
r0   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	ÚStreamHandlerNc                 C   s   t  | |¡ d S r+   )r0   r6   )r5   Ústrmr   r   r   r6   w   ó   zStreamHandler.__init__c                 C   s   t | jdƒr| j ¡  d S d S )NÚclear)rH   r3   rc   ©r5   r   r   r   Úremovez   s   ÿzStreamHandler.removec                 C   ó   d S r+   r   rd   r   r   r   Úreopen~   ó   zStreamHandler.reopenr+   )r   r   r   r6   re   rg   r   r   r   r   r`   v   s    
r`   c                   @   s>   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚBoundIOó    c                 C   s   || _ || _d S r+   )ÚmaxbytesÚbuf)r5   rk   rl   r   r   r   r6   ‚   r7   zBoundIO.__init__c                 C   rf   r+   r   rd   r   r   r   r@   †   rh   zBoundIO.flushc                 C   s   |   ¡  d S r+   )rc   rd   r   r   r   rI   ‰   s   zBoundIO.closec                 C   s>   t |ƒ}t | jƒ| | jkr| j|d … | _|  j|7  _d S r+   )Úlenrl   rk   )r5   ÚbÚblenr   r   r   rR   Œ   s   zBoundIO.writec                 C   s   | j S r+   ©rl   rd   r   r   r   Úgetvalue’   s   zBoundIO.getvaluec                 C   s
   d| _ d S )Nrj   rp   rd   r   r   r   rc   •   r;   zBoundIO.clearN)rj   )	r   r   r   r6   r@   rI   rR   rq   rc   r   r   r   r   ri   �   s    
ri   c                   @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚFileHandlerz3File handler which supports reopening of logs.
    Úabc              
   C   sx   t  | ¡ zt||ƒ| _W n& ty3 } z|dkr(|jtjkr(d}t||ƒ| _n‚ W Y d }~nd }~ww || _|| _d S )Nrs   Úwb)	r0   r6   Úopenr3   ÚOSErrorrC   ÚESPIPEÚbaseFilenameÚmode)r5   Úfilenamery   Úer   r   r   r6   œ   s   
þ€ù
zFileHandler.__init__c                 C   s"   |   ¡  t| j| jƒ| _d| _d S r2   )rI   ru   rx   ry   r3   r4   rd   r   r   r   rg   ¯   s   
zFileHandler.reopenc              
   C   sT   |   ¡  z	t | j¡ W d S  ty) } z|jd tjkr‚ W Y d }~d S d }~ww r?   )rI   Úosre   rx   rv   rB   rC   ÚENOENTrE   r   r   r   re   ´   s   ÿ€ÿzFileHandler.removeN)rs   )r   r   r   Ú__doc__r6   rg   re   r   r   r   r   rr   ˜   s
    
rr   c                   @   sJ   e Zd Z		ddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )ÚRotatingFileHandlerrs   é    r   c                 C   s6   |dkrd}t  | ||¡ || _|| _d| _d| _dS )aú  
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.
        r   rs   r   N)rr   r6   ÚmaxBytesÚbackupCountÚcounterÚevery)r5   rz   ry   r�   r‚   r   r   r   r6   ½   s   
zRotatingFileHandler.__init__c                 C   s   t  | |¡ |  ¡  dS )z„
        Emit a record.

        Output the record to the file, catering for rollover as described
        in doRollover().
        N)rr   rX   Ú
doRollover)r5   rU   r   r   r   rX   Û   s   zRotatingFileHandler.emitc                 C   s
   t  |¡S r+   )r|   re   ©r5   Úfnr   r   r   Ú_removeå   s   
zRotatingFileHandler._removec                 C   s   t  ||¡S r+   )r|   Úrename)r5   ÚsrcÚtgtr   r   r   Ú_renameé   ó   zRotatingFileHandler._renamec                 C   s   t j |¡S r+   )r|   ÚpathÚexistsr†   r   r   r   Ú_existsí   r�   zRotatingFileHandler._existsc              
   C   sœ   |   |¡r(z|  |¡ W n ty' } z|jd tjkr‚ W Y d }~nd }~ww z	|  ||¡ W d S  tyM } z|jd tjkrB‚ W Y d }~d S d }~ww r?   )r�   rˆ   rv   rB   rC   r}   rŒ   )r5   ÚsfnÚdfnrF   r   r   r   ÚremoveAndRenameñ   s"   
ÿ€þÿ€ýz#RotatingFileHandler.removeAndRenamec                 C   s°   | j dkrdS | j ¡ | j ksdS | j ¡  | jdkrOt| jd ddƒD ]}d| j|f }d| j|d f }tj 	|¡rB|  
||¡ q$| jd }|  
| j|¡ t| jdƒ| _dS )z<
        Do a rollover, as described in __init__().
        r   NrY   éÿÿÿÿz%s.%dz.1rt   )r�   r3   ÚtellrI   r‚   Úrangerx   r|   rŽ   r�   r“   ru   )r5   Úir‘   r’   r   r   r   r…     s   


€
zRotatingFileHandler.doRolloverN)rs   r€   r   )
r   r   r   r6   rX   rˆ   rŒ   r�   r“   r…   r   r   r   r   r   ¼   s    
ÿ
r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú	LogRecordc                 K   s   || _ || _|| _d | _d S r+   )r=   rM   rO   Údictrepr)r5   r=   rM   rO   r   r   r   r6     s   
zLogRecord.__init__c                 C   sx   | j d u r9t ¡ }|t|ƒ d }t dt |¡¡}d||f }t| j }t| jƒ}| j	r2|| j	 }|||dœ| _ | j S )Niè  z%Y-%m-%d %H:%M:%Sz%s,%03d)ÚmessageÚ	levelnameÚasctime)
r™   Útimer   ÚstrftimeÚ	localtimeÚLOG_LEVELS_BY_NUMr=   r   rM   rO   )r5   ÚnowÚmsecsÚpart1rœ   r›   rM   r   r   r   rP     s   



ÿzLogRecord.asdictN)r   r   r   r6   rP   r   r   r   r   r˜     s    r˜   c                   @   sn   e Zd Zd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d„ Zdd„ Zdd„ ZdS )ÚLoggerNc                 C   s*   |d u rt j}|| _|d u rg }|| _d S r+   )r   r   r=   Úhandlers)r5   r=   r¥   r   r   r   r6   ,  s   
zLogger.__init__c                 C   s   | j D ]}| ¡  qd S r+   )r¥   rI   )r5   Úhandlerr   r   r   rI   5  s   

ÿzLogger.closec                 K   ó*   t j| jkr| jt j|fi |¤Ž d S d S r+   )r   r   r=   Úlog©r5   rM   rO   r   r   r   r"   9  ó   ÿzLogger.blatherc                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r!   =  rª   zLogger.tracec                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r    A  rª   zLogger.debugc                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r   E  rª   zLogger.infoc                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r   I  rª   zLogger.warnc                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r   M  rª   zLogger.errorc                 K   r§   r+   )r   r   r=   r¨   r©   r   r   r   r   Q  rª   zLogger.criticalc                 K   s6   t ||fi |¤Ž}| jD ]}||jkr| |¡ qd S r+   )r˜   r¥   r=   rX   )r5   r=   rM   rO   rU   r¦   r   r   r   r¨   U  s   


€þz
Logger.logc                 C   s   | j  |¡ d S r+   )r¥   Úappend)r5   Úhdlrr   r   r   Ú
addHandler[  rb   zLogger.addHandlerc                 C   s   t ‚r+   )ÚNotImplementedErrorrd   r   r   r   rq   ^  rh   zLogger.getvalue)NN)r   r   r   r6   rI   r"   r!   r    r   r   r   r   r¨   r­   rq   r   r   r   r   r¤   +  s    
	r¤   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚSyslogHandlerc                 C   s   t  | ¡ td usJ dƒ‚d S )NzSyslog module not present)r0   r6   r   rd   r   r   r   r6   b  s   
zSyslogHandler.__init__c                 C   rf   r+   r   rd   r   r   r   rI   f  rh   zSyslogHandler.closec                 C   rf   r+   r   rd   r   r   r   rg   i  rh   zSyslogHandler.reopenc                 C   s   t   |¡ d S r+   r   )r5   rM   r   r   r   Ú_syslogl  s   zSyslogHandler._syslogc              	   C   s„   z7|  ¡ }|d }| d¡ d¡D ]#}||d< | j| }z|  |¡ W q ty4   |  | d¡¡ Y qw W d S    |  ¡  Y d S )Nrš   Ú
zUTF-8)rP   ÚrstripÚsplitr9   r°   rS   rQ   rT   )r5   rU   Úparamsrš   ÚlinerM   r   r   r   rX   p  s   
ÿûzSyslogHandler.emitN)r   r   r   r6   rI   rg   r°   rX   r   r   r   r   r¯   a  s    r¯   c                 C   s   t | ƒS r+   )r¤   r<   r   r   r   Ú	getLogger~  s   r¶   i    c                 C   s<   t |ƒ}t|ƒ}| | j¡ | |¡ |  |¡ |j| _dS )z2Attach a new BoundIO handler to an existing LoggerN)ri   r`   r>   r=   r:   r­   rq   )Úloggerr9   rk   Úior¦   r   r   r   Úhandle_boundIOƒ  s   

r¹   c                 C   s.   t tjƒ}| |¡ | | j¡ |  |¡ dS )zDAttach a new StreamHandler with stdout handler to an existing LoggerN)r`   r[   Ústdoutr:   r>   r=   r­   ©r·   r9   r¦   r   r   r   Úhandle_stdoutŒ  s   

r¼   c                 C   s*   t ƒ }| |¡ | | j¡ |  |¡ dS )z1Attach a new Syslog handler to an existing LoggerN)r¯   r:   r>   r=   r­   r»   r   r   r   Úhandle_syslog“  s   
r½   Fc                 C   sT   |dkrt ƒ }n|du rt|ƒ}nt|d||ƒ}| |¡ | | j¡ |  |¡ dS )z‰Attach a new file handler to an existing Logger. If the filename
    is the magic name of 'syslog' then make it a syslog handler instead.r   FÚaN)r¯   rr   r   r:   r>   r=   r­   )r·   rz   r9   Úrotatingrk   Úbackupsr¦   r   r   r   Úhandle_fileš  s   

rÁ   r+   )Fr   r   )r~   r|   rC   r[   r�   r\   Úsupervisor.compatr   r   r   r   r   r   r*   r    r/   r0   r`   ri   rr   r   r˜   r¤   r¯   r¶   Ú_2MBr¹   r¼   r½   rÁ   r   r   r   r   Ú<module>   s:    			B$Z6
	