Removed operational log process in favour of error_logger config.
authorStanislaw Klekot <dozzie@jarowit.net>
Sun, 21 Sep 2014 19:13:54 +0000 (21:13 +0200)
committerStanislaw Klekot <dozzie@jarowit.net>
Sun, 21 Sep 2014 19:13:54 +0000 (21:13 +0200)
priv/haircut
src/haircut.app.src
src/haircut_log_h.erl [deleted file]
src/haircut_log_operational.erl [deleted file]
src/haircut_log_sup.erl
src/haircut_syslog_term_h.erl [deleted file]

index 52cd740..5a38be5 100755 (executable)
@@ -66,8 +66,7 @@ start(Opts) ->
   setup_erlang_networking(Opts),
   indira:start_rec(haircut), % before indira to prevent race condition
   indira:start_rec(indira),
-  gen_event:notify(haircut_log_operational,
-                   {info, [{event,<<"haircut started">>}]}),
+  error_logger:info_report([{event,<<"haircut started">>}]),
   case Opts of
     #opts{shell = true} -> shell:start();
     _ -> ok
@@ -82,7 +81,16 @@ setup_indira(Opts) ->
   % TODO: more control sockets?
   indira:add_listener(indira_unix, Opts#opts.control_socket),
   indira:set_commander(haircut_commander),
-  indira:setup_logging(haircut, [{gen_event, haircut_log_h, []}]),
+  case Opts of
+    #opts{op_log = none} ->
+      ok = indira:setup_logging(haircut, []);
+    #opts{op_log = syslog} ->
+      ok = indira:setup_logging(haircut, [syslog]);
+    #opts{op_log = undefined} ->
+      ok = indira:setup_logging(haircut, [syslog]);
+    #opts{op_log = File} when is_list(File) ->
+      ok = indira:setup_logging(haircut, [{file, File}])
+  end,
   ok.
 
 %% }}}
@@ -102,10 +110,6 @@ setup_haircut(Opts) ->
     #opts{nick = Nick} -> indira:set_option(haircut, nick, Nick)
   end,
   case Opts of
-    #opts{op_log = undefined} -> ok;
-    #opts{op_log = OpL} -> indira:set_option(haircut, op_log, OpL)
-  end,
-  case Opts of
     #opts{activity_log = undefined} -> ok;
     #opts{activity_log = AL} -> indira:set_option(haircut, activity_log, AL)
   end,
index a9a61e1..d19e432 100644 (file)
@@ -6,7 +6,6 @@
     haircut_commander,
     haircut_log_sup,
     haircut_log_activity,
-    haircut_log_operational,
     haircut_bot_sup,
     haircut_bot
   ]},
@@ -18,7 +17,6 @@
     {channels, []},
     {nick, user}, % user | string()
     {user, {env, "haircut IRC bot"}}, % {User :: env | string(), FullName :: string()}
-    {op_log, syslog},    % none | syslog | string()
     {activity_log, none} % none | string()
   ]}
 ]}.
