Package Products :: Package ZenUtils :: Module deprecated |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.deprecated

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2011, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  import Globals
    12  import logging
    13  import traceback
    14  from decorator import decorator
    15  from Products.ZenUtils.Utils import zenPath
    16
    17
    18 -class DeprecatedLogger(object):
    19 -    def __init__(self):
    20          self.loggedFunctions = set()
    21          self.config()
    22
    23 -    def config(self, repeat=False, fileName='deprecated.log', delay=True, propagate=True):
    24          """
    25          Initialize or change the behavior of @deprecated.
    26
    27          @param repeat: Log every time the same deprecated function is called, or just once?
    28          @type repeat: boolean
    29          @param fileName: Name of the file, or None for no file.
    30          @type fileName: string
    31          @param delay: Prevent creating the log file until used?
    32          @type delay: boolean
    33          @param propagate: Also log to current file/screen?
    34          @type propagate: boolean
    35          """
    36          self.log = logging.getLogger('zen.deprecated')
    37          # Remove the handler to start from scratch.
    38          if self.log.handlers:
    39              self.log.removeHandler(self.log.handlers[0])
    40          # New settings
    41          self.repeat = repeat
    42          self.log.propagate = propagate
    43          if fileName:
    44              filePath = zenPath('log', fileName)
    45              handler = logging.FileHandler(filePath, delay=delay)
    46              # 2011-11-02 17:44:43,674 WARNING zen.deprecated: ...
    47              formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s: %(message)s')
    48              handler.setFormatter(formatter)
    49              self.log.addHandler(handler)
    50          else:
    51              self.log.addHandler(logging.NullHandler())
    52
    53 -    def logFunction(self, func):
    54          if self.repeat or not func in self.loggedFunctions:
    55              # 2011-11-02 17:51:52,314 WARNING zen.deprecated: Call to deprecated function audit
    56              # Source: /Users/philbowman/zen/home/Products/ZenMessaging/audit/__init__.py:57
    57              # Traceback: ...
    58              stack = ''.join(traceback.format_stack()[:-3])  # exclude this stuff
    59              self.log.warn(
    60                  "Call to deprecated function %s\nSource: %s:%d\nTraceback:%s",
    61                  func.__name__,
    62                  func.func_code.co_filename,
    63                  func.func_code.co_firstlineno + 1,
    64                  stack)
    65
    66          self.loggedFunctions.add(func)
    67
    68 -    def __call__(self): return self     # these 2 lines make this a singleton
    69  DeprecatedLogger = DeprecatedLogger()   # and initialize it on startup
    70
    71
    72  @decorator
    73 -def deprecated(func, *args, **kwargs):
    74      """
    75      This can be used to mark functions as deprecated.
    76      If the function is used it will log a warning to the current
    77      log file and $ZENHOME/log/deprecated.log
    78      """
    79      if Globals.DevelopmentMode:   # Never show to customers.
    80          DeprecatedLogger.logFunction(func)
    81
    82      return func(*args, **kwargs)
    83