o
                         @   s  d Z ddlmZ ddlmZ ddlmZ ddlm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mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlZdZejZdZdZdej fdej!fdejfdej"fdej#fdej#d fgZ$e%e$Z&dZ'e'dgZ(dZ)dZ*dZ+dZ,dZ-dZ.g dZ/edd  e/D Z0G d!d" d"ej1e2Z3G d#d$ d$e2Z4G d%d& d&e2Z5G d'd( d(e2Z6d)d* Z7ej8d+d, Z9G d-d. d.ej:Z;G d/d0 d0ej:Z<G d1d2 d2ej:Z=G d3d4 d4ej:Z>G d5d6 d6ej?Z@G d7d8 d8e2ZAeA ZBeBjCZDeBjEZFeFZGeBjHZHd9d: ZI	;	;dnd<d=ZJdod>d?ZKd@dA ZLdBdC ZMdpdDdEZNdFdG ZOdHdI ZPdJdK ZQdLdM ZRG dNdO dOe2ZSdPdQ ZTdRdS ZUdTdU ZVdVdW ZWdXdY ZXdZd[ ZY	dpd\d]ZZ		dqd^d_Z[		dqd`daZ\		;		dqdbdcZ]		dqdddeZ^		dqdfdgZ_		dqdhdiZ`		;		dqdjdkZa		;		dqdldmZbejcZcejdZdejeZeejfZfejgZgejhZhejiZiejjZjejkZkdS )rz7Module with logging related functionality for calliope.    )absolute_import)division)unicode_literals)OrderedDictN)
properties)console_attr)parser)text)files)timesutf-8warningTdebuginfoerrorcriticalnone
   z.logz.sql3z)\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}z&Running \[gcloud\.([-\w\.]+)\.[-\w]+\]z%Y.%m.%dz%H.%M.%S.%fz0.0.1z%Y-%m-%dT%H:%M:%S.%3f%Ez))versionr   z3Semantic version of the message format. E.g. v0.0.1)	verbosity	levelnamezBLogging Level: e.g. debug, info, warn, error, critical, exception.)	timestampasctimezUTC time event logged)messager   zLog/Error message.)r   r   zEActual exception or error raised, if message contains exception data.c                 c   s    | ]	}|d d V  qd S )N    ).0xr   r   4/tmp/google-cloud-sdk/lib/googlecloudsdk/core/log.py	<genexpr>Y   s    r   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	_NullHandlerztA replication of python2.7's logging.NullHandler.

  We recreate this class here to ease python2.6 compatibility.
  c                 C      d S Nr   selfrecordr   r   r   handlec      z_NullHandler.handlec                 C   r"   r#   r   r$   r   r   r   emitf   r(   z_NullHandler.emitc                 C   s
   d | _ d S r#   )lockr%   r   r   r   
createLocki      
z_NullHandler.createLockN)__name__
__module____qualname____doc__r'   r)   r,   r   r   r   r   r!   ]   s
    r!   c                   @      e Zd ZdZdd ZdS )_UserOutputFilterzA filter to turn on and off user output.

  This filter is used by the ConsoleWriter to determine if output messages
  should be printed or not.
  c                 C   
   || _ dS )zbCreates the filter.

    Args:
      enabled: bool, True to enable output, false to suppress.
    Nenabled)r%   r6   r   r   r   __init__t      
z_UserOutputFilter.__init__Nr.   r/   r0   r1   r7   r   r   r   r   r3   m   s    r3   c                   @   r2   )_StreamWrapperz8A class to hold an output stream that we can manipulate.c                 C   r4   )zSCreates the stream wrapper.

    Args:
      stream: The stream to hold on to.
    N)stream)r%   r;   r   r   r   r7      r8   z_StreamWrapper.__init__Nr9   r   r   r   r   r:   }   s    r:   c                   @   sZ   e Zd ZdZd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S )_ConsoleWriteraV  A class that wraps stdout or stderr so we can control how it gets logged.

  This class is a stripped down file-like object that provides the basic
  writing methods.  When you write to this stream, if it is enabled, it will be
  written to stdout.  All strings will also be logged at DEBUG level so they
  can be captured by the log file.
  Fc                 C   s   || _ || _|| _|| _dS )aw  Creates a new _ConsoleWriter wrapper.

    Args:
      logger: logging.Logger, The logger to log to.
      output_filter: _UserOutputFilter, Used to determine whether to write
        output or not.
      stream_wrapper: _StreamWrapper, The wrapper for the output stream,
        stdout or stderr.
      always_flush: bool, always flush stream_wrapper, default to False.
    N)_ConsoleWriter__logger_ConsoleWriter__filter_ConsoleWriter__stream_wrapper_ConsoleWriter__always_flush)r%   loggeroutput_filterstream_wrapperalways_flushr   r   r   r7      s   
z_ConsoleWriter.__init__c                 C   sf   ||}}t |tjrt }|j|dd}|j||  d}tj|t	dd}tj|t	dd}||fS )a  Converts msg to a console safe pair of plain and ANSI-annotated strings.

    Args:
      msg: str or text.TypedText, the message to parse into plain and
        ANSI-annotated strings.
    Returns:
      str, str: A plain text string and a string that may also contain ANSI
        constrol sequences. If ANSI is not supported or color is disabled,
        then the second string will be identical to the first.
    Fstylizeencodingescape)

isinstancer	   	TypedTextstyle_parserGetTypedTextParserParseTypedTextToStringisattyr   SafeTextLOG_FILE_ENCODING)r%   msg
plain_textstyled_texttyped_text_parserr   r   r   ParseMsg   s   
z_ConsoleWriter.ParseMsgc                 G   sb   g g }}|D ]}|  |\}}|| || qd|d }d|d }| || dS )aT  Writes the given tokens to the output stream, and adds a newline.

    This method has the same output behavior as the builtin print method but
    respects the configured verbosity.

    Args:
      *tokens: str or text.TypedTextor any object with a str() or unicode()
        method, The messages to print, which are joined with ' '.
     
N)rV   appendjoin_Write)r%   tokensplain_tokensstyled_tokenstokenrS   rT   r   r   r   Print   s   


z_ConsoleWriter.Printc                 C   s   | j jS )z)Returns the console writer output stream.)r?   r;   r+   r   r   r   GetConsoleWriterStream   s   z%_ConsoleWriter.GetConsoleWriterStreamc                 C   sn   | j | | jjr3t  }tj||dd}tj	r#|
|p dd}| jj| | jr5|   dS dS dS )a%  Just a helper so we don't have to double encode from Print and write.

    Args:
      msg: A text string that only has characters that are safe to encode with
        utf-8.
      styled_msg: A text string with the same properties as msg but also
        contains ANSI control sequences.
    FrG   r   replaceN)r=   r   r>   r6   r   GetConsoleAttrGetEncodingrP   sixPY2encoder?   r;   writer@   flush)r%   rR   
styled_msgstream_encoding
stream_msgr   r   r   r[      s   

z_ConsoleWriter._Writec                 C   s   |  |\}}| || d S r#   )rV   r[   )r%   rR   rS   rT   r   r   r   rh      s   z_ConsoleWriter.writec                 C   s   |D ]}|  | qd S r#   )rh   )r%   linesliner   r   r   
writelines   s   z_ConsoleWriter.writelinesc                 C   s   | j jr| jj  d S d S r#   )r>   r6   r?   r;   ri   r+   r   r   r   ri      s   z_ConsoleWriter.flushc                 C   s   t | jjdd }|r| S dS )NrO   F)getattrr?   r;   )r%   rO   r   r   r   rO      s   z_ConsoleWriter.isattyN)F)r.   r/   r0   r1   r7   rV   r`   ra   r[   rh   ro   ri   rO   r   r   r   r   r<      s    
r<   c                 C   s   t jr| dS | S )zGets the correct format string to use based on the Python version.

  Args:
    fmt: text string, The format string to convert.

  Returns:
    A byte string on Python 2 or the original string on Python 3.
  r   )re   rf   rg   fmtr   r   r   
