2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 class RarslaveMessage (object):
5 def __init__ (self, msg, printquiet):
7 self.printquiet = printquiet
10 return "%s" % self.msg
13 # A message is verbose only if we should
14 # print it when we are not printing quietly
15 return not self.printquiet
17 class RarslaveLogger (object):
18 # This class will log multiple messages, one for each set that is operated on.
19 # It will then be able to print all of them out.
24 def __get_key (self, id):
25 """__get_key (id) -> key"""
26 # Get a valid key which will always be the same as any other similar key, even if
27 # the actual memory reference is different.
30 def addMessage (self, id, msg, printquiet=True):
31 # Add a message to the dictionary, to be printed later.
32 # The printquiet variable controls whether the message will be printed
33 # normally, or only in verbose mode. The default is to print always.
36 key = self.__get_key (id)
38 # Create the dictionary entry if necessary
39 if key not in self.__msg_dict.keys():
40 self.__msg_dict[key] = []
42 # Get the list from the dictionary, then add the message to it
43 self.__msg_dict[key].append (RarslaveMessage (msg, printquiet))
45 def printAllMessages (self, verbose=False):
46 # Print all messages in groups by id
47 for k in self.__msg_dict.keys():
48 for msg in self.__msg_dict[k]:
50 # Skip verbose messages if we're not in verbose mode
51 if msg.isVerbose() and not verbose:
59 logger = RarslaveLogger ()
60 logger.addMessage (1, 'hello 1')
61 logger.addMessage (2, 'hello 2')
62 logger.addMessage (2, 'world 2')
63 logger.addMessage (1, 'world 1')
64 logger.printAllMessages ()
66 if __name__ == '__main__':