Restored pretty messages for logging.

This commit is contained in:
Kyle Banker 2009-11-02 15:04:06 -05:00
parent 44ff6c5918
commit 95f3686119
3 changed files with 27 additions and 26 deletions

View File

@ -228,7 +228,8 @@ module Mongo
BSON.serialize_cstr(message, "#{@db.name}.#{@name}")
message.put_int(0)
message.put_array(BSON.new.serialize(selector, false).to_a)
@db.send_message_with_operation(Mongo::Constants::OP_DELETE, message)
@db.send_message_with_operation(Mongo::Constants::OP_DELETE, message,
"db.#{@db.name}.remove(#{selector.inspect})")
end
# Remove all records.
@ -258,7 +259,8 @@ module Mongo
message.put_int(options[:upsert] ? 1 : 0) # 1 if a repsert operation (upsert)
message.put_array(BSON.new.serialize(spec, false).to_a)
message.put_array(BSON.new.serialize(document, false).to_a)
@db.send_message_with_operation(Mongo::Constants::OP_UPDATE, message)
@db.send_message_with_operation(Mongo::Constants::OP_UPDATE, message,
"db.#{@name}.update(#{spec.inspect}, #{document.inspect})")
if options[:safe] && error=@db.error
raise OperationFailure, error
@ -483,7 +485,8 @@ EOS
message.put_int(0)
BSON.serialize_cstr(message, "#{@db.name}.#{collection_name}")
documents.each { |doc| message.put_array(BSON.new.serialize(doc, check_keys).to_a) }
@db.send_message_with_operation(Mongo::Constants::OP_INSERT, message)
@db.send_message_with_operation(Mongo::Constants::OP_INSERT, message,
"db.#{collection_name}.insert(#{documents.inspect})")
documents.collect { |o| o[:_id] || o['_id'] }
end

View File

@ -199,7 +199,7 @@ module Mongo
message.put_int(0)
message.put_int(1)
message.put_long(@cursor_id)
@db.send_message_with_operation(Mongo::Constants::OP_KILL_CURSORS, message)
@db.send_message_with_operation(Mongo::Constants::OP_KILL_CURSORS, message, "cursor.close()")
end
@cursor_id = 0
@closed = true
@ -344,7 +344,7 @@ module Mongo
# Cursor id.
message.put_long(@cursor_id)
@db.send_message_with_operation_without_synchronize(Mongo::Constants::OP_GET_MORE, message)
@db.send_message_with_operation_without_synchronize(Mongo::Constants::OP_GET_MORE, message, "cursor.get_more()")
read_all
}
close_cursor_if_query_complete
@ -366,9 +366,10 @@ module Mongo
if @query_run
false
else
message = construct_query_message(@query)
message = construct_query_message
@db._synchronize {
@db.send_message_with_operation_without_synchronize(Mongo::Constants::OP_QUERY, message)
@db.send_message_with_operation_without_synchronize(Mongo::Constants::OP_QUERY, message,
(query_log_message if @db.logger))
@query_run = true
read_all
}
@ -377,7 +378,7 @@ module Mongo
end
end
def construct_query_message(query)
def construct_query_message
message = ByteBuffer.new
message.put_int(query_opts)
db_name = @admin ? 'admin' : @db.name
@ -393,6 +394,11 @@ module Mongo
message
end
def query_log_message
"db.#{@admin ? 'admin' : @db.name}.#{@collection.name}.find(#{@selector.inspect}, #{@fields ? @fields.inspect : '{}'})" +
"#{@skip != 0 ? ('.skip(' + @skip.to_s + ')') : ''}#{@limit != 0 ? ('.limit(' + @limit.to_s + ')') : ''}"
end
def selector_with_special_query_fields
sel = OrderedHash.new
sel['query'] = @selector

View File

@ -431,28 +431,17 @@ module Mongo
self.collection(collection_name).create_index(field_or_spec, unique)
end
def send_to_db(message)
connect_to_master if !connected? && @auto_reconnect
begin
@logger.debug(" MONGODB #{message}") if @logger
@socket.print(message.buf.to_s)
@socket.flush
rescue => ex
close
raise ex
end
end
# Sends a message to MongoDB.
#
# Takes a MongoDB opcode, +operation+, and a message of class ByteBuffer,
# +message+, and sends the message to the databse, adding the necessary headers.
def send_message_with_operation(operation, message)
# Takes a MongoDB opcode, +operation+, a message of class ByteBuffer,
# +message+, and an optional formatted +log_message+.
# Sends the message to the databse, adding the necessary headers.
def send_message_with_operation(operation, message, log_message=nil)
@semaphore.synchronize do
connect_to_master if !connected? && @auto_reconnect
begin
message_with_headers = add_message_headers(operation, message)
@logger.debug(" MONGODB #{message}") if @logger
@logger.debug(" MONGODB #{log_message || message}") if @logger
@socket.print(message_with_headers.to_s)
@socket.flush
rescue => ex
@ -462,11 +451,13 @@ module Mongo
end
end
def send_message_with_operation_without_synchronize(operation, message)
# Note: this is a temporary method. Will be removed in an upcoming
# refactoring.
def send_message_with_operation_without_synchronize(operation, message, log_message=nil)
connect_to_master if !connected? && @auto_reconnect
begin
message_with_headers = add_message_headers(operation, message)
@logger.debug(" MONGODB #{operation} #{message}") if @logger
@logger.debug(" MONGODB #{log_message || message}") if @logger
@socket.print(message_with_headers.to_s)
@socket.flush
rescue => ex
@ -475,6 +466,7 @@ module Mongo
end
end
# Note: this method is a stub. Will be completed in an upcoming refactoring.
def receive_message_with_operation(operation, message)
@semaphore.synchronize do