diff --git a/src/haircut_log_h.erl b/src/haircut_log_h.erl
deleted file mode 100644 (file)
index 00e2fd2..0000000
+++ /dev/null
@@ -1,270 +0,0 @@
-%%%---------------------------------------------------------------------------
-%%% @doc
-%%%   Log handler for {@link error_logger}.
-%%%   This is temporary module intended to work out how logs should be
-%%%   formatted and stored and what log types are to be handled by Indira.
-%%%
-%%% @TODO Send events with stack trace somewhere to log detailed details.
-%%% @TODO Split this module to collect operational logs separately from usage
-%%%   (operational logs will go to Indira in the future).
-%%% @end
-%%%---------------------------------------------------------------------------
-
--module(haircut_log_h).
-
--behaviour(gen_event).
-
-%%% gen_event callbacks
--export([init/1, terminate/2]).
--export([handle_event/2, handle_call/2, handle_info/2]).
--export([code_change/3]).
-
-%%%---------------------------------------------------------------------------
-%%% types
-
--record(state, {}).
-
-%%%---------------------------------------------------------------------------
-%%% gen_event callbacks
-
-%%----------------------------------------------------------
-%% initialization and cleanup {{{
-
-%% @private
-%% @doc Initialize {@link gen_event} state.
-
-init(_Args) ->
-  {ok, #state{}}.
-
-%% @private
-%% @doc Clean up {@link gen_event} state.
-
-terminate(_Reason, _State) ->
-  ok.
-
-%% }}}
-%%----------------------------------------------------------
-%% communication {{{
-
-%% @private
-%% @doc Handle {@link gen_event:notify/2}.
-
-%% log entry emitted when can't connect to epmd starts with:
-%%   Protocol: "inet_tcp": register error: ...
-handle_event({info_msg, _GLead, {_Pid, _Format, _Data}} = _Event, State) ->
-  {ok, State}; % ignore
-
-%% log entry emitted when a gen_server crashes
-handle_event({error_msg, _GLead, {_Pid, _Format, _Data}} = _Event, State) ->
-  {ok, State}; % ignore
-
-%% info-level: application started/stopped, child started, Indira INFO
-handle_event({info_report, _GLead, {_Pid, Type, Report}} = _Event, State) ->
-  case {Type,Report} of
-    {progress, [{application, App}, {started_at, _AppNode}]} ->
-      % application started
-      oplog(info, "application started", [{application, App}]);
-    {progress, [{supervisor, {_SupPid, _SupName}}, {started, _Child}]} ->
-      % child started
-      % TODO: what is `SupName' when supervisor is not a registered process?
-      % TODO: log some operational details, like child's name, PID and MFA
-      ignore;
-    {std_info, [{application, App}, {exited, stopped}, {type, _StartType}]} ->
-      % application stopped
-      oplog(info, "application stopped", [{application, App}]);
-    {std_info, [{application, App}, {exited, Reason}, {type, _StartType}]} ->
-      % application stopped unexpectedly
-      oplog(error, "application crashed",
-            [{application, App}, {reason, normalize_reason(Reason)}]);
-    {std_info, [{indira_info, MsgType} | Context]} ->
-      % Indira INFO messages
-      oplog(info, MsgType, [{context, Context}]);
-    {_,_} ->
-      % TODO: haircut's own logs
-      % TODO: warnings (+W i)
-      io:fwrite("# ignored info type=~p: ~1024p~n", [Type, Report]),
-      ignore
-  end,
-  {ok, State};
-
-%% error-level: crash reports, child start problems, Indira CRITICAL
-handle_event({error_report, _GLead, {Pid, Type, Report}} = _Event, State) ->
-  case {Type,Report} of
-    {crash_report, [_CrashProps, _EmptyList]} ->
-      % gen_server (or supervisor) failed to start, gen_server crashed
-      % _EmptyList: at least it is expected it's `[]'
-      % NOTE: this does not include processes that got `exit(P,Reason)'
-      ignore;
-    {supervisor_report, [{supervisor, {_SupPid, _SupName} = SupId},
-                          {errorContext, start_error},
-                          {reason, Reason}, {offender, ChildProps}]} ->
-      oplog(error, "process start error",
-            [{reason, normalize_reason(Reason)},
-              {supervisor, supervisor_info(Pid, SupId)},
-              {child, child_info(ChildProps)}]);
-    {supervisor_report, [{supervisor, {_SupPid, _SupName} = SupId},
-                          {errorContext, child_terminated},
-                          {reason, Reason}, {offender, ChildProps}]} ->
-      % similar to crash report above, but cleaner MFA specification and is
-      % generated even for processes that got exit signal
-      TrueReason = normalize_reason(Reason),
-      {Level, Message} = case TrueReason of
-        normal   -> {info,  "process stopped"};
-        shutdown -> {info,  "process shut down"};
-        _        -> {error, "process crashed"}
-      end,
-      oplog(Level, Message,
-            [{reason, TrueReason},
-              {supervisor, supervisor_info(Pid, SupId)},
-              {child, child_info(ChildProps)}]);
-    {std_error, [{indira_error, MsgType} | Context]} ->
-      oplog(critical, MsgType, [{context, Context}]);
-    {_,_} ->
-      % TODO: haircut's own logs
-      % TODO: warnings (no +W flag)
-      io:fwrite("# ignored error type=~p: ~1024p~n", [Type, Report]),
-      ignore
-  end,
-  {ok, State};
-
-%% warning-level: Indira ERROR
-handle_event({warning_report, _GLead, {_Pid, Type, Report}} = _Event, State) ->
-  case {Type,Report} of
-    {std_warning, [{indira_error, MsgType} | Context]} ->
-      oplog(error, MsgType, [{context, Context}]);
-    {_,_} ->
-      io:fwrite("# ignored warning type=~p: ~1024p~n", [Type, Report]),
-      ignore
-  end,
-  {ok, State};
-
-%% any other message: ignore
-handle_event(_Event, State) ->
-  {ok, State}.
-
-%% @private
-%% @doc Handle {@link gen_event:call/3}.
-
-handle_call(_Request, State) ->
-  {ok, {error, unknown}, State}.
-
-%% @private
-%% @doc Handle incoming messages.
-
-handle_info(_Msg, State) ->
-  {ok, State}.
-
-%% }}}
-%%----------------------------------------------------------
-%% code change {{{
-
-%% @private
-%% @doc Handle code change.
-
-code_change(_OldVsn, State, _Extra) ->
-  {ok, State}.
-
-%% }}}
-%%----------------------------------------------------------
-
-%%%---------------------------------------------------------------------------
-
-%% @doc Send operational log.
-%%
-%% @spec oplog(atom(), atom() | string() | binary(), list()) ->
-%%   atom()
-
-oplog(Level, Event, Context) when is_list(Event) ->
-  oplog(Level, list_to_binary(Event), Context);
-
-oplog(Level, Event, Context) when is_binary(Event); is_atom(Event) ->
-  try
-    Message = {Level, [{event, Event} | Context]},
-    gen_event:notify(haircut_log_operational, Message)
-  catch
-    error:badarg -> ignore
-  end.
-
-%%%---------------------------------------------------------------------------
-%%% helper functions {{{
-
-supervisor_info(Pid, {local, SupName} = _SupId) ->
-  Info = [
-    {name, SupName},
-    {message_origin, pid_to_binary(Pid)}
-  ],
-  Info;
-
-supervisor_info(Pid, {SupPid, SupName} = _SupId) when is_pid(SupPid) ->
-  Info = [
-    {pid, pid_to_binary(SupPid)},
-    {name, SupName},
-    {message_origin, pid_to_binary(Pid)}
-  ],
-  Info.
-
-%%----------------------------------------------------------
-
-child_info(ChildProps) ->
-  collect_child_info(ChildProps).
-
-%%----------------------------------------------------------
-
-collect_child_info([] = _ChildProps) ->
-  [];
-
-collect_child_info([{pid, undefined} | RestInfo]) ->
-  [{pid, undefined} | collect_child_info(RestInfo)];
-collect_child_info([{pid, Pid} | RestInfo]) when is_pid(Pid) ->
-  [{pid, pid_to_binary(Pid)} | collect_child_info(RestInfo)];
-
-collect_child_info([{name, undefined} | RestInfo]) ->
-  collect_child_info(RestInfo);
-collect_child_info([{name, Name} | RestInfo]) ->
-  [{name, Name} | collect_child_info(RestInfo)];
-
-collect_child_info([{child_type, Type} | RestInfo]) ->
-  [{type, Type} | collect_child_info(RestInfo)];
-
-collect_child_info([_Info | RestInfo]) ->
-  collect_child_info(RestInfo).
-
-%%----------------------------------------------------------
-
-pid_to_binary(Pid) when is_pid(Pid) ->
-  list_to_binary(pid_to_list(Pid)).
-
-%%----------------------------------------------------------
-
-normalize_reason({{TrueReason, _Value}, Stack} = _Reason)
-when is_list(Stack) ->
-  % `{badmatch,V}', `{case_clause,V}', `{try_clause,V}', ...
-  TrueReason;
-
-normalize_reason({undef, [{MissM,MissF,MissArgs} | _] = _Stack} = _Reason) ->
-  % undefined function
-  % TODO: FuncName = <<
-  %   (atom_to_binary(MissM, utf8))/binary, ":",
-  %   (atom_to_binary(MissF, utf8))/binary, "/",
-  %   (list_to_binary(integer_to_list(length(MissArgs))))/binary
-  % >>
-  {undef, {MissM, MissF, length(MissArgs)}};
-
-normalize_reason({TrueReason, Stack} = _Reason) when is_list(Stack) ->
-  % process died (with stack trace)
-  TrueReason;
-
-normalize_reason({'EXIT', TrueReason} = _Reason) ->
-  % `catch(exit(...))'
-  TrueReason;
-
-normalize_reason(Reason) ->
-  Reason.
-
-%%----------------------------------------------------------
-
-%%% }}}
-%%%---------------------------------------------------------------------------
-
-%%%---------------------------------------------------------------------------
-%%% vim:ft=erlang:foldmethod=marker
diff --git a/src/haircut_log_operational.erl b/src/haircut_log_operational.erl
deleted file mode 100644 (file)
index 40dd751..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-%%%---------------------------------------------------------------------------
-%%% @doc
-%%%   Logger for operational events.
-%%% @end
-%%%---------------------------------------------------------------------------
-
--module(haircut_log_operational).
-
-%%% public API
--export([start_link/0]).
-
-%%%---------------------------------------------------------------------------
-%%% public API
-
-start_link() ->
-  case gen_event:start_link({local, ?MODULE}) of
-    {ok, Pid} ->
-      {ok, LogSink} = application:get_env(op_log),
-      add_handlers(Pid, LogSink),
-      {ok, Pid};
-    {error, Reason} ->
-      {error, Reason}
-  end.
-
-%%%---------------------------------------------------------------------------
-
-add_handlers(_Pid, none = _LogSink) ->
-  ok;
-add_handlers(Pid, syslog = _LogSink) ->
-  gen_event:add_handler(Pid, haircut_syslog_term_h, []);
-add_handlers(Pid, LogSink) when is_list(LogSink) ->
-  gen_event:add_handler(Pid, haircut_file_term_h, LogSink).
-
-%%%---------------------------------------------------------------------------
-%%% vim:ft=erlang:foldmethod=marker
index b6daab7..e9a8b30 100644 (file)
@@ -27,9 +27,6 @@ init([] = _Args) ->
   Children = [
     {haircut_log_activity,
       {haircut_log_activity, start_link, []},
-      permanent, 5000, worker, dynamic},
-    {haircut_log_operational,
-      {haircut_log_operational, start_link, []},
       permanent, 5000, worker, dynamic}
   ],
   {ok, {
diff --git a/src/haircut_syslog_term_h.erl b/src/haircut_syslog_term_h.erl
deleted file mode 100644 (file)
index 13caf7c..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-%%%---------------------------------------------------------------------------
-%%% @doc
-%%%   Log handler for {@link error_logger} writing to syslog with term
-%%%   formatting. This handler writes all events (without regard to whether it
-%%%   was `*_msg' or `*_report') to a file formatted as Erlang term.
-%%% @end
-%%%---------------------------------------------------------------------------
-
--module(haircut_syslog_term_h).
-
--behaviour(gen_event).
-
-%%% gen_event callbacks
--export([init/1, terminate/2]).
--export([handle_event/2, handle_call/2, handle_info/2]).
--export([code_change/3]).
-
-%%%---------------------------------------------------------------------------
-%%% types
-
--record(state, {}).
-
-%%%---------------------------------------------------------------------------
-%%% gen_event callbacks
-
-%%----------------------------------------------------------
-%% initialization and cleanup {{{
-
-%% @private
-%% @doc Initialize {@link gen_event} state.
-
-init(_Args) ->
-  {ok, #state{}}.
-
-%% @private
-%% @doc Clean up {@link gen_event} state.
-
-terminate(_Reason, _State) ->
-  ok.
-
-%% }}}
-%%----------------------------------------------------------
-%% communication {{{
-
-%% @private
-%% @doc Handle {@link gen_event:notify/2}.
-
-handle_event({Level, Message} = _Event, State) ->
-  try_syslog(Level, Message),
-  {ok, State};
-
-handle_event(Event, State) ->
-  try_syslog(unknown, Event),
-  {ok, State}.
-
-%% @private
-%% @doc Handle {@link gen_event:call/3}.
-
-handle_call(_Request, State) ->
-  {ok, {error, unknown}, State}.
-
-%% @private
-%% @doc Handle incoming messages.
-
-handle_info(_Msg, State) ->
-  {ok, State}.
-
-%% }}}
-%%----------------------------------------------------------
-%% code change {{{
-
-%% @private
-%% @doc Handle code change.
-
-code_change(_OldVsn, State, _Extra) ->
-  {ok, State}.
-
-%% }}}
-%%----------------------------------------------------------
-
-try_syslog(Level, Message) ->
-  SyslogPriority = case Level of
-    info     -> info;
-    error    -> err;
-    critical -> crit;
-    _        -> warning
-  end,
-  try
-    indira_syslog ! {syslog, haircut, daemon, SyslogPriority, Message}
-  catch
-    error:badarg -> ignore
-  end.
-
-%%%---------------------------------------------------------------------------
-%%% vim:ft=erlang:foldmethod=marker