_FmtString   s   
rs   c                 c   s8    | j }ztj| j |dd| _ dV  W || _ dS || _ w )a  Temporarily modifies a log record to make the message safe to print.

  Python logging creates a single log record for each log event. Each handler
  is given that record and asked format it. To avoid unicode issues, we decode
  all the messages in case they are byte strings. Doing this we also want to
  ensure the resulting string is able to be printed to the given output target.

  Some handlers target the console (which can have many different encodings) and
  some target the log file (which we always write as utf-8. If we modify the
  record, depending on the order of handlers, the log message could lose
  information along the way.

  For example, if the user has an ascii console, we replace non-ascii characters
  in the string with '?' to print. Then if the log file handler is called, the
  original unicode data is gone, even though it could successfully be printed
  to the log file. This context manager changes the log record briefly so it can
  be formatted without changing it for later handlers.

  Args:
    record: The log record.
    encoding: The name of the encoding to SafeDecode with.
  Yields:
    None, yield is necessary as this is a context manager.
  FrG   N)rR   r   rP   )r&   rH   original_msgr   r   r   _SafeDecodedLogRecord  s   ru   c                       s4   e Zd ZdZedZ fddZ fddZ  ZS )_LogFileFormatterz"A formatter for log file contents.z3%(asctime)s %(levelname)-8s %(name)-15s %(message)sc                    s   t t| jtjd d S )Nrq   )superrv   r7   FORMATr+   	__class__r   r   r7   9  s   z_LogFileFormatter.__init__c                    s   t  |}t|jtjrt j|jdd|_t|jt	r>g }|jD ]}t|tjr3t j|dd}|
| q"t	||_t|t tt| |}W d    |S 1 sWw   Y  |S )NFrE   )copyrJ   rR   r	   rK   rL   rM   rN   argstuplerY   ru   rQ   rw   rv   format)r%   r&   new_argsargrR   ry   r   r   r~   <  s(   



z_LogFileFormatter.format)	r.   r/   r0   r1   rs   rx   r7   r~   __classcell__r   r   ry   r   rv   3  s
    rv   c                       s   e Zd ZdZedZedZedZedZedZ	edZ
ee Zdee e iZeje	e e
 e ejee e
 e ejee e e
 iejee	 e e
 e ejee e e
 e ejee e e e
 id	Z fd
dZ fddZ  ZS )_ConsoleFormatterz@A formatter for the console logger, handles colorizing messages.z%(asctime)s z%(levelname)s:z %(message)sz[1;31mz[1;33mz[0mdetailed)standardr   c                    sx   t t|   tjjj }tjjjj	dd }||
 M }|t  M }|r.tj|ni | _tj|tj| _d S )NFvalidate)rw   r   r7   r   VALUEScoreconsole_log_formatGetdisable_colorGetBoolrO   r   rc   SupportsAnsiCOLOR_FORMATSget_formatsDEFAULT_FORMATSDEFAULT_FORMAT_default_format)r%   
out_streamr   	use_colorry   r   r   r7   p  s   

z_ConsoleFormatter.__init__c                    s   | j |j| j}|| _tjr|| j_t	 
 }t|| tt| |}W d    n1 s1w   Y  tjrA||p>dd}|S )Nr   rb   )r   r   levelnor   _fmtre   PY3_styler   rc   rd   ru   rw   r   r~   rf   rg   )r%   r&   rr   rk   rR   ry   r   r   r~     s   z_ConsoleFormatter.format)r.   r/   r0   r1   rs   	TIMESTAMPLEVELMESSAGEREDYELLOWENDr   r   loggingWARNINGERRORFATALr   r7   r~   r   r   r   ry   r   r   S  s,    r   c                       sP   e Zd ZdZ		d fdd	Zdd Zdd Zd	d
 ZdddZdd Z	  Z
S )_JsonFormatterz9A formatter that handles formatting log messages as JSON.Nc                    s0   t t|   || _|| _|ptj| _t| _	d S r#   )
rw   r   r7   required_fieldsjson_encoderjsondumpsjson_serializerSTRUCTURED_TIME_FORMATdefault_time_format)r%   r   r   r   ry   r   r   r7     s
   
z_JsonFormatter.__init__c                 C   s   t  }|jrc|js| |j|_tt|jtr3t|jj|d< t	
|j|d< t|jdd|d< |S tt|jd trR|jd |d< |j|d< |jd |d< |S |j|d< |j|d< |j|d< |S dS )zBExtract exception info from a logging.LogRecord as an OrderedDict.typedetails__traceback__N
stacktracer   r   )r   exc_infoexc_textformatException
issubclassr   rR   BaseExceptionr.   re   	text_typerp   )r%   
log_record
error_dictr   r   r   GetErrorDict  s*   




