
    
hhtL                         d Z ddlmZmZmZ ddlmZ ddlZddlZddl	Z		 ddl
mZ n# e$ r dZY nw xY wddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ d Z G d deee          Z G d deee          ZdS )zProvides managed logging.    )LOGLEVELLOGMASKLOGFACILITY)	MythErrorN)journal)version_infostdoutargv)datetime)allocate_lock)StringIO)
format_excc                      d S N )argskwargss     0/usr/lib/python3/dist-packages/MythTV/logging.py
_donothingr      s    D    c                   ,    e Zd ZddZd ZddZddZdS )DummyLoggerNc                     d S r   r   selfmoduledbs      r   __init__zDummyLogger.__init__         r   c                     d S r   r   r   masks     r   logTBzDummyLogger.logTB   r   r   c                     d S r   r   r   r"   levelmessagedetails        r   logzDummyLogger.log   r   r   c                     d S r   r   r%   s        r   __call__zDummyLogger.__call__   r   r   )NNr   )__name__
__module____qualname__r   r#   r)   r+   r   r   r   r   r      s=        2222::::??????r   r   c                       e Zd ZdZdZed             Zed             Zed             Zed             Z	ed             Z
ed             Zd	 Z fd
ZddZed             Zed             Zed             Zed             Zed d            Zeej        fd            Zed             Zed!d            Zd Zd Zd!dZd Zd Zd Zd Zd!dZ  xZ!S )"MythLoga  
    MythLog(module='pythonbindings', lstr=None, lbit=None,                     db=None) -> logging object

    'module' defines the source of the message in the logs
    'lstr' and 'lbit' define the message filter
        'lbit' takes a bitwise value
        'lstr' takes a string in the normal '-v level' form
        default is set to 'important,general'

    The filter level is global values, shared between all logging instances.
    The logging object is callable, and implements the MythLog.log() method.
    a  Verbose debug levels.
 Accepts any combination (separated by comma) of:

 "  all          "  -  ALL available debug output 
 "  most         "  -  Most debug (nodatabase,notimestamp,noextra) 
 "  important    "  -  Errors or other very important messages 
 "  general      "  -  General info 
 "  record       "  -  Recording related messages 
 "  playback     "  -  Playback related messages 
 "  channel      "  -  Channel related messages 
 "  osd          "  -  On-Screen Display related messages 
 "  file         "  -  File and AutoExpire related messages 
 "  schedule     "  -  Scheduling related messages 
 "  network      "  -  Network protocol related messages 
 "  commflag     "  -  Commercial detection related messages
 "  audio        "  -  Audio related messages 
 "  libav        "  -  Enables libav debugging 
 "  jobqueue     "  -  JobQueue related messages 
 "  siparser     "  -  Siparser related messages 
 "  eit          "  -  EIT related messages 
 "  vbi          "  -  VBI related messages 
 "  database     "  -  Display all SQL commands executed 
 "  dsmcc        "  -  DSMCC carousel related messages 
 "  mheg         "  -  MHEG debugging messages 
 "  upnp         "  -  upnp debugging messages 
 "  socket       "  -  socket debugging messages 
 "  xmltv        "  -  xmltv output and related messages 
 "  dvbcam       "  -  DVB CAM debugging messages 
 "  media        "  -  Media Manager debugging messages 
 "  idle         "  -  System idle messages 
 "  channelscan  "  -  Channel Scanning messages 
 "  extra        "  -  More detailed messages in selected levels 
 "  timestamp    "  -  Conditional data driven messages 
 "  none         "  -  NO debug output 
 
 The default for this program appears to be: '-v  "important,general" '

 Most options are additive except for none, all, and important.
 These three are semi-exclusive and take precedence over any
 prior options given.  You can however use something like
 '-v none,jobqueue' to get only JobQueue related messages
 and override the default verbosity level.
 
 The additive options may also be subtracted from 'all' by 
 prefixing them with 'no', so you may use '-v all,nodatabase'
 to view all but database debug messages.
 
 Some debug levels may not apply to this program.
