From 00d2709afbff6f402d91f5d1dc3e5a5075685c49 Mon Sep 17 00:00:00 2001 From: Filipe David Borba Manana Date: Mon, 7 Feb 2011 08:27:30 +0000 Subject: Merged revision 1067873 from trunk More efficient logging, closes COUCHDB-1054 git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1067877 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_db.hrl | 23 +++------------------ src/couchdb/couch_log.erl | 52 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/couchdb/couch_db.hrl b/src/couchdb/couch_db.hrl index 6e205ec1..6b6c6440 100644 --- a/src/couchdb/couch_db.hrl +++ b/src/couchdb/couch_db.hrl @@ -25,26 +25,9 @@ -define(DEFAULT_ATTACHMENT_CONTENT_TYPE, <<"application/octet-stream">>). --define(LOG_DEBUG(Format, Args), - case couch_log:debug_on() of - true -> - gen_event:sync_notify(error_logger, - {self(), couch_debug, {Format, Args}}); - false -> ok - end). - --define(LOG_INFO(Format, Args), - case couch_log:info_on() of - true -> - gen_event:sync_notify(error_logger, - {self(), couch_info, {Format, Args}}); - false -> ok - end). - --define(LOG_ERROR(Format, Args), - gen_event:sync_notify(error_logger, - {self(), couch_error, {Format, Args}})). - +-define(LOG_DEBUG(Format, Args), couch_log:debug(Format, Args)). +-define(LOG_INFO(Format, Args), couch_log:info(Format, Args)). +-define(LOG_ERROR(Format, Args), couch_log:error(Format, Args)). -record(rev_info, { diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl index 3b6ce97c..9813f812 100644 --- a/src/couchdb/couch_log.erl +++ b/src/couchdb/couch_log.erl @@ -14,6 +14,7 @@ -behaviour(gen_event). -export([start_link/0,stop/0]). +-export([debug/2, info/2, error/2]). -export([debug_on/0,info_on/0,get_level/0,get_level_integer/0, set_level/1]). -export([init/1, handle_event/2, terminate/2, code_change/3, handle_info/2, handle_call/2]). -export([read/2]). @@ -23,6 +24,29 @@ -define(LEVEL_DEBUG, 1). -define(LEVEL_TMI, 0). +debug(Format, Args) -> + case debug_on() of + false -> + ok; + true -> + {ConsoleMsg, FileMsg} = get_log_messages(self(), debug, Format, Args), + gen_event:sync_notify(error_logger, {couch_debug, ConsoleMsg, FileMsg}) + end. + +info(Format, Args) -> + case info_on() of + false -> + ok; + true -> + {ConsoleMsg, FileMsg} = get_log_messages(self(), info, Format, Args), + gen_event:sync_notify(error_logger, {couch_info, ConsoleMsg, FileMsg}) + end. + +error(Format, Args) -> + {ConsoleMsg, FileMsg} = get_log_messages(self(), error, Format, Args), + gen_event:sync_notify(error_logger, {couch_error, ConsoleMsg, FileMsg}). + + level_integer(error) -> ?LEVEL_ERROR; level_integer(info) -> ?LEVEL_INFO; level_integer(debug) -> ?LEVEL_DEBUG; @@ -96,24 +120,26 @@ get_level_integer() -> set_level_integer(Int) -> gen_event:call(error_logger, couch_log, {set_level_integer, Int}). -handle_event({Pid, couch_error, {Format, Args}}, {Fd, _LogLevel, _Sasl}=State) -> - log(Fd, Pid, error, Format, Args), +handle_event({couch_error, ConMsg, FileMsg}, {Fd, _LogLevel, _Sasl}=State) -> + log(Fd, ConMsg, FileMsg), {ok, State}; -handle_event({Pid, couch_info, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) +handle_event({couch_info, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_INFO -> - log(Fd, Pid, info, Format, Args), + log(Fd, ConMsg, FileMsg), {ok, State}; -handle_event({Pid, couch_debug, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) +handle_event({couch_debug, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_DEBUG -> - log(Fd, Pid, debug, Format, Args), + log(Fd, ConMsg, FileMsg), {ok, State}; handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel, Sasl}=State) when Sasl =/= false -> - log(Fd, Pid, error, "~p", [Event]), + {ConMsg, FileMsg} = get_log_messages(Pid, error, "~p", [Event]), + log(Fd, ConMsg, FileMsg), {ok, State}; handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel, Sasl}=State) when Sasl =/= false -> - log(Fd, Pid, error, Format, Args), + {ConMsg, FileMsg} = get_log_messages(Pid, error, Format, Args), + log(Fd, ConMsg, FileMsg), {ok, State}; handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_TMI -> @@ -143,6 +169,16 @@ log(Fd, Pid, Level, Format, Args) -> [global, {return, list}]), ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]). +log(Fd, ConsoleMsg, FileMsg) -> + ok = io:put_chars(ConsoleMsg), + ok = io:put_chars(Fd, FileMsg). + +get_log_messages(Pid, Level, Format, Args) -> + ConsoleMsg = io_lib:format( + "[~s] [~p] " ++ Format ++ "~n", [Level, Pid | Args]), + FileMsg = ["[", httpd_util:rfc1123_date(), "] ", ConsoleMsg], + {iolist_to_binary(ConsoleMsg), iolist_to_binary(FileMsg)}. + read(Bytes, Offset) -> LogFileName = couch_config:get("log", "file"), LogFileSize = filelib:file_size(LogFileName), -- cgit v1.2.3