blob: 80038912b8d0844e85ba9bb0f1736ffdd25ea17d [file] [log] [blame]
Deepak Panickal9b35cf522014-07-01 17:57:19 +00001""" Utility module to help debug Python scripts
2
Bruce Mitchenera18231a2015-11-05 23:57:33 +00003 --------------------------------------------------------------------------
4 File: utilsDebug.py
Deepak Panickal9b35cf522014-07-01 17:57:19 +00005
Bruce Mitchenera18231a2015-11-05 23:57:33 +00006 Overview: Python module to supply functions to help debug Python
7 scripts.
8 Gotchas: None.
9 Copyright: None.
10 --------------------------------------------------------------------------
Deepak Panickal9b35cf522014-07-01 17:57:19 +000011"""
12
13# Python modules:
Bruce Mitchenera18231a2015-11-05 23:57:33 +000014import sys
Deepak Panickal9b35cf522014-07-01 17:57:19 +000015
16# Third party modules:
17
18# In-house modules:
19
20# Instantiations:
21
22#-----------------------------------------------------------------------------
Bruce Mitchenera18231a2015-11-05 23:57:33 +000023# Details: Class to implement simple stack function trace. Instantiation the
24# class as the first function you want to trace. Example:
25# obj = utilsDebug.CDebugFnVerbose("validate_arguments()")
26# Gotchas: This class will not work in properly in a multi-threaded
27# environment.
28# Authors: Illya Rudkin 28/11/2013.
29# Changes: None.
Deepak Panickal9b35cf522014-07-01 17:57:19 +000030#--
Kate Stoneb9c1b512016-09-06 20:57:50 +000031
32
Bruce Mitchenera18231a2015-11-05 23:57:33 +000033class CDebugFnVerbose(object):
34 # Public static properties:
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 bVerboseOn = False # True = turn on function tracing, False = turn off.
Deepak Panickal9b35cf522014-07-01 17:57:19 +000036
Bruce Mitchenera18231a2015-11-05 23:57:33 +000037 # Public:
38 #++------------------------------------------------------------------------
39 # Details: CDebugFnVerbose constructor.
40 # Type: Method.
41 # Args: vstrFnName - (R) Text description i.e. a function name.
42 # Return: None.
43 # Throws: None.
44 #--
45 # CDebugFnVerbose(vstrFnName)
Deepak Panickal9b35cf522014-07-01 17:57:19 +000046
Bruce Mitchenera18231a2015-11-05 23:57:33 +000047 #++------------------------------------------------------------------------
48 # Details: Print out information on the object specified.
49 # Type: Method.
50 # Args: vstrText - (R) Some helper text description.
51 # vObject - (R) Some Python type object.
52 # Return: None.
53 # Throws: None.
54 #--
55 def dump_object(self, vstrText, vObject):
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 if not CDebugFnVerbose.bVerboseOn:
Bruce Mitchenera18231a2015-11-05 23:57:33 +000057 return
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 sys.stdout.write(
59 "%d%s> Dp: %s" %
60 (CDebugFnVerbose.__nLevel,
61 self.__get_dots(),
62 vstrText))
Bruce Mitchenera18231a2015-11-05 23:57:33 +000063 print(vObject)
64
65 #++------------------------------------------------------------------------
66 # Details: Print out some progress text given by the client.
67 # Type: Method.
68 # Args: vstrText - (R) Some helper text description.
69 # Return: None.
70 # Throws: None.
71 #--
72 def dump_text(self, vstrText):
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 if not CDebugFnVerbose.bVerboseOn:
Bruce Mitchenera18231a2015-11-05 23:57:33 +000074 return
75 print(("%d%s> Dp: %s" % (CDebugFnVerbose.__nLevel, self.__get_dots(),
76 vstrText)))
77
78 # Private methods:
79 def __init__(self, vstrFnName):
80 self.__indent_out(vstrFnName)
81
82 #++------------------------------------------------------------------------
83 # Details: Build an indentation string of dots based on the __nLevel.
84 # Type: Method.
85 # Args: None.
86 # Return: Str - variable length string.
87 # Throws: None.
88 #--
89 def __get_dots(self):
90 return "".join("." for i in range(0, CDebugFnVerbose.__nLevel))
91
92 #++------------------------------------------------------------------------
93 # Details: Build and print out debug verbosity text indicating the function
94 # just exited from.
95 # Type: Method.
96 # Args: None.
97 # Return: None.
98 # Throws: None.
99 #--
100 def __indent_back(self):
101 if CDebugFnVerbose.bVerboseOn:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 print(("%d%s< fn: %s" % (CDebugFnVerbose.__nLevel,
103 self.__get_dots(), self.__strFnName)))
Bruce Mitchenera18231a2015-11-05 23:57:33 +0000104 CDebugFnVerbose.__nLevel -= 1
105
106 #++------------------------------------------------------------------------
107 # Details: Build and print out debug verbosity text indicating the function
108 # just entered.
109 # Type: Method.
110 # Args: vstrFnName - (R) Name of the function entered.
111 # Return: None.
112 # Throws: None.
113 #--
114 def __indent_out(self, vstrFnName):
115 CDebugFnVerbose.__nLevel += 1
116 self.__strFnName = vstrFnName
117 if CDebugFnVerbose.bVerboseOn:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 print(("%d%s> fn: %s" % (CDebugFnVerbose.__nLevel,
119 self.__get_dots(), self.__strFnName)))
Bruce Mitchenera18231a2015-11-05 23:57:33 +0000120
121 # Private statics attributes:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 __nLevel = 0 # Indentation level counter
Bruce Mitchenera18231a2015-11-05 23:57:33 +0000123
124 # Private attributes:
125 __strFnName = ""