Skip to content

Commit

Permalink
Elaborate on audit_log:rediscover_logs/N. Remove unused `audit_log_…
Browse files Browse the repository at this point in the history
…disk:async_send_msg/2`.
  • Loading branch information
freza committed Jan 18, 2012
1 parent 449187e commit 0b7ffd8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 37 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Lightweight audit logging library.
## History ## History


* 2005-2010 Chandrashekhar Mullaparthi * 2005-2010 Chandrashekhar Mullaparthi
* 2010-2011 Jachym Holecek * 2010-2012 Jachym Holecek


## Features ## Features


Expand Down Expand Up @@ -150,11 +150,15 @@ audit_log:clean_old(Log) -> ok | {error, _}.
automatically according to log options. automatically according to log options.


```erlang ```erlang
audit_log:rediscover_logs() -> [{Log, Ret}]. audit_log:rediscover_logs() -> ok.
audit_log:rediscover_logs(App) -> ok.
``` ```


* Ensure all currently known logs are up and running. `Ret` is the return value * Ensure all logs are up and running, either for given application or for all loaded
of `audit_log:open_log/N`. applications. List of logs and default options are read from `audit_log` key in
application resource file (`*.app`) ensuring all logs are loaded in configuration
table. Subsequently it is ensured worker processes are running for all entries in
that table.


### Maintenance API ### Maintenance API


Expand Down Expand Up @@ -186,7 +190,9 @@ audit_log:create_db(Mnesia_opts) -> Mnesia_ret.


* This can be used to manually create configuration table in cases where default * This can be used to manually create configuration table in cases where default
table options aren't desirable. Should be called before `audit_log` is first table options aren't desirable. Should be called before `audit_log` is first
started. started. By default configuration table is set up at application startup
ensuring disc copies replica exists on current node and with `local_content`
flag set.


### Utility API ### Utility API


Expand Down
79 changes: 53 additions & 26 deletions src/audit_log.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@


-export([open_log/1, open_log/2, audit_msg/2, audit_msg/3, syslog_msg/1, syslog_msg/2, close_log/1]). -export([open_log/1, open_log/2, audit_msg/2, audit_msg/3, syslog_msg/1, syslog_msg/2, close_log/1]).
-export([get_config/0, get_config/1, set_config/1, set_config/2, get_status/0, get_status/1]). -export([get_config/0, get_config/1, set_config/1, set_config/2, get_status/0, get_status/1]).
-export([change_file/1, clean_old/1, rediscover_logs/0]). -export([change_file/1, clean_old/1, rediscover_logs/0, rediscover_logs/1]).
-export([create_db/0, create_db/1]). -export([create_db/0, create_db/1]).


-import(audit_log_lib, [get_env/3]). -import(audit_log_lib, [get_env/3]).
-import(lists, [foreach/2, keysort/2]). -import(lists, [keysort/2]).


-include("audit_log_db.hrl"). -include("audit_log_db.hrl").


Expand All @@ -57,18 +57,8 @@ open_log(Log) ->
open_log(Log, []). open_log(Log, []).


open_log(Log, Opts) -> open_log(Log, Opts) ->
mnesia:activity(transaction, fun () -> do_set_config(Log, Opts) end), mnesia:activity(transaction, fun () -> edit_log(Log, Opts, false) end),
try audit_log_sup:add_worker(Log) of start_log(Log).
{ok, _} ->
{ok, started};
{error, {already_started, _}} ->
{ok, running};
{error, _} = Err ->
Err
catch
exit : {noproc, _} ->
{ok, added}
end.


close_log(Log) -> close_log(Log) ->
try audit_log_sup:del_worker(Log) catch try audit_log_sup:del_worker(Log) catch
Expand All @@ -84,30 +74,37 @@ clean_old(Log) ->
audit_log_disk:clean_old(Log). audit_log_disk:clean_old(Log).


rediscover_logs() -> rediscover_logs() ->
%% Applications can use this to ensure all logs are usable. mnesia:activity(transaction, fun () -> scan_all_specs() end),
[{Log, open_log(Log)} || Log <- mnesia:dirty_all_keys(audit_log_conf)]. [start_log(Log) || Log <- mnesia:dirty_all_keys(audit_log_conf)],
ok.

rediscover_logs(App) ->
mnesia:activity(transaction, fun () -> scan_app_specs(App) end),
[start_log(Log) || Log <- mnesia:dirty_all_keys(audit_log_conf)],
ok.


%%% Maintenance API. %%% Maintenance API.


get_config() -> get_config() ->
Read = fun () -> Read = fun () ->
[{Log, do_get_config(Log)} || Log <- mnesia:all_keys(audit_log_conf)] [{Log, edit_log(Log, [], false)} || Log <- mnesia:all_keys(audit_log_conf)]
end, end,
keysort(1, mnesia:activity(transaction, Read)). keysort(1, mnesia:activity(transaction, Read)).


