Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (98 sloc) 4.156 kb
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
1 %% Copyright (c) 2010 Basho Technologies, Inc. All Rights Reserved.
2 %%
3 %% This file is provided to you under the Apache License,
4 %% Version 2.0 (the "License"); you may not use this file
5 %% except in compliance with the License. You may obtain
6 %% a copy of the License at
7 %%
8 %% http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing,
11 %% software distributed under the License is distributed on an
12 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 %% KIND, either express or implied. See the License for the
14 %% specific language governing permissions and limitations
15 %% under the License.
16
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
17 %% @doc Small server to monitor the riak_err custom SASL event handler.
18
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
19 -module(riak_err_monitor).
20
21 -behaviour(gen_server).
22
23 -define(NAME, ?MODULE).
24 -define(Timeout, infinity).
25
26 %% External exports
27 -export([start_link/0, stop/0]).
28
29 %% gen_server callbacks
30 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
31 code_change/3]).
32
a06fde8 @slfritchie Add basic trunc_io usage to bare-bones draft, basic timestamps, and
slfritchie authored
33 -record(state, {
e6f6db9 Dialyzed
Fernando Benavides authored
34 max_len = 20*1024 :: pos_integer(),
35 tref :: timer:tref()
a06fde8 @slfritchie Add basic trunc_io usage to bare-bones draft, basic timestamps, and
slfritchie authored
36 }).
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
37
38 %%%----------------------------------------------------------------------
39 %%% API
40 %%%----------------------------------------------------------------------
dc201b5 More Doc improvements
Fernando Benavides authored
41 %% @doc Starts the monitor
e6f6db9 Dialyzed
Fernando Benavides authored
42 -spec start_link() -> {ok, pid()} | ignore | {error, term()}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
43 start_link() ->
44 gen_server:start_link({local, ?NAME}, ?MODULE, [], []).
45
dc201b5 More Doc improvements
Fernando Benavides authored
46 %% @doc Stops the monitor
e6f6db9 Dialyzed
Fernando Benavides authored
47 -spec stop() -> ok.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
48 stop() ->
49 gen_event:call(?NAME, stop, infinity).
50
51 %%%----------------------------------------------------------------------
52 %%% Callback functions from gen_server
53 %%%----------------------------------------------------------------------
54
b579637 Doc improvements
Fernando Benavides authored
55 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
56 -spec init([]) -> {ok, #state{}}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
57 init([]) ->
58 %% Add our custom handler.
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
59 ok = riak_err_handler:add_sup_handler(),
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
60
791b65a @slfritchie Basic formatting & size limiting appears to be working
slfritchie authored
61 %% Disable the default error logger handlers and SASL handlers.
e6f6db9 Dialyzed
Fernando Benavides authored
62 ok = lists:foreach(
63 fun(Handler) ->
64 gen_event:delete_handler(error_logger, Handler, {stop_please, ?MODULE})
65 end,
66 [error_logger, error_logger_tty_h, sasl_report_tty_h, sasl_report_file_h]),
67
b9d2e1e @slfritchie Add mostly-sasl_report_file_h-compatible behavior
slfritchie authored
68 {ok, TRef} = timer:send_interval(1000, reopen_log_file),
69 {ok, #state{tref = TRef}}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
70
b579637 Doc improvements
Fernando Benavides authored
71 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
72 -spec handle_call(stop | term(), reference(), #state{}) -> {stop, normal, ok, #state{}} | {reply, not_implemented, #state{}}.
73 handle_call(stop, _From, State) ->
74 {stop, normal, ok, State};
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
75 handle_call(_Request, _From, State) ->
e6f6db9 Dialyzed
Fernando Benavides authored
76 {reply, not_implemented, State}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
77
b579637 Doc improvements
Fernando Benavides authored
78 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
79 -spec handle_cast(tuple(), #state{}) -> {noreply, #state{}}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
80 handle_cast(Msg, State) ->
a06fde8 @slfritchie Add basic trunc_io usage to bare-bones draft, basic timestamps, and
slfritchie authored
81 {Str, _} = trunc_io:print(Msg, State#state.max_len),
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
82 error_logger:error_msg("~w: ~s:handle_cast got ~s\n",
83 [self(), ?MODULE, Str]),
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
84 {noreply, State}.
85
b579637 Doc improvements
Fernando Benavides authored
86 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
87 -spec handle_info(reopen_log_file | {gen_event_EXIT, ?MODULE, term()} | tuple(), #state{}) -> {noreply, #state{}} | {stop, gen_event_EXIT, #state{}}.
b9d2e1e @slfritchie Add mostly-sasl_report_file_h-compatible behavior
slfritchie authored
88 handle_info(reopen_log_file, State) ->
89 ok = riak_err_handler:reopen_log_file(),
90 {noreply, State};
543fd0d @slfritchie Move gen_event_EXIT event handler from handle_cast to handle_info, duh
slfritchie authored
91 handle_info({gen_event_EXIT, Handler, Reason}, State) ->
92 %% Our handler ought to be bullet-proof ... but it wasn't, bummer.
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
93 %% Double bummer, we cannot use the handler to log this event.
94 %%
543fd0d @slfritchie Move gen_event_EXIT event handler from handle_cast to handle_info, duh
slfritchie authored
95 %% We will stop now, and our supervisor will restart us and thus
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
96 %% reinstate the custom event handler. If all goes well, we will
97 %% be restarted after only a few milliseconds.
98
543fd0d @slfritchie Move gen_event_EXIT event handler from handle_cast to handle_info, duh
slfritchie authored
99 {Str, _} = trunc_io:print(Reason, State#state.max_len),
100 io:format("~w: ~s: handler ~w exited for reason ~s\n",
101 [self(), ?MODULE, Handler, Str]),
102 {stop, gen_event_EXIT, State};
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
103 handle_info(Info, State) ->
a06fde8 @slfritchie Add basic trunc_io usage to bare-bones draft, basic timestamps, and
slfritchie authored
104 {Str, _} = trunc_io:print(Info, State#state.max_len),
637cd29 @slfritchie Add lots of EDoc stuff, minor cleanups & refactoring
slfritchie authored
105 error_logger:error_msg("~w: ~s:handle_info got ~s\n",
106 [self(), ?MODULE, Str]),
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
107 {noreply, State}.
108
b579637 Doc improvements
Fernando Benavides authored
109 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
110 -spec terminate(term(), #state{}) -> ok.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
111 terminate(_Reason, _State) ->
112 ok.
113
b579637 Doc improvements
Fernando Benavides authored
114 %% @hidden
e6f6db9 Dialyzed
Fernando Benavides authored
115 -spec code_change(term(), #state{}, term()) -> {ok, #state{}}.
e12b4c6 @slfritchie Draft 0.2
slfritchie authored
116 code_change(_OldVsn, State, _Extra) ->
b579637 Doc improvements
Fernando Benavides authored
117 {ok, State}.
Something went wrong with that request. Please try again.