c                 2   t          t                    | _        t          j        | _        t          j        | _        t          | _
        | j        | _        d| _        d| _        d | _        d| _        t#                      | _        |                                  d S )Nr   F)classmethodr   _initloggerr   GENERAL_MASKr   INFO_LEVELr	   _LOGFILE_logfile	_logwrite_QUIET_DBLOG_SYSLOG_JOURNALLOGr   _lock_parseinputclss    r   r3   zMythLog._initlogger`   sp    %j11O	]


!OO	r   c                 4   t          t                    }t          |           d}	 	 t          |          }|dk    r| xj        dz  c_        n!|dk    r	d| _        n|dk    r	d| _        n|dk    r#|                     t          |                     n|d	k    r#|                     t          |                     n|d
k    r(|                     t          |                     |dz  }n|dk    r(|                     t          |                     |dz  }nU|dk    r(| 	                    t          |                     |dz  }n'|dk    r| 
                                 |dz  }n|dk    rnJn# t          $ r Y nw xY w|dk    rt          d          d S )Nr   T--quiet   	--nodblogF--enable-dblog
--loglevel	--verbose	--logfile	--logpath--syslog--systemd-journalz--zjError:  These logging options are mutually exclusive: 'logfile', 'logpath', 'syslog' or 'systemd-journal'!)iterr
   nextr;   r<   	_setlevel_setmask_setfile_setpath
_setsyslog_setjournallogStopIterationr   )rB   r   nargs       r   r@   zMythLog._parseinputn   s   DzzT


	4jj)##JJ!OJJJK''!&CJJ,,,!%CJJL((MM$t**----K''LLd,,,,K''LLd,,,FAAK''LLd,,,FAAJ&&NN4::...FAA///&&(((FAAD[[32  	 	 	D	 EE S T T T Es   EE3 3
F ?F c                 p   | j                                         \  }}d}|j        r|j        | _        |j        rd| _        |j        r|                     |j                   |j        r| 	                    |j                   |j
        r|                     |j
                   |dz  }|j        r|                     |j                   |dz  }|j        r|                     |j                   |dz  }|j        r|                                  |dz  }|dk    r| j                             d           d S d S Nr   TrE   zbThese logging options are mutually exclusive: 'logfile', 'logpath', 'syslog' or 'systemd-journal'!_parser
