o
    åÅe6A  ã                   @   sÊ   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	d dl
Z	d dlZ	ddddœZddddœZejejejejejejdœZg d¢Zd	ad
add„ ZG dd„ deƒZdd„ Zdd„ ZdS )é    NÚinfo)ÚconsoleÚfileÚsyslogz&%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%mz*%D{%Y-%m-%dT%H:%M:%S.###Z}|%05N|%c%T|%p|%mzovs|%05N|%c%T|%p|%m)Údbgr   ÚwarnÚerrÚemerÚoff)ÚauthÚauthprivÚcronÚdaemonÚftpÚkernÚlprÚmailÚnewsr   ÚuserÚuucpÚlocal0Úlocal1Úlocal2Úlocal3Úlocal4Úlocal5Úlocal6Úlocal7r   Ú c                 C   s   t  |  ¡ ¡S ©N)ÚLEVELSÚgetÚlower)Ú	level_str© r$   ú*/usr/lib/python3/dist-packages/ovs/vlog.pyÚ	get_level2   s   r&   c                   @   sD  e Zd ZdZdZdZi ZdZdZe	Z
dd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zed>d&d'„ƒZed(d)„ ƒZed*d+„ ƒZed>d,d-„ƒZ ed.d/„ ƒZ!ed0d1„ ƒZ"ed2d3„ ƒZ#ed4d5„ ƒZ$ed6d7„ ƒZ%ed8d9„ ƒZ&ed:d;„ ƒZ'ed<d=„ ƒZ(dS )?ÚVlogFr   Nc                 C   s6   t jrJ ‚| ¡ | _|t jvrt ¡ t j| j< dS dS )zÙCreates a new Vlog object representing a module called 'name'.  The
        created Vlog object will do nothing until the Vlog.init() static method
        is called.  Once called, no more Vlog objects may be created.N)r'   Ú_Vlog__initedr"   ÚnameÚ
_Vlog__mflÚDESTINATIONSÚcopy)Úselfr)   r$   r$   r%   Ú__init__?   s
   


ÿzVlog.__init__c           	      K   sŽ   t jsd S t | ¡ tj¡}t j}t  jd7  _t j| j	  
¡ D ]$\}}t |tj¡}||krD|  ||||¡}t |¡j||fi |¤Ž q d S )Né   )r'   r(   r    r!   r"   ÚloggingÚDEBUGÚ_Vlog__msg_numr*   r)   ÚitemsÚCRITICALÚ_build_messageÚ	getLoggerÚlog)	r-   ÚlevelÚmessageÚkwargsÚ	level_numÚmsg_numÚfÚf_levelÚmsgr$   r$   r%   Ú__logI   s   €üz
Vlog.__logc                 C   s˜  | j | }|}|  |¡}t d|¡}|D ]³}d|v r$|  ||tjj¡}qd|v r1|  ||| j¡}qd|v r=|  |||¡}qd|v rK|  ||t	|ƒ¡}qd|v rWt 
|d|¡}qd|v re|  ||| ¡ ¡}qd	|v ru|  ||t	t ¡ ƒ¡ qd
|v r’tj ¡ }	|	| j }
|
jd }|  ||t	|ƒ¡}qd|v rªt ¡  ¡ }|dkr¢d}|  |||¡}qd|v rÇt ¡  ¡ }|dks¾d |¡}nd}|  |||¡}q| ¡ S )Nz(%-?[0]?[0-9]?[AcmNnpPrtT])ÚAÚcÚmÚNÚnÚ
ÚpÚPÚréè  ÚtÚ
MainThreadÚmainÚTz({})r   )Ú_Vlog__log_patternsÚ_format_timeÚreÚfindallÚ_format_fieldÚovsÚutilÚPROGRAM_NAMEr)   ÚstrÚsubÚupperÚosÚgetpidÚdatetimeÚutcnowÚ_Vlog__start_timeÚmicrosecondsÚ	threadingÚcurrentThreadÚgetNameÚformatÚstrip)r-   r9   Údestinationr8   r<   ÚpatternÚtmpÚmatchesrC   ÚnowÚdeltaÚmsÚ
subprogramr$   r$   r%   r5   W   sJ   




