o ¯b‡'ã@s˜dZddlZddlZddlZddlZddlmZmZmZddl m Z Gdd„dƒZ Gdd„de ƒZ e   e ¡Gd d „d e ƒZe   e¡Gd d „d ƒZdS) z! A rotating, browsable log file. éN)ÚBinaryIOÚOptionalÚcast)Ú threadablec @s~eZdZdZddgZ ddededeeddfd d „Ze d d „ƒZ d d„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„ZdS)Ú BaseLogFilez< The base class for a log file that can be rotated. ÚwriteÚrotateNÚnameÚ directoryÚ defaultModeÚreturncCs`||_||_tj ||¡|_|dur'tj |j¡r't t |j¡tj¡|_ n||_ |  ¡dS)a Create a log file. @param name: name of the file @param directory: directory holding the file @param defaultMode: permissions used to create the file. Default to current permissions of the file if the file exists. N) r r ÚosÚpathÚjoinÚexistsÚstatÚS_IMODEÚST_MODEr Ú _openFile)Úselfr r r ©rú8/usr/lib/python3/dist-packages/twisted/python/logfile.pyÚ__init__s ÿ zBaseLogFile.__init__cOs4tj |¡}|tj |¡tj |¡g|¢Ri|¤ŽS)z= Construct a log file from a full file path. )r rÚabspathÚbasenameÚdirname)ÚclsÚfilenameÚargsÚkwargsÚlogPathrrrÚ fromFullPath2s (zBaseLogFile.fromFullPathcCst‚)zc Override with a method to that returns true if the log should be rotated. )ÚNotImplementedError©rrrrÚ shouldRotate:szBaseLogFile.shouldRotatec CsÐd|_tj |j¡rttt|jddƒƒ|_|j dd¡n.|j dur@t  d¡}zttt|jddƒƒ|_Wt  |¡nt  |¡wttt|jddƒƒ|_|j durfz t  |j|j ¡WdSt yeYdSwdS)z[ Open the log file. The log file is always opened in binary mode. Fzrb+réNiÿzwb+) Úclosedr rrrrÚopenÚ_fileÚseekr ÚumaskÚchmodÚOSError)rÚoldUmaskrrrrAs"    þýzBaseLogFile._openFilecCs<| ¡r | ¡| ¡t|tƒr| d¡}|j |¡dS)z¢ Write some data to the file. @param data: The data to write. Text will be encoded as UTF-8. @type data: L{bytes} or L{unicode} Úutf8N)r$ÚflushrÚ isinstanceÚstrÚencoder(r©rÚdatarrrr\s   zBaseLogFile.writecCs|j ¡dS)z! Flush the file. N)r(r/r#rrrr/jszBaseLogFile.flushcCsd|_|j ¡|`dS)z[ Close the file. The file cannot be used once it has been closed. TN)r&r(Úcloser#rrrr5ps zBaseLogFile.closecCs| ¡| ¡dS)a" Reopen the log file. This is mainly useful if you use an external log rotation tool, which moves under your feet. Note that on Windows you probably need a specific API to rename the file, as it's not supported to simply use os.rename, for example. N)r5rr#rrrÚreopenzs zBaseLogFile.reopencCs t|jƒS)z> Return a LogReader for the current log file. )Ú LogReaderrr#rrrÚ getCurrentLog…s zBaseLogFile.getCurrentLog©N)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú synchronizedr1rÚintrÚ classmethodr!r$rrr/r5r6r8rrrrrs,ÿÿÿÿ þ  rc@sXeZdZdZ   ddd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dS)ÚLogFilezf A log file that can be rotated. A rotateLength of None disables automatic log rotation. é@BNcCs t ||||¡||_||_dS)a` Create a log file rotating on length. @param name: file name. @type name: C{str} @param directory: path of the log file. @type directory: C{str} @param rotateLength: size of the log file where it rotates. Default to 1M. @type rotateLength: C{int} @param defaultMode: mode used to create the file. @type defaultMode: C{int} @param maxRotatedFiles: if not None, max number of log files the class creates. Warning: it removes all log files above this number. @type maxRotatedFiles: C{int} N)rrÚ rotateLengthÚmaxRotatedFiles)rr r rCr rDrrrr“s zLogFile.__init__cCst |¡|j ¡|_dSr9)rrr(ÚtellÚsizer#rrrr¯s zLogFile._openFilecCs|jo|j|jkS)zL Rotate when the log file size is larger than rotateLength. )rCrFr#rrrr$³szLogFile.shouldRotatecCs*d|j|f}tj |¡stdƒ‚t|ƒS)zK Given an integer, return a LogReader for an old log file. ú%s.%dúno such logfile exists)rr rÚ ValueErrorr7©rÚ identifierrrrrÚgetLog¹s zLogFile.getLogcCs"t ||¡|jt|ƒ7_dS)z. Write some data to the file. N)rrrFÚlenr3rrrrÂs z LogFile.writecCs¶t |jtj¡rt |jtj¡sdS| ¡}| ¡|D])}|jdur3||jkr3t d|j|f¡qt  d|j|fd|j|df¡q|j   ¡t  |jd|j¡|  ¡dS)z° Rotate the file and create a new one. If it's not possible to open new logfile, this will fail silently, and continue logging to old logfile. NrGéz%s.1) r Úaccessr ÚW_OKrÚlistLogsÚreverserDÚremoveÚrenamer(r5r)rÚlogsÚirrrrÉs &  zLogFile.rotatec Cs\g}t d|j¡D]}zt| d¡dƒ}|r| |¡Wq ty'Yq w| ¡|S)zM Return sorted list of integers - the old logs' identifiers. z%s.*Ú.éÿÿÿÿ)Úglobrr?ÚsplitÚappendrIÚsort)rÚresultr ÚcounterrrrrQÝs € ÿzLogFile.listLogscCót |¡}|d=|S)NrF©rÚ __getstate__©rÚstaterrrraìó zLogFile.__getstate__)rBNN) r:r;r<r=rrr$rLrrrQrarrrrrAŒs  ú  rAc@sPeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dS)Ú DailyLogFilezAA log file that is rotated daily (at or after midnight localtime)cCs&t |¡| t |j¡d¡|_dS)Né)rrÚtoDater rrÚlastDater#rrrrøs zDailyLogFile._openFilecCs| ¡|jkS)z1Rotate when the date has changed since last write)rgrhr#rrrr$üszDailyLogFile.shouldRotatecGstj|Ždd…S)aConvert a unixtime to (year, month, day) localtime tuple, or return the current (year, month, day) localtime tuple. This function primarily exists so you may overload it with gmtime, or some cruft to make unit testing possible. Né)ÚtimeÚ localtime)rrrrrrgszDailyLogFile.toDatec Cs<z d tt|ƒ¡WStyd tt| |¡ƒ¡YSw)z>Return the suffix given a (year, month, day) tuple or unixtimeÚ_)rÚmapr1Ú BaseExceptionrg)rÚ tupledaterrrÚsuffix s  þzDailyLogFile.suffixcCsJ| |¡|jkr | ¡S|j›d| |¡›}tj |¡s!tdƒ‚t|ƒS)z:Given a unix time, return a LogReader for an old log file.rWrH) rgrhr8rrpr rrIr7rJrrrrLs  zDailyLogFile.getLogcCs"t ||¡t|j| ¡ƒ|_dS)zWrite some data to the log fileN)rrÚmaxrhrgr3rrrrs zDailyLogFile.writecCspt |jtj¡rt |jtj¡sdS|j›d| |j¡›}tj |¡r&dS|j  ¡t  |j|¡|  ¡dS)z§Rotate the file and create a new one. If it's not possible to open new logfile, this will fail silently, and continue logging to old logfile. NrW) r rOr rPrrprhrr(r5rTr)rÚnewpathrrrr#s    zDailyLogFile.rotatecCr_)Nrhr`rbrrrra2rdzDailyLogFile.__getstate__N) r:r;r<r=rr$rgrprLrrrarrrrreõs   rec@s*eZdZdZdd„Zd dd„Zdd„Zd S) r7zRead from a log file.cCst|ƒ|_dS)zŽ Open the log file for reading. The comments about binary-mode for L{BaseLogFile._openFile} also apply here. N)r'r()rr rrrr>szLogReader.__init__é cCs4g}t|ƒD]}|j ¡}|s|S| |¡q|S)zRead a list of lines from the log file. This doesn't returns all of the files lines - call it multiple times. )Úranger(Úreadliner[)rÚlinesr]rVÚlinerrrÚ readLinesGs   ÿzLogReader.readLinescCs|j ¡dSr9)r(r5r#rrrr5TszLogReader.closeN)rs)r:r;r<r=rrxr5rrrrr7;s  r7)r=rYr rrjÚtypingrrrÚtwisted.pythonrrrAÚ synchronizerer7rrrrÚs w f C