3 fI @sddlZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl Z y ddl Z Wne k rxdZ YnXdZdZdZdZdZdZd'ZGd d d ejZGd d d eZGdddeZGdddejZGdddejZGdddeZGdddejZGdddejZGdddejZGdddejZ GdddejZ!Gd d!d!e!Z"Gd"d#d#ejZ#e rGd$d%d%e$Z%dS)(N)ST_DEVST_INOST_MTIMEi<#i=#i>#i?#i<c@s.eZdZd ddZddZddZd d ZdS) BaseRotatingHandlerNFcCs0tjj|||||||_||_d|_d|_dS)N)logging FileHandler__init__modeencodingnamerrotator)selffilenamer r delayr(/usr/lib64/python3.6/logging/handlers.pyr 5s zBaseRotatingHandler.__init__c CsHy$|j|r|jtjj||Wntk rB|j|YnXdS)N)shouldRollover doRolloverrr emit Exception handleError)rrecordrrrr?s  zBaseRotatingHandler.emitcCst|js|}n |j|}|S)N)callabler )rZ default_nameresultrrrrotation_filenameMs  z%BaseRotatingHandler.rotation_filenamecCs4t|js$tjj|r0tj||n |j||dS)N)rrospathexistsrename)rsourcedestrrrrotate`s  zBaseRotatingHandler.rotate)NF)__name__ __module__ __qualname__r rrr#rrrrr/s rc@s&eZdZd ddZddZd d ZdS) RotatingFileHandlerarNFcCs.|dkr d}tj|||||||_||_dS)Nrr()rr maxBytes backupCount)rrr r)r*r rrrrr zs zRotatingFileHandler.__init__cCs|jr|jjd|_|jdkrxtt|jdddD]^}|jd|j|f}|jd|j|df}tjj|r4tjj|rtj |tj ||q4W|j|jd}tjj|rtj ||j |j||j s|j |_dS)Nrz%s.%dz.1)streamcloser*ranger baseFilenamerrrremover r#r_open)riZsfndfnrrrrs$        zRotatingFileHandler.doRollovercCs|tjj|jr"tjj|j r"dS|jdkr6|j|_|jdkrxd|j|}|jj dd|jj t ||jkrxdSdS)NFrz%s T) rrrr0isfiler-r2r)formatseektelllen)rrmsgrrrrs   z"RotatingFileHandler.shouldRollover)r(rrNF)r$r%r&r rrrrrrr'us r'c@s6eZdZdddZdd Zd d Zd d ZddZdS)TimedRotatingFileHandlerhr+rNFc Cstj||d|||j|_||_||_||_|jdkrNd|_d|_d|_ n|jdkrld|_d|_d |_ n|jd krd|_d |_d |_ n|jd ks|jdkrd|_d|_d|_ n|jj dr.d|_t |jdkrt d|j|jddks|jddkrt d|jt |jd|_d|_d|_ nt d|jtj|j tj|_ |j||_|j}tjj|r~tj|t} n t tj} |j| |_dS) Nr(Sr+z%Y-%m-%d_%H-%M-%Sz-^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$Mrz%Y-%m-%d_%H-%Mz'^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}(\.\w+)?$Hz %Y-%m-%d_%Hz!^\d{4}-\d{2}-\d{2}_\d{2}(\.\w+)?$DMIDNIGHTrz%Y-%m-%dz^\d{4}-\d{2}-\d{2}(\.\w+)?$Wr5zHYou must specify a day for weekly rollover from 0 to 6 (0 is Monday): %s06z-Invalid day specified for weekly rollover: %sz'Invalid rollover interval specified: %siiiQiiQi: )rr upperwhenr*utcatTimeintervalsuffixextMatch startswithr: ValueErrorint dayOfWeekrecompileASCIIr0rrrstatrtimecomputeRollover rolloverAt) rrrHrKr*r rrIrJtrrrr sL        z!TimedRotatingFileHandler.__init__cCsd||j}|jdks"|jjdr`|jr4tj|}n tj|}|d}|d}|d}|d}|jdkrnt}n |jj d|jj d|jj }||d|d|} | dkr| t7} |d d }|| }|jjdr`|} | |j kr`| |j kr|j | } nd| |j d } || d} |js\|d} tj| d}| |kr\| sPd}nd }| |7} | }|S)NrBrCrrr+rDriiiQr,r,i) rKrHrNrIrVgmtime localtimerJ _MIDNIGHTZhourZminutesecondrQ)r currentTimerrYZ currentHourZ currentMinuteZ currentSecondZ currentDayZ rotate_tsrZdayZ daysToWait newRolloverAtdstNow dstAtRolloveraddendrrrrWsH           z(TimedRotatingFileHandler.computeRollovercCs@tjj|jr"tjj|j r"dSttj}||jkr|jjdrx|j rxtj| d } || krx|sld }nd}| |7} | |_dS) Nr+irhrrBrCr,r,ir,i)r-r.rPrVr_rXrKrIr^rr0ZstrftimerLrrrr1r#r*rprr2rWrHrN) rrbrerYZ timeTupleZdstThenrgr4srdrfrrrrtsH            $ z#TimedRotatingFileHandler.doRollover)r=r+rNFFN)r$r%r&r rWrrprrrrrr<s  9Ir<c@s.eZdZd ddZddZdd Zd d ZdS) WatchedFileHandlerr(NFcCs,tjj|||||d\|_|_|jdS)Nr+r,r,)r,r,)rr r devino _statstream)rrr r rrrrr s zWatchedFileHandler.__init__cCs0|jr,tj|jj}|t|t|_|_dS)N)r-rfstatfilenorrrsrt)rsresrrrruszWatchedFileHandler._statstreamc Csytj|j}Wntk r(d}YnX| sL|t|jksL|t|jkr|jdk r|jj |jj d|_|j |_|j dS)N) rrUr0FileNotFoundErrorrrsrrtr-flushr.r2ru)rrxrrrreopenIfNeededs  "    z!WatchedFileHandler.reopenIfNeededcCs|jtjj||dS)N)r{rr r)rrrrrrszWatchedFileHandler.emit)r(NF)r$r%r&r rur{rrrrrrrs rrc@sNeZdZddZdddZddZdd Zd d Zd d ZddZ ddZ dS) SocketHandlercCsZtjj|||_||_|dkr(||_n ||f|_d|_d|_d|_d|_ d|_ d|_ dS)NFg?g>@g@) rHandlerr hostportaddresssock closeOnError retryTime retryStartretryMax retryFactor)rr~rrrrr s  zSocketHandler.__init__r+c Csj|jdk rtj|j|d}nJtjtjtj}|j|y|j|jWntk rd|j YnX|S)N)timeout) rsocketZcreate_connectionrAF_UNIX SOCK_STREAMZ settimeoutconnectOSErrorr.)rrrrrr makeSockets  zSocketHandler.makeSocketc Cstj}|jdkrd}n ||jk}|ry|j|_d|_WnVtk r|jdkr^|j|_n"|j|j|_|j|jkr|j|_||j|_YnXdS)NT) rVrrrrrZ retryPeriodrr)rZnowZattemptrrr createSocket"s       zSocketHandler.createSocketc CsR|jdkr|j|jrNy|jj|Wn$tk rL|jjd|_YnXdS)N)rrsendallrr.)rrqrrrsend>s  zSocketHandler.sendcCsj|j}|r|j|}t|j}|j|d<d|d<d|d<|jddtj|d}tj dt |}||S)Nr;argsexc_infomessager+z>L) rr7dict__dict__Z getMessagepoppickledumpsstructZpackr:)rrZeiZdummydrqZslenrrr makePickleQs     zSocketHandler.makePicklecCs0|jr|jr|jjd|_ntjj||dS)N)rrr.rr}r)rrrrrrgs  zSocketHandler.handleErrorc Cs<y|j|}|j|Wntk r6|j|YnXdS)N)rrrr)rrrqrrrrus  zSocketHandler.emitc Cs@|jz(|j}|r"d|_|jtjj|Wd|jXdS)N)acquirerr.rr}release)rrrrrr.szSocketHandler.closeN)r+) r$r%r&r rrrrrrr.rrrrr|s  r|c@s$eZdZddZddZddZdS)DatagramHandlercCstj|||d|_dS)NF)r|r r)rr~rrrrr szDatagramHandler.__init__cCs*|jdkrtj}ntj}tj|tj}|S)N)rrrZAF_INET SOCK_DGRAM)rZfamilyrqrrrrs  zDatagramHandler.makeSocketcCs&|jdkr|j|jj||jdS)N)rrsendtor)rrqrrrrs zDatagramHandler.sendN)r$r%r&r rrrrrrrs  rc@seZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZdZdZdZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZeeee eeee eeeed Zeeeeee ee eeee eeeeeeeeedZ ddddddZ!de"fe dfdd Z#d!d"Z$d#d$Z%d%d&Z&d'd(Z'd)Z(d*Z)d+d,Z*dS)- SysLogHandlerrr+r5rZr[r\r]rD ) ZalertZcritcriticaldebugZemergerrerrorinfoZnoticeZpanicwarnwarning)ZauthZauthprivZcrondaemonZftpZkernZlprZmailZnewsZsecurityZsysloguserZuucpZlocal0Zlocal1Zlocal2Zlocal3Zlocal4Zlocal5Zlocal6Zlocal7rrrrr)DEBUGINFOWARNINGERRORCRITICALZ localhostNcCs0tjj|||_||_||_t|trTd|_y|j |Wnt k rPYnXnd|_|dkrht j }|\}}t j ||d|}|st dx|D]|}|\}}} } } d} } y(t j ||| } |t jkr| j| PWqt k r }z|} | dk r| jWYdd}~XqXqW| dk r | | |_ ||_dS)NTFrz!getaddrinfo returns an empty list)rr}r rfacilitysocktype isinstancestr unixsocket_connect_unixsocketrrrZ getaddrinforrr.)rrrrr~rZressresZafproto_Zsarrexcrrrr #sB      zSysLogHandler.__init__cCs|j}|dkrtj}tjtj||_y|jj|||_Wnxtk r|jj|jdk r`tj}tjtj||_y|jj|||_Wn tk r|jjYnXYnXdS)N)rrrrrrr.r)rrZ use_socktyperrrrYs&       z!SysLogHandler._connect_unixsocketcCs4t|tr|j|}t|tr(|j|}|d>|BS)NrZ)rrfacility_namespriority_names)rrZpriorityrrrencodePriorityqs     zSysLogHandler.encodePriorityc Cs2|jz|jjtjj|Wd|jXdS)N)rrr.rr}r)rrrrr.~s  zSysLogHandler.closecCs|jj|dS)Nr) priority_mapget)rZ levelNamerrr mapPriorityszSysLogHandler.mapPriorityTcCsy|j|}|jr|j|}|jr*|d7}d|j|j|j|j}|jd}|jd}||}|jry|j j |Wqt k r|j j |j |j|j j |YqXn*|jt jkr|j j||jn |j j|Wntk r|j|YnXdS)Nz<%d>zutf-8)r7ident append_nulrrrZ levelnameencoderrrrr.rrrrrrrr)rrr;Zpriorrrrs.        zSysLogHandler.emit)+r$r%r&Z LOG_EMERGZ LOG_ALERTZLOG_CRITZLOG_ERRZ LOG_WARNINGZ LOG_NOTICEZLOG_INFOZ LOG_DEBUGZLOG_KERNZLOG_USERZLOG_MAILZ LOG_DAEMONZLOG_AUTHZ LOG_SYSLOGZLOG_LPRZLOG_NEWSZLOG_UUCPZLOG_CRONZ LOG_AUTHPRIVZLOG_FTPZ LOG_LOCAL0Z LOG_LOCAL1Z LOG_LOCAL2Z LOG_LOCAL3Z LOG_LOCAL4Z LOG_LOCAL5Z LOG_LOCAL6Z LOG_LOCAL7rrrSYSLOG_UDP_PORTr rrr.rrrrrrrrrs5   rc@s&eZdZd ddZddZddZdS) SMTPHandlerN@cCstjj|t|ttfr(|\|_|_n|d|_|_t|ttfrR|\|_|_ nd|_||_ t|t rn|g}||_ ||_ ||_||_dS)N)rr}r rlisttuplemailhostmailportusernamepasswordfromaddrrtoaddrssubjectsecurer)rrrrr credentialsrrrrrr s  zSMTPHandler.__init__cCs|jS)N)r)rrrrr getSubjectszSMTPHandler.getSubjectc Csyddl}ddlm}ddl}|j}|s.|j}|j|j||jd}|}|j |d<dj |j |d<|j ||d<|j j|d<|j|j||jr|jdk r|j|j|j|j|j|j|j|j||jWntk r|j|YnXdS) Nr) EmailMessage)rZFrom,ZToZSubjectZDate)smtplibZ email.messagerZ email.utilsrZ SMTP_PORTZSMTPrrrrmrrZutilsr_Z set_contentr7rrZehloZstarttlsZloginrZ send_messagequitrr)rrrrZemailrZsmtpr;rrrrs0      zSMTPHandler.emit)NNr)r$r%r&r rrrrrrrs " rc@s>eZdZdddZddZddZd d Zd d Zd dZdS)NTEventLogHandlerN ApplicationcCstjj|yddl}ddl}||_||_|s`tjj |jj }tjj |d}tjj |dd}||_ ||_ |jj||||j|_tj|jtj|jtj|jtj|jtj|ji|_Wn"tk rtdd|_YnXdS)Nrzwin32service.pydzWThe Python Win32 extensions for NT (service, event logging) appear not to be available.)rr}r win32evtlogutil win32evtlogappname_welurrri__file__rmdllnamelogtypeZAddSourceToRegistryZEVENTLOG_ERROR_TYPEdeftyperZEVENTLOG_INFORMATION_TYPErrZEVENTLOG_WARNING_TYPErrtypemap ImportErrorprint)rrrrrrrrrr s* zNTEventLogHandler.__init__cCsdS)Nr+r)rrrrr getMessageID.szNTEventLogHandler.getMessageIDcCsdS)Nrr)rrrrrgetEventCategory8sz"NTEventLogHandler.getEventCategorycCs|jj|j|jS)N)rrlevelnor)rrrrr getEventTypeAs zNTEventLogHandler.getEventTypec Csn|jrjyD|j|}|j|}|j|}|j|}|jj|j||||gWntk rh|j|YnXdS)N) rrrrr7Z ReportEventrrr)rridcattyper;rrrrNs    zNTEventLogHandler.emitcCstjj|dS)N)rr}r.)rrrrr._s zNTEventLogHandler.close)Nr) r$r%r&r rrrrr.rrrrr s     rc@s&eZdZd ddZddZdd ZdS) HTTPHandlerGETFNcCsbtjj||j}|dkr$td| r:|dk r:td||_||_||_||_||_ ||_ dS)NrPOSTzmethod must be GET or POSTz3context parameter only makes sense with secure=True)rr) rr}r rGrOr~urlmethodrrcontext)rr~rrrrrrrrr qs zHTTPHandler.__init__cCs|jS)N)r)rrrrr mapLogRecordszHTTPHandler.mapLogRecordc CsxyPddl}ddl}|j}|jr4|jj||jd}n |jj|}|j}|j j |j |}|j dkr|j ddkrvd}nd}|d||f}|j|j ||j d} | dkr|d| }|j dkr|jd d |jd tt||jr$ddl} d |jjd } d| j| jjd} |jd| |j|j dkrH|j|jd |jWn tk rr|j|YnXdS)Nr)rr?&z%c%s:rz Content-typez!application/x-www-form-urlencodedzContent-lengthz%s:%szutf-8zBasic asciiZ Authorization)Z http.clientZ urllib.parser~rZclientZHTTPSConnectionrZHTTPConnectionrparseZ urlencoderrfindZ putrequestZ putheaderrr:rbase64rZ b64encodestripdecodeZ endheadersrZ getresponserr) rrZhttpZurllibr~r=rdatasepr3rrqrrrrs@        zHTTPHandler.emit)rFNN)r$r%r&r rrrrrrrls rc@s4eZdZddZddZddZddZd d Zd S) BufferingHandlercCstjj|||_g|_dS)N)rr}r capacitybuffer)rr rrrr s zBufferingHandler.__init__cCst|j|jkS)N)r:r r )rrrrr shouldFlushszBufferingHandler.shouldFlushcCs"|jj||j|r|jdS)N)r rlr rz)rrrrrrs  zBufferingHandler.emitc Cs"|jz g|_Wd|jXdS)N)rr r)rrrrrzs zBufferingHandler.flushc Cs z |jWdtjj|XdS)N)rzrr}r.)rrrrr.s zBufferingHandler.closeN)r$r%r&r r rrzr.rrrrr s    r c@s>eZdZejddfddZddZddZd d Zd d Z dS) MemoryHandlerNTcCs"tj||||_||_||_dS)N)r r flushLeveltarget flushOnClose)rr rrrrrrr s zMemoryHandler.__init__cCst|j|jkp|j|jkS)N)r:r r rr)rrrrrr szMemoryHandler.shouldFlushcCs ||_dS)N)r)rrrrr setTargetszMemoryHandler.setTargetc CsD|jz,|jr2x|jD]}|jj|qWg|_Wd|jXdS)N)rrr handler)rrrrrrzs  zMemoryHandler.flushcCsBz|jr|jWd|jzd|_tj|Wd|jXXdS)N)rrzrrr r.r)rrrrr.,s zMemoryHandler.close) r$r%r&rrr r rrzr.rrrrrs  rc@s,eZdZddZddZddZddZd S) QueueHandlercCstjj|||_dS)N)rr}r queue)rrrrrr Hs zQueueHandler.__init__cCs|jj|dS)N)r put_nowait)rrrrrenqueueOszQueueHandler.enqueuecCs"|j||j|_d|_d|_|S)N)r7rr;rr)rrrrrprepareYs  zQueueHandler.preparec Cs8y|j|j|Wntk r2|j|YnXdS)N)rrrr)rrrrrrrszQueueHandler.emitN)r$r%r&r rrrrrrrr=s  rc@sVeZdZdZddddZddZdd Zd d Zd d ZddZ ddZ ddZ dS) QueueListenerNF)respect_handler_levelcGs||_||_d|_||_dS)N)rhandlers_threadr)rrrrrrrr szQueueListener.__init__cCs |jj|S)N)rr)rblockrrrdequeueszQueueListener.dequeuecCs&tj|jd|_}d|_|jdS)N)rT) threadingZThread_monitorrrstart)rrYrrrr!szQueueListener.startcCs|S)Nr)rrrrrrszQueueListener.preparecCsD|j|}x4|jD]*}|js"d}n |j|jk}|r|j|qWdS)NT)rrrrlevelr)rrZhandlerZprocessrrrrs   zQueueListener.handlec Csd|j}t|d}xNy0|jd}||jkr*P|j||r@|jWqtjk rZPYqXqWdS)N task_doneT)rhasattrr _sentinelrr#ZEmpty)rqZ has_task_donerrrrr s     zQueueListener._monitorcCs|jj|jdS)N)rrr%)rrrrenqueue_sentinelszQueueListener.enqueue_sentinelcCs|j|jjd|_dS)N)r'rrm)rrrrstops zQueueListener.stop) r$r%r&r%r rr!rrr r'r(rrrrr~s     riiQ)&rrrrrrVrRrUrrrrrrZDEFAULT_TCP_LOGGING_PORTZDEFAULT_UDP_LOGGING_PORTZDEFAULT_HTTP_LOGGING_PORTZDEFAULT_SOAP_LOGGING_PORTrZSYSLOG_TCP_PORTr`r rr'r<rrr}r|rrrrrr rrobjectrrrrrs@8  FOcE(*PbO9I@