def debug( self, txt ): ''' Print a debug message which always go to the log file but only appears on the terminal if debug mode is active. ''' if self.__debug_mode: self.terminal.write( 'DEBUG: ' + txt + '\n' ) self.log.write( Colors.strip( txt ) + '\n' ) # strip color codes here
def __call__(self, *args, **kwargs): ''' Output the payload. The following keyword arguments are supported: debug = <level> tag this message with a debug level verbosity = <level> same as 'debug' syslog = True|False temporarily set the syslog flag for this function call. Useful when creating a two-column output lw = <colWidth> left justify message in <colWidth> rw = <colWidth> right justify message in <colWidth> Typical calling syntax: log = Message() # default log.to(sys.stdout) # prints message to stdout log('hello, world\n') log.verbosity(1) # With verbosity set to 1, messages tagged with # a debug level greater than 1 will not be # printed to stdout. log('hello world\n', debug=5) # Setting verbosity to 10 will allow the previous # message to be printed. log.verbosity(10) log('hello, world\n', debug=5) # Simple two-column justifed output can be created by # first printing a message left justifed in a column of # width 40 (with no carriage return, i.e. no '\n') log('starting process...', lw=40) # followed by a right justified message (with a '\n') log('[ ok ]\n', rw=20) The payload presentation is dependent on several internal flags: 1. If the class verbosity() is set, i.e. message.verbosity(10), then messages are only output to the console if the internal verbosity level is less than the tagged level of the message. 2. If the 'tee' flag is set, and if the output destination is anything other than sys.stdout, the message will always be echoed to the console (irrespective of any 'debug' tags) ''' b_syslog = self._b_syslog str_prepend = '' str_msg = '' verbosity = 0 lw = 0 rw = 0 for key, value in kwargs.iteritems(): if key == 'debug' or key == 'verbose': verbosity = value if key == 'lw': lw = -value if key == 'rw': rw = value if key == 'syslog': self._b_syslog = value if self._b_tag and len(self._str_tag): str_prepend = Colors.LIGHT_CYAN + self._str_tag + ' ' + Colors.NO_COLOUR if self._b_syslog: self._str_syslog = '%s: ' % self.syslog_generate( self._processName, self._pid) str_prepend = Colors.LIGHT_GRAY + self._str_syslog + Colors.NO_COLOUR if len(args): str_msg = '%s%s' % (str_prepend, args[0]) else: str_msg = '%s%s' % (str_prepend, self._str_payload) self._str_payload = '' if lw: str_msg = '%*s' % (lw, str_msg) if rw: str_msg = '%*s' % (rw, str_msg) if self._logHandle == sys.stdout: if verbosity: if self.canPrintVerbose(verbosity): self._sys_stdout.write(str_msg) else: self._sys_stdout.write(str_msg) else: self._sys_stdout.write(Colors.strip(str_msg)) self._sys_stdout.flush() if self._b_tee and self._logHandle != sys.stdout: if verbosity: if self.canPrintVerbose(verbosity): sys.stdout.write(str_msg) else: sys.stdout.write(str_msg) sys.stdout.flush() self.syslog(b_syslog)
def write( self, message ): ''' Write both to the terminal and to the logfile. ''' self.terminal.write( message ) self.log.write( Colors.strip( message ) ) # strip color codes here
def __call__(self, *args, **kwargs): ''' Output the payload. The following keyword arguments are supported: debug = <level> tag this message with a debug level verbosity = <level> same as 'debug' syslog = True|False temporarily set the syslog flag for this function call. Useful when creating a two-column output lw = <colWidth> left justify message in <colWidth> rw = <colWidth> right justify message in <colWidth> Typical calling syntax: log = Message() # default log.to(sys.stdout) # prints message to stdout log('hello, world\n') log.verbosity(1) # With verbosity set to 1, messages tagged with # a debug level greater than 1 will not be # printed to stdout. log('hello world\n', debug=5) # Setting verbosity to 10 will allow the previous # message to be printed. log.verbosity(10) log('hello, world\n', debug=5) # Simple two-column justified output can be created by # first printing a message left justified in a column of # width 40 (with no carriage return, i.e. no '\n') log('starting process...', lw=40) # followed by a right justified message (with a '\n') log('[ ok ]\n', rw=20) The payload presentation is dependent on several internal flags: 1. If the class verbosity() is set, i.e. message.verbosity(10), then messages are only output to the console if the internal verbosity level is less than the tagged level of the message. 2. If the 'tee' flag is set, and if the output destination is anything other than sys.stdout, the message will always be echoed to the console (irrespective of any 'debug' tags) ''' b_syslog = self._b_syslog str_prepend = '' str_msg = '' verbosity = 0 lw = 0 rw = 0 str_end = ' ' for key, value in kwargs.items(): if key == 'debug' or key == 'verbose': verbosity = value if key == 'lw': lw = -value if key == 'rw': rw = value if key == 'syslog': self._b_syslog = value if key == 'end': str_end = value if self._b_tag and len(self._str_tag): str_prepend = Colors.LIGHT_CYAN + self._str_tag + ' ' + Colors.NO_COLOUR if self._b_syslog: self._str_syslog = '%s: ' % self.syslog_generate( self._processName, self._pid) str_prepend = Colors.LIGHT_GRAY + self._str_syslog + Colors.NO_COLOUR if len(args): str_msg = '%s%s' % (str_prepend, args[0]) else: str_msg = '%s%s' % (str_prepend, self._str_payload) self._str_payload = '' if lw: str_msg = '%*s' % (lw, str_msg) if rw: str_msg = '%*s' % (rw, str_msg) if self._b_flushNewLine and str_end != '': str_msg += '\n' if self._logHandle == sys.stdout: if verbosity: if self.canPrintVerbose(verbosity): self._sys_stdout.write(str_msg) else: self._sys_stdout.write(str_msg) else: self._sys_stdout.write(Colors.strip(str_msg)) self._sys_stdout.flush() if self._b_tee and self._logHandle != sys.stdout: if verbosity: if self.canPrintVerbose(verbosity): sys.stdout.write(str_msg) else: sys.stdout.write(str_msg) sys.stdout.flush() self.syslog(b_syslog)