€zVlog._build_messagec                 C   s|   t  d¡}| |¡}| d¡s|dkr| t|ƒd ¡}| d¡r3t| d¡ƒ}t|ƒ|k r3| |¡}t  || dd¡|¡S )Nz^%(0)?([1-9])?r/   r   é   ú\z\\)	rQ   ÚcompileÚmatchÚgroupÚcenterÚlenÚintrX   Úreplace)r-   rg   rp   ru   Ú
formattingrh   Ú	min_widthr$   r$   r%   rS   €   s   



zVlog._format_fieldc                 C   sò   t  d¡}| |¡}|d u r|S | d¡dkrtj ¡ }n| d¡dkr)tj ¡ }| d¡retj || d¡¡}z#t	t  d| d¡¡ d¡ƒ}d	j
t|jd
 ƒ|d}t  d||¡}W n tyd   Y nw tj |jdd¡}|  || d¡|¡S )Nz(%(0?[1-9]?[dD])(\{(.*)\})?)rm   ÚdÚDé   é   z#+r   z{0:0>{i}.{i}}rJ   )Úi)Úmicrosecondr/   )rQ   ro   Úsearchrq   r\   ri   r]   ÚdateÚstrftimers   rc   rW   r}   rX   ÚAttributeErrorÚ	isoformatru   rS   )r-   rg   Ú
date_regexrp   ri   Útimer|   Úmsecr$   r$   r%   rP      s&   



ÿzVlog._format_timec                 K   ó   | j d|fi |¤Ž d S ©NÚEMER©Ú
_Vlog__log©r-   r9   r:   r$   r$   r%   r	   ¨   ó   z	Vlog.emerc                 K   r†   ©NÚERRr‰   r‹   r$   r$   r%   r   «   rŒ   zVlog.errc                 K   r†   ©NÚWARNr‰   r‹   r$   r$   r%   r   ®   rŒ   z	Vlog.warnc                 K   r†   ©NÚINFOr‰   r‹   r$   r$   r%   r   ±   rŒ   z	Vlog.infoc                 K   r†   ©NÚDBGr‰   r‹   r$   r$   r%   r   ´   rŒ   zVlog.dbgc                 C   sL   t  | ¡ tj¡}tj| j  ¡ D ]\}}t  |tj	¡}||kr# dS qdS )NTF)
r    r!   r"   r0   r1   r'   r*   r)   r3   r4   )r-   r8   r=   r>   r$   r$   r%   Ú__is_enabled·   s   ÿzVlog.__is_enabledc                 C   ó
   |   d¡S r‡   ©Ú_Vlog__is_enabled©r-   r$   r$   r%   Úemer_is_enabled¿   ó   
zVlog.emer_is_enabledc                 C   r–   r   r—   r™   r$   r$   r%   Úerr_is_enabledÂ   r›   zVlog.err_is_enabledc                 C   r–   r   r—   r™   r$   r$   r%   Úwarn_is_enabledÅ   r›   zVlog.warn_is_enabledc                 C   r–   r‘   r—   r™   r$   r$   r%   Úinfo_is_enabledÈ   r›   zVlog.info_is_enabledc                 C   r–   r“   r—   r™   r$   r$   r%   Údbg_is_enabledË   r›   zVlog.dbg_is_enabledc                 C   s   | j |dd dS )zYLogs 'message' at ERR log level.  Includes a backtrace when in
        exception context.T)Úexc_infoN)r   )r-   r9   r$   r$   r%   Ú	exceptionÎ   s   zVlog.exceptionc              
   C   sH  t jrdS dt _tj ¡ t _dt_| t _tD ]I}t 	|¡}| 
tj¡ z-|dkr2| t tj¡¡ n|dkr;t  ¡  n|dkrOt jrOt t j¡t _| t j¡ W q ttjfy_   d|_Y qw tj ddd	d	t jd¡ tj d
dd	d	t jd¡ ztj}W n ty‰   tj}Y nw tj ddd|t jd¡ tj ddd	d	t j d¡ dS )zÍIntializes the Vlog module.  Causes Vlog to write to 'log_file' if
        not None.  Should be called after all Vlog objects have been created.
        No logging will occur until this function is called.NTFr   r   r   zvlog/reopenr   r   z
