
    %              	           % S r SSKrSSKrSSKrSSKrSSKJrJrJr  SSK	J
r
  SSK	Jr  SSKJr  Sq\\S'   S	\4S
 jrSS\\   S	\\   4S jjr SS\S\\\4   S\\   S	S4S jjrS\4S jrSS\\   4S jjrS\S	\4S jrSS\\   4S jjrg)z%Utility functions for BQ CLI logging.    N)OptionalTextIOUnion)flags)logging)model _UNIQUE_SUFFIXreturnc                  ~    [         (       d-  [        R                  R                  5       R                  S5      q [         $ )Nz%z_%Y%m%d_%H%M%S.%f)r
   datetimenowstrftime     platform/bq/utils/bq_logging.pyGetUniqueSuffixr      s-    	&&**,556KLN	r   apilogc                    U (       a'  [         R                  R                  U 5      (       a  U nOS[         R                  ;   a2  [         R                  R	                  [         R                  S   S5      nOGS[         R                  ;   a2  [         R                  R	                  [         R                  S   S5      nOg[         R
                  " USS9  U$ )zReturns a directory to log to.TEST_UNDECLARED_OUTPUTS_DIRbq_logsKOKORO_ARTIFACTS_DIRNT)exist_ok)ospathisdirenvironjoinmakedirs)r   	full_paths     r   GetLogDirectoryr!      s    f%%I %

2


019I
 +RZZ(>?KI++i$'	r   file_prefixcontentc                 B   [        U5      nU(       d  g[        U[        5      (       a  UR                  S5      nU  S[	        5        S3n[
        R                  R                  X45      n[        US5       nUR                  U5        SSS5        g! , (       d  f       g= f)z4Saves string content to a file in the log directory.Nzutf-8_.logw)
r!   
isinstancebytesdecoder   r   r   r   openwrite)r"   r#   r   log_dirfilenamer   fs          r   #SaveStringToLogDirectoryIfAvailabler0   ,   sx     F#'	
nnW%G}Ao/05(	g	($D#!GGG s   5B
Blogfilec                 t    [         R                  " SS9  U [         R                  " 5       R                  l        g )NT)quiet)absl_logginguse_python_loggingget_absl_handlerpython_handlerstream)r1   s    r   _SetLogFiler9   >   s'    !!-:A,!007r   c                 F   / nU c  [        5       n UR                  S5        U c<  [        R                  " S5        [        R                  " [        R
                  5        GO#U S;   a  [        [        R                  5        OU S:X  a  [        [        R                  5        OU (       az  [        R                  R                  U 5      (       a@  UR                  SU  35        [        R                  R                  U S[        5        S35      n [        [        U S	5      5        O"[        R                   " [        R"                  S
9  [%        [&        R(                  S5      (       a  S[&        R(                  l        OS[,        l        U H  n[        R.                  " U5        M     g)a  Sets up Python logger.

Applications can configure logging however they want, but this
captures one pattern of logging which seems useful when dealing with
a single command line option for determining logging.

Args:
  apilog: To log to sys.stdout, specify '', '-', '1', 'true', or 'stdout'. To
    log to sys.stderr, specify 'stderr'. To log to a file, specify the file
    path. Specify None to disable logging.
NzkNo logging set and we are in a test environment, logs will be in a directory based on the test environment.z<There is no apilog flag so non-critical logging is disabled.)r	   -1truestdoutstderrzLogging to directory: bq_cli_r&   a)leveldump_request_responseT)r!   appendr   debugdisableCRITICALr9   sysr>   r?   r   r   r   r   r   r+   basicConfigINFOhasattrr   FLAGSrC   r   info)r   log_messageslogs      r   ConfigurePythonLoggerrP   C   s.    ,^F	4 ^MMF OOG$$%11#**	8	#**		v		4VH=>o'(-
 $vs#$- u{{344*.ekk'$(e!cLL r   oc                     [        [        R                  SS5      =(       d    Sn[        U [	        S5      5      (       a'  [        U [
        5      (       d  U R                  US5      $ [        U 5      $ )z7Safely encode an object as the encoding for sys.stdout.encodingNasciir	   backslashreplace)getattrrH   r>   r(   typestrencode)rQ   rS   s     r   EncodeForPrintingrZ   v   sU     SZZT2=g( 48Z3%7%788H011q6Mr   c                      [        U 5        g ! [         aT  nUR                  S:X  a=  [        SUR                  < SUR
                  < 35        [        R                  " S5        UeS nAff = f)N   zCould not configure logging. z:    )rP   IOErrorerrnoprintstrerrorr.   rH   exit)r   es     r   ConfigureLoggingrd      sO    &!	 ww!|QZZLM	hhqk
G	s    
A,AA''A,)N)__doc__r   r   r   rH   typingr   r   r   abslr   r4   googleapiclientr   r
   rX   __annotations__r   r!   r)   r0   r9   rP   objectrZ   rd   r   r   r   <module>rk      s    +   	 
 * *  ( !  HSM Xc] . !3: SM 
	$B B
0(3- 0f C "Xc] r   