From 8b62e3329ebcb21268175abf081136b586970c44 Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 10 Jan 2008 11:27:36 -0800 Subject: [PATCH] add --no-syslog --- .gitignore | 1 + History.txt | 4 +++ bin/god | 10 ++++--- lib/god.rb | 14 +++++----- lib/god/cli/run.rb | 12 ++++++--- lib/god/diagnostics.rb | 10 +++++++ lib/god/logger.rb | 73 +++++++++++++++++++++++++++++++++++++++++--------- lib/god/timer.rb | 1 + 8 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 lib/god/diagnostics.rb diff --git a/.gitignore b/.gitignore index e2c182a..98537ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage pkg *.log +logs diff --git a/History.txt b/History.txt index 13b05ed..9f2ae10 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,7 @@ +== 0.6.7 / + * Minor Enhancements + * Add --no-syslog option to disable Syslog + == 0.6.6 / 2008-01-07 * Bug Fixes * Redo Timer mutexing to reduce synchronization needs diff --git a/bin/god b/bin/god index a262504..3303088 100755 --- a/bin/god +++ b/bin/god @@ -9,7 +9,7 @@ require 'optparse' require 'drb' begin - options = {:daemonize => true, :port => 17165} + options = {:daemonize => true, :port => 17165, :syslog => true} opts = OptionParser.new do |opts| opts.banner = <<-EOF @@ -72,15 +72,19 @@ begin options[:info] = true end - opts.on("--log-level LEVEL", "Log level [debug|info|fatal]") do |x| + opts.on("--log-level LEVEL", "Log level [debug|info|warn|error|fatal]") do |x| options[:log_level] = x.to_sym end + + opts.on("--no-syslog", "Disable output to syslog") do + options[:syslog] = false + end end opts.parse! # validate - if options[:log_level] && ![:debug, :info, :fatal].include?(options[:log_level]) + if options[:log_level] && ![:debug, :info, :warn, :error, :fatal].include?(options[:log_level]) abort("Invalid log level '#{options[:log_level]}'") end diff --git a/lib/god.rb b/lib/god.rb index 8686b7e..ed82193 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -8,7 +8,6 @@ require 'stringio' require 'logger' # stdlib -require 'syslog' # internal requires require 'god/errors' @@ -58,6 +57,8 @@ require 'god/sugar' require 'god/cli/version' require 'god/cli/command' +require 'god/diagnostics' + $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god]) # App wide logging system @@ -76,13 +77,6 @@ $run ||= nil GOD_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) -# Ensure that Syslog is open -begin - Syslog.open('god') -rescue RuntimeError - Syslog.reopen('god') -end - # Return the binding of god's root level def root_binding binding @@ -196,6 +190,8 @@ module God # log level log_level_map = {:debug => Logger::DEBUG, :info => Logger::INFO, + :warn => Logger::WARN, + :error => Logger::ERROR, :fatal => Logger::FATAL} LOG.level = log_level_map[self.log_level] @@ -566,6 +562,8 @@ module God # mark as running self.running = true + # start_dike + # join the timer thread so we don't exit Timer.get.join end diff --git a/lib/god/cli/run.rb b/lib/god/cli/run.rb index 4c640b1..d4e838c 100644 --- a/lib/god/cli/run.rb +++ b/lib/god/cli/run.rb @@ -50,13 +50,19 @@ module God God.pid = @options[:pid] end - # set log level if requested - if @options[:log_level] - God.log_level = @options[:log_level] + unless @options[:syslog] + Logger.syslog = false end # load config if @options[:config] + # set log level, defaults to WARN + if @options[:log_level] + God.log_level = @options[:log_level] + else + God.log_level = :warn + end + unless File.exist?(@options[:config]) abort "File not found: #{@options[:config]}" end diff --git a/lib/god/diagnostics.rb b/lib/god/diagnostics.rb new file mode 100644 index 0000000..c59112b --- /dev/null +++ b/lib/god/diagnostics.rb @@ -0,0 +1,10 @@ +def start_dike + require 'dike' + Thread.new do + Dike.logfactory File.join(File.dirname(__FILE__), *%w[.. .. logs]) + loop do + Dike.finger + sleep(1) + end + end +end \ No newline at end of file diff --git a/lib/god/logger.rb b/lib/god/logger.rb index 030e5c7..d773578 100644 --- a/lib/god/logger.rb +++ b/lib/god/logger.rb @@ -9,27 +9,48 @@ module God attr_accessor :logs + class << self + attr_accessor :syslog + end + + self.syslog ||= true + + # Instantiate a new Logger object def initialize super($stdout) self.logs = {} @mutex = Mutex.new @capture = nil + load_syslog end - def start_capture - @mutex.synchronize do - @capture = StringIO.new - end - end - - def finish_capture - @mutex.synchronize do - cap = @capture.string - @capture = nil - cap + # If Logger.syslog is true then attempt to load the syslog bindings. If syslog + # cannot be loaded, then set Logger.syslog to false and continue. + # + # Returns nothing + def load_syslog + return unless Logger.syslog + + begin + require 'syslog' + + # Ensure that Syslog is open + begin + Syslog.open('god') + rescue RuntimeError + Syslog.reopen('god') + end + rescue Exception + Logger.syslog = false end end + # Log a message + # +watch+ is the String name of the Watch (may be nil if not Watch is applicable) + # +level+ is the log level [:debug|:info|:warn|:error|:fatal] + # +text+ is the String message + # + # Returns nothing def log(watch, level, text) # initialize watch log if necessary self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch @@ -49,9 +70,14 @@ module God self.send(level, text % []) # send to syslog - Syslog.send(SYSLOG_EQUIVALENTS[level], text) + Syslog.send(SYSLOG_EQUIVALENTS[level], text) if Logger.syslog end + # Get all log output for a given Watch since a certain Time. + # +watch_name+ is the String name of the Watch + # +since+ is the Time since which to fetch log lines + # + # Returns String def watch_log_since(watch_name, since) # initialize watch log if necessary self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) @@ -65,6 +91,29 @@ module God end.join end end + + # private + + # Enable capturing of log + # + # Returns nothing + def start_capture + @mutex.synchronize do + @capture = StringIO.new + end + end + + # Disable capturing of log and return what was captured since + # capturing was enabled with Logger#start_capture + # + # Returns String + def finish_capture + @mutex.synchronize do + cap = @capture.string + @capture = nil + cap + end + end end end \ No newline at end of file diff --git a/lib/god/timer.rb b/lib/god/timer.rb index 658e453..46a38fe 100644 --- a/lib/god/timer.rb +++ b/lib/god/timer.rb @@ -89,6 +89,7 @@ module God applog(nil, :fatal, message) ensure # sleep until next check + GC.start sleep INTERVAL end end -- 2.11.4.GIT