lib/zlogger/client.rb in zlogger-0.0.2 vs lib/zlogger/client.rb in zlogger-0.1.0

- old
+ new

@@ -15,11 +15,11 @@ def initialize(options={}) @options = options super(nil) @logdev = LogDevice.new(self) @logdev.run_socket_thread - + @formatter = proc do |severity, time, progname, msg| if msg.is_a?(Exception) "#{severity}: #{msg.message} (#{msg.class})\n" + (msg.backtrace || []).join("\n") else "#{severity}: #{msg}" @@ -45,10 +45,18 @@ def name options[:name] || "#{File.basename($0)}:#{Process.pid}" end + def log_device + @logdev + end + + def close + @logdev.close + end + class LogDevice attr :client def initialize(client) @client = client @@ -62,25 +70,27 @@ client.queue << self end # it is not threadsafe to access ZMQ sockets, so we only write to the logging socket from a single thread. def run_socket_thread - @thread ||= Thread.new do - begin - socket = client.context.socket :PUB - socket.connect("tcp://#{client.connect_address}:#{client.port}") - loop do - object = client.queue.pop - break if object == self - message = ZMQ::Message.new - message.addstr(client.name) - message.addstr(object.to_s) - socket.send_message(message) - end - socket.close - rescue StandardError => e - puts "Logging socket thread error: #{e}" + @thread ||= Thread.new { run_socket_loop } + end + + def run_socket_loop + begin + socket = client.context.socket :PUB + socket.connect("tcp://#{client.connect_address}:#{client.port}") + loop do + object = client.queue.pop + break if object == self + message = ZMQ::Message.new + message.addstr(client.name) + message.addstr(object.to_s) + socket.send_message(message) end + socket.close + rescue StandardError => e + puts "Logging socket thread error: #{e}" end end end end end