Skip to content

Commit

Permalink
read log level from config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
koseki committed Nov 7, 2008
1 parent 05fca8e commit 272d2a8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -2,6 +2,9 @@

* rescue Process.eid NotImplementedError.
* warn when Process.eid can't be changed.
* read log level from config file.
* change param name from Loglevel to LogLevel
* add debug log.

== 0.0.2 / 2008-11-03

Expand Down
29 changes: 23 additions & 6 deletions lib/mocksmtpd.rb
Expand Up @@ -132,33 +132,47 @@ def stop
puts "done"
end

def create_logger(file = nil)
file = file.to_s.strip
file = nil if file.empty?
lvstr = @conf[:LogLevel].to_s.strip
lvstr = "INFO" unless %w{FATAL ERROR WARN INFO DEBUG}.include?(lvstr)
level = WEBrick::BasicLog.const_get(lvstr)
logger = WEBrick::Log.new(file, level)
logger.debug("Logger initialized")
return logger
end

def start
load_conf
@logger = WEBrick::Log.new(@logfile.to_s, WEBrick::BasicLog::INFO)
@logger = create_logger(@logfile)
@daemon = true
smtpd
end

def console
load_conf
@logger = WEBrick::Log.new
@logger = create_logger
@daemon = false
smtpd
end

def create_pid_file
if @pidfile.exist?
pid = @pidfile.read
@logger.warn("pid file already exists: #{pid}")
@logger.warn("pid file already exists: pid=#{pid}")
exit 1
end
pid = Process.pid
open(@pidfile, "w") do |io|
io << Process.pid
io << pid
end
@logger.debug("pid file saved: pid=#{pid} file=#{@pidfile}")
end

def delete_pid_file
File.delete(@pidfile)
@logger.debug("pid file deleted: file=#{@pidfile}")
end

def init_permission
Expand All @@ -170,6 +184,7 @@ def init_permission
Process.egid = gid
Process.euid = uid
rescue NotImplementedError => e
@logger.debug("Process.euid= not implemented.")
rescue Errno::EPERM => e
@logger.warn("could not change euid/egid. #{e}")
end
Expand All @@ -179,8 +194,8 @@ def smtpd
start_cb = Proc.new do
@logger.info("Inbox: #{@inbox}")
if @daemon
@logger.info("LogFile: #{@logfile}")
@logger.info("PidFile: #{@pidfile}")
@logger.debug("LogFile: #{@logfile}")
@logger.debug("PidFile: #{@pidfile}")
end

begin
Expand Down Expand Up @@ -269,6 +284,7 @@ def save_mail(mail)
open(mail[:path], "w") do |io|
io << @templates[:mail].result(binding)
end
@logger.debug("mail saved: #{mail[:path]}")
end

def save_index(mail)
Expand All @@ -286,6 +302,7 @@ def save_index(mail)
open(path, "w") do |io|
io << htmlsrc
end
@logger.debug("index saved: #{path}")
end

end
2 changes: 1 addition & 1 deletion mocksmtpd.gemspec
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.email = %q{koseki@gmail.com}
s.executables = ["mocksmtpd"]
s.extra_rdoc_files = ["README", "ChangeLog"]
s.files = ["README", "ChangeLog", "Rakefile", "bin/mocksmtpd", "test/mocksmtpd_test.rb", "test/test_helper.rb", "lib/mocksmtpd.rb", "lib/mocksmtpd.rb~", "lib/smtpserver.rb", "templates/html", "templates/html/index.erb", "templates/html/index.erb~", "templates/html/index_entry.erb", "templates/html/index_entry.erb~", "templates/html/mail.erb", "templates/html/mail.html.erb~", "templates/mocksmtpd.conf.erb"]
s.files = ["README", "ChangeLog", "Rakefile", "bin/mocksmtpd", "test/mocksmtpd_test.rb", "test/test_helper.rb", "lib/mocksmtpd.rb", "lib/smtpserver.rb", "templates/html", "templates/html/index.erb", "templates/html/index_entry.erb", "templates/html/mail.erb", "templates/mocksmtpd.conf.erb"]
s.has_rdoc = true
s.homepage = %q{http://github.com/koseki/mocksmtpd/}
s.rdoc_options = ["--title", "mocksmtpd documentation", "--charset", "utf-8", "--opname", "index.html", "--line-numbers", "--main", "README", "--inline-source", "--exclude", "^(examples|extras)/"]
Expand Down
2 changes: 1 addition & 1 deletion templates/mocksmtpd.conf.erb
Expand Up @@ -2,7 +2,7 @@ ServerName: mocksmtpd
Port: 25
RequestTimeout: 120
LineLengthLimit: 1024
Loglevel: INFO
LogLevel: INFO

LogFile: ./log/mocksmtpd.log
PidFile: ./log/mocksmtpd.pid
Expand Down

0 comments on commit 272d2a8

Please sign in to comment.