From da4e0cfa841f0d93527bfd45155bd03397407845 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Tue, 11 Dec 2012 17:13:12 -0500 Subject: [PATCH] Support backend modules defining their own gen_event handler ID --- src/lager_app.erl | 18 +++++++++++------- src/lager_file_backend.erl | 11 +++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/lager_app.erl b/src/lager_app.erl index b757c0c..63fcb28 100644 --- a/src/lager_app.erl +++ b/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}. diff --git a/src/lager_file_backend.erl b/src/lager_file_backend.erl index 6a5048f..1c01426 100644 --- a/src/lager_file_backend.erl +++ b/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