diff options
author | Adam Kocoloski <kocolosk@apache.org> | 2010-06-13 14:15:29 +0000 |
---|---|---|
committer | Adam Kocoloski <kocolosk@apache.org> | 2010-06-13 14:15:29 +0000 |
commit | 3c83fe3827bcebf1e8bc8c5c16af1ad9fd9e0555 (patch) | |
tree | 469c71ebab8ec516ca19ae42f47cae586b0480e3 | |
parent | d8c25ff8ee02aa5bb6f3a8aecf479109226d42db (diff) |
synchronous logging, COUCHDB-761. Patch by Randall Leeds.
This patch fixes a bug where calls to get the log level could time
out, resulting in an unexpected {ref(), integer()} message in the
client's mailbox. This would crash some gen_servers that did not
expect the message.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@954229 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/couchdb/couch_db.hrl | 11 | ||||
-rw-r--r-- | src/couchdb/couch_log.erl | 46 |
2 files changed, 36 insertions, 21 deletions
diff --git a/src/couchdb/couch_db.hrl b/src/couchdb/couch_db.hrl index f82406e8..f1b88a05 100644 --- a/src/couchdb/couch_db.hrl +++ b/src/couchdb/couch_db.hrl @@ -28,18 +28,23 @@ -define(LOG_DEBUG(Format, Args), case couch_log:debug_on() of - true -> error_logger:info_report(couch_debug, {Format, Args}); + 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 -> error_logger:info_report(couch_info, {Format, Args}); + true -> + gen_event:sync_notify(error_logger, + {self(), couch_info, {Format, Args}}); false -> ok end). -define(LOG_ERROR(Format, Args), - error_logger:error_report(couch_error, {Format, Args})). + gen_event:sync_notify(error_logger, + {self(), couch_error, {Format, Args}})). -record(rev_info, diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl index 5a45c207..5c8a5e5c 100644 --- a/src/couchdb/couch_log.erl +++ b/src/couchdb/couch_log.erl @@ -50,14 +50,23 @@ init([]) -> fun("log", "file") -> ?MODULE:stop(); ("log", "level") -> + ?MODULE:stop(); + ("log", "include_sasl") -> ?MODULE:stop() end), Filename = couch_config:get("log", "file", "couchdb.log"), - Level = couch_config:get("log", "level", "info"), + Level = level_integer(list_to_atom(couch_config:get("log", "level", "info"))), + Sasl = list_to_atom(couch_config:get("log", "include_sasl", "true")), + + case ets:info(?MODULE) of + undefined -> ets:new(?MODULE, [named_table]); + _ -> ok + end, + ets:insert(?MODULE, {level, Level}), {ok, Fd} = file:open(Filename, [append]), - {ok, {Fd, level_integer(list_to_atom(Level))}}. + {ok, {Fd, Level, Sasl}}. debug_on() -> get_level_integer() =< ?LEVEL_DEBUG. @@ -72,29 +81,31 @@ get_level() -> level_atom(get_level_integer()). get_level_integer() -> - catch gen_event:call(error_logger, couch_log, get_level_integer). + ets:lookup_element(?MODULE, level, 2). set_level_integer(Int) -> gen_event:call(error_logger, couch_log, {set_level_integer, Int}). -handle_event({error_report, _, {Pid, couch_error, {Format, Args}}}, {Fd, _LogLevel}=State) -> - log(Fd, Pid, error, Format, Args), - {ok, State}; -handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel}=State) -> - log(Fd, Pid, error, "~p", [Event]), - {ok, State}; -handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel}=State) -> +handle_event({Pid, couch_error, {Format, Args}}, {Fd, _LogLevel, _Sasl}=State) -> log(Fd, Pid, error, Format, Args), {ok, State}; -handle_event({info_report, _, {Pid, couch_info, {Format, Args}}}, {Fd, LogLevel}=State) +handle_event({Pid, couch_info, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_INFO -> log(Fd, Pid, info, Format, Args), {ok, State}; -handle_event({info_report, _, {Pid, couch_debug, {Format, Args}}}, {Fd, LogLevel}=State) +handle_event({Pid, couch_debug, {Format, Args}}, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_DEBUG -> log(Fd, Pid, debug, Format, Args), {ok, State}; -handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel}=State) +handle_event({error_report, _, {Pid, _, _}}=Event, {Fd, _LogLevel, Sasl}=State) +when Sasl =/= false -> + log(Fd, Pid, error, "~p", [Event]), + {ok, State}; +handle_event({error, _, {Pid, Format, Args}}, {Fd, _LogLevel, Sasl}=State) +when Sasl =/= false -> + log(Fd, Pid, error, Format, Args), + {ok, State}; +handle_event({_, _, {Pid, _, _}}=Event, {Fd, LogLevel, _Sasl}=State) when LogLevel =< ?LEVEL_TMI -> % log every remaining event if tmi! log(Fd, Pid, tmi, "~p", [Event]), @@ -102,10 +113,9 @@ when LogLevel =< ?LEVEL_TMI -> handle_event(_Event, State) -> {ok, State}. -handle_call(get_level_integer, {_Fd, LogLevel}=State) -> - {ok, LogLevel, State}; -handle_call({set_level_integer, NewLevel}, {Fd, _LogLevel}) -> - {ok, ok, {Fd, NewLevel}}. +handle_call({set_level_integer, NewLevel}, {Fd, _LogLevel, Sasl}) -> + ets:insert(?MODULE, {level, NewLevel}), + {ok, ok, {Fd, NewLevel, Sasl}}. handle_info(_Info, State) -> {ok, State}. @@ -113,7 +123,7 @@ handle_info(_Info, State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -terminate(_Arg, {Fd, _LoggingLevel}) -> +terminate(_Arg, {Fd, _LoggingLevel, _Sasl}) -> file:close(Fd). log(Fd, Pid, Level, Format, Args) -> |