z_JsonFormatter.GetErrorDictc                 C   sB   t  }t| jD ]\}}|dkrt||< q	|j|||< q	|S )a  Converts a logging.LogRecord object to a JSON serializable OrderedDict.

    Utilizes supplied set of required_fields to determine output fields.

    Args:
      log_record: logging.LogRecord, log record to be converted

    Returns:
      OrderedDict of required_field values.
    r   )r   re   	iteritemsr   STRUCTURED_RECORD_VERSION__dict__r   )r%   r   message_dictoutfieldlogfieldr   r   r   BuildLogMsg  s   
z_JsonFormatter.BuildLogMsgc                 C   s.   |  |}|ds|d | j|| jdS )z)Returns a json string of the log message.r   )cls)r   r   popr   r   )r%   r   log_messager   r   r   LogRecordToJson  s   


z_JsonFormatter.LogRecordToJsonc                 C   s   t jt |j|t jdS )N)rr   tzinfo)r   FormatDateTimeGetDateTimeFromTimeStampcreatedUTC)r%   r&   datefmtr   r   r   
formatTime  s
   
z_JsonFormatter.formatTimec                 C   s4   |  ||jd< | |_| || j|_| |S )z,Formats a log record and serializes to json.r   )r   r   
getMessager   r   r   r   r   r$   r   r   r   r~     s   

z_JsonFormatter.formatNNr#   )r.   r/   r0   r1   r7   r   r   r   r   r~   r   r   r   ry   r   r     s    
	r   c                   @   s,   e Zd ZdZ	d	ddZdd Zdd ZdS )
_ConsoleLoggingFormatterMuxerz/Logging Formatter Composed of other formatters.Nc                 C   s,   t j|  |p
t j| _|| _| | _d S r#   )r   	Formatterr7   default_formatterstructured_formatterrO   terminal)r%   r   stream_writterr   r   r   r   r7     s   z&_ConsoleLoggingFormatterMuxer.__init__c                 C   s>   t jjj }t|dko| j|dko| j |dkgrdS dS )z=Returns True if output should be Structured, False otherwise.r   logalwaysTF)r   r   r   show_structured_logsr   anyr   )r%   show_messagesr   r   r   ShowStructuredOutput  s   z2_ConsoleLoggingFormatterMuxer.ShowStructuredOutputc                 C   s   |   }| jo	| }t|}t|jtjr!t j	|j|d|_t|j
trHg }|j
D ]}t|tjr=t j	||d}|| q,t||_
|rP| j|S | j|S )z.Formats the record using the proper formatter.rE   )r   r   r{   rJ   rR   r	   rK   rL   rM   rN   r|   r}   rY   r   r~   r   )r%   r&   show_structured_outputrF   r   r   r   r   r   r~     s&   


z$_ConsoleLoggingFormatterMuxer.formatr#   )r.   r/   r0   r1   r7   r   r~   r   r   r   r   r     s    
	
r   c                   @   r2   )NoHeaderErrorFilterzkFilter out urllib3 Header Parsing Errors due to a urllib3 bug.

  See https://bugs.python.org/issue36226
  c                 C   s   d|  vS )z!Filter out Header Parsing Errors.zFailed to parse headers)r   r$   r   r   r   filter     zNoHeaderErrorFilter.filterN)r.   r/   r0   r1   r   r   r   r   r   r     s    r   c                   @   s   e Zd ZdZ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S ) _LogManagerzA class to manage the logging handlers based on how calliope is being used.

  We want to always log to a file, in addition to logging to stdout if in CLI
  mode.  This sets up the required handlers to do this.
  ___FILE_ONLY___c                 C   s   t  | _t | _| jtj ttj| _	| j	tj d| j	_
g | _d | _tt| _td | _td | _t| j	| j| j| _t| j	| j| jdd| _d | _d | _d | _| tjtj d S )NFT)rD   )rv   _file_formatterr   	getLogger_root_loggersetLevelNOTSETr   FILE_ONLY_LOGGER_NAMEfile_only_logger	propagate
_logs_dirs_console_formatterr3   DEFAULT_USER_OUTPUT_ENABLED_user_output_filterr:   stdout_stream_wrapperstderr_stream_wrapperr<   stdout_writerstderr_writerr   user_output_enabledcurrent_log_fileResetsysstdoutstderrr+   r   r   r   r7   '  s0   



