Permalink
Browse files

Logger now logs to files properly. Now straightening out the ruby log…

…ging interface.

git-svn-id: svn+ssh://rubyforge.org/var/svn/alogr/trunk@8 5d4fd1e0-7adf-40a7-adde-e9af6235188f
  • Loading branch information...
1 parent a83be59 commit 1a48c91397d625c6c069bf9a1d9121af15417946 wayneeseguin committed Sep 19, 2007
Showing with 78 additions and 60 deletions.
  1. +2 −2 README
  2. +20 −21 ext/aio_logger/aio_logger.c
  3. +0 −12 ext/aio_logger/mkmf.log
  4. +32 −23 lib/alogr.rb
  5. +1 −1 specs/base.rb
  6. +1 −1 specs/default.rb
  7. +11 −0 specs/many_logs.rb
  8. +11 −0 specs/regexen_logs.rb
View
@@ -46,12 +46,12 @@ Feature Requests:
= Examples
* Example 1
-require "lib/alogr"
+require "alogr"
$logger = AlogR::Logger.new(:log => "/Users/wayne/projects/ruby/alogr/trunk/log/default.log")
"a test, should go to the logs 10 times\n".log.log.log.log.log.log.log.log.log.log
* Example 2
-require "lib/alogr"
+require "alogr"
$logger = AlogR.new(
:log => "/Users/wayne/projects/ruby/alogr/trunk/log/production.log",
:error => "/Users/wayne/projects/ruby/alogr/trunk/log/error.log"
@@ -8,18 +8,15 @@
#include <stdio.h>
#include <fcntl.h> // File modes O_*
-static struct aiocb control_block;
-
// Asynchronously log the string of specified length to given file
int aio_log(char * string, int length, char * file_name) {
- int file_descriptor;
+ int file_descriptor, return_value, bytes_written;
const struct aiocb *aio_control_block_list;
- printf("(%d,%s,%s)\n", length,string,file_name);
+ struct aiocb control_block;
- //printf("string %s, length: %d, file_name: %s\n", string, length, file_name);
+ bzero( &control_block, sizeof (struct aiocb));
if ((file_descriptor = open(file_name, O_CREAT | O_RDWR | O_APPEND)) == -1) {
- //printf("Failed to open %s: %s\n", file_name, strerror(errno));
return 1;
}
@@ -30,42 +27,44 @@ int aio_log(char * string, int length, char * file_name) {
control_block.aio_offset = 0;
// Perform the asynch write
- aio_write(&control_block);
+ return_value = aio_write(&control_block);
+ if (return_value) perror("aio_write:");
// Wait for write to finish
aio_control_block_list = &control_block;
aio_suspend(&aio_control_block_list, 1, NULL);
-
- //printf("AIO operation returned %d\n", aio_return(&control_block));
-
- close(file_descriptor); // Is this necessary? It appears not but...?
+ bytes_written = aio_return( &control_block);
return 0;
}
VALUE rb_flush_log_buffer() {
- VALUE work, rb_string, buffer, log_files;
- int log_level;
+ VALUE packet, rb_string, buffer, log_files;
+ int log_level, return_value;
log_files = rb_gv_get("$alogr_log_files");
buffer= rb_gv_get("$alogr_buffer");
- // Remove the first item from the buffer
- work = rb_ary_shift(buffer);
- while( !NIL_P(work) ) {
- log_level = FIX2INT(rb_ary_shift(work));
- rb_string = rb_ary_shift(work);
+
+ packet = rb_ary_shift(buffer); // Remove the first log packet from the buffer
+ while( !NIL_P(packet) ) {
+ rb_string = rb_ary_shift(packet);
+ log_level = FIX2INT(rb_ary_shift(packet));
- aio_log(
+ return_value = aio_log(
RSTRING(rb_string)->ptr, // The string to log
RSTRING(rb_string)->len, // Real length of string to log
RSTRING(rb_ary_entry(log_files, log_level))->ptr // log file name
);
- work = rb_ary_shift(buffer); // Fetch the next item of work
+ if( return_value > 0 ) {
+ // Unable to open the log file
+ return Qnil; // TODO: false
+ }
+ packet = rb_ary_shift(buffer); // Fetch the next log packet
}
- return Qnil;
+ return Qnil; // TODO: true
}
static VALUE rb_mAlogR;
@@ -1,12 +0,0 @@
-have_library: checking for main() in -lc... -------------------- yes
-
-"gcc -o conftest -I. -I/usr/local/lib/ruby/1.8/i686-darwin8.9.1 -I. -g -O2 -pipe -fno-common conftest.c -L"/usr/local/lib" -lruby-static -lc -lpthread -ldl -lobjc "
-checked program was:
-/* begin */
-1: /*top*/
-2: int main() { return 0; }
-3: int t() { void ((*volatile p)()); p = (void ((*)()))main; return 0; }
-/* end */
-
---------------------
-
View
@@ -7,35 +7,48 @@ module AlogR
class Logger
class << self
- attr_accessor :default_log_level
- attr_accessor :log_interval
+ 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
- def initialize(options = {})
+ def initialize( options = {} )
if options.class == String
- options = {:log => options}
+ options = { :log => options }
elsif options.class == Hash
options[:log] ||= "log/default.log"
else
raise "AlogR: Invalid configuration"
end
-
- log_interval = options[:log_interval] || 0.01
- default_log_level = options[:default_log_level] || :info
+
+ config = options
AlogR::Levels.keys.each do | key |
file_name = options[key] || options[:log]
$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}")
end
- Object.logger = self # Default logger
+ Object.logger = self # This becomes the default logger
Thread.abort_on_exception = true
Thread.new do
loop do
- sleep( log_interval )
+ sleep( config[:log_interval] || 0.25 )
flush_log_buffer
end
Thread.exit
@@ -44,18 +57,6 @@ def initialize(options = {})
self
end
- def log(string, level = default_log_level)
- $alogr_buffer << [level, string]
- end
-
- def method_missing( method, *options )
- if AlogR::Levels.include?( method )
- log( method, options.first )
- else
- super.method_missing( method, *options )
- end
- end
-
end
end
@@ -64,12 +65,16 @@ class << self
attr_accessor :logger
end
- def log(level = :info)
+ def log(*args) # level = :info
+ 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])
else
- raise "Error: No such log level #{level}"
+ raise "Error: No such level: #{level}"
end
else
self.to_s.log
@@ -78,3 +83,7 @@ def log(level = :info)
end
end
+
+at_exit do
+ sleep(1) # Allow log buffer to get emptied
+end
View
@@ -1,4 +1,4 @@
-#require "lib/alogr"
+require "lib/alogr"
describe "AlogR" do
before :each do
View
@@ -1,4 +1,4 @@
-#require "lib/alogr"
+require "lib/alogr"
describe "AlogR" do
before do
View
@@ -0,0 +1,11 @@
+require "lib/alogr"
+
+describe "AlogR" do
+ before do
+ puts "Removing log/ directory"
+ system("rm -rf log/")
+ end
+
+ it "should log to many logs, as specified to the initialization." do
+ end
+end
View
@@ -0,0 +1,11 @@
+require "lib/alogr"
+
+describe "AlogR" do
+ before do
+ puts "Removing log/ directory"
+ system("rm -rf log/")
+ end
+
+ it "should log to logs based on a RegExp, as specified to the initialization." do
+ end
+end

0 comments on commit 1a48c91

Please sign in to comment.