Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ST: Added #open! method to Base adapter and refactored classes accord…

…ingly
  • Loading branch information...
commit 0b901ac29291a143cfcd465ecf78200a9005a05e 1 parent f33f704
@rudionrails authored
View
20 lib/yell/adapters/base.rb
@@ -64,11 +64,21 @@ def write( &block )
compile!( :write!, &block )
end
+ # Define your open method with this helper.
+ #
+ # @example Open a file handle
+ # open do
+ # @stream = ::File.open( 'test.log', ::File::WRONLY|::File::APPEND|::File::CREAT )
+ # end
+ def open( &block )
+ compile!( :open!, &block )
+ end
+
# Define your close method with this helper.
#
# @example Closing a file handle
# close do
- # @file_handle.close
+ # @stream.close
# end
def close( &block )
compile!( :close!, &block )
@@ -171,6 +181,14 @@ def write!( event )
# Not implemented
end
+ # Perform the actual open.
+ #
+ # Adapter classes should provide their own implementation
+ # of this method.
+ def open!
+ # Not implemented
+ end
+
# Perform the actual close.
#
# Adapter classes should provide their own implementation
View
24 lib/yell/adapters/file.rb
@@ -9,32 +9,14 @@ class File < Yell::Adapters::Io
setup do |options|
@filename = ::File.expand_path options.fetch(:filename, default_filename)
-
- # sync immediately to IO (or not)
- self.sync = options.fetch(:sync, true)
end
-
- # Sets the “sync mode” to true or false.
- #
- # When true (default), every log event is immediately written to the file.
- # When false, the log event is buffered internally.
- attr_accessor :sync
-
-
- private
-
- # @overload Lazily open the file handle
- def stream
- synchronize { @stream or open! }
+ open do
+ @stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
end
- def open!
- @stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
- @stream.sync = sync
- @stream
- end
+ private
def default_filename #:nodoc:
logdir = ::File.expand_path("log")
View
22 lib/yell/adapters/io.rb
@@ -20,8 +20,14 @@ class Io < Yell::Adapters::Base
setup do |options|
@stream = nil
+ # colorize the log output (default: false)
self.colors = options.fetch(:colors, false)
+
+ # format the log message (default: nil)
self.format = options.fetch(:format, nil)
+
+ # sync immediately to IO (default: true)
+ self.sync = options.fetch(:sync, true)
end
write do |event|
@@ -35,7 +41,12 @@ class Io < Yell::Adapters::Base
# add new line if there is none
message << "\n" unless message[-1] == ?\n
- stream.write( message )
+ stream.syswrite( message )
+ end
+
+ open do
+ @stream.sync = self.sync if @stream.respond_to? :sync
+ @stream.flush if @stream.respond_to? :flush
end
close do
@@ -44,6 +55,12 @@ class Io < Yell::Adapters::Base
end
+ # Sets the “sync mode” to true or false.
+ #
+ # When true (default), every log event is immediately written to the file.
+ # When false, the log event is buffered internally.
+ attr_accessor :sync
+
# Sets colored output on or off (default off)
#
# @example Enable colors
@@ -67,8 +84,9 @@ def colorize!; @colors = true; end
# Adapter classes should provide their own implementation
# of this method.
def stream
- raise 'Not implemented'
+ synchronize { open! if @stream.nil?; @stream }
end
+
end
end
View
16 lib/yell/adapters/streams.rb
@@ -5,22 +5,18 @@ module Adapters #:nodoc:
class Stdout < Yell::Adapters::Io
- private
-
- # @overload Lazily open the STDOUT stream
- def stream
- @stream ||= $stdout.clone
+ open do
+ @stream = $stdout.clone
end
+
end
class Stderr < Yell::Adapters::Io
- private
-
- # @overload Lazily open the STDERR stream
- def stream
- @stream ||= $stderr.clone
+ open do
+ @stream = $stderr.clone
end
+
end
register( :stdout, Yell::Adapters::Stdout )
View
16 lib/yell/formatter.rb
@@ -130,14 +130,14 @@ def message( *messages )
def to_message( m )
case m
- when Hash
- m.map { |k,v| "#{k}: #{v}" }.join( ", " )
- when Exception
- backtrace = m.backtrace ? "\n\t#{m.backtrace.join("\n\t")}" : ""
-
- "%s: %s%s" % [m.class, m.message, backtrace]
- else
- m
+ when Hash
+ m.map { |k,v| "#{k}: #{v}" }.join( ", " )
+ when Exception
+ backtrace = m.backtrace ? "\n\t#{m.backtrace.join("\n\t")}" : ""
+
+ "%s: %s%s" % [m.class, m.message, backtrace]
+ else
+ m
end
end
View
2  spec/spec_helper.rb
@@ -14,10 +14,10 @@
config.before do
Yell::Repository.loggers.clear
+ Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
end
config.after do
- Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
end
private
View
8 spec/yell/adapters/io_spec.rb
@@ -40,12 +40,6 @@
end
end
- context :stream do
- it "should raise" do
- lambda { Yell::Adapters::Io.new.send :stream }.should raise_error("Not implemented" )
- end
- end
-
context :write do
let( :event ) { Yell::Event.new(1, "Hello World") }
let( :adapter ) { Yell::Adapters::Io.new }
@@ -63,7 +57,7 @@
it "should print formatted message to stream" do
formatted = Yell::Formatter.new.format( event )
- mock( stream ).write( formatted << "\n" )
+ mock( stream ).syswrite( formatted << "\n" )
adapter.write( event )
end
View
4 spec/yell/logger_spec.rb
@@ -148,8 +148,8 @@ def bar
factory = LoggerFactory.new
factory.logger = logger
- mock( adapter.send(:stream) ).write( "#{__FILE__}, 7: foo\n" )
- mock( adapter.send(:stream) ).write( "#{__FILE__}, 11: bar\n" )
+ mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 7: foo\n" )
+ mock( adapter.send(:stream) ).syswrite( "#{__FILE__}, 11: bar\n" )
factory.foo
factory.bar
Please sign in to comment.
Something went wrong with that request. Please try again.