z_LogManager.__init__c                 C   s   g | j jdd< || j_|| j_tt}t|}t|| j	|d}|| _
t|| _| j| j
 | jt | j | j | jjD ]}|  qAg | jjdd< | jt  | jtj | d | d d| _tdt  dS )z6Resets all logging functionality to its default state.N)r   zurllib3.connectionpool)r   handlersr   r;   r   r   !REQUIRED_STRUCTURED_RECORD_FIELDSr   r   r   r   r   StreamHandlerstderr_handlersetFormatterr   DEFAULT_VERBOSITY
addHandlerr   closer!   r   SetVerbositySetUserOutputEnabledr   r   	addFilterr   )r%   r   r   json_formatterstd_console_formatterconsole_formatterfr   r   r   r   J  s4   



z_LogManager.Resetc                 C   sd   |du rt jjj }|durt| }|du rt}| j|kr$| jS | j	
| | j}|| _|S )a9  Sets the active verbosity for the logger.

    Args:
      verbosity: int, A verbosity constant from the logging module that
        determines what level of logs will show in the console. If None, the
        value from properties or the default will be used.

    Returns:
      int, The current verbosity.
    N)r   r   r   r   r   VALID_VERBOSITY_STRINGSr   lowerr   r   r   )r%   r   verbosity_stringold_verbosityr   r   r   r   q  s   
z_LogManager.SetVerbosityc                 C   s>   |du rt jjjjdd}|du rt}|| j_| j}|| _|S )zSets whether user output should go to the console.

    Args:
      enabled: bool, True to enable output, False to suppress.  If None, the
        value from properties or the default will be used.

    Returns:
      bool, The old value of enabled.
    NFr   )r   r   r   r   r   r   r   r6   )r%   r6   old_enabledr   r   r   r     s   

z _LogManager.SetUserOutputEnabledc                 C   s   t jjj S )zyGets the max log days for the logger.

    Returns:
      max_log_days: int, the maximum days for log file retention
    )r   r   r   max_log_daysGetIntr+   r   r   r   _GetMaxLogDays  s   z_LogManager._GetMaxLogDaysc                 C   s   d|    S )z$Gets max_log_day's worth of seconds.iQ )r  r+   r   r   r   
_GetMaxAge  r   z_LogManager._GetMaxAgec                 C   s   t j|  dS )N)days)datetime	timedeltar  r+   r   r   r   _GetMaxAgeTimeDelta  s   z_LogManager._GetMaxAgeTimeDeltac                 C   s   t j tj|tS r#   )r  strptimeospathbasenameDAY_DIR_FORMAT)r%   r  r   r   r   _GetFileDatetime  s   z_LogManager._GetFileDatetimec              
   C   s   |r|| j v r	dS | j | | | tjjj rdS z| |}t	j
|td}W n# tttjfyN } ztd|t|j| W Y d}~dS d}~ww || _|t	j || j | j| | j| dS )zAdds a new logging directory and configures file logging.

    Args:
      logs_dir: str, Path to a directory to store log files under.  This method
        has no effect if this is None, or if this directory has already been
        registered.
    N)rH   zCould not setup log file in {0}, ({1}: {2}.
The configuration directory may not be writable. To learn more, see https://cloud.google.com/sdk/docs/configurations#creating_a_configuration)r   rY   _CleanUpLogsr   r   r   disable_file_loggingr   _SetupLogsDirr   FileHandlerrQ   OSErrorIOErrorr
   Errorr   r~   r   r.   r   r   r   r   r   r   r   r   )r%   logs_dirlog_filefile_handlerexpr   r   r   
AddLogsDir  s2   


	z_LogManager.AddLogsDirc                 C   s2   |   rz| | W dS  ty   Y dS w dS )z7Clean up old log files if log cleanup has been enabled.N)r  _CleanLogsDirr  )r%   r  r   r   r   r    s   z_LogManager._CleanUpLogsc           	   	   C   s   t j  }t }zt|}W n ttfy   Y dS w |D ]:}tj||}| 	||rYt|D ]}tj||}| 
