Permalink
Browse files

Switched over to C logging method, since Ruby blocks in a C extension.

git-svn-id: svn+ssh://rubyforge.org/var/svn/alogr/trunk@14 5d4fd1e0-7adf-40a7-adde-e9af6235188f
  • Loading branch information...
1 parent d89bbc0 commit 03600d9c8d8048d75161485e1fe538adf4853499 wayneeseguin committed Sep 20, 2007
Showing with 87 additions and 37 deletions.
  1. +74 −32 ext/aio_logger/aio_logger.c
  2. +13 −5 lib/alogr.rb
View
106 ext/aio_logger/aio_logger.c
@@ -8,16 +8,25 @@
#include <stdio.h>
#include <fcntl.h> // File modes O_*
+// TODO: build a switch for aio_write vs aio_write64
+
// Asynchronously log the string of specified length to given file
-int aio_log(char * string, int length, char * file_name) {
+VALUE rb_aio_log(VALUE rb_string, VALUE rb_file_name) {
int file_descriptor, return_value, bytes_written;
const struct aiocb *aio_control_block_list;
struct aiocb control_block;
- bzero( &control_block, sizeof (struct aiocb));
+ bzero( &control_block, sizeof (struct aiocb));
+ char * string, file_name;
+ int length;
+
+ file_name = RSTRING(rb_file_name)->ptr;
+ string = RSTRING(rb_string)->ptr;
+ length = RSTRING(rb_string)->len;
+ printf("string: '%s', filename: '%s'", string, file_name);
if ((file_descriptor = open(file_name, O_CREAT | O_RDWR | O_APPEND)) == -1) {
- return 1;
+ return Qfalse;
}
//Set up the control block
@@ -37,36 +46,68 @@ int aio_log(char * string, int length, char * file_name) {
close(file_descriptor);
- return 0;
+ return Qtrue;
}
-VALUE rb_flush_log_buffer() {
- 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");
-
- 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));
-
- 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
- );
-
- if( return_value > 0 ) {
- // Unable to open the log file
- return Qfalse;
- }
- packet = rb_ary_shift(buffer); // Fetch the next log packet
- }
-
- return Qtrue;
-}
+//// Asynchronously log the string of specified length to given file
+//int aio_log(char * string, int length, char * file_name) {
+// int file_descriptor, return_value, bytes_written;
+// const struct aiocb *aio_control_block_list;
+// struct aiocb control_block;
+//
+// bzero( &control_block, sizeof (struct aiocb));
+//
+// if ((file_descriptor = open(file_name, O_CREAT | O_RDWR | O_APPEND)) == -1) {
+// return 1;
+// }
+//
+// //Set up the control block
+// control_block.aio_buf = (void *) string;
+// control_block.aio_fildes = file_descriptor; //fileno(file);
+// control_block.aio_nbytes = length;
+// control_block.aio_offset = 0;
+//
+// // Perform the asynch write
+// 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);
+// bytes_written = aio_return( &control_block);
+//
+// close(file_descriptor);
+//
+// return 0;
+//}
+//
+//VALUE rb_flush_log_buffer() {
+// 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");
+//
+// 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));
+//
+// 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
+// );
+//
+// if( return_value > 0 ) {
+// // Unable to open the log file
+// return Qfalse;
+// }
+// packet = rb_ary_shift(buffer); // Fetch the next log packet
+// }
+//
+// return Qtrue;
+//}
static VALUE rb_mAlogR;
static VALUE rb_cLogger;
@@ -79,5 +120,6 @@ void Init_aio_logger() {
rb_gv_set("alogr_buffer", rb_ary_new());
rb_gv_set("alogr_log_files", rb_ary_new());
- rb_define_method(rb_cLogger, "flush_log_buffer", rb_flush_log_buffer, 0);
+// rb_define_method(rb_cLogger, "flush_log_buffer", rb_flush_log_buffer, 0);
+ rb_define_method(rb_cLogger, "aio_log", rb_aio_log, 2);
}
View
18 lib/alogr.rb
@@ -1,5 +1,5 @@
require "aio_logger"
-# require "YAML"
+# require "yaml"
module AlogR
@@ -36,9 +36,17 @@ def initialize( options = {} )
Thread.new do
loop do
sleep( @@config[:log_interval] || 0.25 )
- unless flush_log_buffer
- raise "Unable to open a log file" # TODO: Be more specific
- end
+ packet = $alogr_buffer.shift
+ while !packet.nil? do
+ string = packet.shift
+ level = packet.shift
+ puts "packet: string: #{string}, filename: #{$alogr_log_files[level]ma}"
+ aio_log( string, $alogr_log_files[level])
+ packet = $alogr_buffer.shift
+ end
+ #unless flush_log_buffer
+ # raise "Unable to open a log file" # TODO: Be more specific
+ #end
end
Thread.exit
end
@@ -53,7 +61,7 @@ def initialize( options = {} )
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]
+ $alogr_buffer << [ string, level ]
end
def method_missing( method, *options )

0 comments on commit 03600d9

Please sign in to comment.