Skip to content
Browse files

First revision that works as expected!

git-svn-id: svn+ssh://rubyforge.org/var/svn/alogr/trunk@9 5d4fd1e0-7adf-40a7-adde-e9af6235188f
  • Loading branch information...
1 parent 1a48c91 commit 216f972eaa6ce468aa0423af3510b188e51d4e57 wayneeseguin committed Sep 19, 2007
Showing with 49 additions and 41 deletions.
  1. +5 −4 ext/aio_logger/aio_logger.c
  2. +43 −36 lib/alogr.rb
  3. +1 −1 lib/alogr/version.rb
View
9 ext/aio_logger/aio_logger.c
@@ -35,6 +35,8 @@ int aio_log(char * string, int length, char * file_name) {
aio_suspend(&aio_control_block_list, 1, NULL);
bytes_written = aio_return( &control_block);
+ close(file_descriptor);
+
return 0;
}
@@ -44,7 +46,6 @@ VALUE rb_flush_log_buffer() {
log_files = rb_gv_get("$alogr_log_files");
buffer= rb_gv_get("$alogr_buffer");
-
packet = rb_ary_shift(buffer); // Remove the first log packet from the buffer
while( !NIL_P(packet) ) {
@@ -59,12 +60,12 @@ VALUE rb_flush_log_buffer() {
if( return_value > 0 ) {
// Unable to open the log file
- return Qnil; // TODO: false
+ return Qfalse;
}
packet = rb_ary_shift(buffer); // Fetch the next log packet
}
-
- return Qnil; // TODO: true
+
+ return Qtrue;
}
static VALUE rb_mAlogR;
View
79 lib/alogr.rb
@@ -1,61 +1,68 @@
require "aio_logger"
+# require "YAML"
module AlogR
Levels = { :emergency => 0, :alert => 1, :critical => 2, :error => 3, :warning => 4, :notice => 5, :info => 6, :debug => 7 }
class Logger
- class << self
- attr_accessor :config
-
- def log( string, level = ( config[:default_log_level] || :info ) )
- level = AlogR::Levels[level] if AlogR::Levels.has_key?( level )
- $alogr_buffer << [config[:newline] ? (string << "\n") : string, level]
- end
-
- def method_missing( method, *options )
- if AlogR::Levels.include?( method )
- log( method, options.first )
- else
- super.method_missing( method, *options )
- end
- end
-
- end
+ @@config = {}
def initialize( options = {} )
-
+ # TODO: Load config from YAML file, if :config has a valid filename and is valid yaml,
+ # fallback to options hash values
if options.class == String
options = { :log => options }
elsif options.class == Hash
options[:log] ||= "log/default.log"
else
- raise "AlogR: Invalid configuration"
+ raise ArgumentError, "Invalid configuration specified"
end
- config = options
+ @@config = options
+ @@config[:line_ending] = options[:line_ending] || "\n" unless options[:newline] == false
AlogR::Levels.keys.each do | key |
- file_name = options[key] || options[:log]
+ file_name = options[key] || options[:log] || options[:default]
$alogr_log_files[AlogR::Levels[key]] = file_name.freeze
# TODO: Verifiy valid filename, if not raise an error
- system("mkdir -p #{File.dirname(file_name)} ; touch #{file_name}")
+ system( "mkdir -p #{File.dirname(file_name)} ; touch #{file_name}" )
end
Object.logger = self # This becomes the default logger
Thread.abort_on_exception = true
Thread.new do
loop do
- sleep( config[:log_interval] || 0.25 )
- flush_log_buffer
+ sleep( @@config[:log_interval] || 0.25 )
+ unless flush_log_buffer
+ raise "Unable to open a log file" # TODO: Be more specific
+ end
end
Thread.exit
end
-
+
+ at_exit do
+ sleep(@@config[:log_interval].to_f * 1.2) # Allow log buffer to get emptied before exiting
+ end
+
self
end
+
+ def buffer( string, level = ( @@config[:default_log_level] || :info ) )
+ level = AlogR::Levels[level] if AlogR::Levels.has_key?( level )
+ string << @@config[:line_ending]
+ $alogr_buffer << [ string, level]
+ end
+
+ def method_missing( method, *options )
+ if AlogR::Levels.include?( method )
+ log( options.first, method )
+ else
+ super.method_missing( method, *options )
+ end
+ end
end
end
@@ -65,25 +72,25 @@ class << self
attr_accessor :logger
end
- def log(*args) # level = :info
+ def log( *args )
Object.logger ||= AlogR::Logger.new
if self.class == String
- require "ruby-debug" and debugger
level = args.first || :info
- unless AlogR::Levels[level].nil?
- Object.logger.log(self, AlogR::Levels[level])
+ unless AlogR::Levels[level.to_sym].nil?
+ Object.logger.buffer( self, AlogR::Levels[level.to_sym] )
+ self
else
- raise "Error: No such level: #{level}"
+ raise ArgumentError, "Error: No such level: #{level}"
end
else
- self.to_s.log
+ if args.first.class == String
+ args.first.log args[1]
+ else
+ self.to_s.log
+ end
+ self
end
- self
end
end
-
-at_exit do
- sleep(1) # Allow log buffer to get emptied
-end
View
2 lib/alogr/version.rb
@@ -2,7 +2,7 @@ module AlogR #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 0
- TINY = 2
+ TINY = 1
STRING = [MAJOR, MINOR, TINY].join('.')
end

0 comments on commit 216f972

Please sign in to comment.
Something went wrong with that request. Please try again.