||rGt| q3zt| W q tyX   Y qw qdS )z{Cleans up old log files form the given logs directory.

    Args:
      logs_dir: str, The path to the logs directory.
    N)r  nowtimer  listdirr  UnicodeErrorr  rZ   _ShouldDeleteDir_ShouldDeleteFileremovermdir)	r%   r  r$  now_secondsdirnamesdirnamedir_pathfilenamelog_file_pathr   r   r   r#    s,   

z_LogManager._CleanLogsDirc                 C   sR   t j|sdS z| |}W n
 ty   Y dS w || }||  td kS )a  Determines if the directory should be deleted.

    True iff:
    * path is a directory
    * path name is formatted according to DAY_DIR_FORMAT
    * age of path (according to DAY_DIR_FORMAT) is slightly older than the
      MAX_AGE of a log file

    Args:
      now: datetime.datetime object indicating the current date/time.
      path: the full path to the directory in question.

    Returns:
      bool, whether the path is a valid directory that should be deleted
    F   )r  r  isdirr  
ValueErrorr  r  r  )r%   r$  r  dir_datedir_ager   r   r   r(  	  s   z_LogManager._ShouldDeleteDirc                 C   s4   t j|d tvrdS t |}||j |  kS )a7  Determines if the file is old enough to be deleted.

    If the file is not a file that we recognize, return False.

    Args:
      now_seconds: int, The current time in seconds.
      path: str, The file or directory path to check.

    Returns:
      bool, True if it should be deleted, False otherwise.
    r2  F)r  r  splitext_KNOWN_LOG_FILE_EXTENSIONSstatst_mtimer  )r%   r,  r  	stat_infor   r   r   r)  &  s   
z_LogManager._ShouldDeleteFilec                 C   sR   t j  }|t}tj||}t| dj	|t
td}tj||}|S )a  Creates the necessary log directories and get the file name to log to.

    Logs are created under the given directory.  There is a sub-directory for
    each day, and logs for individual invocations are created under that.

    Deletes files in this directory that are older than MAX_AGE.

    Args:
      logs_dir: str, Path to a directory to store log files under

    Returns:
      str, The path to the file to log to
    z{timestamp}{ext})r   ext)r  r$  strftimer  r  r  rZ   r
   MakeDirr~   FILENAME_FORMATLOG_FILE_EXTENSION)r%   r  r$  day_dir_nameday_dir_pathr0  r  r   r   r   r  8  s   



z_LogManager._SetupLogsDirN)r.   r/   r0   r1   r   r7   r   r   r   r  r  r  r  r"  r  r#  r(  r)  r  r   r   r   r   r     s"    #'	,r   c                  G   s   t j|   dS )zWrites the given message to the output stream, and adds a newline.

  This method has the same output behavior as the builtin print method but
  respects the configured user output setting.

  Args:
    *msg: str, The messages to print.
  N)outr`   )rR   r   r   r   r`   l  s   	r`   Fc                 C   s`   | dkr|rt tj| dS t| dS |r$t j| ||||d dS t j| ||||d dS )a  Writes content to the specified file or stdout if path is '-'.

  Args:
    path: str, The path of the file to write.
    content: str, The content to write to the file.
    overwrite: bool, Whether or not to overwrite the file if it exists.
    binary: bool, True to open the file in binary mode.
    private: bool, Whether to write the file in private mode.
    create_path: bool, True to create intermediate directories, if needed.

  Raises:
    Error: If the file cannot be written.
  -)	overwriteprivatecreate_pathN)r
   WriteStreamBytesr   r   rC  rh   WriteBinaryFileContentsWriteFileContents)r  contentrE  binaryrF  rG  r   r   r   WriteToFileOrStdoutx  s   