parse_argsquietr;   dblogr<   loglevelrP   verboserQ   logfilerR   logpathrS   syslogrT   
journallogrU   error)rB   optsr   rW   s       r   _optparseinputzMythLog._optparseinput   sW   [++--
d: 	$CJ: 	CJ= 	)MM$-(((< 	'LL&&&< 	LL&&&FA< 	LL&&&FA; 	NN4;'''FA? 	   FAEEK U V V V V V Er   c                 j   | j                                         }d}|j        r|j        | _        |j        rd| _        |j        r|                     |j                   |j        r| 	                    |j                   |j
        r|                     |j
                   |dz  }|j        r|                     |j                   |dz  }|j        r|                     |j                   |dz  }|j        r|                                  |dz  }|dk    r| j                             d           d S d S rZ   r[   )rB   rg   rW   s      r   _argparseinputzMythLog._argparseinput   sS   {%%'': 	$CJ: 	CJ= 	)MM$-(((< 	'LL&&&< 	LL&&&FA< 	LL&&&FA; 	NN4;'''FA? 	   FAEEK U V V V V V Er   c                    || _         | j        | _        |                    dddd           |                    dddd	           |                    d
ddd           |                    ddddd           |                    ddddd           |                    ddddd           |                    ddddd           |                    ddddd           |                    ddd d!           d S )"NrD   countr^   =Run quiet. One use squelches terminal, two stops all logging.actiondesthelprF   
store_truenodblog>Prevent logging to the database (legacy: disabled by default).rG   r_   Enable logging to the database.rH   stringstorer`   4Specify log verbosity, using standard syslog levels.)typero   rp   rq   rI   ra   9Specify log mask, deciding what areas are allowed to log.rJ   rb   "Specify file to log all output to.rK   rc   DSpecify directory to log to, filename will be automatically decided.rL   rd   "Specify syslog facility to log to.rM   re   "Specify systemd-journal to log to.)r\   rh   r@   
add_option)rB   parsers     r   loadOptParsezMythLog.loadOptParse   s   ,)G'P 	 	R 	R 	R+lQ 	 	S 	S 	S*<g2 	 	4 	4 	4,XgJG 	 	I 	I 	I+HW9L 	 	N 	N 	N+HW95 	 	7 	7 	7+HW9W 	 	Y 	Y 	Y*8G(5 	 	7 	7 	7-l5 	 	7 	7 	7 	7 	7r   c                    ddl m}  G d d|          }|| _        | j        | _        |                    d|ddd           |                    d	d
dd           |                    dd
dd           |                    dddd           |                    dddd           |                    dddd           |                    dddd           |                    dddd            |                    d!d
d"d#           d S )$Nr   )Actionc                       e Zd ZddZdS )#MythLog.loadArgParse.<locals>.CountNc                 p    t          || j                  }|d}n|dz  }t          || j        |           d S )NrE   )getattrrp   setattr)r   r   	namespacevaluesoption_strings        r   r+   z,MythLog.loadArgParse.<locals>.Count.__call__   sB     DI66>FFaKF	49f55555r   r   )r,   r-   r.   r+   r   r   r   Countr      s(        6 6 6 6 6 6r   r   rD   r^   rm   )ro   nargsrp   rq   rF   rr   rs   rt   rn   rG   r_   ru   rH   rw   r`   rx   rI   ra   rz   rJ   rb   r{   rK   rc   r|   rL   rd   r}   rM   re   r~   )argparser   r\   rj   r@   add_argument)rB   r   r   r   s       r   loadArgParsezMythLog.loadArgParse   s   ######	6 	6 	6 	6 	6V 	6 	6 	6 ,Ie17P 	 	R 	R 	RK9Q 	 	S 	S 	S,\2 	 	4 	4 	4LzG 	 	I 	I 	IKiL 	 	N 	N 	NKi5 	 	7 	7 	7KiW 	 	Y 	Y 	YJwX5 	 	7 	7 	7/<5 	 	7 	7 	7 	7 	7r   c                 
   dt          | j                                      d          d                             d          d         d| j        dt	          | j                  dt          t          |                     d		S )
N<'rE   .z 'z','z' at >)str	__class__splitr   binr5   hexidr   s    r   __repr__zMythLog.__repr__  sp     T^$$**3//288==bAAAc$*oooos2d88}}}}> 	>r   c                 |    |                                   t          t          |                               |           S r   )r3   superr0   __new__)rB   r   r   r   s      r   r   zMythLog.__new__
  s3     	Wc""**3///r   pythonbindingsNc                 "    || _         || _        d S r   )r   r   r   s      r   r   zMythLog.__init__  s    r   c                     |                                   	 t          |          }|| _        d S #  |dvrY d S t          | |                                          | _        Y d S xY w)N)
anyemergalertcriterrwarninginfonoticedebugunknown)r3   intr7   r   upper)rB   r&   s     r   rP   zMythLog._setlevel  sq    	5JJECJJJ	5 N N N ekkmm44CJJJJs   . A!'A!c                     |                                   	 t          |          | _        dS #  |                     |          | _        Y dS xY w)zManually set loglevel.N)r3   r   r5   
_parsemask)rB   r"   s     r   rQ   zMythLog._setmask   sL     		-D		CIII	-t,,CIIIIs	   , Ac                     |                                   |                     t          j        |dd                     dS )z'Redirect log output to a specific file.wzutf-8)encodingN)r3   _setfileobjectcodecsopen)rB   filenames     r   rR   zMythLog._setfile)  s?     	6;xwGGGHHHHHr   c                 p   |                                   |                     t          j                            |d                    t          d                             d          d         t          j	                    
                    d          t          j                                                   d S )Nz{0}.{1}.{2}.logr   /r   z%Y%m%d%H%M%S)r3   rR   ospathjoinformatr
   r   r   nowstrftimegetpid)rB   filepaths     r   rS   zMythLog._setpath/  s    RW\\(,=#VDGMM#$6$6r$:$,LNN$;$;N$K$K$&IKK1 12 2 	3 	3 	3 	3 	3r   Tc                 &   |                                   | j                                        dk    r|r| j                                         || _        | j        | _        | j        rd| _        t          j                     | j	        r	d| _	        dS dS )z.Redirect log output to an opened file pointer.rE   NF)
r3   r8   filenocloser9   r:   r=   rd   closelogr>   )rB   
fileobjectr   s      r   r   zMythLog._setfileobject7  s     	L!!Q&&E&L   !; 	CKO? 	$#COOO	$ 	$r   c                 ^   |                                   	 t          |          }t          t                    D ]'}d|v rt	          t          |          |k    rd|z   } n(t          d          nh# t          $ r[ |                    d          sd|                                z   }t          t          |dd                    st          d          Y nw xY w|| _
        t          d         }d|v r|                    dd          d         }t          j        |t          j        t          j        z  t	          t          |                     | j        | _        | j        r=| j                                        dk    r| j                                         d | _        | j        r	d| _        d S d S )	N_LOG_zInvalid syslog facility   r   r   rE   F)r3   r   dirr   r   r   