get_config(Log) -> get_config(Log) ->
mnesia:activity(transaction, fun () -> do_get_config(Log) end). mnesia:activity(transaction, fun () -> edit_log(Log, [], false) end).


set_config(Opts) -> set_config(Opts) ->
Edit = fun () -> Edit = fun () ->
foreach(fun (Log) -> do_set_config(Log, Opts) end, All = mnesia:all_keys(audit_log_conf)), All_logs = mnesia:all_keys(audit_log_conf),
All [edit_log(Log, Opts, true) || Log <- All_logs],
All_logs
end, end,
[(catch audit_log_disk:set_options(Log)) || Log <- mnesia:activity(transaction, Edit)], [(catch audit_log_disk:set_options(Log)) || Log <- mnesia:activity(transaction, Edit)],
ok. ok.


set_config(Log, Opts) -> set_config(Log, Opts) ->
mnesia:activity(transaction, fun () -> do_set_config(Log, Opts) end), mnesia:activity(transaction, fun () -> edit_log(Log, Opts, true) end),
audit_log_disk:set_options(Log). audit_log_disk:set_options(Log).


get_status() -> get_status() ->
Expand All @@ -132,13 +129,43 @@ create_db(Opts) ->


%%% Utilities. %%% Utilities.


do_get_config(Log) -> %% Attempt to start log process for some log.
[#audit_log_conf{opts = Opts}] = mnesia:read(audit_log_conf, Log), start_log(Log) ->
Opts. try audit_log_sup:add_worker(Log) of
{ok, _} ->
{ok, started};
{error, {already_started, _}} ->
{ok, running};
{error, _} = Err ->
Err
catch
exit : {noproc, _} ->
{ok, added}
end.

%% Ensure all logs mentioned in *.app files of loaded applications are provisioned in Mnesia.
scan_all_specs() ->
[scan_app_specs(App) || {App, _, _} <- application:loaded_applications()],
ok.

scan_app_specs(App) ->
[edit_log(Log, Opts, false) || {Log, Opts} <- get_key(App, audit_log, [])],
ok.

get_key(App, Key, Def) ->
case application:get_key(App, Key) of
{ok, Val} ->
Val;
undefined ->
Def
end.


do_set_config(Log, Opts) -> %% NB `edit_log(Log, [], false)` can be used to just fetch log options.
edit_log(Log, Opts, Do_force) ->
case mnesia:read(audit_log_conf, Log) of case mnesia:read(audit_log_conf, Log) of
[#audit_log_conf{opts = Prev} = CF] -> [#audit_log_conf{opts = Os}] when Do_force == false ->
Os;
[#audit_log_conf{opts = Prev} = CF] when Do_force == true ->
mnesia:write(CF#audit_log_conf{log = Log, opts = Os = merge_opts(Opts, Prev)}), mnesia:write(CF#audit_log_conf{log = Log, opts = Os = merge_opts(Opts, Prev)}),
Os; Os;
[] -> [] ->
Expand Down
6 changes: 0 additions & 6 deletions src/audit_log_disk.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


-export([start_link/1, send_msg/2, set_options/1, get_status/1, change_file/1, clean_old/1]). -export([start_link/1, send_msg/2, set_options/1, get_status/1, change_file/1, clean_old/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([async_send_msg/2]).


-import(audit_log_lib, [get_value/3, make_two/1, secs_to_midnight/0]). -import(audit_log_lib, [get_value/3, make_two/1, secs_to_midnight/0]).
-import(filename, [basename/1, dirname/1, rootname/2, absname/1, join/1, split/1]). -import(filename, [basename/1, dirname/1, rootname/2, absname/1, join/1, split/1]).
Expand Down Expand Up @@ -59,9 +58,6 @@ change_file(Pid) ->
clean_old(Pid) -> clean_old(Pid) ->
gen_server:call(Pid, clean_old). gen_server:call(Pid, clean_old).


async_send_msg(Pid, Msg) ->
gen_server:cast(Pid, {send_msg, Msg}).

%%% Generic server. %%% Generic server.


-record(state, { -record(state, {
Expand Down Expand Up @@ -123,8 +119,6 @@ handle_info({'EXIT', _, Reason}, State) when Reason /= normal ->
handle_info(_, State) -> handle_info(_, State) ->
{noreply, State}. {noreply, State}.


handle_cast({send_msg, Msg}, State) ->
{noreply, write(State, Msg)};
handle_cast(_, State) -> handle_cast(_, State) ->
{noreply, State}. {noreply, State}.


Expand Down

0 comments on commit 0b7ffd8

Please sign in to comment.