Skip to content

Commit

Permalink
Added unit test for src/util/log.rb and more debugging info.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Nov 11, 2014
1 parent 36dd29a commit e9f1fbc
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 73 deletions.
13 changes: 7 additions & 6 deletions src/util/log.rb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
# encoding: utf-8
# Log -- oddb -- 23.05.2003 -- hwyss@ywesee.com
# Log -- oddb -- 23.05.2003 -- hwyss@ywesee.com

require 'util/persistence'
require 'config'
Expand All @@ -13,7 +13,7 @@ class Log
include Persistence
ODBA_SERIALIZABLE = ['@change_flags', '@pointers', '@recipients',
'@files']
attr_accessor :report, :pointers, :recipients, :change_flags,
attr_accessor :report, :pointers, :recipients, :change_flags,
:files, :parts, :date_str
attr_reader :date
LOG_RECIPIENTS = [ 'log' ]
Expand All @@ -28,11 +28,11 @@ def initialize(date)
def notify(subject = nil)
@recipients = LOG_RECIPIENTS if @recipients.size == 0
subj = [
'ch.ODDB.org Report',
subject,
'ch.ODDB.org Report',
subject,
(@date_str || @date.strftime('%m/%Y')),
].compact.join(' - ')
LogFile.append('oddb/debug', "log notify #{subject}: start outgoing process #{@recipients.inspect} ", Time.now)
LogFile.append('oddb/debug', "log notify #{subject}: start outgoing process #{@recipients.inspect}. Must attach #{@files.size} files and #{@parts.size} parts. ", Time.now)
attachments = []
@files.each { |path, (mime, iconv)|
begin
Expand All @@ -47,7 +47,7 @@ def notify(subject = nil)
}
@parts.each{
|part|
LogFile.append('oddb/debug', " part " + part.inspect.to_s, Time.now)
LogFile.append('oddb/debug', " part mime #{part[0]} #{part[2].size} bytes #{part.inspect[0..80]}...", Time.now)
attachments << { :filename => File.basename(part[1]), :mime_type => part[0], :content => part[2] }
}
if attachments.size > 0
Expand All @@ -56,6 +56,7 @@ def notify(subject = nil)
Util.send_mail(@recipients, subj, @report)
end
LogFile.append('oddb/debug', "log notify #{subject}: sent mail", Time.now)
@recipients
end
end
end
33 changes: 16 additions & 17 deletions src/util/mail.rb
Expand Up @@ -43,7 +43,7 @@ def Util.get_mailing_list_receivers(list_id)
end
def Util.get_mailing_list_anrede(list_id)
Util.configure_mail unless @mail_configured
return [] unless @cfg and @cfg[MailingListIds]
return [] unless @cfg and @cfg[MailingListIds]
anreden = []
lists = list_id.is_a?(Array) ? list_id : [list_id]
lists.each{ |list_name|
Expand Down Expand Up @@ -106,19 +106,17 @@ def Util.send_mail(list_and_recipients, mail_subject, mail_body, override_from =

def Util.send_mail_with_attachments(list_and_recipients, mail_subject, mail_body, attachments, override_from = nil)
LogFile.append('oddb/debug', "Util.send_mail send_mail_with_attachments #{list_and_recipients}", Time.now)
recipients = Util.check_and_get_all_recipients(list_and_recipients)
mail = Mail.new
mail.from override_from ? override_from : Util.mail_from
mail.to recipients
mail.subject mail_subject
mail.body mail_body
attachments.each { |attachment|
mail.attachments[attachment[:filename]] = {
:mime_type => attachment[:mime_type],
:content => attachment[:content],
}
}
log_and_deliver_mail(mail)
Mail.deliver do
from override_from ? override_from : Util.mail_from
to Util.check_and_get_all_recipients(list_and_recipients)
subject mail_subject.respond_to?(:force_encoding) ? mail_subject.force_encoding("utf-8") : mail_subject
body mail_body
attachments.each {
|attachment|
add_file :filename => attachment[:filename], :content => attachment[:content], :mime_type => attachment[:mime_type]
}
end

rescue => e
msg = "Util.send_mail_with_attachments rescue: error is #{e.inspect} #{caller[0..10].inspect}"
Util.debug_msg(msg)
Expand Down Expand Up @@ -158,13 +156,14 @@ def Util.check_and_get_all_recipients(list_and_recipients)
raise "No recipients defined for list_and_recipients #{list_and_recipients}" unless recipients.size > 0
recipients.sort
end

def Util.log_and_deliver_mail(mail)
Util.configure_mail unless @mail_configured
mail.from << @cfg.mail_from unless mail.from.size > 0
mail.reply_to = @cfg['reply_to']
Util.debug_msg("Util.log_and_deliver_mail to=#{mail.to} subject #{mail.subject} size #{mail.body.inspect}")
mail.deliver
Util.debug_msg("Util.log_and_deliver_mail to=#{mail.to} subject #{mail.subject} size #{mail.body.to_s.size} with #{mail.attachments.size} attachments. #{mail.body.inspect}")
res = mail.deliver
res
end

def Util.debug_msg(msg)
Expand Down

0 comments on commit e9f1fbc

Please sign in to comment.