ValueError
startswithr   hasattrr=   r
   rsplitrd   openlog
LOG_NDELAYLOG_PID
_logsyslogr:   r8   r   r   r>   )rB   facilityfacapplications       r   rT   zMythLog._setsyslogF  s   	;8}}H;'' ; ;#::;,,88%czHE 9   9:::   	; 	; 	;&&v.. 3!(.."2"22;55 ; 9:::; ;	; 1g+%,,S!44Q7K{F$5fn$D$+FH$=$=	? 	? 	?< 	 |""$$))""$$$CL? 	$#COOO	$ 	$s   AA1 1A"CCc                 N   |                                   t          r|d| _        | j        | _        | j        r=| j                                        dk    r| j                                         d | _        | j        rd | _        t          j
                     d S d S t          d          )NTrE   z]Error: Python module 'systemd.journal' not available! Please install 'python-systemd' module.)r3   r   r>   _logjournallogr:   r8   r   r   r=   rd   r   r   rA   s    r   rU   zMythLog._setjournallogi  s     	G"CO.CM| $<&&((A--L&&(((#{ ""!!!!!" "  F G G Gr   c           	         d}|r| j         }|                    d          D ]}	 |dv r#t          | |                                          }n||v r&|t          | |                                          z  }n]t	          |          dk    rJ|dd         dk    r<|dd          |v r0||t          | |dd                                                    z  z  }# t
          $ r Y w xY w|S |                                  g }t          |          D ],\  }}| j        dt          z  z  r|
                    |           -d                    |          S )N)	importantgeneralrecordplaybackchannelosdfileschedulenetworkcommflagaudiolibavjobqueuesiparsereitvbidatabasedsmccmhegupnpsocketxmltvdvbcammediaidlechannelscanextra	timestamp,)allmostnone   r   no)NONEr   r   r   lenAttributeErrorr3   	enumerater5   lappendr   )rB   mstrbwlistr"   mvs         r   r   zMythLog._parsemask{  sf   7
  	"8DZZ__  111&sAGGII66fQWWYY 7 77Q!QqS6T>> u $WS!ABB%++---H-H(H H%   DKOOD (( # #!9QT> #KKNNN88D>>!s   B.C
C C c                 N    t          j                                        d          S )Nz%Y-%m-%d %H:%M:%S.%f)r   r   r   r   s    r   timezMythLog.time  s    8<>>223IJJJr   c                 V    |                      || j        t                                 dS )z
        MythLog.logTB(mask) -> None

        'mask' sets the bitwise log mask, to be matched against the log
                    filter. If any bits match true, the message will be logged.
            This will log the latest traceback.
        N)r)   CRITr   r!   s     r   r#   zMythLog.logTB  s&     	ty*,,/////r   c                     || j         k    rdS || j        z  sdS | j        dk    rdS | j        5  |                     ||||           ddd           n# 1 swxY w Y   |                     ||||           dS )a  
        MythLog.log(mask, message, detail=None) -> None

        'mask' sets the bitwise log mask, to be matched against the log
                    filter. If any bits match true, the message will be logged.
        'message' and 'detail' set the log message content using the format:
                <timestamp> <module>: <message>
                        ---- or ----
                <timestamp> <module>: <message> -- <detail>
        NrE   )r7   r5   r;   r?   r:   _logdatabaser%   s        r   r)   zMythLog.log  s     4;FDJ 	F;??FZ 	9 	9NN4888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9$w77777s   AAAc           	         | j         r| j        t          k    rd S t                      }|                    d                    |                                 t          j                    | j	        g d|                              d}d|v rd}n|rd|v rd}|rc|
                    d          D ]}|                    d|z             |r0|
                    d          D ]}|                    d|z             n/|                    |           |r|                    d|z             |                    d           | j                            |                                           | j                                         d S )	Nz{0} {3} [{1}] {2} )!ACEWNIDF
