changeset 201:de574314990e

log some more things when Bot.debug is False Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 25 Feb 2010 20:48:33 +0100
parents 740effa74c18
children 2a1ee46f86af
files bot.py bridge.py
diffstat 2 files changed, 19 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/bot.py
+++ b/bot.py
@@ -62,15 +62,17 @@ class Bot(threading.Thread):
 		self.xmpp_thread.start()
 	
 	
-	def error(self, importance, message, debug=False, send_to_admins=False):
+	def error(self, importance, message, debug=False, no_debug_add='', send_to_admins=False):
 		"""Output an error message."""
+		if not self.debug:
+			 message += no_debug_add
 		if send_to_admins == True:
 			self._send_message_to_admins(importance, message)
 		if importance == -1:
 			return
 		if not debug:
 			self.error_fd.write(self.format_message(importance, message).encode('utf-8')+'\n')
-		if debug and self.debug:
+		elif self.debug:
 			self.error_fd.write('='*importance+'> '+message.encode('utf-8')+'\n')
 	
 	
@@ -130,6 +132,8 @@ class Bot(threading.Thread):
 		
 		self.error(2, 'Received XMPP presence.\n'+presence.__str__(fancy=1), debug=True)
 		
+		no_debug_add = '\n'+presence.__str__(fancy=1)
+		
 		from_ = xmpp.protocol.JID(presence.getFrom())
 		bare_jid = unicode(from_.getNode()+'@'+from_.getDomain())
 		for bridge in self.bridges:
@@ -191,11 +195,11 @@ class Bot(threading.Thread):
 								return
 							item = x.getTag('item')
 							if not item:
-								self.error(1, 'bad stanza, no item element', debug=True)
+								self.error(say_levels.debug, 'bad stanza, no item element', no_debug_add=no_debug_add)
 								return
 							new_nick = item.getAttr('nick')
 							if not new_nick:
-								self.error(1, 'bad stanza, new nick is not given', debug=True)
+								self.error(say_levels.debug, 'bad stanza, new nick is not given', no_debug_add=no_debug_add)
 								return
 							p.changeNickname(new_nick, 'irc')
 							
@@ -287,10 +291,7 @@ class Bot(threading.Thread):
 					
 				return
 		
-		if self.debug:
-			self.error(1, 'presence was not handled', debug=True)
-		else:
-			self.error(say_levels.debug, 'Unhandled XMPP presence:\n'+message.__str__(fancy=1))
+		self.error(say_levels.debug, 'Unhandled XMPP presence', no_debug_add='\n'+presence.__str__(fancy=1))
 	
 	
 	def _xmpp_iq_handler(self, dispatcher, iq):
@@ -339,7 +340,7 @@ class Bot(threading.Thread):
 							else:
 								self.error(1, 'won\'t answer.', debug=True)
 							return
-						self.error(1, 'XMPP chat message not relayed', debug=True)
+						self.error(say_levels.debug, 'XMPP chat message not relayed', no_debug_add='\n'+message.__str__(fancy=1))
 						return
 			
 			# message does not come from a room
@@ -398,7 +399,7 @@ class Bot(threading.Thread):
 							participant = bridge.getParticipant(resource)
 						except Bridge.NoSuchParticipantException:
 							if resource != self.nickname:
-								self.error(1, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', debug=True)
+								self.error(say_levels.debug, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', no_debug_add='\n'+message.__str__(fancy=1))
 							return
 						
 						participant.sayOnIRC(message.getBody())
@@ -478,8 +479,8 @@ class Bot(threading.Thread):
 		
 		
 		# A string representation of the event
-		event_str = 'connection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments())
-		debug_str = 'Received IRC event.\n'+event_str
+		event_str = '\nconnection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments())
+		debug_str = 'Received IRC event.'+event_str
 		printed_event = False
 		
 		
@@ -501,7 +502,7 @@ class Bot(threading.Thread):
 					printed_event = True
 			
 			if event.eventtype() == 'kick' and len(event.arguments()) < 1:
-				self.error(1, 'at least 1 argument is needed for a '+event.eventtype()+' event', debug=True)
+				self.error(say_levels.debug, 'at least 1 argument is needed for a '+event.eventtype()+' event', no_debug_add=event_str)
 				return
 			
 			if event.eventtype() in ['pubmsg', 'action']:
@@ -559,7 +560,7 @@ class Bot(threading.Thread):
 									bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)')
 							return
 						except Bridge.NoSuchParticipantException:
-							self.error(1, 'a participant that was not here has been kicked ? WTF ?', debug=True)
+							self.error(say_levels.debug, 'a participant that was not here has been kicked ? WTF ?', no_debug_add=event_str)
 							return
 					else:
 						continue
@@ -709,10 +710,10 @@ class Bot(threading.Thread):
 		
 		# Unhandled events
 		if not printed_event:
-			self.error(say_levels.debug, 'The following IRC event was not handled:\n'+event_str+'\n', send_to_admins=True)
+			self.error(say_levels.debug, 'The following IRC event was not handled:'+event_str+'\n', send_to_admins=True)
 		else:
 			self.error(1, 'event not handled', debug=True)
-			self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:\n'+event_str)
+			self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:'+event_str)
 	
 	
 	def _send_message_to_admins(self, importance, message):
--- a/bridge.py
+++ b/bridge.py
@@ -153,8 +153,7 @@ class Bridge:
 		try:
 			p = Participant(self, from_protocol, nickname, real_jid=real_jid)
 		except:
-			self.bot.error(3, 'unknown error while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True)
-			traceback.print_exc()
+			self.bot.error(say_levels.debug, 'unknown error while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"\n'+traceback.format_exc())
 			return
 		self.lock.acquire()
 		self.participants.append(p)
@@ -335,7 +334,7 @@ class Bridge:
 					self.show_participants_list_on(protocols=['xmpp'])
 			
 		else:
-			self.bot.error(1, 'Bad decision tree,  p.protocol='+p.protocol+'  left_protocol='+left_protocol+'\np.xmpp_c='+str(p.xmpp_c)+'\np.irc_connection='+str(p.irc_connection), debug=True)
+			self.bot.error(say_levels.debug, 'Bad decision tree,  p.protocol='+p.protocol+'  left_protocol='+left_protocol+'\np.xmpp_c='+str(p.xmpp_c)+'\np.irc_connection='+str(p.irc_connection), send_to_admins=True)
 	
 	
 	def restart(self, log=True):