Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 3 | %%% |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | -module(thrift_logger). |
| 8 | |
| 9 | -behaviour(gen_event). |
| 10 | |
Christopher Piro | fa0c857 | 2007-08-11 01:15:57 +0000 | [diff] [blame] | 11 | %% TODO(cpiro): either |
| 12 | %% make exceptions know whether they need to be displayed |
| 13 | %% or not exit with tExecptions for non-errors |
| 14 | %% or "register" tExceptions with the logger (I LIKE!) |
| 15 | %% ... we shouldn't need to build any specifics in here |
| 16 | -include("thrift.hrl"). |
| 17 | -include("oop.hrl"). |
| 18 | -include("transport/tTransportException.hrl"). |
| 19 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 20 | %% gen_event callbacks |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 21 | -export([init/1, handle_event/2, handle_call/2, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 22 | handle_info/2, terminate/2, code_change/3]). |
| 23 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 24 | -export([install/0]). |
| 25 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 26 | %% |
| 27 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 28 | -record(state, {}). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 29 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 30 | -define(GS_TERM_FORMAT, "** Generic server ~p terminating \n** Last message in was ~p~n** When Server state == ~p~n** Reason for termination == ~n** ~p~n"). |
| 31 | |
| 32 | %% |
| 33 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 34 | %% ensure the regular logger is out and ours is in |
| 35 | install() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 36 | %% remove loggers |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 37 | io:format("starting logger~n"), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 38 | lists:foreach(fun(Logger) -> |
| 39 | case Logger of |
| 40 | _ -> gen_event:delete_handler(error_logger, Logger, normal) |
| 41 | end end, |
| 42 | gen_event:which_handlers(error_logger)), |
| 43 | |
| 44 | %% TODO(cpiro): sasl someday? |
| 45 | %% gen_event:add_handler(error_logger, sasl_report_file_h, {LogFile, all}), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 46 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 47 | gen_event:add_handler(error_logger, ?MODULE, []), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 48 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 49 | ok. |
| 50 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 51 | %%==================================================================== |
| 52 | %% gen_event callbacks |
| 53 | %%==================================================================== |
| 54 | %%-------------------------------------------------------------------- |
| 55 | %% @spec init(Args) -> {ok, State}. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 56 | %% |
| 57 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 58 | %% Whenever a new event handler is added to an event manager, |
| 59 | %% this function is called to initialize the event handler. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 60 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 61 | %%-------------------------------------------------------------------- |
| 62 | init([]) -> |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 63 | State = #state{}, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 64 | {ok, State}. |
| 65 | |
| 66 | %%-------------------------------------------------------------------- |
| 67 | %% @spec handle_event(Event, State) -> {ok, State} | |
| 68 | %% {swap_handler, Args1, State1, Mod2, Args2} | |
| 69 | %% remove_handler. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 70 | %% |
| 71 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 72 | %% Whenever an event manager receives an event sent using |
| 73 | %% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 74 | %% each installed event handler to handle the event. |
| 75 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 76 | %%-------------------------------------------------------------------- |
| 77 | handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string |
| 78 | {ok, MessageSafe, NL} = regexp:gsub(Message, "[\n]+", " "), % collapse whitespace to one space |
| 79 | |
| 80 | Type1 = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 81 | case Type of |
| 82 | "" -> ""; |
| 83 | _ -> sformat("~p ", [Type]) |
| 84 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 85 | |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 86 | Banner = |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 87 | case config(show_pid) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 88 | true -> |
| 89 | sformat("~s ~s ~s", [Symbol, Pid, Type1]); |
| 90 | false -> |
| 91 | sformat("~s~i ~s", [Symbol, Pid, Type1]) |
| 92 | end, |
| 93 | BannerLen = length(Banner), |
| 94 | |
| 95 | %% there's no way to see if Message is a string? just try |
| 96 | Output = sformat("~s", [Message]), |
| 97 | OutputSafe = sformat("~s", [MessageSafe]), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 98 | |
| 99 | Length = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 100 | case (length(OutputSafe) + BannerLen) < config(term_width) of |
| 101 | true -> short; |
| 102 | false -> long |
| 103 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 104 | |
| 105 | OneLine = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 106 | case NL == 0 of |
| 107 | true -> oneliner; |
| 108 | false -> multiline |
| 109 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 110 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 111 | case { config(force_one_line), Length, OneLine } of |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 112 | %% one line and short ... print as is |
| 113 | {_, short, oneliner} -> |
| 114 | format("~s~s~n", [Banner, OutputSafe]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 115 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 116 | %% too long ... squash to one |
| 117 | {true, long, _} -> |
| 118 | O = Banner ++ OutputSafe, |
| 119 | Format = sformat("~~~ps >~n", [config(term_width)-2]), % e.g. "~80s >~n" |
| 120 | format(Format, [O]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 121 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 122 | %% short but multiline... collapse to one |
| 123 | {true, short, multiline} -> |
| 124 | format("~s~s~n", [Banner, OutputSafe]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 125 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 126 | %% just print it |
| 127 | _ -> |
| 128 | format("~s~n~s~n~n", [Banner, Output]) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 129 | end. |
| 130 | |
| 131 | %% |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 132 | |
Christopher Piro | efd5eec | 2007-10-02 01:33:37 +0000 | [diff] [blame] | 133 | handle_event1({What, _Gleader, {Ref, Format, Data}}, State) when is_list(Format) -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 134 | Symbol = symbol(What), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 135 | |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 136 | case {Format, Data} of |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 137 | {?GS_TERM_FORMAT, [Ref, LastMessage, Obj, Reason]} -> |
| 138 | %% TODO: move as much logic as possible out of thrift_logger |
| 139 | Ignore = |
| 140 | begin |
| 141 | is_tuple(Reason) andalso |
| 142 | size(Reason) >= 1 andalso element(1, Reason) == timeout |
| 143 | end |
| 144 | orelse |
| 145 | begin |
| 146 | case thrift_utils:unnest_record(Reason, tTransportException) of |
| 147 | error -> false; |
| 148 | {ok, TTE} -> |
| 149 | oop:get(TTE, type) == ?tTransportException_NOT_OPEN andalso |
| 150 | oop:get(TTE, message) == "in tSocket:read/2: gen_tcp:recv" |
| 151 | end |
| 152 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 153 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 154 | case Ignore of |
| 155 | true -> |
| 156 | ok; |
| 157 | false -> |
| 158 | Format1 = "** gen_server terminating in message ~p~n** State = ~s~n** Reason = ~s~n", |
| 159 | Message = sformat(Format1, [LastMessage, oop:inspect(Obj), oop:inspect(Reason)]), %% TODO(cpiro): hope Reason is an object? |
| 160 | handle_event2(Symbol, Ref, "", Message, State) |
| 161 | end; |
| 162 | {?GS_TERM_FORMAT, _Dta} -> |
| 163 | Message = sformat("DATA DIDN'T MATCH: ~p~n", [Data]) ++ sformat(Format, Data), |
| 164 | handle_event2(Symbol, Ref, "", Message, State); |
| 165 | {_, _} -> |
| 166 | case lists:member(Format, config(omit_fmt)) of |
| 167 | false -> |
| 168 | Message = sformat(Format, Data), |
| 169 | handle_event2(Symbol, Ref, "", Message, State); |
| 170 | true -> |
| 171 | ok |
| 172 | end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 173 | end, |
| 174 | {ok, State}; |
| 175 | |
| 176 | handle_event1({What, _Gleader, {Pid, Type, Report}}, State) -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 177 | Symbol = symbol(What), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 178 | |
| 179 | case Type of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 180 | crash_report -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 181 | print_crash_report(Report); |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 182 | progress -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 183 | ok; |
| 184 | _ -> |
| 185 | Message = sformat("|| ~s", [oop:inspect(Report)]), |
| 186 | handle_event2(Symbol, Pid, Type, Message, State) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 187 | end, |
| 188 | {ok, State}; |
| 189 | |
| 190 | handle_event1(_Event, State) -> |
| 191 | handle_event2("??", "<?.?.?>", "", _Event, State), |
| 192 | {ok, State}. |
| 193 | |
| 194 | handle_event(Event, State) -> |
| 195 | try |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 196 | handle_event1(Event, State) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 197 | catch |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 198 | _:E -> |
| 199 | format("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n error logger error:~n ~p~n Event = ~p~n State = ~p~n ~p~n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n", |
| 200 | [E, Event, State, erlang:get_stacktrace()]), |
| 201 | {ok, State} |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 202 | end. |
| 203 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 204 | %%-------------------------------------------------------------------- |
| 205 | %% @spec handle_call(Request, State) -> {ok, Reply, State} | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 206 | %% {swap_handler, Reply, Args1, State1, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 207 | %% Mod2, Args2} | |
| 208 | %% {remove_handler, Reply}. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 209 | %% |
| 210 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 211 | %% Whenever an event manager receives a request sent using |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 212 | %% gen_event:call/3,4, this function is called for the specified event |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 213 | %% handler to handle the request. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 214 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 215 | %%-------------------------------------------------------------------- |
| 216 | handle_call(_Request, State) -> |
| 217 | Reply = ok, |
| 218 | {ok, Reply, State}. |
| 219 | |
| 220 | %%-------------------------------------------------------------------- |
| 221 | %% @spec handle_info(Info, State) -> {ok, State} | |
| 222 | %% {swap_handler, Args1, State1, Mod2, Args2} | |
| 223 | %% remove_handler. |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 224 | %% |
| 225 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 226 | %% This function is called for each installed event handler when |
| 227 | %% an event manager receives any other message than an event or a synchronous |
| 228 | %% request (or a system message). |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 229 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 230 | %%-------------------------------------------------------------------- |
| 231 | handle_info(_Info, State) -> |
| 232 | {ok, State}. |
| 233 | |
| 234 | %%-------------------------------------------------------------------- |
| 235 | %% @spec terminate(Reason, State) -> void(). |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 236 | %% |
| 237 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 238 | %% Whenever an event handler is deleted from an event manager, |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 239 | %% this function is called. It should be the opposite of Module:init/1 and |
| 240 | %% do any necessary cleaning up. |
| 241 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 242 | %%-------------------------------------------------------------------- |
| 243 | terminate(normal, _State) -> |
| 244 | ok; |
| 245 | terminate(Reason, _State) -> |
| 246 | format("*****************~n~n frick, error logger terminating: ~p~n~n*****************~n~n", [Reason]), |
| 247 | ok. |
| 248 | |
| 249 | %%-------------------------------------------------------------------- |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 250 | %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}. |
| 251 | %% |
| 252 | %% @doc |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 253 | %% Convert process state when code is changed |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 254 | %% @end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 255 | %%-------------------------------------------------------------------- |
| 256 | code_change(_OldVsn, State, _Extra) -> |
| 257 | {ok, State}. |
| 258 | |
| 259 | %%==================================================================== |
| 260 | %%% Internal functions |
| 261 | %%==================================================================== |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 262 | |
| 263 | %% how to output |
| 264 | format(Format, Data) -> |
| 265 | io:format(Format, Data). |
| 266 | |
| 267 | %% convenience |
| 268 | sformat(Format, Data) -> |
| 269 | thrift_utils:sformat(Format, Data). |
| 270 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 271 | config(Item) -> |
| 272 | thrift:config(Item). |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 273 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 274 | symbol(error_report) -> "!!"; |
| 275 | symbol(warning_report) -> "**"; |
| 276 | symbol(info_report) -> ".."; |
| 277 | symbol(_Else) -> "??". |
| 278 | |
| 279 | print_crash_report(Report) -> |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 280 | case Report of |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 281 | [[_,_,{error_info, XX}|_] | _] -> |
| 282 | case thrift_utils:first_item(XX) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 283 | tTransportException -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 284 | ok; |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 285 | _ -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 286 | io:format("~~~~ crash report: ~P~n", [XX, 2]) |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 287 | end; |
| 288 | _ -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame^] | 289 | io:format("~~~~ crash report (?): ~p~n", [Report]) |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 290 | end. |