rM  c                 C   s   t | ptj|p
tj dS )a  Reinitialize the logging system.

  This clears all loggers registered in the logging module, and reinitializes
  it with the specific loggers we want for calliope.

  This will set the initial values for verbosity or user_output_enabled to their
  values saved in the properties.

  Since we are using the python logging module, and that is all statically
  initialized, this method does not actually turn off all the loggers.  If you
  hold references to loggers or writers after calling this method, it is
  possible they will continue to work, but their behavior might change when the
  logging framework is reinitialized.  This is useful mainly for clearing the
  loggers between tests so stubs can get reset.

  Args:
    stdout: the file-like object to restore to stdout. If not given, sys.stdout
      is used
    stderr: the file-like object to restore to stderr. If not given, sys.stderr
      is used
  N)_log_managerr   r   r   r   )r   r   r   r   r   r     s   r   c                 C   
   t | S )a+  Sets the active verbosity for the logger.

  Args:
    verbosity: int, A verbosity constant from the logging module that
      determines what level of logs will show in the console. If None, the
      value from properties or the default will be used.

  Returns:
    int, The current verbosity.
  )rN  r   r   r   r   r   r     s   
r   c                   C      t jS )zRGets the current verbosity setting.

  Returns:
    int, The current verbosity.
  )rN  r   r   r   r   r   GetVerbosity     rR  c                 C   s6   | du rt  } ttD ]\}}| |kr|  S qdS )zGets the name for the current verbosity setting or verbosity if not None.

  Args:
    verbosity: int, Returns the name for this verbosity if not None.

  Returns:
    str, The verbosity name or None if the verbosity is unknown.
  N)rR  re   r   r  )r   namenumr   r   r   GetVerbosityName  s   	rV  c                   C   s   dd t D S )zFGets all the valid verbosity names from most verbose to least verbose.c                 S   s   g | ]\}}|qS r   r   )r   rT  _r   r   r   
<listcomp>  s    z)OrderedVerbosityNames.<locals>.<listcomp>)_VERBOSITY_LEVELSr   r   r   r   OrderedVerbosityNames  s   rZ  c                 C   s
   | pt jS )zFReturns the effective verbosity for verbosity. Handles None => NOTSET.)r   r   rP  r   r   r   _GetEffectiveVerbosity  s   
r[  c                 C   s   t tj| S )a  Sets the log file verbosity.

  Args:
    verbosity: int, A verbosity constant from the logging module that
      determines what level of logs will be written to the log file. If None,
      the default will be used.

  Returns:
    int, The current verbosity.
  )r[  rN  r   r   rP  r   r   r   SetLogFileVerbosity  s   
r\  c                   C   s   t tj S )zdGets the current log file verbosity setting.

  Returns:
    int, The log file current verbosity.
  )r[  rN  r   getEffectiveLevelr   r   r   r   GetLogFileVerbosity  s   r^  c                   @   r    )	LogFileVerbositya*  A log file verbosity context manager.

  Attributes:
    _context_verbosity: int, The log file verbosity during the context.
    _original_verbosity: int, The original log file verbosity before the
      context was entered.

  Returns:
    The original verbosity is returned in the "as" clause.
  c                 C   s
   || _ d S r#   )_context_verbosity)r%   r   r   r   r   r7     r-   zLogFileVerbosity.__init__c                 C   s   t | j| _| jS r#   )r\  r`  _original_verbosityr+   r   r   r   	__enter__  s   zLogFileVerbosity.__enter__c                 C   s   t | j dS )NF)r\  ra  )r%   exc_type	exc_value	tracebackr   r   r   __exit__  s   
zLogFileVerbosity.__exit__N)r.   r/   r0   r1   r7   rb  rf  r   r   r   r   r_    s
    r_  c                 C   rO  )zSets whether user output should go to the console.

  Args:
    enabled: bool, True to enable output, false to suppress.

  Returns:
    bool, The old value of enabled.
  )rN  r   r5   r   r   r   r     s   
	r   c                   C   rQ  )zuGets whether user output is enabled or not.

  Returns:
    bool, True if user output is enabled, False otherwise.
  )rN  r   r   r   r   r   IsUserOutputEnabled   rS  rg  c                 C   s   t j| d dS )zwAdds a new logging file handler to the root logger.

  Args:
    logs_dir: str, The root directory to store logs in.
  r  N)rN  r"  rh  r   r   r   AddFileLogging)  s   ri  c                  C   s   t j} | sdS tj| S )zhGets the path to the currently in use log directory.

  Returns:
    str, The logging directory path.
  N)rN  r   r  r  r.  )r  r   r   r   	GetLogDir2  s   rj  c                 C   s4   t j}|sdS tj|}|dtt  }||  S )zReturns a new log file name based on the currently active log file.

  Args:
    suffix: str, A suffix to add to the current log file name.

  Returns:
    str, The name of a log file, or None if file logging is not on.
  N)rN  r   r  r  r  lenr@  )suffixr  log_filenamelog_file_root_namer   r   r   GetLogFileName>  s   	ro  c                   C   rQ  )zReturn the path to the currently active log file.

  Returns:
    str, The name of a log file, or None if file logging is not on.
  )rN  r   r   r   r   r   GetLogFilePathO  rS  rp  c                 C   s  g }|r| d | |  n |r| |   | d n|p$d| }| d|  |r;| d | | |rN| d | tjt| |rZ| d | | |rf| d | | t|d 	drqd	nd}	| |	 t
