a
    V$c                      @   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file	softspacenewlines	raw_writerenpyiosreal_stdout)selfr   r   r   r    r    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 )NTF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fnaltfnmoder    r    r!   r   R   sj    





zLogFile.openc                 G   s   |   rt|ts|d}| jsXz|| }W n  tyN   t|f| }Y n0 |d7 }| j| | j	rz| j	  W n ty   d| _	Y n0 dS )zF
        Formats `s` with args, and writes it to the logfile.
        zlatin-1
FN)
r   
isinstancer   decoder   r9   reprr   r:   r   )r   sargsr    r    r!   r:      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   r:   rM   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_cacher1   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   rV   r    r    r!   r"      s    zTimeLog.__init__c                 C   s&   t   }t| ||f | | d S rU   )r;   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   )r;   rV   pop)r   rZ   r    r    r!   rX      s    zTimeLog.prune)N)rN   rO   rP   rQ   r"   r   rX   r    r    r    r!   rT      s   rT   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   r]   rL   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&   replacerF   r)   )rG   r   r   r*   r+   	real_filer:   r   r   r^   splitget_callbacksr9   r]   )r   rJ   lines	callbackslir    r    r!   r:      s*    





zStdioRedirector.writec                 C   s   |D ]}|  | qd S rU   )r:   )r   rd   rg   r    r    r!   
writelines  s    zStdioRedirector.writelinesc                 C   s&   z| j   W n ty    Y n0 d S rU   )ra   r   r9   rL   r    r    r!   r     s    zStdioRedirector.flushc                 C   s   d S rU   r    rL   r    r    r!   close  s    zStdioRedirector.closec                 C   s   g S rU   r    rL   r    r    r!   rc   !  s    zStdioRedirector.get_callbacksN)rN   rO   rP   sysstderrra   r"   r:   rh   r   ri   rc   r    r    r    r!   r\      s    r\   ZRENPY_NO_REDIRECT_STDIOc                   @   s   e Zd ZeZdd ZdS )StdoutRedirectorc                 C   s   t jjS rU   )r   r*   stdout_callbacksrL   r    r    r!   rc   *  s    zStdoutRedirector.get_callbacksN)rN   rO   rP   r   ra   rc   r    r    r    r!   rl   '  s   rl   c                   @   s   e Zd ZeZdd ZdS )StderrRedirectorc                 C   s   t jjS rU   )r   r*   stderr_callbacksrL   r    r    r!   rc   2  s    zStderrRedirector.get_callbacksN)rN   rO   rP   rA   ra   rc   r    r    r    r!   rn   /  s   rn   c                   C   s   t t_tt_d S rU   )
sys_stdoutrj   stdout
sys_stderrrk   r    r    r    r!   	post_init=  s    rs   )FFF).
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r/   r?   r=   r;   r4   rj   r6   Zencodings.latin_1	encodingsr   rq   r   rk   rA   log_fileobjectr   rR   rW   rT   r\   r0   rl   rp   rn   rr   rs   r    r    r    r!   <module>   s6   8 
9