Browse files

Added lager for logging. added enable and disable functions.

  • Loading branch information...
1 parent 943f505 commit a261ae965a671873ce9f1bcd01ed5ac8c7a31db1 @Licenser committed Mar 15, 2013
Showing with 116 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. BIN plugins/example/example.beam
  3. +2 −1 rebar.config
  4. +2 −1 src/eplugin.app.src
  5. +111 −16 src/eplugin.erl
View
1 .gitignore
@@ -1,3 +1,4 @@
*.beam
.eunit
ebin
+deps
View
BIN plugins/example/example.beam
Binary file not shown.
View
3 rebar.config
@@ -1,5 +1,6 @@
{lib_dirs, ["deps"]}.
-{erl_opts, [{i, ".."}, {i,"deps"}, debug_info]}.
+{erl_opts, [{i, ".."}, {i,"deps"}, {parse_transform, lager_transform}, debug_info, warnings_as_errors]}.
{deps,
[
+ {lager, ".*", {git, "git://github.com/basho/lager.git", {tag, "1.0.0"}}}
]}.
View
3 src/eplugin.app.src
@@ -6,7 +6,8 @@
{applications, [
kernel,
stdlib,
- sasl
+ sasl,
+ lager
]},
{mod, { eplugin_app, []}},
{env, [{plugin_dir, "plugins"}]}
View
127 src/eplugin.erl
@@ -8,45 +8,123 @@
callbacks/1,
apply/2,
apply/1,
- config/1]).
+ config/1,
+ plugins/0,
+ enable/1,
+ disable/1]).
-define(TABLE, plugins).
-define(CONFTABLE, plugin_config).
start() ->
application:start(sasl),
+ application:start(lager),
application:start(eplugin).
discover() ->
discover("plugins").
discover(Base) ->
+ lager:info("[eplugin] discovering plugins in ~s", [Base]),
WC = filename:join([Base, "*", "plugin.conf"]),
- Plugins = filelib:wildcard(WC),
- Loaded = [{filename:dirname(Plugin), file:consult(Plugin)} || Plugin <- Plugins],
- [{Path, Config} || {Path, {ok, [Config]}} <- Loaded].
+ filelib:wildcard(WC).
load(Path) ->
[load_plugin(P) || P <- discover(Path)].
load() ->
[load_plugin(P) || P <- discover()].
-load_plugin({Path, {Name, Modules, Config}}) ->
+load_plugin(ConfigFile) ->
+ %% Loaded = [{filename:dirname(Plugin), file:consult(Plugin)} || Plugin <- Plugins],
+ %% [{Path, Config} || {Path, {ok, [Config]}} <- Loaded].
- io:format("Loading ~p with modules ~p and config ~p.~n", [Name, Modules, Config]),
- ModuleFiles = [filename:join([Path, M]) || {M, _RegisterFor} <- Modules],
- [compile:file(F, [{outdir, Path}]) || F <- ModuleFiles],
- [code:load_abs(F) || F <- ModuleFiles],
- ets:insert(?CONFTABLE, {Name, Config}),
- [register_callbacks(M) || M <- Modules].
+ Path = filename:dirname(ConfigFile),
+ lager:info("[plugin] Loading plugin from ~s", [Path]),
+ case file:consult(ConfigFile) of
+ {ok, [{Name, Modules, Config}]} ->
+ lager:info("[eplugin::~s] Config loaded.", [Name]),
+ case compile_modules(Name, Path, Modules) of
+ error ->
+ {error, Name};
+ ok ->
+ case load_modules(Name, Path, Modules) of
+ error ->
+ {error, Name};
+ ok ->
+ case proplists:get_value(disabled, Config) of
+ true ->
+ lager:warning("[eplugin::~p] Disabled.", [Name]);
+ _ ->
+ [register_callbacks(Name, M) || M <- Modules]
+ end,
+ ets:insert(?CONFTABLE, {Name, Config, Modules}),
+ {ok, Name}
+ end
+ end;
+ {ok, Other} ->
+ lager:info("[eplugin] failed to load plugin config ~s with error: ~p", [ConfigFile, {invalid, Other}]), {error, {invalid, Other}};
+ {error, Reason} ->
+ lager:info("[eplugin] failed to load plugin config ~s with error: ~p", [ConfigFile, Reason]),
+ {error, Reason}
+ end.
+
+load_modules(Name, _Path, []) ->
+ lager:info("[eplugin::~p] All module loaded loaded successfuly.", [Name]),
+ ok;
+
+load_modules(Name, Path, [{M, _RegisterFor} | Modules]) ->
+ File = filename:join([Path, M]),
+ case code:load_abs(File) of
+ {error, Reason} ->
+ lager:error("[eplugin::~p] Failed to load module ~p(~s): ~p.", [Name, M, File, Reason]),
+ lager:error("[eplugin::~p] Aborting initialisation.", [Name]),
+ error;
+ _ ->
+ lager:info("[eplugin::~p] Module loaded ~p(~s).", [Name, M, File]),
+ load_modules(Name, Path, Modules)
+ end.
+
+compile_modules(Name, _Paht, []) ->
+ lager:info("[eplugin::~p] All modules compiled successfully.", [Name]),
+ ok;
+
+compile_modules(Name, Path, [{M, _RegisterFor} | Modules]) ->
+ case compile_module(Name, Path, M) of
+ {ok, _} ->
+ compile_modules(Name, Path, Modules);
+ {error, _} ->
+ lager:error("[eplugin::~p] Aborting initialisation.", [Name]),
+ error
+ end.
+
+compile_module(Name, Path, Module) ->
+ File = filename:join([Path, Module]),
+ lager:info("[eplugin::~p] Compiling moduke ~s", [Name, File]),
+ case compile:file(File, [{outdir, Path}]) of
+ error ->
+ lager:error("[eplugin::~p] Compiling failed.", [Name]),
+ {error, unknown};
+ {error, Errors, Warnings} ->
+ lager:error("[eplugin::~p] Compiling failed. Errors: ~p Warnings: ~p.", [Name, Errors, Warnings]),
+ {error, {Errors, Warnings}};
+ {ok, ModuleName} ->
+ lager:info("[eplugin::~p] Compiling successfull of ~p.", [Name, ModuleName]),
+ {ok, ModuleName};
+ {ok, ModuleName,Warnings} ->
+ lager:info("[eplugin::~p] Compiling successfull of ~p with warnings: ~p.", [Name, ModuleName, Warnings]),
+ {ok, ModuleName}
+ end.
-register_callbacks({M, Callbacks}) ->
- Objs = [{Callback, M, Fun} || {Callback, Fun} <- Callbacks ],
- ets:insert(?TABLE, Objs).
+register_callbacks(Name, {Module, Callbacks}) ->
+ [register_callback(Name, Callback, Module, Function) || {Callback, Function} <- Callbacks ].
+
+register_callback(Name, Callback, Module, Function) ->
+ lager:info("[eplugin::~p] Registering callback ~p with ~p:~p.", [Name, Callback, Module, Function]),
+ ets:insert(?TABLE, {Callback, Name, Module, Function}).
callbacks(Name) ->
- [{M, F} || {_, M, F} <- ets:lookup(?TABLE, Name)].
+ [{M, F} || {_, _, M, F} <- ets:lookup(?TABLE, Name)].
apply(Name) ->
eplugin:apply(Name, []).
@@ -58,6 +136,23 @@ config(Plugin) ->
case ets:lookup(?CONFTABLE, Plugin) of
[] ->
undefined;
- [{_, Conf}] ->
+ [{_, Conf, _}] ->
{ok, Conf}
end.
+
+disable(Plugin) ->
+ lager:info("[eplugin::~p] disabled.", [Plugin]),
+ ets:match_delete(?TABLE, {'_', Plugin, '_', '_'}).
+
+enable(Plugin) ->
+ case ets:lookup(?CONFTABLE, Plugin) of
+ [] ->
+ undefined;
+ [{_, _, Modules}] ->
+ [register_callbacks(Plugin, M) || M <- Modules],
+ ok
+ end.
+
+
+plugins() ->
+ [P || [P] <- ets:match(?CONFTABLE, {'$1', '_', '_'})].

0 comments on commit a261ae9

Please sign in to comment.