summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul J. Davis <paul.joseph.davis@gmail.com>2014-08-23 12:30:38 -0500
committerJay Doane <jaydoane@apache.org>2021-04-19 00:35:19 -0700
commit0669ccaec9bb86cdc39e10bb75a109fcc5a540fd (patch)
tree2a2d6db504f781f669421c2a5e2f26620807e251
parentd85d3016ec3bab58e422ed135573e298674c02ca (diff)
downloadcouchdb-0669ccaec9bb86cdc39e10bb75a109fcc5a540fd.tar.gz
Remove twig dependency
-rw-r--r--src/weatherreport/src/weatherreport.app.src3
-rw-r--r--src/weatherreport/src/weatherreport.erl4
-rw-r--r--src/weatherreport/src/weatherreport_check.erl4
-rw-r--r--src/weatherreport/src/weatherreport_config.erl19
-rw-r--r--src/weatherreport/src/weatherreport_log.erl71
-rw-r--r--src/weatherreport/src/weatherreport_node.erl12
-rw-r--r--src/weatherreport/src/weatherreport_util.erl37
7 files changed, 88 insertions, 62 deletions
diff --git a/src/weatherreport/src/weatherreport.app.src b/src/weatherreport/src/weatherreport.app.src
index 43bd32a4f..4778ac476 100644
--- a/src/weatherreport/src/weatherreport.app.src
+++ b/src/weatherreport/src/weatherreport.app.src
@@ -34,8 +34,7 @@
{applications, [
kernel,
stdlib,
- inets,
- twig
+ inets
]},
{mod, {weatherreport_app, []}}
]}.
diff --git a/src/weatherreport/src/weatherreport.erl b/src/weatherreport/src/weatherreport.erl
index 86c627376..6982a5da2 100644
--- a/src/weatherreport/src/weatherreport.erl
+++ b/src/weatherreport/src/weatherreport.erl
@@ -124,10 +124,10 @@ run(InputChecks) ->
_ ->
%% Print the most critical messages first
FilteredMessages = lists:filter(fun({_,Level,_,_}) ->
- weatherreport_util:should_log(Level)
+ weatherreport_log:should_log(Level)
end, Messages),
SortedMessages = lists:sort(fun({_, ALevel, _, _}, {_, BLevel, _, _}) ->
- twig_util:level(ALevel) =< twig_util:level(BLevel)
+ weatherreport_log:level(ALevel) =< weatherreport_log:level(BLevel)
end, FilteredMessages),
case SortedMessages of
[] ->
diff --git a/src/weatherreport/src/weatherreport_check.erl b/src/weatherreport/src/weatherreport_check.erl
index f50616fb6..e01cb8cc1 100644
--- a/src/weatherreport/src/weatherreport_check.erl
+++ b/src/weatherreport/src/weatherreport_check.erl
@@ -101,7 +101,7 @@ modules() ->
print({Node, Level, Mod, Data}) ->
case Mod:format(Data) of
{Format, Terms} ->
- weatherreport_util:log(Node, Level, Format, Terms);
+ weatherreport_log:log(Node, Level, Format, Terms);
String ->
- weatherreport_util:log(Node, Level, String)
+ weatherreport_log:log(Node, Level, String)
end.
diff --git a/src/weatherreport/src/weatherreport_config.erl b/src/weatherreport/src/weatherreport_config.erl
index cee6c9a75..267562d21 100644
--- a/src/weatherreport/src/weatherreport_config.erl
+++ b/src/weatherreport/src/weatherreport_config.erl
@@ -29,8 +29,7 @@
%% the {@link weatherreport. weatherreport} module calls {@link
%% prepare/0. prepare/0}, CouchDB's <code>default.ini</code>,
%% <code>local.ini</code> and <code>vm.args</code> files will be
-%% parsed and memoized, and twig will be started on the console at the
-%% configured severity level.
+%% parsed and memoized.
%% @end
-module(weatherreport_config).
@@ -49,7 +48,7 @@
%% not need to invoke it.
-spec prepare() -> ok | {error, iodata()}.
prepare() ->
- prepare([fun start_twig/0, fun load_app_config/0, fun load_vm_args/0]).
+ prepare([fun load_app_config/0, fun load_vm_args/0]).
prepare([]) ->
ok;
@@ -130,25 +129,15 @@ cookie() ->
list_to_atom(Cookie)
end.
-%% Private functions
-start_twig() ->
- application:load(twig),
- case application:get_env(weatherreport, log_level) of
- undefined ->
- {error, "Log level not set!"};
- {ok, _Level} ->
- ok
- end.
-
load_app_config() ->
Etc = ?MODULE:etc_dir(),
IniFiles = [
filename:join(Etc, "default.ini"),
filename:join(Etc, "local.ini")
],
- weatherreport_util:log(node(), debug, "Reading config from files: ~p", [IniFiles]),
+ weatherreport_log:log(node(), debug, "Reading config from files: ~p", [IniFiles]),
config:start_link(IniFiles),
- weatherreport_util:log(node(), debug, "Local node config: ~p~n", [config:all()]).
+ weatherreport_log:log(node(), debug, "Local node config: ~p~n", [config:all()]).
load_vm_args() ->
VmArgs = case init:get_argument(vm_args) of
diff --git a/src/weatherreport/src/weatherreport_log.erl b/src/weatherreport/src/weatherreport_log.erl
new file mode 100644
index 000000000..012837cc7
--- /dev/null
+++ b/src/weatherreport/src/weatherreport_log.erl
@@ -0,0 +1,71 @@
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+
+-module(weatherreport_log).
+-export([
+ level/1,
+ log/3,
+ log/4,
+ should_log/1,
+ iso8601_timestamp/0,
+ format/2
+]).
+
+
+level(debug) -> 7;
+level(info) -> 6;
+level(notice) -> 5;
+level(warn) -> 4;
+level(warning) -> 4;
+level(err) -> 3;
+level(error) -> 3;
+level(crit) -> 2;
+level(alert) -> 1;
+level(emerg) -> 0;
+level(panic) -> 0;
+
+level(I) when is_integer(I), I >= 0, I =< 7 ->
+ I;
+level(_BadLevel) ->
+ 3.
+
+
+log(Node, Level, Format, Terms) ->
+ case should_log(Level) of
+ true ->
+ Prefix = get_prefix(Node, Level),
+ Message = io_lib:format(Format, Terms),
+ io:format("~s ~s~n", [Prefix, Message]);
+ false ->
+ ok
+ end.
+
+log(Node, Level, String) ->
+ case should_log(Level) of
+ true ->
+ Prefix = get_prefix(Node, Level),
+ io:format("~s ~s~n", [Prefix, String]);
+ false ->
+ ok
+ end.
+
+should_log(Level) ->
+ AppLevel = case application:get_env(weatherreport, log_level) of
+ undefined -> info;
+ {ok, L0} -> L0
+ end,
+ level(AppLevel) >= level(Level).
+
+get_prefix(Node, Level) ->
+ io_lib:format("[~w] [~w]", [Node, Level]).
diff --git a/src/weatherreport/src/weatherreport_node.erl b/src/weatherreport/src/weatherreport_node.erl
index e353bf3bf..85f0e3be8 100644
--- a/src/weatherreport/src/weatherreport_node.erl
+++ b/src/weatherreport/src/weatherreport_node.erl
@@ -67,7 +67,7 @@ local_command(Module, Function, Args) ->
local_command(Module, Function, Args, Timeout) ->
case is_cluster_node() of
true ->
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Local function call: ~p:~p(~p)",
@@ -75,7 +75,7 @@ local_command(Module, Function, Args, Timeout) ->
),
erlang:apply(Module, Function, Args);
_ ->
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Local RPC: ~p:~p(~p) [~p]",
@@ -110,7 +110,7 @@ can_connect() ->
case is_connected() or is_cluster_node() of
true -> true;
false ->
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Not connected to the local cluster node, trying to connect. alive:~p connect_failed:~p",
@@ -167,7 +167,7 @@ try_connect() ->
case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of
{true, pong} ->
application:set_env(weatherreport, connect_failed, false),
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Connected to local cluster node ~p.",
@@ -176,7 +176,7 @@ try_connect() ->
true;
_ ->
application:set_env(weatherreport, connect_failed, true),
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
warning,
"Could not connect to the local cluster node ~p, some checks will not run.",
@@ -193,7 +193,7 @@ connect_failed() ->
end.
start_net() ->
- weatherreport_util:log(node(), debug, "Starting distributed Erlang."),
+ weatherreport_log:log(node(), debug, "Starting distributed Erlang."),
{Type, NodeName} = weatherreport_config:node_name(),
ThisNode = append_node_suffix(NodeName, "_diag"),
{ok, _} = net_kernel:start([ThisNode, Type]),
diff --git a/src/weatherreport/src/weatherreport_util.erl b/src/weatherreport/src/weatherreport_util.erl
index ab5370812..7e8eade03 100644
--- a/src/weatherreport/src/weatherreport_util.erl
+++ b/src/weatherreport/src/weatherreport_util.erl
@@ -30,9 +30,7 @@
-module(weatherreport_util).
-export([short_name/1,
run_command/1,
- log/3,log/4,
binary_to_float/1,
- should_log/1,
flush_stdout/0,
check_proc_count/3]).
@@ -48,7 +46,7 @@ short_name(Mod) when is_atom(Mod) ->
%% redirected to stdout so its output will be included.
-spec run_command(Command::iodata()) -> StdOut::iodata().
run_command(Command) ->
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Running shell command: ~s",
@@ -60,7 +58,7 @@ run_command(Command) ->
do_read(Port, Acc) ->
receive
{Port, {data, StdOut}} ->
- weatherreport_util:log(
+ weatherreport_log:log(
node(),
debug,
"Shell command output: ~n~s~n",
@@ -81,37 +79,6 @@ do_read(Port, Acc) ->
binary_to_float(Bin) ->
list_to_float(binary_to_list(Bin)).
-get_prefix(Node, Level) ->
- io_lib:format("[~w] [~w]", [Node, Level]).
-
-log(Node, Level, Format, Terms) ->
- case should_log(Level) of
- true ->
- Prefix = get_prefix(Node, Level),
- Message = io_lib:format(Format, Terms),
- io:format("~s ~s~n", [Prefix, Message]);
- false ->
- ok
- end,
- twig:log(Level, Format, Terms).
-
-log(Node, Level, String) ->
- case should_log(Level) of
- true ->
- Prefix = get_prefix(Node, Level),
- io:format("~s ~s~n", [Prefix, String]);
- false ->
- ok
- end,
- twig:log(Level, String).
-
-should_log(Level) ->
- AppLevel = case application:get_env(weatherreport, log_level) of
- undefined -> info;
- {ok, L0} -> L0
- end,
- twig_util:level(AppLevel) >= twig_util:level(Level).
-
flush_stdout() ->
timer:sleep(1000).