Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Topic: merge Add a sink for writing to stdout/stderr. (add topic merge)
Browse files Browse the repository at this point in the history
Test plan:
Added unit tests.

Change-Id: I4460f158ef153b2ce3db25e74f01d5448833c8c5
  • Loading branch information
mpage committed Jun 8, 2011
1 parent b166775 commit 1c25415
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions vcap_logging/lib/vcap/logging/sink.rb
@@ -1,3 +1,4 @@
require 'vcap/logging/sink/base_sink'
require 'vcap/logging/sink/file_sink'
require 'vcap/logging/sink/stdio_sink'
require 'vcap/logging/sink/syslog_sink'
40 changes: 40 additions & 0 deletions vcap_logging/lib/vcap/logging/sink/stdio_sink.rb
@@ -0,0 +1,40 @@
require 'vcap/logging/sink/base_sink'

module VCAP::Logging::Sink

# A sink for writing to stderr/stdout
# Usage:
# stdout_sink = VCAP::Logging::Sink::StdioSink.new(STDOUT)
#
class StdioSink < BaseSink
def initialize(io, formatter=nil)
super(formatter)
@io = io
@out = nil
open
end

def open
@mutex.synchronize do
@opened = true
@out = @io
end
end

def close
@mutex.synchronize do
@opened = false
@out = nil
end
end

private

def write(message)
@mutex.synchronize do
@io.write(message)
end
end

end
end
20 changes: 20 additions & 0 deletions vcap_logging/spec/unit/stdio_sink_spec.rb
@@ -0,0 +1,20 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe VCAP::Logging::Sink::StdioSink do
it 'should not close the underlying io object when closed' do
io = mock(:stdout)
io.should_not_receive(:close)
sink = VCAP::Logging::Sink::StdioSink.new(io)
sink.close
end

it 'should write formatted messages to the underlying io device' do
msg = 'test log message'
fmt = mock(:formatter)
fmt.should_receive(:format_record).with(nil).and_return(msg)
io = mock(:stdio)
io.should_receive(:write).with(msg)
sink = VCAP::Logging::Sink::StdioSink.new(io, fmt)
sink.add_record(nil)
end
end

0 comments on commit 1c25415

Please sign in to comment.