
    %              	       
   U d Z ddlZddlZddlZddlZ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aeed<   d	efd
Zddee   d	ee   fdZ	 ddedeeef   dee   d	dfdZdefdZddee   fdZded	efdZddee   fdZy)z%Utility functions for BQ CLI logging.    N)OptionalTextIOUnion)flags)logging)model _UNIQUE_SUFFIXreturnc                  t    t         s-t        j                  j                         j                  d      a t         S )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                    | r"t         j                  j                  |       r| }ndt         j                  v r2t         j                  j	                  t         j                  d   d      }nEdt         j                  v r2t         j                  j	                  t         j                  d   d      }nyt        j
                  |d       |S )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                 &   t        |      }|syt        |t              r|j                  d      }|  dt	                d}t
        j                  j                  ||      }t        |d      5 }|j                  |       ddd       y# 1 sw Y   yxY w)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   ,   sw     F#'	
nnW%G}Ao/05(	gx	($D#!GGG s   ,BBlogfilec                 v    t        j                  d       | t        j                         j                  _        y )NT)quiet)absl_logginguse_python_loggingget_absl_handlerpython_handlerstream)r1   s    r   _SetLogFiler9   >   s'    !!-:A,!007r   c                    g }| t               } |j                  d       | :t        j                  d       t        j                  t        j
                         n| dv rt        t        j                         n| dk(  rt        t        j                         n| rut        j                  j                  |       r@|j                  d|         t        j                  j                  | dt                d      } t        t        | d	             n$t        j                   t        j"                  
       t%        t&        j(                  d      rdt&        j(                  _        ndt,        _        |D ]  }t        j.                  |        y)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*.ekk'$(e!cLL r   oc                     t        t        j                  dd      xs d}t        | t	        d            r"t        | t
              s| j                  |d      S t        |       S )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   sO     SZZT2=g( 48Z3%788H011q6Mr   c                     	 t        |        y # t        $ rP}|j                  dk(  r:t        d|j                  d|j
                         t        j                  d       |d }~ww xY w)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   