Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set wmtrace from config file + add configuration to disable console output #3

Merged
merged 1 commit into from Aug 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/webmachine_logger.erl
Expand Up @@ -72,7 +72,7 @@ code_change(_OldVsn, State, _Extra) ->

log_open(FileName, DateHour) ->
LogName = FileName ++ suffix(DateHour),
io:format("opening log file: ~p~n", [LogName]),
webmachine_util:to_console("opening log file: ~p~n", [LogName]),
{ok, FD} = file:open(LogName, [read, write, raw]),
{ok, Location} = file:position(FD, eof),
fix_log(FD, Location),
Expand All @@ -81,10 +81,9 @@ log_open(FileName, DateHour) ->

log_write({?MODULE, _Name, FD}, IoData) ->
file:write(FD, lists:flatten(IoData)).


log_close({?MODULE, Name, FD}) ->
io:format("~p: closing log file: ~p~n", [?MODULE, Name]),
webmachine_util:to_console("~p: closing log file: ~p~n", [?MODULE, Name]),
file:close(FD).

maybe_rotate(State, Time) ->
Expand Down
5 changes: 2 additions & 3 deletions src/webmachine_perf_logger.erl
Expand Up @@ -68,7 +68,7 @@ code_change(_OldVsn, State, _Extra) ->

log_open(FileName, DateHour) ->
LogName = FileName ++ suffix(DateHour),
io:format("opening log file: ~p~n", [LogName]),
webmachine_util:to_console("opening log file: ~p~n", [LogName]),
{ok, FD} = file:open(LogName, [read, write, raw]),
{ok, Location} = file:position(FD, eof),
fix_log(FD, Location),
Expand All @@ -77,10 +77,9 @@ log_open(FileName, DateHour) ->

log_write({?MODULE, _Name, FD}, IoData) ->
file:write(FD, lists:flatten(IoData)).


log_close({?MODULE, Name, FD}) ->
io:format("~p: closing log file: ~p~n", [?MODULE, Name]),
webmachine_util:to_console("~p: closing log file: ~p~n", [?MODULE, Name]),
file:close(FD).

maybe_rotate(State, Time) ->
Expand Down
9 changes: 8 additions & 1 deletion src/webmachine_sup.erl
Expand Up @@ -79,7 +79,14 @@ init([]) ->
{ok, {{one_for_one, 9, 10}, Processes}}.

init_wmtrace() ->
Dir = "priv/wmtrace", %%TODO: move it to config file...
Dir = valid_wmtrace_dir(application:get_env(wmtrace_dir)),
ok = filelib:ensure_dir(filename:join(Dir, "test")),
ets:new(?WMTRACE_CONF_TBL, [set, public, named_table]),
ets:insert(?WMTRACE_CONF_TBL, {trace_dir, Dir}).

valid_wmtrace_dir(undefined) -> filename:join([get_path(), "priv", "wmtrace"]);
valid_wmtrace_dir({ok, Dir}) when is_list(Dir) -> Dir.

get_path() ->
{ok, CWD} = file:get_cwd(),
CWD.
16 changes: 16 additions & 0 deletions src/webmachine_util.erl
Expand Up @@ -27,6 +27,7 @@
-export([media_type_to_detail/1,
quoted_string/1,
split_quoted_strings/1]).
-export([to_console/2]).

-ifdef(TEST).
-ifdef(EQC).
Expand Down Expand Up @@ -323,6 +324,21 @@ now_diff_milliseconds({M,S,U}, {M,S1,U1}) ->
now_diff_milliseconds({M,S,U}, {M1,S1,U1}) ->
((M-M1)*1000000+(S-S1))*1000 + ((U-U1) div 1000).

%% @doc Use application config file to determine if any output to console is allowed.
%% @spec is_silent_console() -> boolean()
is_silent_console() -> valid_silent_console(application:get_env(silent_console)).

valid_silent_console(undefined) -> true; % No console output allowed by default
valid_silent_console({ok, Silent}) when is_boolean(Silent) -> Silent.

%% @doc Write a message to console if allowed.
%% @spec to_console(Format, Data) -> ok
to_console(Format, Data) when is_list(Format), is_list(Data) ->
to_console(Format, Data, is_silent_console()).

to_console(_Format, _Data, true) -> ok;
to_console(Format, Data, false) -> io:format(Format, Data).

%%
%% TEST
%%
Expand Down