• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17import os
18import sys
19import traceback
20
21# TODO(mdan): Use a custom logger class.
22from tensorflow.python.platform import tf_logging as logging
23from tensorflow.python.util.tf_export import tf_export
24
25VERBOSITY_VAR_NAME = 'AUTOGRAPH_VERBOSITY'
26DEFAULT_VERBOSITY = 0
27
28verbosity_level = None  # vlog-like. Takes precedence over the env variable.
29echo_log_to_stdout = False
30
31# In interactive Python, logging echo is enabled by default.
32if hasattr(sys, 'ps1') or hasattr(sys, 'ps2'):
33  echo_log_to_stdout = True
34
35
36@tf_export('autograph.set_verbosity')
37def set_verbosity(level, alsologtostdout=False):
38  """Sets the AutoGraph verbosity level.
39
40  _Debug logging in AutoGraph_
41
42  More verbose logging is useful to enable when filing bug reports or doing
43  more in-depth debugging.
44
45  There are two means to control the logging verbosity:
46
47   * The `set_verbosity` function
48
49   * The `AUTOGRAPH_VERBOSITY` environment variable
50
51  `set_verbosity` takes precedence over the environment variable.
52
53  For example:
54
55  ```python
56  import os
57  import tensorflow as tf
58
59  os.environ['AUTOGRAPH_VERBOSITY'] = '5'
60  # Verbosity is now 5
61
62  tf.autograph.set_verbosity(0)
63  # Verbosity is now 0
64
65  os.environ['AUTOGRAPH_VERBOSITY'] = '1'
66  # No effect, because set_verbosity was already called.
67  ```
68
69  Logs entries are output to [absl](https://abseil.io)'s
70  [default output](https://abseil.io/docs/python/guides/logging),
71  with `INFO` level.
72  Logs can be mirrored to stdout by using the `alsologtostdout` argument.
73  Mirroring is enabled by default when Python runs in interactive mode.
74
75  Args:
76    level: int, the verbosity level; larger values specify increased verbosity;
77      0 means no logging. When reporting bugs, it is recommended to set this
78      value to a larger number, like 10.
79    alsologtostdout: bool, whether to also output log messages to `sys.stdout`.
80  """
81  global verbosity_level
82  global echo_log_to_stdout
83  verbosity_level = level
84  echo_log_to_stdout = alsologtostdout
85
86
87@tf_export('autograph.trace')
88def trace(*args):
89  """Traces argument information at compilation time.
90
91  `trace` is useful when debugging, and it always executes during the tracing
92  phase, that is, when the TF graph is constructed.
93
94  _Example usage_
95
96  ```python
97  import tensorflow as tf
98
99  for i in tf.range(10):
100    tf.autograph.trace(i)
101  # Output: <Tensor ...>
102  ```
103
104  Args:
105    *args: Arguments to print to `sys.stdout`.
106  """
107  print(*args)
108
109
110def get_verbosity():
111  global verbosity_level
112  if verbosity_level is not None:
113    return verbosity_level
114  return int(os.getenv(VERBOSITY_VAR_NAME, DEFAULT_VERBOSITY))
115
116
117def has_verbosity(level):
118  return get_verbosity() >= level
119
120
121def _output_to_stdout(msg, *args, **kwargs):
122  print(msg % args)
123  if kwargs.get('exc_info', False):
124    traceback.print_exc()
125
126
127def error(level, msg, *args, **kwargs):
128  if has_verbosity(level):
129    logging.error(msg, *args, **kwargs)
130    if echo_log_to_stdout:
131      _output_to_stdout('ERROR: ' + msg, *args, **kwargs)
132
133
134def log(level, msg, *args, **kwargs):
135  if has_verbosity(level):
136    logging.info(msg, *args, **kwargs)
137    if echo_log_to_stdout:
138      _output_to_stdout(msg, *args, **kwargs)
139
140
141def warning(msg, *args, **kwargs):
142  logging.warning(msg, *args, **kwargs)
143  if echo_log_to_stdout:
144    _output_to_stdout('WARNING: ' + msg, *args, **kwargs)
145    sys.stdout.flush()
146