vlog/closezvlog/setÚspecr/   z	vlog/list)!r'   r(   r\   r]   r^   r0   ÚraiseExceptionsÚ_Vlog__log_filer+   r6   ÚsetLevelr1   Ú
addHandlerÚStreamHandlerÚsysÚstderrÚadd_syslog_handlerÚFileHandlerÚ_Vlog__file_handlerÚIOErrorÚsocketÚerrorÚdisabledrT   ÚunixctlÚcommand_registerÚ_unixctl_vlog_reopenÚ_unixctl_vlog_closeÚmaxintr   ÚmaxsizeÚ_unixctl_vlog_setÚ_unixctl_vlog_list)Úlog_filer=   ÚloggerÚmaxsize_intr$   r$   r%   ÚinitÓ   sL   

€
ÿÿÿ

ÿÿÿz	Vlog.initc                 C   s®   |   ¡ } |  ¡ }|  ¡ }|dkr|tvrdS | dkr!| tjvr!dS |tvr'dS | dkr3ttj ¡ ƒ}n| g}|dkrAtt ¡ ƒ}n|g}|D ]}|D ]	}|tj| |< qJqFdS )a   Sets the log level of the 'module'-'destination' tuple to 'level'.
        All three arguments are strings which are interpreted the same as
        arguments to the --verbose flag.  Should be called after all Vlog
        objects have already been created.ÚanyN)r"   r+   r'   r*   r    ÚlistÚkeys)Úmodulere   r8   ÚmodulesÚdestinationsrC   r=   r$   r$   r%   Ú	set_levelÿ   s(   ÿÿzVlog.set_levelc                 C   s   |   ¡ } |tj| < dS )z8 Sets the log pattern of the 'destination' to 'pattern' N)r"   r'   rO   )re   rf   r$   r$   r%   Úset_pattern!  s   zVlog.set_patternc                 C   sx   | r| t kr
tr
d S t d¡}tj d¡dkrd|_d S | d u r"t } tjj	d| d}tr1| 
t¡ |a| a | t¡ d S )Nr   ÚOVS_SYSLOG_METHODÚnullTz/dev/log)ÚaddressÚfacility)Úsyslog_facilityÚsyslog_handlerr0   r6   rZ   Úenvironr!   r°   ÚhandlersÚSysLogHandlerÚremoveHandlerr¦   )rÈ   rº   Únew_handlerr$   r$   r%   rª   '  s"   
ÿ

zVlog.add_syslog_handlerc              	      sŒ  d }d }d }t  d| ¡‰ ˆ d dkrLz/ˆ d tv r;ˆ d r;‡ fdd„tdtˆ ƒƒD ƒ}d |¡}t ˆ d |¡ W d S d	ˆ d  W S  tyK   Y d
S w ˆ d dkr~ˆ d t	v rxz
t 
ˆ d ¡ W d S  ttjfyw   t d¡}d|_Y d S w dˆ d  S dd„ ˆ D ƒD ]1}|dkrŒq…|tv r˜|r• dS |}q…|tv r¤|r¡ dS |}q…|tjv r±|r® dS |}q…d|   S t |p¼d|p¿d|pÂd¡ d S )Nz[ :]r   rf   r/   rm   c                    s   g | ]}ˆ | ‘qS r$   r$   )Ú.0r|   ©Úwordsr$   r%   Ú
<listcomp>P  ó    z/Vlog.set_levels_from_string.<locals>.<listcomp>r   zDestination %s does not existz-Please supply a valid pattern and destinationÚFACILITYr   TzFacility %s is invalidc                 S   s   g | ]}|  ¡ ‘qS r$   )r"   )rÐ   Úwr$   r$   r%   rÓ   c  rÔ   r½   z$cannot specify multiple destinationszcannot specify multiple levelszcannot specify multiple modulesz%no destination, level, or module "%s")rQ   Úsplitr+   Úrangers   Újoinr'   rÄ   Ú
IndexErrorÚ
FACILITIESrª   r­   r®   r¯   r0   r6   r°   r    r*   rÃ   )ÚsrÀ   r8   re   Úsegmentsrf   rº   Úwordr$   rÑ   r%   Úset_levels_from_stringF  sV   
ÿý
ý
zVlog.set_levels_from_stringc                  C   s,   ddg} |   tdd„ tjD ƒƒ¡ d | ¡S )Nz+                 console    syslog    file
z-                 -------    ------    ------
c                 S   s:   g | ]}d |t j| d t j| d t j| d f ‘qS )z%-16s  %4s       %4s       %4s
r   r   r   )r'   r*   )rÐ   rC   r$   r$   r%   rÓ   {  s    üýÿz#Vlog.get_levels.<locals>.<listcomp>r   )ÚextendÚsortedr'   r*   rÙ   )Úlinesr$   r$   r%   Ú
get_levelsw  s   ÿ
ü
zVlog.get_levelsc                  C   s>   t jrt d¡} |  t j¡ t t j¡t _|  t j¡ dS dS )z§Closes and then attempts to re-open the current log file.  (This is
        useful just after log rotation, to ensure that the new log file starts
        being used.)r   N)r'   r¤   r0   r6   rÎ   r¬   r«   r¦   ©rº   r$   r$   r%   Úreopen_log_file‚  s   
