a
    B^ie                     @   sH  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZm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 dlZd dlZejZejZ dZ!G dd de"Z#i Z$dddZG d	d
 d
e%Z&G dd de"Z'dej(vr0G dd de'Z)e)  e_Z*G dd de'Z+e+  e_Z,nejZ*ejZ,dd Z-dS )    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeNc                   @   s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )LogFilez+
    This manages one of our logfiles.
    FTc                 C   s@   || _ || _|| _|| _d| _d| _d| _d| _tj	r<t
| _dS )a  
        `name`
            The name of the logfile, without the .txt extension.
        `append`
            If true, we will append to the logfile. If false, we will truncate
            it to an empty file the first time we write to it.
        `developer`
            If true, nothing happens if config.developer is not set to True.
        `flush`
            Determines if the file is flushed after each write.
        Nr   F)nameappend	developerflushfileZ	softspacenewlines	raw_writerenpyiosreal_stdout)selfr   r   r   r    r   /C:\Users\PahaGest\Downloads\Selene\renpy\log.py__init__5   s    zLogFile.__init__c                 C   s  t jjrt| _dS | jrdS | jdu r*dS t jr4dS | jrFt jjsFdS t jjsRdS t jjd u rbd S zt	j
dt jj}|d u rW dS t	j|| jd }t	jt d| j d }| jrd}nd}z:tj||dd	| _zt j| W n ty   Y n0 W nL tyL   tj||dd	| _zt j| W n tyF   Y n0 Y n0 | jrt| d
 | d | d
 | dt  z| dt  W n ty   | d Y n0 | dt j | dt jjt jj | d
 W dS  ty   d| _tjtd Y dS 0 d S )NTFZRENPY_LOG_BASEz.txtzrenpy-awutf-8)encoding zN==============================================================================%szUnknown platform.z%s %s)r   )r   configlog_to_stdoutr   r   macappr   
log_enablelogdirosenvirongetpathjoinr   tempfile
gettempdirr   ior   utilexpose_file	Exceptionwritetimectimeplatformversion	traceback	print_excreal_stderr)r   basefnZaltfnmoder   r   r    r   R   sj    





zLogFile.openc                 G   sx   |   rtt|ts|d}| jsXz|| }W n  tyN   t|f| }Y n0 |d7 }| j| | j	rt| j	  dS )zF
        Formats `s` with args, and writes it to the logfile.
        zlatin-1
N)
r   
isinstancer   decoder   r7   reprr   r8   r   )r   sargsr   r   r    r8      s    

zLogFile.writec                 C   s   d| _ td|  d| _ dS )z6
        Writes the exception to the logfile.
        TNF)r   r=   r>   r   r   r   r    	exception   s    zLogFile.exceptionN)FFT)__name__
__module____qualname____doc__r!   r   r8   rJ   r   r   r   r    r   0   s
   
Jr   Fc                 C   s0   t | d }|d u r,t| |||d}|t | < |S )N)r   r   r   )	log_cacher/   r   )r   r   r   r   rvr   r   r    r      s
    r   c                   @   s*   e Zd ZdZdd Zdd Zd	ddZdS )
TimeLogzO
    This represents a log that is limited to the last `duration` seconds.
    c                 C   s
   || _ d S N)duration)r   rS   r   r   r    r!      s    zTimeLog.__init__c                 C   s&   t   }t| ||f | | d S rR   )r9   listr   prune)r   vnowr   r   r    r      s    zTimeLog.appendNc                 C   s6   |d u rt   }| d d || j k r2| d qd S )Nr   )r9   rS   pop)r   rW   r   r   r    rU      s    zTimeLog.prune)N)rK   rL   rM   rN   r!   r   rU   r   r   r   r    rQ      s   rQ   c                   @   sB   e Zd ZejZdd Zdd Zdd Zdd Z	d	d
 Z
dd ZdS )StdioRedirectorc                 C   s   d| _ tddddd| _d S )Nr&   logFT)r   r   r   )bufferr   rZ   rI   r   r   r    r!      s    zStdioRedirector.__init__c              
   C   s   t |tst|dd}tjjs4| j| | j  tjr>d S | j	| }|
d}z|  }W n tyt   g }Y n0 |d d D ]<}| jd| |D ]$}z|| W q ty   Y q0 qq|d | _	d S )Nr$   replacerC   r'   )rD   r   r   r(   r)   	real_filer8   r   r   r[   splitget_callbacksr7   rZ   )r   rG   linesZ	callbackslir   r   r    r8      s*    





zStdioRedirector.writec                 C   s   |D ]}|  | qd S rR   )r8   )r   ra   rc   r   r   r    
writelines  s    zStdioRedirector.writelinesc                 C   s   | j   d S rR   )r^   r   rI   r   r   r    r     s    
zStdioRedirector.flushc                 C   s   d S rR   r   rI   r   r   r    close  s    zStdioRedirector.closec                 C   s   g S rR   r   rI   r   r   r    r`     s    zStdioRedirector.get_callbacksN)rK   rL   rM   sysstderrr^   r!   r8   rd   r   re   r`   r   r   r   r    rY      s    rY   ZRENPY_NO_REDIRECT_STDIOc                   @   s   e Zd ZeZdd ZdS )StdoutRedirectorc                 C   s   t jjS rR   )r   r(   stdout_callbacksrI   r   r   r    r`   $  s    zStdoutRedirector.get_callbacksN)rK   rL   rM   r   r^   r`   r   r   r   r    rh   !  s   rh   c                   @   s   e Zd ZeZdd ZdS )StderrRedirectorc                 C   s   t jjS rR   )r   r(   stderr_callbacksrI   r   r   r    r`   ,  s    zStderrRedirector.get_callbacksN)rK   rL   rM   r?   r^   r`   r   r   r   r    rj   )  s   rj   c                   C   s   t t_tt_d S rR   )
sys_stdoutrf   stdout
sys_stderrrg   r   r   r   r    	post_init7  s    ro   )FFF).
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r-   r=   r;   r9   r2   rf   r4   Zencodings.latin_1	encodingsr   rm   r   rg   r?   Zlog_fileobjectr   rO   rT   rQ   rY   r.   rh   rl   rj   rn   ro   r   r   r   r    <module>   s6   8 
7