Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
| 3 | %%% |
| 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 |
| 21 | -export([init/1, handle_event/2, handle_call/2, |
| 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 | |
| 26 | -record(state, {}). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 27 | |
| 28 | %% ensure the regular logger is out and ours is in |
| 29 | install() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 30 | %% remove loggers |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 31 | io:format("starting logger~n"), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 32 | lists:foreach(fun(Logger) -> |
| 33 | case Logger of |
| 34 | _ -> gen_event:delete_handler(error_logger, Logger, normal) |
| 35 | end end, |
| 36 | gen_event:which_handlers(error_logger)), |
| 37 | |
| 38 | %% TODO(cpiro): sasl someday? |
| 39 | %% gen_event:add_handler(error_logger, sasl_report_file_h, {LogFile, all}), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 40 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 41 | gen_event:add_handler(error_logger, ?MODULE, []), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 42 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 43 | ok. |
| 44 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 45 | %%==================================================================== |
| 46 | %% gen_event callbacks |
| 47 | %%==================================================================== |
| 48 | %%-------------------------------------------------------------------- |
| 49 | %% @spec init(Args) -> {ok, State}. |
| 50 | %% |
| 51 | %% @doc |
| 52 | %% Whenever a new event handler is added to an event manager, |
| 53 | %% this function is called to initialize the event handler. |
| 54 | %% @end |
| 55 | %%-------------------------------------------------------------------- |
| 56 | init([]) -> |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 57 | State = #state{}, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 58 | {ok, State}. |
| 59 | |
| 60 | %%-------------------------------------------------------------------- |
| 61 | %% @spec handle_event(Event, State) -> {ok, State} | |
| 62 | %% {swap_handler, Args1, State1, Mod2, Args2} | |
| 63 | %% remove_handler. |
| 64 | %% |
| 65 | %% @doc |
| 66 | %% Whenever an event manager receives an event sent using |
| 67 | %% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for |
| 68 | %% each installed event handler to handle the event. |
| 69 | %% @end |
| 70 | %%-------------------------------------------------------------------- |
| 71 | handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string |
| 72 | {ok, MessageSafe, NL} = regexp:gsub(Message, "[\n]+", " "), % collapse whitespace to one space |
| 73 | |
| 74 | Type1 = |
| 75 | case Type of |
| 76 | "" -> ""; |
| 77 | _ -> sformat("~p ", [Type]) |
| 78 | end, |
| 79 | |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 80 | Banner = |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 81 | case config(show_pid) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 82 | true -> |
| 83 | sformat("~s ~s ~s", [Symbol, Pid, Type1]); |
| 84 | false -> |
| 85 | sformat("~s~i ~s", [Symbol, Pid, Type1]) |
| 86 | end, |
| 87 | BannerLen = length(Banner), |
| 88 | |
| 89 | %% there's no way to see if Message is a string? just try |
| 90 | Output = sformat("~s", [Message]), |
| 91 | OutputSafe = sformat("~s", [MessageSafe]), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 92 | |
| 93 | Length = |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 94 | case (length(OutputSafe) + BannerLen) < config(term_width) of |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 95 | true -> short; |
| 96 | false -> long |
| 97 | end, |
| 98 | |
| 99 | OneLine = |
| 100 | case NL == 0 of |
| 101 | true -> oneliner; |
| 102 | false -> multiline |
| 103 | end, |
| 104 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 105 | case { config(force_one_line), Length, OneLine } of |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 106 | %% one line and short ... print as is |
| 107 | {_, short, oneliner} -> |
| 108 | format("~s~s~n", [Banner, OutputSafe]); |
| 109 | |
| 110 | %% too long ... squash to one |
| 111 | {true, long, _} -> |
| 112 | O = Banner ++ OutputSafe, |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 113 | Format = sformat("~~~ps >~n", [config(term_width)-2]), % e.g. "~80s >~n" |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 114 | format(Format, [O]); |
| 115 | |
| 116 | %% short but multiline... collapse to one |
| 117 | {true, short, multiline} -> |
| 118 | format("~s~s~n", [Banner, OutputSafe]); |
| 119 | |
| 120 | %% just print it |
| 121 | _ -> |
| 122 | format("~s~n~s~n~n", [Banner, Output]) |
| 123 | end. |
| 124 | |
| 125 | %% |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 126 | -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"). |
| 127 | |
Christopher Piro | efd5eec | 2007-10-02 01:33:37 +0000 | [diff] [blame] | 128 | handle_event1({What, _Gleader, {Ref, Format, Data}}, State) when is_list(Format) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 129 | Symbol = case What of |
| 130 | error -> "!!"; |
| 131 | warning_msg -> "**"; |
| 132 | info_msg -> ".."; |
| 133 | _Else -> "??" |
| 134 | end, |
| 135 | |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 136 | case {Format, Data} of |
| 137 | {?GS_TERM_FORMAT, [Ref, LastMessage, Obj, Reason]} -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 138 | %% TODO: move as much logic as possible out of thrift_logger |
Christopher Piro | fa0c857 | 2007-08-11 01:15:57 +0000 | [diff] [blame] | 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 | |
| 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? |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 160 | handle_event2(Symbol, Ref, "", Message, State) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 161 | end; |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 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); |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 165 | {_, _} -> |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 166 | case lists:member(Format, config(omit_fmt)) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 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) -> |
| 177 | Symbol = case What of |
| 178 | error_report -> "!!"; |
| 179 | warning_report -> "**"; |
| 180 | info_report -> ".."; |
| 181 | _Else -> "??" |
| 182 | end, |
| 183 | |
| 184 | case Type of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 185 | crash_report -> |
| 186 | case do_print_crash_report(Report) of |
| 187 | true -> |
| 188 | io:format("~~~~ crash report: '~p'~n", [Report]); |
| 189 | false -> |
| 190 | ok |
| 191 | end; |
| 192 | %% crash_report -> |
| 193 | %% [Cruft|_] = Report, |
| 194 | %% {value, {_, Reason}} = lists:keysearch(error_info, 1, Cruft), |
| 195 | %% {value, {_, {_, _, [_,_,_,_, Obj, []]}}} = lists:keysearch(initial_call, 1, Cruft), |
| 196 | %% sformat("state == ~s~nreason ==~s", [oop:inspect(Obj), oop:inspect(Reason)]), |
| 197 | %% ok; |
| 198 | progress -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 199 | ok; |
| 200 | |
| 201 | _ -> |
| 202 | Message = sformat("|| ~s", [oop:inspect(Report)]), |
| 203 | handle_event2(Symbol, Pid, Type, Message, State) |
| 204 | end, |
| 205 | {ok, State}; |
| 206 | |
| 207 | handle_event1(_Event, State) -> |
| 208 | handle_event2("??", "<?.?.?>", "", _Event, State), |
| 209 | {ok, State}. |
| 210 | |
| 211 | handle_event(Event, State) -> |
| 212 | try |
| 213 | handle_event1(Event, State) |
| 214 | catch |
| 215 | _:E -> |
| 216 | format("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n error logger error:~n ~p~n Event = ~p~n State = ~p~n ~p~n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n", |
| 217 | [E, Event, State, erlang:get_stacktrace()]), |
| 218 | {ok, State} |
| 219 | end. |
| 220 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 221 | %%-------------------------------------------------------------------- |
| 222 | %% @spec handle_call(Request, State) -> {ok, Reply, State} | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 223 | %% {swap_handler, Reply, Args1, State1, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 224 | %% Mod2, Args2} | |
| 225 | %% {remove_handler, Reply}. |
| 226 | %% |
| 227 | %% @doc |
| 228 | %% Whenever an event manager receives a request sent using |
| 229 | %% gen_event:call/3,4, this function is called for the specified event |
| 230 | %% handler to handle the request. |
| 231 | %% @end |
| 232 | %%-------------------------------------------------------------------- |
| 233 | handle_call(_Request, State) -> |
| 234 | Reply = ok, |
| 235 | {ok, Reply, State}. |
| 236 | |
| 237 | %%-------------------------------------------------------------------- |
| 238 | %% @spec handle_info(Info, State) -> {ok, State} | |
| 239 | %% {swap_handler, Args1, State1, Mod2, Args2} | |
| 240 | %% remove_handler. |
| 241 | %% |
| 242 | %% @doc |
| 243 | %% This function is called for each installed event handler when |
| 244 | %% an event manager receives any other message than an event or a synchronous |
| 245 | %% request (or a system message). |
| 246 | %% @end |
| 247 | %%-------------------------------------------------------------------- |
| 248 | handle_info(_Info, State) -> |
| 249 | {ok, State}. |
| 250 | |
| 251 | %%-------------------------------------------------------------------- |
| 252 | %% @spec terminate(Reason, State) -> void(). |
| 253 | %% |
| 254 | %% @doc |
| 255 | %% Whenever an event handler is deleted from an event manager, |
| 256 | %% this function is called. It should be the opposite of Module:init/1 and |
| 257 | %% do any necessary cleaning up. |
| 258 | %% @end |
| 259 | %%-------------------------------------------------------------------- |
| 260 | terminate(normal, _State) -> |
| 261 | ok; |
| 262 | terminate(Reason, _State) -> |
| 263 | format("*****************~n~n frick, error logger terminating: ~p~n~n*****************~n~n", [Reason]), |
| 264 | ok. |
| 265 | |
| 266 | %%-------------------------------------------------------------------- |
| 267 | %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}. |
| 268 | %% |
| 269 | %% @doc |
| 270 | %% Convert process state when code is changed |
| 271 | %% @end |
| 272 | %%-------------------------------------------------------------------- |
| 273 | code_change(_OldVsn, State, _Extra) -> |
| 274 | {ok, State}. |
| 275 | |
| 276 | %%==================================================================== |
| 277 | %%% Internal functions |
| 278 | %%==================================================================== |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 279 | |
| 280 | %% how to output |
| 281 | format(Format, Data) -> |
| 282 | io:format(Format, Data). |
| 283 | |
| 284 | %% convenience |
| 285 | sformat(Format, Data) -> |
| 286 | thrift_utils:sformat(Format, Data). |
| 287 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 288 | config(Item) -> |
| 289 | thrift:config(Item). |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 290 | |
| 291 | do_print_crash_report(Report) -> |
| 292 | case Report of |
| 293 | [[_,_,{error_info, XXX}|_] | _] -> |
| 294 | case thrift_utils:first_item(XXX) of |
| 295 | tTransportException -> |
| 296 | false; |
| 297 | _ -> |
| 298 | true |
| 299 | end; |
| 300 | _ -> |
| 301 | true |
| 302 | end. |