Home | History | Annotate | Download | only in utils
      1 # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Logging and debugging utilities."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 import os
     22 import sys
     23 
     24 # TODO(mdan): Use a custom logger class.
     25 from tensorflow.python.platform import tf_logging as logging
     26 from tensorflow.python.util.tf_export import tf_export
     27 
     28 VERBOSITY_VAR_NAME = 'AUTOGRAPH_VERBOSITY'
     29 DEFAULT_VERBOSITY = 0
     30 
     31 verbosity_level = None  # vlog-like. Takes precedence over the env variable.
     32 echo_log_to_stdout = False
     33 
     34 # In interactive Python, logging echo is enabled by default.
     35 if hasattr(sys, 'ps1') or hasattr(sys, 'ps2'):
     36   echo_log_to_stdout = True
     37 
     38 
     39 @tf_export('autograph.set_verbosity')
     40 def set_verbosity(level, alsologtostdout=False):
     41   """Sets the AutoGraph verbosity level.
     42 
     43   _Debug logging in AutoGraph_
     44 
     45   More verbose logging is useful to enable when filing bug reports or doing
     46   more in-depth debugging.
     47 
     48   There are two controls that control the logging verbosity:
     49 
     50    * The `set_verbosity` function
     51 
     52    * The `AUTOGRAPH_VERBOSITY` environment variable
     53 
     54   `set_verbosity` takes precedence over the environment variable.
     55 
     56   For example:
     57 
     58   ```python
     59   import os
     60   import tensorflow as tf
     61 
     62   os.environ['AUTOGRAPH_VERBOSITY'] = 5
     63   # Verbosity is now 5
     64 
     65   tf.autograph.set_verbosity(0)
     66   # Verbosity is now 0
     67 
     68   os.environ['AUTOGRAPH_VERBOSITY'] = 1
     69   # No effect, because set_verbosity was already called.
     70   ```
     71 
     72   Logs entries are output to [absl](https://abseil.io)'s default output,
     73   with `INFO` level.
     74   Logs can be mirrored to stdout by using the `alsologtostdout` argument.
     75   Mirroring is enabled by default when Python runs in interactive mode.
     76 
     77   Args:
     78     level: int, the verbosity level; larger values specify increased verbosity;
     79       0 means no logging. When reporting bugs, it is recommended to set this
     80       value to a larges number, like 10.
     81     alsologtostdout: bool, whether to also output log messages to `sys.stdout`.
     82   """
     83   global verbosity_level
     84   global echo_log_to_stdout
     85   verbosity_level = level
     86   echo_log_to_stdout = alsologtostdout
     87 
     88 
     89 @tf_export('autograph.trace')
     90 def trace(*args):
     91   """Traces argument information at compilation time.
     92 
     93   `trace` is useful when debugging, and it always executes during the tracing
     94   phase, that is, when the TF graph is constructed.
     95 
     96   _Example usage_
     97 
     98   ```python
     99   import tensorflow as tf
    100 
    101   for i in tf.range(10):
    102     tf.autograph.trace(i)
    103   # Output: <Tensor ...>
    104   ```
    105 
    106   Args:
    107     *args: Arguments to print to `sys.stdout`.
    108   """
    109   print(*args)
    110 
    111 
    112 def get_verbosity():
    113   global verbosity_level
    114   if verbosity_level is not None:
    115     return verbosity_level
    116   return int(os.getenv(VERBOSITY_VAR_NAME, DEFAULT_VERBOSITY))
    117 
    118 
    119 def has_verbosity(level):
    120   return get_verbosity() >= level
    121 
    122 
    123 def error(level, msg, *args, **kwargs):
    124   if has_verbosity(level):
    125     logging.error(msg, *args, **kwargs)
    126     if echo_log_to_stdout:
    127       print(msg % args)
    128 
    129 
    130 def log(level, msg, *args, **kwargs):
    131   if has_verbosity(level):
    132     logging.info(msg, *args, **kwargs)
    133     if echo_log_to_stdout:
    134       print(msg % args)
    135 
    136 
    137 def warn(msg, *args, **kwargs):
    138   logging.warn(msg, *args, **kwargs)
    139   if echo_log_to_stdout:
    140     print('WARNING:', msg % args)
    141 
    142 
    143 def warn_first_n(msg, *args, **kwargs):
    144   logging.log_first_n(logging.WARN, msg, *args, **kwargs)
    145