Permalink
Browse files

Replace mochiglobal with a public ETS table

This also removes the dependency on the syntax_tools and compiler
applications, which interfered with lager's startup/shutdown order.
  • Loading branch information...
1 parent d648264 commit a192bd9e919b70e1e3aa06f6ce4f52870b28a79a @Vagabond Vagabond committed Nov 14, 2012
View
@@ -24,7 +24,7 @@ docs:
./rebar doc
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
- xmerl webtool snmp public_key mnesia eunit syntax_tools compiler
+ xmerl webtool snmp public_key mnesia eunit
COMBO_PLT = $(HOME)/.riak_combo_dialyzer_plt
check_plt: compile
View
@@ -55,7 +55,7 @@
end).
-define(SHOULD_LOG(Level),
- lager_util:level_to_num(Level) =< element(1, lager_mochiglobal:get(loglevel, {?LOG_NONE, []}))).
+ lager_util:level_to_num(Level) =< element(1, lager_config:get(loglevel, {?LOG_NONE, []}))).
-define(NOTIFY(Level, Pid, Format, Args),
gen_event:notify(lager_event, {log, lager_msg:new(io_lib:format(Format, Args),
View
@@ -7,9 +7,7 @@
{modules, []},
{applications, [
kernel,
- stdlib,
- compiler,
- syntax_tools
+ stdlib
]},
{registered, []},
{mod, {lager_app, []}},
View
@@ -19,7 +19,6 @@
-module(lager).
-include("lager.hrl").
--include_lib("eunit/include/eunit.hrl").
%% API
-export([start/0,
@@ -60,7 +59,7 @@ dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
%% lager isn't running
{error, lager_not_running};
Pid ->
- {LevelThreshold,TraceFilters} = lager_mochiglobal:get(loglevel,{?LOG_NONE,[]}),
+ {LevelThreshold,TraceFilters} = lager_config:get(loglevel,{?LOG_NONE,[]}),
SeverityAsInt=lager_util:level_to_num(Severity),
Destinations = case TraceFilters of
[] -> [];
@@ -115,10 +114,10 @@ trace_file(File, Filter, Level) ->
case Res of
{ok, _} ->
%% install the trace.
- {MinLevel, Traces} = lager_mochiglobal:get(loglevel),
+ {MinLevel, Traces} = lager_config:get(loglevel),
case lists:member(Trace, Traces) of
false ->
- lager_mochiglobal:put(loglevel, {MinLevel, [Trace|Traces]});
+ lager_config:set(loglevel, {MinLevel, [Trace|Traces]});
_ ->
ok
end,
@@ -137,10 +136,10 @@ trace_console(Filter, Level) ->
Trace0 = {Filter, Level, lager_console_backend},
case lager_util:validate_trace(Trace0) of
{ok, Trace} ->
- {MinLevel, Traces} = lager_mochiglobal:get(loglevel),
+ {MinLevel, Traces} = lager_config:get(loglevel),
case lists:member(Trace, Traces) of
false ->
- lager_mochiglobal:put(loglevel, {MinLevel, [Trace|Traces]});
+ lager_config:set(loglevel, {MinLevel, [Trace|Traces]});
_ -> ok
end,
{ok, Trace};
@@ -149,9 +148,9 @@ trace_console(Filter, Level) ->
end.
stop_trace({_Filter, _Level, Target} = Trace) ->
- {MinLevel, Traces} = lager_mochiglobal:get(loglevel),
+ {MinLevel, Traces} = lager_config:get(loglevel),
NewTraces = lists:delete(Trace, Traces),
- lager_mochiglobal:put(loglevel, {MinLevel, NewTraces}),
+ lager_config:set(loglevel, {MinLevel, NewTraces}),
case get_loglevel(Target) of
none ->
%% check no other traces point here
@@ -167,8 +166,8 @@ stop_trace({_Filter, _Level, Target} = Trace) ->
ok.
clear_all_traces() ->
- {MinLevel, _Traces} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {MinLevel, []}),
+ {MinLevel, _Traces} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {MinLevel, []}),
lists:foreach(fun(Handler) ->
case get_loglevel(Handler) of
none ->
@@ -196,16 +195,16 @@ status() ->
[begin
io_lib:format("Tracing messages matching ~p at level ~p to ~p\n",
[Filter, lager_util:num_to_level(Level), Destination])
- end || {Filter, Level, Destination} <- element(2, lager_mochiglobal:get(loglevel))]],
+ end || {Filter, Level, Destination} <- element(2, lager_config:get(loglevel))]],
io:put_chars(Status).
%% @doc Set the loglevel for a particular backend.
set_loglevel(Handler, Level) when is_atom(Level) ->
Reply = gen_event:call(lager_event, Handler, {set_loglevel, Level}, infinity),
%% recalculate min log level
MinLog = minimum_loglevel(get_loglevels()),
- {_, Traces} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {MinLog, Traces}),
+ {_, Traces} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {MinLog, Traces}),
Reply.
%% @doc Set the loglevel for a particular backend that has multiple identifiers
@@ -215,8 +214,8 @@ set_loglevel(Handler, Ident, Level) when is_atom(Level) ->
Reply = gen_event:call(lager_event, {Handler, Ident}, {set_loglevel, Level}, infinity),
%% recalculate min log level
MinLog = minimum_loglevel(get_loglevels()),
- {_, Traces} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {MinLog, Traces}),
+ {_, Traces} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {MinLog, Traces}),
Reply.
%% @doc Get the loglevel for a particular backend. In the case that the backend
View
@@ -33,8 +33,6 @@ start() ->
application:start(lager).
start(_StartType, _StartArgs) ->
- %% until lager is completely started, allow all messages to go through
- lager_mochiglobal:put(loglevel, {?DEBUG, []}),
{ok, Pid} = lager_sup:start_link(),
Handlers = case application:get_env(lager, handlers) of
undefined ->
@@ -51,8 +49,8 @@ start(_StartType, _StartArgs) ->
%% mask the messages we have no use for
MinLog = lager:minimum_loglevel(lager:get_loglevels()),
- {_, Traces} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {MinLog, Traces}),
+ {_, Traces} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {MinLog, Traces}),
SavedHandlers = case application:get_env(lager, error_logger_redirect) of
{ok, false} ->
View
@@ -0,0 +1,61 @@
+%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+
+%% @doc Helper functions for working with lager's runtime config
+
+-module(lager_config).
+
+-include("lager.hrl").
+
+-export([new/0, get/1, get/2, set/2]).
+
+-define(TBL, lager_config).
+
+new() ->
+ %% set up the ETS configuration table
+ try ets:new(?TBL, [named_table, public, set, {keypos, 1}, {read_concurrency, true}]) of
+ _Result ->
+ ok
+ catch
+ error:badarg ->
+ ?INT_LOG(warning, "Table ~p already exists", [?TBL])
+ end,
+ %% use insert_new here so that if we're in an appup we don't mess anything up
+ %%
+ %% until lager is completely started, allow all messages to go through
+ ets:insert_new(?TBL, {loglevel, {?DEBUG, []}}),
+ ok.
+
+
+get(Key) ->
+ case ets:lookup(?TBL, Key) of
+ [] ->
+ undefined;
+ [{Key, Res}] ->
+ Res
+ end.
+
+get(Key, Default) ->
+ case ?MODULE:get(Key) of
+ undefined ->
+ Default;
+ Res ->
+ Res
+ end.
+
+set(Key, Value) ->
+ ets:insert(?TBL, {Key, Value}).
+
@@ -116,8 +116,6 @@ console_log_test_() ->
application:load(lager),
application:set_env(lager, handlers, []),
application:set_env(lager, error_logger_redirect, false),
- application:start(compiler),
- application:start(syntax_tools),
application:start(lager),
whereis(user)
end,
@@ -135,7 +133,7 @@ console_log_test_() ->
unregister(user),
register(user, Pid),
erlang:group_leader(Pid, whereis(lager_event)),
- lager_mochiglobal:put(loglevel, {?INFO, []}),
+ lager_config:set(loglevel, {?INFO, []}),
lager:log(info, self(), "Test message"),
receive
{io_request, From, ReplyAs, {put_chars, unicode, Msg}} ->
@@ -154,7 +152,7 @@ console_log_test_() ->
register(user, Pid),
erlang:group_leader(Pid, whereis(lager_event)),
gen_event:add_handler(lager_event, lager_console_backend, [info, true]),
- lager_mochiglobal:put(loglevel, {?INFO, []}),
+ lager_config:set(loglevel, {?INFO, []}),
lager:info("Test message"),
lager:info("Test message"),
PidStr = pid_to_list(self()),
@@ -174,7 +172,7 @@ console_log_test_() ->
register(user, Pid),
gen_event:add_handler(lager_event, lager_console_backend, info),
erlang:group_leader(Pid, whereis(lager_event)),
- lager_mochiglobal:put(loglevel, {?INFO, []}),
+ lager_config:set(loglevel, {?INFO, []}),
lager:debug("Test message"),
receive
{io_request, From, ReplyAs, {put_chars, unicode, _Msg}} ->
@@ -202,7 +200,7 @@ console_log_test_() ->
unregister(user),
register(user, Pid),
gen_event:add_handler(lager_event, lager_console_backend, info),
- lager_mochiglobal:put(loglevel, {?INFO, []}),
+ lager_config:set(loglevel, {?INFO, []}),
erlang:group_leader(Pid, whereis(lager_event)),
lager:debug("Test message"),
receive
View
@@ -241,8 +241,6 @@ filesystem_test_() ->
application:set_env(lager, handlers, [{lager_test_backend, info}]),
application:set_env(lager, error_logger_redirect, true),
application:unset_env(lager, crash_log),
- application:start(compiler),
- application:start(syntax_tools),
application:start(lager),
timer:sleep(100),
lager_test_backend:flush()
View
@@ -241,8 +241,6 @@ filesystem_test_() ->
application:load(lager),
application:set_env(lager, handlers, [{lager_test_backend, info}]),
application:set_env(lager, error_logger_redirect, false),
- application:start(compiler),
- application:start(syntax_tools),
application:start(lager)
end,
fun(_) ->
@@ -352,8 +350,8 @@ filesystem_test_() ->
{"test.log", critical}),
lager:error("Test message"),
?assertEqual({ok, <<>>}, file:read_file("test.log")),
- {Level, _} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {Level, [{[{module,
+ {Level, _} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {Level, [{[{module,
?MODULE}], ?DEBUG,
{lager_file_backend, "test.log"}}]}),
lager:error("Test message"),
@@ -370,8 +368,8 @@ filesystem_test_() ->
{ok, Bin1} = file:read_file("test.log"),
?assertMatch([_, _, "[critical]", _, "Test message\n"], re:split(Bin1, " ", [{return, list}, {parts, 5}])),
ok = file:delete("test.log"),
- {Level, _} = lager_mochiglobal:get(loglevel),
- lager_mochiglobal:put(loglevel, {Level, [{[{module,
+ {Level, _} = lager_config:get(loglevel),
+ lager_config:set(loglevel, {Level, [{[{module,
?MODULE}], ?DEBUG,
{lager_file_backend, "test.log"}}]}),
lager:critical("Test message"),
@@ -112,8 +112,6 @@ reinstall_on_initial_failure_test_() ->
application:set_env(lager, handlers, [{lager_test_backend, info}, {lager_crash_backend, [from_now(2), undefined]}]),
application:set_env(lager, error_logger_redirect, false),
application:unset_env(lager, crash_log),
- application:start(compiler),
- application:start(syntax_tools),
application:start(lager),
try
?assertEqual(1, lager_test_backend:count()),
@@ -140,8 +138,6 @@ reinstall_on_runtime_failure_test_() ->
application:set_env(lager, handlers, [{lager_test_backend, info}, {lager_crash_backend, [undefined, from_now(5)]}]),
application:set_env(lager, error_logger_redirect, false),
application:unset_env(lager, crash_log),
- application:start(compiler),
- application:start(syntax_tools),
application:start(lager),
try
?assertEqual(0, lager_test_backend:count()),
Oops, something went wrong.

0 comments on commit a192bd9

Please sign in to comment.