üzVlog.reopen_log_filec                  C   s.   t jrt d¡} |  t j¡ t j ¡  dS dS )zCloses the current log file. (This is useful on Windows, to ensure
        that a reference to the file is not kept by the daemon in case of
        detach.)r   N)r'   r¤   r0   r6   rÎ   r¬   Úcloserä   r$   r$   r%   Úclose_log_fileŽ  s
   
ýzVlog.close_log_filec                 C   s*   t jrt  ¡  |  d ¡ d S |  d¡ d S )NzLogging to file not configured)r'   r¤   rå   Úreply©ÚconnÚunused_argvÚ
unused_auxr$   r$   r%   r³   ˜  s   zVlog._unixctl_vlog_reopenc                 C   s>   t jrtjdkrt d¡}| t j¡ nt  ¡  |  	d ¡ d S )NÚwin32r   )
r'   r¤   r¨   Úplatformr0   r6   rÎ   r¬   rç   rè   )rê   rë   rì   rº   r$   r$   r%   r´      s   

zVlog._unixctl_vlog_closec                 C   s6   |D ]}t  |¡}|r|  |¡  d S q|  d ¡ d S r   )r'   rß   rè   )rê   Úargvrì   Úargr?   r$   r$   r%   r·   ª  s   

þzVlog._unixctl_vlog_setc                 C   s   |   t ¡ ¡ d S r   )rè   r'   rã   ré   r$   r$   r%   r¸   ³  s   zVlog._unixctl_vlog_listr   ))Ú__name__Ú
__module__Ú__qualname__r(   r2   r^   r*   r¤   r¬   ÚPATTERNSrO   r.   rŠ   r5   rS   rP   r	   r   r   r   r   r˜   rš   rœ   r   rž   rŸ   r¡   Ústaticmethodr¼   rÃ   rÄ   rª   rß   rã   rå   rç   r³   r´   r·   r¸   r$   r$   r$   r%   r'   6   sb    
)+
!

0



	

	
r'   c                 C   s4   | j dd}|jddddd |jdd	d
dd dS )z‘Adds vlog related options to 'parser', an ArgumentParser object.  The
    resulting arguments parsed by 'parser' should be passed to handle_args.zLogging Options)Útitlez
--log-fileú?ÚdefaultzLEnables logging to a file.  Default log file is used if LOG_FILE is omitted.)ÚnargsÚconstÚhelpz-vz	--verboseÚ*z;Sets logging levels, see ovs-vswitchd(8).  Defaults to dbg.)rù   rû   N)Úadd_argument_groupÚadd_argument)Úparserrq   r$   r$   r%   Úadd_args¸  s   
ÿ

ÿr   c                 C   s„   | j }|dkrdtjjtjjf }| jdu rg | _n	| jg kr#dg| _| jD ]}t |¡}|r:tj 	dd||f ¡ q&t 
|¡ dS )z¹ Handles command line arguments ('args') parsed by an ArgumentParser.
    The ArgumentParser should have been primed by add_args().  Also takes care
    of initializing the Vlog module.rø   z	%s/%s.logNzany:any:dbgr   zprocessing "%s": %s)r¹   rT   ÚdirsÚLOGDIRrU   rV   Úverboser'   rß   Ú	ovs_fatalr¼   )Úargsr¹   r  r?   r$   r$   r%   Úhandle_argsÅ  s   



€r  )r\   r0   Úlogging.handlersrZ   rQ   r®   r¨   r`   Úovs.dirsrT   Úovs.unixctlÚovs.utilr+   rô   r1   r’   ÚWARNINGÚERRORr4   r    rÛ   rÉ   rÊ   r&   Úobjectr'   r   r  r$   r$   r$   r%   Ú<module>   sB   ýú   