Home | History | Annotate | Download | only in video
      1 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 
      7 
      8 def log(method_to_log):
      9     """ A decorator method to log when 'decorated' methods have been executed.
     10      This greatly simplifies tracing of the method calls.
     11 
     12      To log execution of a method just decorate it with *log
     13 
     14      The decorator logs the method to be executed, its class, the arguments
     15      supplied to it and its return value.
     16 
     17      @param method_to_log: Method object that will be logged and invoked.
     18 
     19     """
     20     def log_wrapper(self, *args, **kwargs):
     21         """ Actual method doing the logging and also invokes method_to_log
     22         """
     23 
     24         log_str = '%s.%s' % (self.__class__.__name__, method_to_log.__name__)
     25 
     26         logging.debug('+ ' + log_str)
     27 
     28         have_args = len(args) > 0
     29         have_kwargs = len(kwargs) > 0
     30 
     31         if have_args:
     32             logging.debug('*** Begin arguments:')
     33             logging.debug(args)
     34             logging.debug('=== End arguments.')
     35 
     36         if have_kwargs:
     37             logging.debug('*** Begin keyword arguments:')
     38             logging.debug(kwargs)
     39             logging.debug('=== End keyword arguments.')
     40 
     41         result = method_to_log(self, *args, **kwargs)
     42 
     43         if result is not None:
     44             logging.debug('### Begin results :')
     45             logging.debug(result)
     46             logging.debug('--- End results.')
     47 
     48         logging.debug('- ' + log_str)
     49 
     50         return result
     51 
     52     return log_wrapper