couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fdman...@apache.org
Subject svn commit: r1056462 - /couchdb/trunk/src/couchdb/couch_log.erl
Date Fri, 07 Jan 2011 19:08:27 GMT
Author: fdmanana
Date: Fri Jan  7 19:08:27 2011
New Revision: 1056462

URL: http://svn.apache.org/viewvc?rev=1056462&view=rev
Log:
Small refactoring of couch_log

Also added a precompiled end-of-line regexp to the state of the gen_event and a missing
file:close/1 call.

Modified:
    couchdb/trunk/src/couchdb/couch_log.erl

Modified: couchdb/trunk/src/couchdb/couch_log.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_log.erl?rev=1056462&r1=1056461&r2=1056462&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_log.erl (original)
+++ couchdb/trunk/src/couchdb/couch_log.erl Fri Jan  7 19:08:27 2011
@@ -13,26 +13,34 @@
 -module(couch_log).
 -behaviour(gen_event).
 
--export([start_link/0,stop/0]).
--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]).
+% public API
+-export([start_link/0, stop/0]).
+-export([debug_on/0, info_on/0, get_level/0, get_level_integer/0, set_level/1]).
 -export([read/2]).
 
+% gen_event callbacks
+-export([init/1, handle_event/2, terminate/2, code_change/3]).
+-export([handle_info/2, handle_call/2]).
+
 -define(LEVEL_ERROR, 3).
 -define(LEVEL_INFO, 2).
 -define(LEVEL_DEBUG, 1).
--define(LEVEL_TMI, 0).
+
+-record(state, {
+    fd,
+    level,
+    sasl,
+    eol_re
+}).
 
 level_integer(error)    -> ?LEVEL_ERROR;
 level_integer(info)     -> ?LEVEL_INFO;
 level_integer(debug)    -> ?LEVEL_DEBUG;
-level_integer(tmi)      -> ?LEVEL_TMI;
 level_integer(_Else)    -> ?LEVEL_ERROR. % anything else default to ERROR level
 
 level_atom(?LEVEL_ERROR) -> error;
 level_atom(?LEVEL_INFO) -> info;
-level_atom(?LEVEL_DEBUG) -> debug;
-level_atom(?LEVEL_TMI) -> tmi.
+level_atom(?LEVEL_DEBUG) -> debug.
 
 
 start_link() ->
@@ -57,7 +65,7 @@ init([]) ->
 
     Filename = couch_config:get("log", "file", "couchdb.log"),
     Level = level_integer(list_to_atom(couch_config:get("log", "level", "info"))),
-    Sasl = list_to_atom(couch_config:get("log", "include_sasl", "true")),
+    Sasl = couch_config:get("log", "include_sasl", "true") =:= "true",
 
     case ets:info(?MODULE) of
     undefined -> ets:new(?MODULE, [named_table]);
@@ -67,7 +75,8 @@ init([]) ->
 
     case file:open(Filename, [append]) of
     {ok, Fd} ->
-        {ok, {Fd, Level, Sasl}};
+        {ok, EolRe} = re:compile("\\r\\n|\\r|\\n"),
+        {ok, #state{fd = Fd, level = Level, sasl = Sasl, eol_re = EolRe}};
     {error, eacces} ->
         {stop, {file_permission_error, Filename}};
     Error ->
@@ -96,36 +105,29 @@ 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({Pid, couch_error, {Format, Args}}, State) ->
+    log(State, Pid, error, Format, Args),
     {ok, State};
-handle_event({Pid, couch_info, {Format, Args}}, {Fd, LogLevel, _Sasl}=State)
+handle_event({Pid, couch_info, {Format, Args}}, #state{level = LogLevel} = St)
 when LogLevel =< ?LEVEL_INFO ->
-    log(Fd, Pid, info, Format, Args),
-    {ok, State};
-handle_event({Pid, couch_debug, {Format, Args}}, {Fd, LogLevel, _Sasl}=State)
+    log(St, Pid, info, Format, Args),
+    {ok, St};
+handle_event({Pid, couch_debug, {Format, Args}}, #state{level = LogLevel} = St)
 when LogLevel =< ?LEVEL_DEBUG ->
-    log(Fd, Pid, debug, Format, Args),
-    {ok, 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]),
+    log(St, Pid, debug, Format, Args),
+    {ok, St};
+handle_event({error_report, _, {Pid, _, _}}=Event, #state{sasl = true} = St) ->
+    log(St, Pid, error, "~p", [Event]),
+    {ok, St};
+handle_event({error, _, {Pid, Format, Args}}, #state{sasl = true} = State) ->
+    log(State, Pid, error, Format, Args),
     {ok, State};
 handle_event(_Event, State) ->
     {ok, State}.
 
-handle_call({set_level_integer, NewLevel}, {Fd, _LogLevel, Sasl}) ->
+handle_call({set_level_integer, NewLevel}, State) ->
     ets:insert(?MODULE, {level, NewLevel}),
-    {ok, ok, {Fd, NewLevel, Sasl}}.
+    {ok, ok, State#state{level = NewLevel}}.
 
 handle_info(_Info, State) ->
     {ok, State}.
@@ -133,15 +135,15 @@ handle_info(_Info, State) ->
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
-terminate(_Arg, {Fd, _LoggingLevel, _Sasl}) ->
+terminate(_Arg, #state{fd = Fd}) ->
     file:close(Fd).
 
-log(Fd, Pid, Level, Format, Args) ->
+log(#state{fd = Fd, eol_re = EolRe}, Pid, Level, Format, Args) ->
     Msg = io_lib:format(Format, Args),
     ok = io:format("[~s] [~p] ~s~n", [Level, Pid, Msg]), % dump to console too
-    Msg2 = re:replace(lists:flatten(Msg),"\\r\\n|\\r|\\n", "\r\n",
-        [global, {return, list}]),
-    ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]).
+    Msg2 = re:replace(Msg, EolRe, "\r\n", [global]),
+    ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n",
+        [httpd_util:rfc1123_date(), Level, Pid, Msg2]).
 
 read(Bytes, Offset) ->
     LogFileName = couch_config:get("log", "file"),
@@ -154,4 +156,5 @@ read(Bytes, Offset) ->
     % TODO: make streaming
 
     {ok, Chunk} = file:pread(Fd, Start, LogFileSize),
+    ok = file:close(Fd),
     Chunk.



Mime
View raw message