logger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import traceback
3 import rospy
4 
5 
6 class Logger(object):
7  """ Bundles static methods for test case logging. """
8 
9  @classmethod
10  def _param_positive(cls):
11  return not cls._param_compact() and rospy.get_param('~print_debug_positive', True)
12 
13  @classmethod
14  def _param_negative(cls):
15  return cls._param_compact() or rospy.get_param('~print_debug_negative', True)
16 
17  @classmethod
18  def _param_compact(cls):
19  return rospy.get_param('~compact_format', False)
20 
21  @classmethod
22  def _prefix(cls):
23  return ' >' if cls._param_compact() else '>>>'
24 
25  @classmethod
26  def _counter(cls):
27  cls._counter_value += 1
28  return cls._counter_value
29  _counter_value = 0
30 
31  @classmethod
32  def mute_rospy(cls):
33  """ Conditionally mute the rospy logging channels. """
34  if cls._param_compact() or rospy.get_param('~mute_info', False):
35  rospy.loginfo = rospy.logdebug
36  if cls._param_compact() or rospy.get_param('~mute_warn', False):
37  rospy.logwarn = rospy.logdebug
38  if not cls._param_compact() and rospy.get_param('~mute_error', False):
39  rospy.logerr = rospy.logdebug
40 
41  @classmethod
42  def print_positive(cls, text):
43  """ Print a positive intermediate result. """
44  if cls._param_positive():
45  print('\033[0m\033[1m +\033[0m %s' % str(text))
46 
47  @classmethod
48  def print_negative(cls, text):
49  """ Print a negative intermediate result. """
50  if cls._param_negative():
51  print('\033[0m\033[1m -\033[0m %s' % str(text))
52 
53  @classmethod
54  def print_title(cls, test_name, test_class, result=None):
55  """ Print the title of the test, should be called once and before any other print method. """
56  test_result = ' > %s' % result if result is not None else ''
57  print('\033[34;1m#%2d %s \033[0m\033[34m(%s%s)\033[0m' % (
58  cls._counter(), test_name, test_class, test_result
59  ))
60 
61  @classmethod
62  def print_result(cls, test_name, success):
63  """ Print the result, should be called once and after any other print method. """
64  test_result = 'completed' if success else 'failed'
65  color = '32' if success else '31'
66  print('\033[%s;1m%s\033[0m\033[%sm %s %s!\033[0m' % (color, cls._prefix(), color, test_name, test_result))
67 
68  @classmethod
69  def print_failure(cls, text):
70  """ Instead of a result, print the failure of a test case once after any other print method. """
71  traceback.print_exc()
72  print('\033[31;1m%s\033[0m\033[31m %s\033[0m' % (cls._prefix(), str(text)))
73 
74  @classmethod
75  def print_error(cls, text):
76  """ Print an internal error that might cause unexpected behavior, but does not cause failure itself. """
77  print('\033[33;1m \033[0m\033[33m %s\033[0m' % str(text))
78 
79  def __init__(self):
80  """ DO NOT USE: use class print methods instead. """
81  raise NotImplementedError("use static methods and attributes")
def print_result(cls, test_name, success)
Definition: logger.py:62
def print_positive(cls, text)
Definition: logger.py:42
def print_negative(cls, text)
Definition: logger.py:48
def print_error(cls, text)
Definition: logger.py:75
def print_failure(cls, text)
Definition: logger.py:69
def print_title(cls, test_name, test_class, result=None)
Definition: logger.py:54


flexbe_testing
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:44