a
    ij                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ e	 4 ddl
mZ ddl
mZ ddl
mZ W d   n1 sv0    Y  ejZG dd	 d	Zdd
ddZeeedddZejd
ddZdd ZdS )zLogging utils.    N)epy)	_internal)app)flags)loggingc                   @   s<   e Zd ZdZeddddZddddZddd	d
ZdS )
TqdmStreamay  File-object-like abstraction which wrap`tqdm.write`.

  By default using `logging.info` inside a `tqdm` scope creates visual
  artifacts. This simple wrapper uses `tqdm.write` instead.

  Usage:

  ```python
  logger = logging.getLogger()
  logger.addHandler(logging.StreamHandler(TqdmStream()))

  for _ in tqdm.tqdm(range(10)):
    logger.info('No visual artifacts')
  ```
  N)xreturnc                 C   s   dd l }|j j|dd d S )Nr    )end)tqdmwrite)selfr   r    r   V/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/eapp/logging_utils.pyr   3   s    zTqdmStream.writer	   c                 C   s   d S Nr   r   r   r   r   flush8   s    zTqdmStream.flushc                 C   s   d S r   r   r   r   r   r   close;   s    zTqdmStream.close)__name__
__module____qualname____doc__strr   r   r   r   r   r   r   r   "   s   r   r   c                  C   sr   t  sdS t jst jrdS tjdd d} tjd|  dddd	}t j	}|
| d
tjv rn|t  dS )z!Modify Python logging (internal).NT)quietz{filename}:{lineno}z{levelname:1.1} {asctime} [z]: {message}z%H:%M:%S{)datefmtstyler   )FLAGSZ	is_parsedZlogtostderrZalsologtostderrabsl_loggingZuse_python_logging
py_logging	FormatterZget_absl_handlerpython_handlersetFormattersysmodules	setStreamr   )Z	file_link	formatterr#   r   r   r   _better_logging?   s    




r)   )uritextr	   c                 C   s   d}d| d|  d| dS )z)Returns a clickable link on the terminal.r
   z]8;;z\z]8;;\r   )r*   r+   
parametersr   r   r   _terminal_linkb   s    r.   c                 O   s   | |i |}|S )zUpdate the logs.r   )Zold_factoryargskwargsrecordr   r   r   _new_factoryi   s    r2   c                   C   s   t t dS )a*  Improve Python logging when running locally.

  * Display Python logs by default (even when user forgot `--logtostderr`),
    without being polluted by hundreds of C++ logs.
  * Cleaner minimal log format (e.g. `I 15:04:05 [main.py:24]:`)
  * Avoid visual artifacts between TQDM & `logging`
  * Clickable hyperlinks redirecting to code search (require terminal support)

  Usage:

  ```python
  if __name__ == '__main__':
    eapp.better_logging()
    app.run(main)
  ```

  Note this has only effect when user run locally and without `--logtostderr`.
  N)r   Zcall_after_initr)   r   r   r   r   better_loggingp   s    r3   )r   	functoolsr   r!   r%   Zetilsr   Z	etils.epyr   Zcheck_missing_depsZabslr   r   r    r   r   r)   r   r.   	LogRecordr2   r3   r   r   r   r   <module>   s   
*#