
    h!                        S r SSKrSSKrSSKJrJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  Sr  " S S5      r!\" 5       r"\!" \"\RF                  \\5      r$g)z
This module includes process-global state associated with the logging system,
and implementation of logic for managing that global state.
    N)IOAnyIterableOptionalType)currentframe)qual   )LimitedHistoryLogObserver)FileLogObserver)FilteringLogObserverLogLevelFilterPredicate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.c                       \ rS rSrSrSr SS\S\\   S\	S\S	\
\   S
S4S jjr  SS\\   S\S\S
S4S jjr  SS\S\\   S\S\S\
\\      S\
\   S
S4S jjrSrg)LogBeginner!   a  
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errorStreamstdiowarningsModuleinitialBufferSizereturnc           
      p   Uc  U R                   n[        US9U l        Xl        [	        US9U l        X0l        X@l        [        U R                  [        [        US 5      [        [        R                  S9/5      5      U l        SU l        UR!                  U R                  5        UR"                  U l        g)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                     [        U SSS9S-   $ )NF)includeTimestampincludeSystem
r   )events    ]/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/logger/_global.py<lambda>&LogBeginner.__init__.<locals>.<lambda>a   s    +).&+#
 #    )defaultLogLevel) r   )_DEFAULT_BUFFER_SIZEr   _initialBuffer
_publisherr   _log_stdio_warningsModuler   r   r   r   r   critical_temporaryObserver_previousBeginaddObservershowwarning_oldshowwarning)selfr   r   r   r   r   s         r(   __init__LogBeginner.__init__F   s     $ $ 9 97=NO#I.	-:F  )9J9JKL;
 &d556-99r+   	observersdiscardBufferredirectStandardIOc           
      .   [        S5      nUR                  R                  nUR                  nU H  nU R                  R                  U5        M      U R                  bt  U R                  R                  U R                  5        U(       d%  U R                  R                  U R                  5        SU l        U R                  U R                  l
        O/U R                  u  pU R                  R                  [        UUUU	S9  XV4U l        U(       a%  S[         R"                  4S[         R$                  4/n
O/ n
U
 HX  u  p['        U R(                  U5      n[+        [-        XR                  S9U['        USS5      S9n[/        U R(                  X5        MZ     g)	a  
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)fileNowlineNowfileThenlineThenstdoutstderr)	namespacer"   encoding)loggerlevelrH   )r   f_codeco_filenamef_linenor0   r7   r5   removeObserverr/   replayTor8   r3   r6   r1   warnMORE_THAN_ONCE_WARNINGr   infoerrorgetattrr2   r   r   setattr)r:   r=   r>   r?   callerfilenamelinenor"   previousFilepreviousLinestreamsstreamrJ   	oldStreamloggingFiles                  r(   beginLoggingToLogBeginner.beginLoggingToo   sS   H a==,,!HOO''1 " "".OO**4+B+BC ##,,T__=&*D#/3/?/?D  ,)-)<)<&LIINN& %%    (0 (--08X^^2LMGG$MFV4I%I J=K
 DKK5 %r+   messagecategoryrW   rX   filelinec                 ~    Uc'  U R                   R                  SU[        U5      UUS9  gU R                  XX4XV5        g)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})warningrb   rW   rX   )r1   rP   r	   r9   )r:   ra   rb   rW   rX   rc   rd   s          r(   r8   LogBeginner.showwarning   sE    8 <IINN<h!     HdQr+   )r/   r1   r9   r6   r0   r2   r5   r3   )N)FT)NN)__name__
__module____qualname____firstlineno____doc__r.   r   r   r   objectr   intr;   r   r   boolr_   strr   Warningr8   __static_attributes__ r+   r(   r   r   !   s    D  ,0':': W': 	':
 ': $C=': 
':X $#'	H6L)H6 H6 !	H6
 
H6` #'"%R%R w-%R 	%R
 %R r#w%R sm%R 
%R %Rr+   r   )%rl   syswarningstypingr   r   r   r   r   twisted.python.compatr   twisted.python.reflectr	   _bufferr   _filer   _filterr   r   _formatr   _interfacesr   _ior   _levelsr   _loggerr   	_observerr   rQ   r   globalLogPublisherrF   globalLogBeginnerrs   r+   r(   <module>r      sr   

   4 4 . ' . " B   %    # }R }R@ "^  2CJJXN r+   