|}|rtntj}
|
| d
S )a2  Prints a status message for operation on resource.

  The non-failure status messages are disabled when user output is disabled.

  Args:
    operation: str, The completed operation name.
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message. For commands that operate on multiple
      resources and report all successes and failures before exiting. Failure
      messages use log.error. This will display the message on the standard
      error even when user output is disabled.
    operation_past_tense: str, The past tense version of the operation verb.
      If None assumes operation + 'd'
  z
Failed to z in progress forz{0}dz{0}rW   z: . N)rY   
capitalizer~   r	   	TextTypesRESOURCE_NAMEre   r   strendswithrK   r   statusr`   )	operationresourcekindis_asyncr   failedoperation_past_tenserR   verbperiodwriterr   r   r   _PrintResourceChangeX  s4   









r  c                 C      t d| |||| dS )a]  Prints a status message indicating that a resource was created.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  createNr  r{  r|  r}  r   r~  r   r   r   CreatedResource     r  c                 C   r  )a]  Prints a status message indicating that a resource was deleted.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  deleteNr  r  r   r   r   DeletedResource  r  r  c              	   C      t d| ||||dd dS )a^  Prints a status message indicating that a resource was detached.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  detachdetachedr  Nr  r  r   r   r   DetachedResource     
r  c                 C   r  )a^  Prints a status message indicating that a resource was restored.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  restoreNr  r  r   r   r   RestoredResource  r  r  c                 C   r  )a]  Prints a status message indicating that a resource was updated.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  updateNr  r  r   r   r   UpdatedResource  r  r  c              	   C   s   t d| ||||dd dS )a[  Prints a status message indicating that a resource was reset.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  resetr  Nr  r  r   r   r   ResetResource  s   
r  c              	   C   r  )a^  Prints a status message indicating that a resource was imported.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  importimportedr  Nr  r  r   r   r   ImportResource  r  r  c              	   C   r  )a^  Prints a status message indicating that a resource was exported.

  Args:
    resource: str, The resource name.
    kind: str, The resource kind (instance, cluster, project, etc.).
    is_async: bool, True if the operation is in progress.
    details: str, Extra details appended to the message. Keep it succinct.
    failed: str, Failure message.
  exportexportedr  Nr  r  r   r   r   ExportResource  r  r  )TFFFr   r#   )NFNN)lr1   
__future__r   r   r   collectionsr   
contextlibr{   r  r   r   r  r   r%  googlecloudsdk.corer   googlecloudsdk.core.consoler   !googlecloudsdk.core.console.styler   rL   r	   googlecloudsdk.core.utilr
   r   re   rQ   r   r   DEFAULT_VERBOSITY_STRINGr   DEBUGINFOr   CRITICALrY  dictr  r@  r8  LOG_PREFIX_PATTERNUSED_SURFACE_PATTERNr  r?  r   r   STRUCTURED_RECORD_FIELDSr   Handlerobjectr!   r3   r:   r<   rs   contextmanagerru   r   rv   r   r   r   Filterr   r   rN  r   rC  r   errry  r   r`   rM  r   r   rR  rV  rZ  r[  r\  r^  r_  r   rg  ri  rj  ro  rp  r  r  r  r  r  r  r  r  r  r   r   r   r   r   r   r   fatal	exceptionr   r   r   r   <module>   s   
u
" AP0  4

 
	
		
7








