Author: reinhard Date: 2007-07-24 17:42:50 -0500 (Tue, 24 Jul 2007) New Revision: 9767
Modified: trunk/gnue-common/src/base/log.py Log: Added logging functions to debug the call stack at a given point. issue123 in-progress Modified: trunk/gnue-common/src/base/log.py =================================================================== --- trunk/gnue-common/src/base/log.py 2007-07-24 22:26:45 UTC (rev 9766) +++ trunk/gnue-common/src/base/log.py 2007-07-24 22:42:50 UTC (rev 9767) @@ -39,10 +39,12 @@ __all__ = ['logged_f', 'deprecated_f', 'logged_f_n', 'deprecated_f_n', + 'excepthook', + 'exception', 'stack', + 'exception_n', 'stack_n', 'debug', 'info', 'warning', 'deprecated', 'error', 'critical', - 'exception', 'debug_n', 'info_n', 'warning_n', 'deprecated_n', 'error_n', - 'critical_n', 'exception_n'] + 'critical_n'] # TODO: # - allow for __gnue_logger__ module global variable to use as a logger name @@ -318,6 +320,64 @@ exc_info=(etype, evalue, etraceback)) # ----------------------------------------------------------------------------- +# Exception and call stack logging +# ----------------------------------------------------------------------------- + +def exception(msg, *args, **kwargs): + """ + Log an exception to the logger named after the calling module. + + This function can be used in a try...except block to log an exception + before it is discarded. + + @param msg: Message to log. + @type msg: string or unicode + @return: always True, so the call can be prefixed with assert. + @rtype: bool + """ + return exception_n(__caller(), msg, *args, **kwargs) + +# ----------------------------------------------------------------------------- + +def stack(msg, *args, **kwargs): + """ + Write a debug message to the logger named after the calling module + including information about the current call stack. + """ + debug(msg + '\n' + ''.join(traceback.format_stack( + inspect.currentframe().f_back)), *args, **kwargs) + +# ----------------------------------------------------------------------------- + +def exception_n(name, msg, *args, **kwargs): + """ + Log an exception to the logger defined through the C{name} parameter. + + This function can be used in a try...except block to log an exception + before it is discarded. + + @param name: Logger name to log to. + @type name: string + @param msg: Message to log. + @type msg: string or unicode + @return: always True, so the call can be prefixed with assert. + @rtype: bool + """ + logging.getLogger(name).exception(msg, *args, **kwargs) + return True + +# ----------------------------------------------------------------------------- + +def stack_n(name, msg, *args, **kwargs): + """ + Write a debug message to the logger named after the calling module + including information about the current call stack. + """ + debug_n(name, msg + '\n' + ''.join(traceback.format_stack( + inspect.currentframe().f_back)), *args, **kwargs) + + +# ----------------------------------------------------------------------------- # Log to the logger named after the calling module # ----------------------------------------------------------------------------- @@ -400,22 +460,6 @@ # ----------------------------------------------------------------------------- -def exception(msg, *args, **kwargs): - """ - Log an exception to the logger named after the calling module. - - This function can be used in a try...except block to log an exception - before it is discarded. - - @param msg: Message to log. - @type msg: string or unicode - @return: always True, so the call can be prefixed with assert. - @rtype: bool - """ - return exception_n(__caller(), msg, *args, **kwargs) - -# ----------------------------------------------------------------------------- - def __caller(): return inspect.stack()[2][0].f_globals['__name__'] @@ -520,26 +564,7 @@ logging.getLogger(name).critical(msg, *args, **kwargs) return True -# ----------------------------------------------------------------------------- -def exception_n(name, msg, *args, **kwargs): - """ - Log an exception to the logger defined through the C{name} parameter. - - This function can be used in a try...except block to log an exception - before it is discarded. - - @param name: Logger name to log to. - @type name: string - @param msg: Message to log. - @type msg: string or unicode - @return: always True, so the call can be prefixed with assert. - @rtype: bool - """ - logging.getLogger(name).exception(msg, *args, **kwargs) - return True - - # ----------------------------------------------------------------------------- # Derivate from standard Logger to output correct caller information # ----------------------------------------------------------------------------- _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue