@@ -315,12 +315,16 @@ def reset_call_stack(self, **kwargs) -> None:
315
315
if self .is_on_level (level ):
316
316
self .__send_log_entry (level , None , LogEntryType .RESET_CALLSTACK , ViewerId .NO_VIEWER )
317
317
318
- def enter_method (self , method_name : str , * args , ** kwargs ) -> None :
318
+ def enter_method (self , method_name : str = "" , * args , ** kwargs ) -> None :
319
319
"""
320
320
This method used to enter a method using default level or custom log level (if provided via kwargs).
321
- The resulting method name consists of the method_name string formatted using
322
- optional args and kwargs. The EnterMethod method notifies the Console
323
- that a new method has been entered. The Console includes the method in the
321
+ If a method name string is provided via method_name argument, the resulting method name consists
322
+ of the method_name string formatted using optional args and kwargs.
323
+ If the default value is used (empty string) for the method name, SmartInspect will try
324
+ to get the calling method name out of the stack together with the module name and line of code where
325
+ this method is located.
326
+ The enter_method() method notifies the Console that a new method has been entered.
327
+ The Console includes the method in the
324
328
method hierarchy. If this method is used consequently, a full call stack
325
329
is visible in the console which helps in locating bugs in the source code.
326
330
Please see the leave_method() method as the counter piece to enter_method().
@@ -343,18 +347,45 @@ def enter_method(self, method_name: str, *args, **kwargs) -> None:
343
347
try :
344
348
if not isinstance (method_name , str ):
345
349
raise TypeError ('Method name must be a string' )
346
- method_name = method_name .format (* args , ** kwargs )
347
-
348
- instance = kwargs .get ("instance" )
349
- if instance is not None :
350
- class_name = instance .__class__ .__name__
351
- method_name = f"{ class_name } .{ method_name } "
350
+ if method_name :
351
+ method_name = method_name .format (* args , ** kwargs )
352
+
353
+ instance = kwargs .get ("instance" )
354
+ if instance is not None :
355
+ class_name = instance .__class__ .__name__
356
+ method_name = f"{ class_name } .{ method_name } "
357
+ else :
358
+ method_name = self ._get_method_name ()
352
359
except Exception as e :
353
360
return self .__process_internal_error (e )
354
361
355
362
self .__send_log_entry (level , method_name , LogEntryType .ENTER_METHOD , ViewerId .TITLE )
356
363
self .__send_process_flow (level , method_name , ProcessFlowType .ENTER_METHOD )
357
364
365
+ # noinspection PyBroadException
366
+ @staticmethod
367
+ def _get_method_name () -> str :
368
+ method_name = "<Unknown>"
369
+
370
+ try :
371
+ stack_frame = inspect .stack ()[2 ]
372
+ if stack_frame is None :
373
+ return method_name
374
+
375
+ # extract the parts of the stack frame.
376
+ filepath , line , func_name = stack_frame [1 :4 ]
377
+ method_name = func_name .strip ()
378
+ module_name = os .path .basename (filepath )
379
+
380
+ # add source position to method name.
381
+ if module_name is not None :
382
+ method_name += " ({0}, line {1})" .format (module_name , line )
383
+
384
+ return method_name
385
+
386
+ except Exception :
387
+ return method_name
388
+
358
389
def __process_internal_error (self , e : Exception ) -> None :
359
390
tb = e .__traceback__
360
391
calling_method_name = traceback .extract_tb (tb )[- 1 ].name
@@ -372,11 +403,15 @@ def __get_level(self, **kwargs):
372
403
373
404
return level
374
405
375
- def leave_method (self , method_name : str , * args , ** kwargs ) -> None :
406
+ def leave_method (self , method_name : str = "" , * args , ** kwargs ) -> None :
376
407
"""
377
408
Leaves a method by using default level or custom log level (if provided via kwargs).
378
- The resulting method name consists of the method_name string formatted using
379
- optional args and kwargs. The leave_method() method notifies the Console that a method
409
+ If a method name string is provided via method_name argument, the resulting method name consists
410
+ of the method_name string formatted using optional args and kwargs.
411
+ If the default value is used (empty string) for the method name, SmartInspect will try
412
+ to get the calling method name out of the stack together with the module name and line of code where
413
+ this method is located.
414
+ The leave_method() method notifies the Console that a method
380
415
has been left. The Console closes the current method in the method hierarchy. If this method is used
381
416
consequently, a full call stack is visible in the Console which helps locate bugs in the source code.
382
417
Please see the enter_method() method as the counter piece to leave_method().
@@ -397,11 +432,14 @@ def leave_method(self, method_name: str, *args, **kwargs) -> None:
397
432
try :
398
433
if not isinstance (method_name , str ):
399
434
raise TypeError ('Method name must be a string' )
400
- method_name = method_name .format (* args , ** kwargs )
401
- instance = kwargs .get ("instance" )
402
- if instance is not None :
403
- class_name = instance .__class__ .__name__
404
- method_name = f"{ class_name } .{ method_name } "
435
+ if method_name :
436
+ method_name = method_name .format (* args , ** kwargs )
437
+ instance = kwargs .get ("instance" )
438
+ if instance is not None :
439
+ class_name = instance .__class__ .__name__
440
+ method_name = f"{ class_name } .{ method_name } "
441
+ else :
442
+ method_name = self ._get_method_name ()
405
443
except Exception as e :
406
444
return self .__process_internal_error (e )
407
445
0 commit comments