#!/usr/bin/env python
-# vim: set ts=4 sts=4 sw=4 textwidth=112:
+# vim: set ts=4 sts=4 sw=4 textwidth=92:
+
+class MessageType (object):
+ Fatal = -1
+ Normal = 0
+ Verbose = 1
+ Debug = 2
class RarslaveMessage (object):
- def __init__ (self, msg, printquiet):
+ def __init__ (self, msg, type=MessageType.Normal):
self.msg = msg
- self.printquiet = printquiet
+ self.type = type
def __repr__ (self):
return "%s" % self.msg
+ def isFatal (self):
+ return self.type == MessageType.Fatal
+
+ def isNormal (self):
+ return self.type == MessageType.Normal
+
def isVerbose (self):
- # A message is verbose only if we should
- # print it when we are not printing quietly
- return not self.printquiet
+ return self.type == MessageType.Verbose
+
+ def isDebug (self):
+ return self.type == MessageType.Debug
class RarslaveLogger (object):
# This class will log multiple messages, one for each set that is operated on.
# It will then be able to print all of them out.
def __init__ (self):
- self.__uniq_num = 0
- self.__msg_dict = {}
+ self.__messages = []
- def getUniqueID (self):
- # Generate a unique id. This implementation is pretty simple,
- # just generating an increasing set of integers.
+ def addMessage (self, msg, type=MessageType.Normal):
+ self.__messages.append (RarslaveMessage (msg, type))
- # This function also sets up the dictionary which will hold the messages.
- self.__uniq_num += 1
- self.__msg_dict[self.__uniq_num] = []
+ def hasFatalMessages (self):
+ for m in self.__messages:
+ if m.isFatal ():
+ return True
- return self.__uniq_num
+ return False
- def isValidID (self, id):
- # Check if the id given is a valid one. This is done by checking if
- # the id is in the dictionary.
- return id in self.__msg_dict.keys()
+ def hasNormalMessages (self):
+ for m in self.__messages:
+ if m.isNormal ():
+ return True
- def addMessage (self, id, msg, printquiet=True):
- # Add a message to the dictionary, to be printed later.
- # The printquiet variable controls whether the message will be printed
- # normally, or only in verbose mode. The default is to print always.
+ return False
- # Check the id
- assert self.isValidID (id)
+ def hasVerboseMessages (self):
+ for m in self.__messages:
+ if m.isVerbose ():
+ return True
- # Get the list from the dictionary, then add the message to it
- self.__msg_dict[id].append (RarslaveMessage (msg, printquiet))
+ return False
- def printAllMessages (self, verbose=False):
- # Print all messages in groups by id
- for k in self.__msg_dict.keys():
- for msg in self.__msg_dict[k]:
+ def hasDebugMessages (self):
+ for m in self.__messages:
+ if m.isDebug ():
+ return True
- # Skip verbose messages if we're not in verbose mode
- if msg.isVerbose() and not verbose:
- continue
+ return False
- print msg
+ def printAllMessages (self, level=MessageType.Normal):
+ # Print all messages with level upto and including $level.
+ for m in self.__messages:
+ if m.type <= level:
+ print m
- print
+ def printLoglevel (self, level=MessageType.Normal):
+ # Print all message with exactly the loglevel given
+ for m in self.__messages:
+ if m.type == level:
+ print m
def main ():
pass