Ϫf'dZddlZddlZddlZddlZddlmZmZmZddl m Z GddZ Gdde Z e je Gd d e Ze jeGd d Zy) z! A rotating, browsable log file. N)BinaryIOOptionalcast) threadablec teZdZdZddgZ ddededeeddfd Ze d Z d Z d Z d Z dZdZdZdZy) BaseLogFilez< The base class for a log file that can be rotated. writerotateNname directory defaultModereturnc||_||_tjj |||_|rtjj |jrIt jtj |jt j|_ n||_ |jy)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 ospathjoinexistsstatS_IMODEST_MODEr _openFile)selfr r r s 8/usr/lib/python3/dist-packages/twisted/python/logfile.py__init__zBaseLogFile.__init__s# GGLLD1  277>>$))#<.2ll "4<<0/D  +D  ctjj|}|tjj|tjj |g|i|S)z= Construct a log file from a full file path. )rrabspathbasenamedirname)clsfilenameargskwargslogPaths r fromFullPathzBaseLogFile.fromFullPath2sJ ''//(+277##G,bggoog.FXXQWXXrct)zc Override with a method to that returns true if the log should be rotated. )NotImplementedErrorrs r shouldRotatezBaseLogFile.shouldRotate:s "!rcd|_tjj|jrGt t t |jdd|_|jjddn|jVtjd} t t t |jdd|_tj|n*t t t |jdd|_|j, tj|j|jyy#tj|wxYw#t$rYywxYw)z[ Open the log file. The log file is always opened in binary mode. Fzrb+rNizwb+) closedrrrrropen_fileseekr umaskchmodOSError)roldUmasks rrzBaseLogFile._openFileAs  77>>$)) $hTYYq(ABDJ JJOOAq !+88E?'!%hTYYq0I!JDJHHX&!(DE1,EF    ' D$4$45 (HHX&   s*D=*E=E E#"E#c|jr |j|jt|tr|j d}|j j|y)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 isinstancestrencoder.r rdatas rr zBaseLogFile.write\sL     JJL KKM dC ;;v&D rc8|jjy)z! Flush the file. N)r.r6r(s rr6zBaseLogFile.flushjs rcJd|_|jj|`y)z[ Close the file. The file cannot be used once it has been closed. TN)r,r.closer(s rr>zBaseLogFile.closeps    JrcD|j|jy)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)r>rr(s rreopenzBaseLogFile.reopenzs  rc,t|jS)z> Return a LogReader for the current log file. ) LogReaderrr(s r getCurrentLogzBaseLogFile.getCurrentLogs##rN)__name__ __module__ __qualname____doc__ synchronizedr8rintr classmethodr%r)rr r6r>r@rCrrrrs|X&LGK$'6>sm ,YY"6   $rrcHeZdZdZ d dZdZdZdZdZdZ d Z d Z y) LogFilezf A log file that can be rotated. A rotateLength of None disables automatic log rotation. NcPtj||||||_||_y)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 rotateLengthmaxRotatedFiles)rr r rPr rQs rrzLogFile.__init__s(0 T4K@(.rcltj||jj|_yrD)rrr.tellsizer(s rrzLogFile._openFiles"d#JJOO% rcP|jxr|j|jk\S)zL Rotate when the log file size is larger than rotateLength. )rPrTr(s rr)zLogFile.shouldRotates$  CTYY$2C2C%CCrcd|j|fz}tjj|s tdt |S)zK Given an integer, return a LogReader for an old log file. %s.%dno such logfile exists)rrr ValueErrorrBr identifierr!s rgetLogzLogFile.getLogs>dii44ww~~h'56 6""rcltj|||xjt|z c_y)z. Write some data to the file. N)rr rTlenr:s rr z LogFile.writes& $% SY rctj|jtjr.tj|jtjsy|j }|j |D]y}|j4||jk\r%tjd|j|fzCtjd|j|fzd|j|dzfz{|jjtj|jd|jz|jy)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. NrWz%s.1) raccessr W_OKrlistLogsreverserQremoverenamer.r>r)rlogsis rr zLogFile.rotates $.."''2ryyBGG7T }}  RA##/A9M9M4M 'TYYN23 'TYYN2Gtyy!a%>P4PQ  R  $))Vdii/0 rcg}tjd|jzD]3} t|jdd}|r|j |5|j |S#t $rYSwxYw)zM Return sorted list of integers - the old logs' identifiers. z%s.*.)globrrJsplitappendrYsort)rresultr counters rrczLogFile.listLogsszIIftyy01 D djjob12MM'*       s0A-- A98A9c6tj|}|d=|S)NrTr __getstate__rstates rrtzLogFile.__getstate__s((. &M r)i@BNN) rErFrGrHrrr)r\r r rcrtrLrrrNrNs; /8&D #( rrNc@eZdZdZdZdZdZdZdZdZ dZ d Z y ) DailyLogFilezAA log file that is rotated daily (at or after midnight localtime)ctj||jtj|j d|_y)N)rrtoDaterrrlastDater(s rrzDailyLogFile._openFiles2d# BGGDII$6q$9: rc<|j|jkDS)z1Rotate when the date has changed since last write)r{r|r(s rr)zDailyLogFile.shouldRotates{{}t}},,rc,tj|ddS)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)rr"s rr{zDailyLogFile.toDates~~t$Ra((rc  djtt|S#t$r1djtt|j |cYSwxYw)z>Return the suffix given a (year, month, day) tuple or unixtime_)rmapr8 BaseExceptionr{)r tupledates rsuffixzDailyLogFile.suffix sK >88CY/0 0 >88CT[[%;<= = >s!7AAc|j||jk(r|jS|jd|j |}t jj |s tdt|S)z:Given a unix time, return a LogReader for an old log file.rjrX) r{r|rCrrrrrYrBrZs rr\zDailyLogFile.getLogsk ;;z "dmm 3%%' 'ii[$++j"9!:;ww~~h'56 6""rctj||t|j|j |_y)zWrite some data to the log fileN)rr maxr|r{r:s rr zDailyLogFile.writes,$%DMM4;;=9 rctj|jtjr.tj|jtjsy|jd|j |j }tjj|ry|jjtj|j||jy)zRotate 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. Nrj) rrar rbrrr|rr.r>rfr)rnewpaths rr zDailyLogFile.rotate#s  $.."''2ryyBGG7T YYKqT]]!; <= 77>>' "   $))W% rc6tj|}|d=|S)Nr|rsrus rrtzDailyLogFile.__getstate__2s((. *  rN) rErFrGrHrr)r{rr\r r rtrLrrrxrxs-K;-)>#: rrxc$eZdZdZdZddZdZy)rBzRead from a log file.c$t||_y)z Open the log file for reading. The comments about binary-mode for L{BaseLogFile._openFile} also apply here. N)r-r.)rr s rrzLogReader.__init__>s$Z rcg}t|D]2}|jj}|s|S|j|4|S)zRead a list of lines from the log file. This doesn't returns all of the files lines - call it multiple times. )ranger.readlinern)rlinesrprhlines r readLineszLogReader.readLinesGsN u A::&&(D  MM$    rc8|jjyrD)r.r>r(s rr>zLogReader.closeTs rN) )rErFrGrHrrr>rLrrrBrB;s  rrB)rHrlrrrtypingrrrtwisted.pythonrrrN synchronizerxrBrLrrrsz   ++%t$t$nckcL w@;@F |$r