Ϫf! dZddlZddlZddlmZmZmZmZmZddl m Z ddl m Z ddl mZddlmZdd lmZmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZdZ GddZ!eZ"e!e"ejFeeZ$y)z This module includes process-global state associated with the logging system, and implementation of logic for managing that global state. N)IOAnyIterableOptionalType) currentframe)qual)LimitedHistoryLogObserver)FileLogObserver)FilteringLogObserverLogLevelFilterPredicate eventAsText) ILogObserver) LoggingFile)LogLevel)Logger) LogPublisherzWarning: primary log target selected twice at <{fileNow}:{lineNow}> - previously selected at <{fileThen}:{lineThen}>. Remove one of the calls to beginLoggingTo.ceZdZdZdZ ddedeededede e d df d Z dd e e d ed ed dfdZ ddedeedede de eede ed dfdZy) LogBeginnera A L{LogBeginner} holds state related to logging before logging has begun, and begins logging when told to do so. Logging "begins" when someone has selected a set of observers, like, for example, a L{FileLogObserver} that writes to a file on disk, or to standard output. Applications will not typically need to instantiate this class, except those which intend to initialize the global logging system themselves, which may wish to instantiate this for testing. The global instance for the current process is exposed as L{twisted.logger.globalLogBeginner}. Before logging has begun, a L{LogBeginner} will: 1. Log any critical messages (e.g.: unhandled exceptions) to the given file-like object. 2. Save (a limited number of) log events in a L{LimitedHistoryLogObserver}. @cvar _DEFAULT_BUFFER_SIZE: The default size for the initial log events buffer. @ivar _initialBuffer: A buffer of messages logged before logging began. @ivar _publisher: The log publisher passed in to L{LogBeginner}'s constructor. @ivar _log: The logger used to log messages about the operation of the L{LogBeginner} itself. @ivar _stdio: An object with C{stderr} and C{stdout} attributes (like the L{sys} module) which will be replaced when redirecting standard I/O. @ivar _temporaryObserver: If not L{None}, an L{ILogObserver} that observes events on C{_publisher} for this L{LogBeginner}. N publisher errorStreamstdiowarningsModuleinitialBufferSizereturnc | |j}t||_||_t ||_||_||_t|jtt|dttjg|_d|_|j!|j|j"|_y)z Initialize this L{LogBeginner}. @param initialBufferSize: The size of the event buffer into which events are collected until C{beginLoggingTo} is called. Or C{None} to use the default size. N)size)observerc$t|dddzS)NF)includeTimestamp includeSystem r)events 8/usr/lib/python3/dist-packages/twisted/logger/_global.pyz&LogBeginner.__init__..as+).&+#  #)defaultLogLevel)r)_DEFAULT_BUFFER_SIZEr _initialBuffer _publisherr_log_stdio_warningsModulerr r rrcritical_temporaryObserver_previousBegin addObserver showwarning_oldshowwarning)selfrrrrrs r'__init__zLogBeginner.__init__Fs  $ $ 9 9 7=NO#I.  -:F    )9J9JKL ; &d556-99r) observers discardBufferredirectStandardIOc ,td}|jj}|j}|D]}|jj ||j o|jj|j |s%|jj|jd|_|j|j_ n3|j\}} |jjt|||| ||f|_ |r%dt j"fdt j$fg} ng} | D]`\} } t'|j(| } t+t-| |j| t'| dd}t/|j(| |by) aw Begin logging to the given set of observers. This will: 1. Add all the observers given in C{observers} to the L{LogPublisher} associated with this L{LogBeginner}. 2. Optionally re-direct standard output and standard error streams to the logging system. 3. Re-play any messages that were previously logged to that publisher to the new observers, if C{discardBuffer} is not set. 4. Stop logging critical errors from the L{LogPublisher} as strings to the C{errorStream} associated with this L{LogBeginner}, and allow them to be logged normally. 5. Re-direct warnings from the L{warnings} module associated with this L{LogBeginner} to log messages. @note: Since a L{LogBeginner} is designed to encapsulate the transition between process-startup and log-system-configuration, this method is intended to be invoked I{once}. @param observers: The observers to register. @param discardBuffer: Whether to discard the buffer and not re-play it to the added observers. (This argument is provided mainly for compatibility with legacy concerns.) @param redirectStandardIO: If true, redirect standard output and standard error to the observers. r N)fileNowlineNowfileThenlineThenstdoutstderr) namespacer!encoding)loggerlevelrE)rf_code co_filenamef_linenor.r5r3removeObserverr-replayTor6r1r4r/warnMORE_THAN_ONCE_WARNINGrinfoerrorgetattrr0rrsetattr)r8r:r;r<callerfilenamelinenor! previousFile previousLinestreamsstreamrG oldStream loggingFiles r'beginLoggingTozLogBeginner.beginLoggingToosgHa==,,! 2H OO ' ' 1 2  " " . OO * *4+B+B C ##,,T__=&*D #/3/?/?D ,)-)<)< &L, IINN& %%   (0  (--08X^^2LMGG$ 6MFE V4I%I J=K DKK 5 6r)messagecategoryrTrUfilelinec|*|jjd|t|||y|j||||||y)a] Twisted-enabled wrapper around L{warnings.showwarning}. If C{file} is L{None}, the default behaviour is to emit the warning to the log system, otherwise the original L{warnings.showwarning} Python function is called. @param message: A warning message to emit. @param category: A warning category to associate with C{message}. @param filename: A file name for the source code file issuing the warning. @param lineno: A line number in the source file where the warning was issued. @param file: A file to write the warning message to. If L{None}, write to L{sys.stderr}. @param line: A line of source code to include with the warning message. If L{None}, attempt to read the line from C{filename} and C{lineno}. Nz*{filename}:{lineno}: {category}: {warning})warningr^rTrU)r/rMr r7)r8r]r^rTrUr_r`s r'r6zLogBeginner.showwarningsK8 < IINN<h!    (HfdD Qr))N)FT)NN)__name__ __module__ __qualname____doc__r,rrrobjectrintr9rrboolr\strrWarningr6r)r'rr!s D,0 ':':W': ':  ': $C= ': ':X$#' H6L)H6H6! H6  H6`#'"%R%Rw-%R %R  %R r#w %Rsm%R %Rr)r)%rfsyswarningstypingrrrrrtwisted.python.compatrtwisted.python.reflectr _bufferr _filer _filterr r_formatr _interfacesr_ior_levelsr_loggerr _observerrrNrglobalLogPublisherrCglobalLogBeginnerrlr)r'r}sr   44.'."B %#}R}R@"^ 2CJJXNr)