1# Copyright 2015 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 16"""Logging utilities.""" 17# pylint: disable=unused-import 18# pylint: disable=g-bad-import-order 19# pylint: disable=invalid-name 20from __future__ import absolute_import 21from __future__ import division 22from __future__ import print_function 23 24import logging as _logging 25import os as _os 26import sys as _sys 27import time as _time 28import traceback as _traceback 29from logging import DEBUG 30from logging import ERROR 31from logging import FATAL 32from logging import INFO 33from logging import WARN 34import threading 35 36import six 37 38from tensorflow.python.util.tf_export import tf_export 39 40# Don't use this directly. Use get_logger() instead. 41_logger = None 42_logger_lock = threading.Lock() 43 44 45def _get_caller(offset=3): 46 """Returns a code and frame object for the lowest non-logging stack frame.""" 47 # Use sys._getframe(). This avoids creating a traceback object. 48 # pylint: disable=protected-access 49 f = _sys._getframe(offset) 50 # pylint: enable=protected-access 51 our_file = f.f_code.co_filename 52 f = f.f_back 53 while f: 54 code = f.f_code 55 if code.co_filename != our_file: 56 return code, f 57 f = f.f_back 58 return None, None 59 60 61# The definition of `findCaller` changed in Python 3.2 62if _sys.version_info.major >= 3 and _sys.version_info.minor >= 2: 63 def _logger_find_caller(stack_info=False): # pylint: disable=g-wrong-blank-lines 64 code, frame = _get_caller(4) 65 sinfo = None 66 if stack_info: 67 sinfo = '\n'.join(_traceback.format_stack()) 68 if code: 69 return (code.co_filename, frame.f_lineno, code.co_name, sinfo) 70 else: 71 return '(unknown file)', 0, '(unknown function)', sinfo 72else: 73 def _logger_find_caller(): # pylint: disable=g-wrong-blank-lines 74 code, frame = _get_caller(4) 75 if code: 76 return (code.co_filename, frame.f_lineno, code.co_name) 77 else: 78 return '(unknown file)', 0, '(unknown function)' 79 80 81@tf_export('get_logger') 82def get_logger(): 83 """Return TF logger instance.""" 84 global _logger 85 86 # Use double-checked locking to avoid taking lock unnecessarily. 87 if _logger: 88 return _logger 89 90 _logger_lock.acquire() 91 92 try: 93 if _logger: 94 return _logger 95 96 # Scope the TensorFlow logger to not conflict with users' loggers. 97 logger = _logging.getLogger('tensorflow') 98 99 # Override findCaller on the logger to skip internal helper functions 100 logger.findCaller = _logger_find_caller 101 102 # Don't further configure the TensorFlow logger if the root logger is 103 # already configured. This prevents double logging in those cases. 104 if not _logging.getLogger().handlers: 105 # Determine whether we are in an interactive environment 106 _interactive = False 107 try: 108 # This is only defined in interactive shells. 109 if _sys.ps1: _interactive = True 110 except AttributeError: 111 # Even now, we may be in an interactive shell with `python -i`. 112 _interactive = _sys.flags.interactive 113 114 # If we are in an interactive environment (like Jupyter), set loglevel 115 # to INFO and pipe the output to stdout. 116 if _interactive: 117 logger.setLevel(INFO) 118 _logging_target = _sys.stdout 119 else: 120 _logging_target = _sys.stderr 121 122 # Add the output handler. 123 _handler = _logging.StreamHandler(_logging_target) 124 _handler.setFormatter(_logging.Formatter(_logging.BASIC_FORMAT, None)) 125 logger.addHandler(_handler) 126 127 _logger = logger 128 return _logger 129 130 finally: 131 _logger_lock.release() 132 133 134@tf_export(v1=['logging.log']) 135def log(level, msg, *args, **kwargs): 136 get_logger().log(level, msg, *args, **kwargs) 137 138 139@tf_export(v1=['logging.debug']) 140def debug(msg, *args, **kwargs): 141 get_logger().debug(msg, *args, **kwargs) 142 143 144@tf_export(v1=['logging.error']) 145def error(msg, *args, **kwargs): 146 get_logger().error(msg, *args, **kwargs) 147 148 149@tf_export(v1=['logging.fatal']) 150def fatal(msg, *args, **kwargs): 151 get_logger().fatal(msg, *args, **kwargs) 152 153 154@tf_export(v1=['logging.info']) 155def info(msg, *args, **kwargs): 156 get_logger().info(msg, *args, **kwargs) 157 158 159@tf_export(v1=['logging.warn']) 160def warn(msg, *args, **kwargs): 161 get_logger().warn(msg, *args, **kwargs) 162 163 164@tf_export(v1=['logging.warning']) 165def warning(msg, *args, **kwargs): 166 get_logger().warning(msg, *args, **kwargs) 167 168 169_level_names = { 170 FATAL: 'FATAL', 171 ERROR: 'ERROR', 172 WARN: 'WARN', 173 INFO: 'INFO', 174 DEBUG: 'DEBUG', 175} 176 177# Mask to convert integer thread ids to unsigned quantities for logging 178# purposes 179_THREAD_ID_MASK = 2 * _sys.maxsize + 1 180 181_log_prefix = None # later set to google2_log_prefix 182 183# Counter to keep track of number of log entries per token. 184_log_counter_per_token = {} 185 186 187@tf_export(v1=['logging.TaskLevelStatusMessage']) 188def TaskLevelStatusMessage(msg): 189 error(msg) 190 191 192@tf_export(v1=['logging.flush']) 193def flush(): 194 raise NotImplementedError() 195 196 197# Code below is taken from pyglib/logging 198@tf_export(v1=['logging.vlog']) 199def vlog(level, msg, *args, **kwargs): 200 get_logger().log(level, msg, *args, **kwargs) 201 202 203def _GetNextLogCountPerToken(token): 204 """Wrapper for _log_counter_per_token. 205 206 Args: 207 token: The token for which to look up the count. 208 209 Returns: 210 The number of times this function has been called with 211 *token* as an argument (starting at 0) 212 """ 213 global _log_counter_per_token # pylint: disable=global-variable-not-assigned 214 _log_counter_per_token[token] = 1 + _log_counter_per_token.get(token, -1) 215 return _log_counter_per_token[token] 216 217 218@tf_export(v1=['logging.log_every_n']) 219def log_every_n(level, msg, n, *args): 220 """Log 'msg % args' at level 'level' once per 'n' times. 221 222 Logs the 1st call, (N+1)st call, (2N+1)st call, etc. 223 Not threadsafe. 224 225 Args: 226 level: The level at which to log. 227 msg: The message to be logged. 228 n: The number of times this should be called before it is logged. 229 *args: The args to be substituted into the msg. 230 """ 231 count = _GetNextLogCountPerToken(_GetFileAndLine()) 232 log_if(level, msg, not (count % n), *args) 233 234 235@tf_export(v1=['logging.log_first_n']) 236def log_first_n(level, msg, n, *args): # pylint: disable=g-bad-name 237 """Log 'msg % args' at level 'level' only first 'n' times. 238 239 Not threadsafe. 240 241 Args: 242 level: The level at which to log. 243 msg: The message to be logged. 244 n: The number of times this should be called before it is logged. 245 *args: The args to be substituted into the msg. 246 """ 247 count = _GetNextLogCountPerToken(_GetFileAndLine()) 248 log_if(level, msg, count < n, *args) 249 250 251@tf_export(v1=['logging.log_if']) 252def log_if(level, msg, condition, *args): 253 """Log 'msg % args' at level 'level' only if condition is fulfilled.""" 254 if condition: 255 vlog(level, msg, *args) 256 257 258def _GetFileAndLine(): 259 """Returns (filename, linenumber) for the stack frame.""" 260 code, f = _get_caller() 261 if not code: 262 return ('<unknown>', 0) 263 return (code.co_filename, f.f_lineno) 264 265 266def google2_log_prefix(level, timestamp=None, file_and_line=None): 267 """Assemble a logline prefix using the google2 format.""" 268 # pylint: disable=global-variable-not-assigned 269 global _level_names 270 # pylint: enable=global-variable-not-assigned 271 272 # Record current time 273 now = timestamp or _time.time() 274 now_tuple = _time.localtime(now) 275 now_microsecond = int(1e6 * (now % 1.0)) 276 277 (filename, line) = file_and_line or _GetFileAndLine() 278 basename = _os.path.basename(filename) 279 280 # Severity string 281 severity = 'I' 282 if level in _level_names: 283 severity = _level_names[level][0] 284 285 s = '%c%02d%02d %02d:%02d:%02d.%06d %5d %s:%d] ' % ( 286 severity, 287 now_tuple[1], # month 288 now_tuple[2], # day 289 now_tuple[3], # hour 290 now_tuple[4], # min 291 now_tuple[5], # sec 292 now_microsecond, 293 _get_thread_id(), 294 basename, 295 line) 296 297 return s 298 299 300@tf_export(v1=['logging.get_verbosity']) 301def get_verbosity(): 302 """Return how much logging output will be produced.""" 303 return get_logger().getEffectiveLevel() 304 305 306@tf_export(v1=['logging.set_verbosity']) 307def set_verbosity(v): 308 """Sets the threshold for what messages will be logged.""" 309 get_logger().setLevel(v) 310 311 312def _get_thread_id(): 313 """Get id of current thread, suitable for logging as an unsigned quantity.""" 314 # pylint: disable=protected-access 315 thread_id = six.moves._thread.get_ident() 316 # pylint:enable=protected-access 317 return thread_id & _THREAD_ID_MASK 318 319 320_log_prefix = google2_log_prefix 321 322tf_export(v1=['logging.DEBUG']).export_constant(__name__, 'DEBUG') 323tf_export(v1=['logging.ERROR']).export_constant(__name__, 'ERROR') 324tf_export(v1=['logging.FATAL']).export_constant(__name__, 'FATAL') 325tf_export(v1=['logging.INFO']).export_constant(__name__, 'INFO') 326tf_export(v1=['logging.WARN']).export_constant(__name__, 'WARN') 327