ó
n@µ\c           @  sk  d  d l  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 d e f d „  ƒ  YZ i  Z e e e d „ Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e j k rLd e f d „  ƒ  YZ e ƒ  e	 _ Z d e f d „  ƒ  YZ e ƒ  e	 _ Z  n e	 j Z e	 j Z  d „  Z! d S(   iÿÿÿÿ(   t   print_functionNt   LogFilec           B  s;   e  Z d  Z e e e d „ Z d „  Z d „  Z d „  Z RS(   s+   
    This manages one of our logfiles.
    c         C  sa   | |  _  | |  _ | |  _ | |  _ d |  _ d |  _ d |  _ t |  _	 t
 j r] t |  _ n  d S(   s©  
        `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.
        i    N(   t   namet   appendt	   developert   flusht   Nonet   filet	   softspacet   newlinest   Falset	   raw_writet   renpyt   iost   real_stdout(   t   selfR   R   R   R   (    (    s   renpy/log.pyt   __init__3   s    									c         C  s+  |  j  r t S|  j  t k r  t St j r- t S|  j rG t j j rG t St j j sW t Sy¬t j	 j
 d t j j p{ t j j ƒ } | d  k r‘ t St j j | |  j d ƒ } t j j t j ƒ  d |  j d ƒ } |  j rå d } n d } t j j rt |  _  n> y t j | | d ƒ |  _  Wn t j | | d ƒ |  _  n X|  j rx|  j d ƒ |  j d d	 ƒ |  j d ƒ n  |  j d
 t j ƒ  ƒ y |  j d
 t j ƒ  ƒ Wn |  j d ƒ n X|  j d
 t j ƒ |  j d t j j t j j ƒ |  j d ƒ t SWn! t |  _  t j d t ƒ t SXd  S(   Nt   RENPY_LOG_BASEs   .txts   renpy-t   at   ws   utf-8t    t   =iN   s   %ss   Unknown platform.s   %s %sR   (    R   t   TrueR
   R   t   macappR   t   configt
   log_enablet   ost   environt   gett   logdirt   basedirR   t   patht   joinR   t   tempfilet
   gettempdirR   t   log_to_stdoutR   t   codecst   opent   writet   timet   ctimet   platformt   versiont	   tracebackt	   print_exct   real_stderr(   R   t   baset   fnt   altfnt   mode(    (    s   renpy/log.pyR%   P   sT    		'&				c         G  s°   |  j  ƒ  r¬ |  j sM y | | } Wn t | f | ƒ } n X| d 7} n  t | t ƒ sn | j d ƒ } n  | j d d ƒ } |  j j | ƒ |  j	 r¬ |  j j	 ƒ  q¬ n  d S(   sF   
        Formats `s` with args, and writes it to the logfile.
        s   
s   latin-1s   
N(
   R%   R   t   reprt
   isinstancet   unicodet   decodet   replaceR   R&   R   (   R   t   st   args(    (    s   renpy/log.pyR&      s    		c         C  s&   t  |  _ t j d |  ƒ t |  _ d S(   s6   
        Writes the exception to the logfile.
        N(   R   R   R+   R,   R   R
   (   R   (    (    s   renpy/log.pyt	   exception¨   s    	(	   t   __name__t
   __module__t   __doc__R
   R   R   R%   R&   R9   (    (    (    s   renpy/log.pyR   .   s
   	?	c         C  sM   t  j |  d  ƒ } | d  k rI t |  d | d | d | ƒ} | t  |  <n  | S(   NR   R   R   (   t	   log_cacheR   R   R   (   R   R   R   R   t   rv(    (    s   renpy/log.pyR%   ¶   s
    t   TimeLogc           B  s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   sO   
    This represents a log that is limited to the last `duration` seconds.
    c         C  s   | |  _  d  S(   N(   t   duration(   R   R@   (    (    s   renpy/log.pyR   È   s    c         C  s3   t  j  ƒ  } t j |  | | f ƒ |  j | ƒ d  S(   N(   R'   t   listR   t   prune(   R   t   vt   now(    (    s   renpy/log.pyR   Ë   s    c         C  sN   | d  k r t j ƒ  } n  x, |  d d | |  j k  rI |  j d ƒ q Wd  S(   Ni    (   R   R'   R@   t   pop(   R   RD   (    (    s   renpy/log.pyRB   Ñ   s    N(   R:   R;   R<   R   R   R   RB   (    (    (    s   renpy/log.pyR?   Ã   s   		t   StdioRedirectorc           B  s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s(   d |  _  t d d t d t ƒ|  _ d  S(   NR   t   logR   R   (   t   bufferR%   R
   RG   (   R   (    (    s   renpy/log.pyR   ß   s    	c         C  sï   t  | t ƒ r! | j d ƒ } n | } |  j j | ƒ |  j j ƒ  t j rQ d  S|  j | } | j	 d ƒ } y |  j
 ƒ  } Wn g  } n XxN | d  D]B } |  j j d | ƒ x& | D] } y | | ƒ Wq¸ q¸ Xq¸ Wq˜ W| d |  _ d  S(   Ns   utf-8s   
iÿÿÿÿs   %s(   R3   R4   t   encodet	   real_fileR&   R   R   R   RH   t   splitt   get_callbacksRG   (   R   R7   t   est   linest	   callbackst   lt   i(    (    s   renpy/log.pyR&   ã   s*    	
c         C  s"   x | D] } |  j  | ƒ q Wd  S(   N(   R&   (   R   RN   RQ   (    (    s   renpy/log.pyt
   writelines  s    c         C  s   |  j  j ƒ  d  S(   N(   RJ   R   (   R   (    (    s   renpy/log.pyR   	  s    c         C  s   d  S(   N(    (   R   (    (    s   renpy/log.pyt   close  s    (   R:   R;   R   R&   RR   R   RS   (    (    (    s   renpy/log.pyRF   Ý   s
   		"		t   RENPY_NO_REDIRECT_STDIOt   StdoutRedirectorc           B  s   e  Z e Z d  „  Z RS(   c         C  s
   t  j j S(   N(   R   R   t   stdout_callbacks(   R   (    (    s   renpy/log.pyRL     s    (   R:   R;   R   RJ   RL   (    (    (    s   renpy/log.pyRU     s   t   StderrRedirectorc           B  s   e  Z e Z d  „  Z RS(   c         C  s
   t  j j S(   N(   R   R   t   stderr_callbacks(   R   (    (    s   renpy/log.pyRL     s    (   R:   R;   R-   RJ   RL   (    (    (    s   renpy/log.pyRW     s   c           C  s   t  t _ t t _ d  S(   N(   t
   sys_stdoutt   syst   stdoutt
   sys_stderrt   stderr(    (    (    s   renpy/log.pyt	   post_init)  s    	("   t
   __future__R    t   os.pathR   R$   R+   R)   R'   R!   RZ   t   encodings.latin_1t	   encodingst   renpy.configR   R[   R   R]   R-   R   t   log_filet   objectR   R=   R
   R%   RA   R?   RF   R   RU   RY   RW   R\   R^   (    (    (    s   renpy/log.pyt   <module>   s2   		…4		