Permalink
Browse files

Support backend modules defining their own gen_event handler ID

  • Loading branch information...
1 parent 8d353aa commit da4e0cfa841f0d93527bfd45155bd03397407845 @Vagabond Vagabond committed Dec 11, 2012
Showing with 22 additions and 7 deletions.
  1. +11 −7 src/lager_app.erl
  2. +11 −0 src/lager_file_backend.erl
View
18 src/lager_app.erl
@@ -79,17 +79,21 @@ stop(Handlers) ->
expand_handlers([]) ->
[];
expand_handlers([{lager_file_backend, Configs}|T]) ->
- [ to_config(Config) || Config <- Configs] ++
+ [ {lager_file_backend:config_to_id(Config), Config} || Config <- Configs] ++
expand_handlers(T);
+expand_handlers([{Mod, Config}|T]) when is_atom(Mod) ->
+ %% allow the backend to generate a gen_event handler id, if it wants to
+ code:load_file(Mod),
+ Res = case erlang:function_exported(Mod, config_to_id, 1) of
+ true ->
+ {Mod:config_to_id(Config), Config};
+ false ->
+ {Mod, Config}
+ end,
+ [Res | expand_handlers(T)];
expand_handlers([H|T]) ->
[H | expand_handlers(T)].
-to_config({Name,Severity}) ->
- {{lager_file_backend, Name}, {Name, Severity}};
-to_config({Name,_Severity,_Size,_Rotation,_Count}=Config) ->
- {{lager_file_backend, Name}, Config};
-to_config([{Name,_Severity,_Size,_Rotation,_Count}, _Format] = Config) ->
- {{lager_file_backend, Name}, Config}.
View
11 src/lager_file_backend.erl
@@ -41,6 +41,8 @@
-export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
code_change/3]).
+-export([config_to_id/1]).
+
-record(state, {
name :: string(),
level :: integer(),
@@ -121,6 +123,15 @@ terminate(_Reason, #state{fd=FD}) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
+%% convert the config into a gen_event handler ID
+config_to_id({Name,_Severity}) ->
+ {?MODULE, Name};
+config_to_id({Name,_Severity,_Size,_Rotation,_Count}) ->
+ {?MODULE, Name};
+config_to_id([{Name,_Severity,_Size,_Rotation,_Count}, _Format]) ->
+ {?MODULE, Name}.
+
+
write(#state{name=Name, fd=FD, inode=Inode, flap=Flap, size=RotSize,
count=Count} = State, Level, Msg) ->
case lager_util:ensure_logfile(Name, FD, Inode, true) of

0 comments on commit da4e0cf

Please sign in to comment.