Tz
    %sz
        %sz -- %s)r;   r8   r	   r   writer   r	  r   r   r   r   getvalueflush)r   r"   r&   r'   r(   buff	multilinelines           r   r9   zMythLog._logfile  s   ; 	DMV33Fzz

'VDIIKKdk555e<> >	? 	? 	? 	7??II 	!v~~ 	 		.d++ . .

:,---- 6"LL.. 6 6DJJ~45555JJw .

8f,---

4DMMOO,,,r   c                 d    t          j         |||rd                    |          ndz              d S )N -- {0} )rd   r   r%   s        r   r   zMythLog._logsyslog  sG    eV!K!1!1&!9!9!9L	N 	N 	N 	N 	Nr   c                     |rd                     |          }nd}t          d         }d|v r|                    dd          d         }d                     | j        ||          }t	          j        |||           d S )Nr  r   r   r   rE   z[{0}]: {1}{2})PRIORITYSYSLOG_IDENTIFIER)r   r
   r   r   r   send)r   r"   r&   r'   r(   r   msgs          r   r   zMythLog._logjournallog  s     	%%f--FFF1g+%,,S!44Q7K%%dk7FCCS5KIIIIIIr   c                    | j         r| j        r| j                             t                                5 }t          d         }d|v r|                    dd          d         }|                    d| j                                         |t          j	                    | j
        |                                 |||rd                    |          ndz   f           d d d            d S # 1 swxY w Y   d S d S d S )Nr   r   rE   zINSERT INTO logging
                                    (host, application, pid, thread,
                                     msgtime, level, message)
                                  VALUES (?, ?, ?, ?, ?, ?, ?)r  r   )r   r<   cursorr   r
   r   executegethostnamer   r   r   r	  r   )r   r"   r&   r'   r(   r'  r   s          r   r  zMythLog._logdatabase  s\   7 	Nt{ 	N.. N&"1g+%%"-"4"4S!"<"<Q"?K  B W((**KY[[$+tyy{{EF J	 0 0 8 8 8KM	N N NN N N N N N N N N N N N N N N N N N	N 	N 	N 	Ns   B$C&&C*-C*c                 6    |                      ||||           d S r   )r)   r%   s        r   r+   zMythLog.__call__  s     ugv.....r   )r   N)Tr   )"r,   r-   r.   __doc__helptextr2   r3   r@   rh   rj   r   r   r   r   r   rP   rQ   rR   rS   r   r   USERrT   rU   r   r	  r#   r)   r9   r   r   r  r+   __classcell__)r   s   @r   r0   r0      s        0Hd   [ $T $T [$TL V V [V8 V V [V6 7 7 [7, 7 7 [7>> > >
0 0 0 0 0    	5 	5 [	5 - - [- I I [I
 3 3 [3 $ $ $ [$ !,!1  $  $  $ [ $D G G [G" " " " ["@ KJJ0 0 08 8 8 8,  @N N N	J 	J 	JN N N/ / / / / / / /r   r0   )r+  MythTV.staticr   r   r   MythTV.exceptionsr   r   rd   r   systemdr   ImportErrorsysr   r	   r
   r   _threadr   ior   	tracebackr   r   r   r0   r   r   r   <module>r7     s     8 8 8 8 8 8 8 8 8 8 ' ' ' ' ' ' 				     GGG + * * * * * * * * *       ! ! ! ! ! !                  	 	 	@ @ @ @ @8Wk @ @ @^/ ^/ ^/ ^/ ^/x+ ^/ ^/ ^/ ^/ ^/s   ' 11