Merge with add-logger
[rarslave2.git] / RarslaveLogger.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=112:
3
4 class RarslaveMessage (object):
5         def __init__ (self, msg, printquiet):
6                 self.msg = msg
7                 self.printquiet = printquiet
8
9         def __repr__ (self):
10                 return "%s" % self.msg
11
12         def isVerbose (self):
13                 # A message is verbose only if we should
14                 # print it when we are not printing quietly
15                 return not self.printquiet
16
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.
20
21         def __init__ (self):
22                 self.__uniq_num = 0
23                 self.__msg_dict = {}
24
25         def getUniqueID (self):
26                 # Generate a unique id. This implementation is pretty simple,
27                 # just generating an increasing set of integers.
28
29                 # This function also sets up the dictionary which will hold the messages.
30                 self.__uniq_num += 1
31                 self.__msg_dict[self.__uniq_num] = []
32
33                 return self.__uniq_num
34
35         def isValidID (self, id):
36                 # Check if the id given is a valid one. This is done by checking if
37                 # the id is in the dictionary.
38                 return id in self.__msg_dict.keys()
39
40         def addMessage (self, id, msg, printquiet=True):
41                 # Add a message to the dictionary, to be printed later.
42                 # The printquiet variable controls whether the message will be printed
43                 # normally, or only in verbose mode. The default is to print always.
44
45                 # Check the id
46                 assert self.isValidID (id)
47
48                 # Get the list from the dictionary, then add the message to it
49                 self.__msg_dict[id].append (RarslaveMessage (msg, printquiet))
50
51         def printAllMessages (self, verbose=False):
52                 # Print all messages in groups by id
53                 for k in self.__msg_dict.keys():
54                         for msg in self.__msg_dict[k]:
55
56                                 # Skip verbose messages if we're not in verbose mode
57                                 if msg.isVerbose() and not verbose:
58                                         continue
59
60                                 print msg
61
62                         print
63
64 def main ():
65         pass
66
67 if __name__ == '__main__':
68         main ()
69