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 | -include("thrift.hrl"). |
| 12 | -include("oop.hrl"). |
Christopher Piro | fa0c857 | 2007-08-11 01:15:57 +0000 | [diff] [blame] | 13 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 14 | %% gen_event callbacks |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 15 | -export([init/1, handle_event/2, handle_call/2, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 16 | handle_info/2, terminate/2, code_change/3]). |
| 17 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 18 | -export([install/0]). |
| 19 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 20 | %% |
| 21 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 22 | -record(state, {}). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 23 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 24 | -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"). |
| 25 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 26 | %%% |
| 27 | %%% ensure the regular logger is out and ours is in |
| 28 | %%% |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 29 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 30 | install() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 31 | %% remove loggers |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 32 | io:format("starting logger~n"), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 33 | lists:foreach(fun(Logger) -> |
| 34 | case Logger of |
| 35 | _ -> gen_event:delete_handler(error_logger, Logger, normal) |
| 36 | end end, |
| 37 | gen_event:which_handlers(error_logger)), |
| 38 | |
| 39 | %% TODO(cpiro): sasl someday? |
| 40 | %% gen_event:add_handler(error_logger, sasl_report_file_h, {LogFile, all}), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 41 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 42 | gen_event:add_handler(error_logger, ?MODULE, []), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 43 | |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 44 | ok. |
| 45 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 46 | %%% |
| 47 | %%% init |
| 48 | %%% |
| 49 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 50 | init([]) -> |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 51 | State = #state{}, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 52 | {ok, State}. |
| 53 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 54 | %%% |
| 55 | %%% handle_event |
| 56 | %%% |
| 57 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 58 | handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string |
| 59 | {ok, MessageSafe, NL} = regexp:gsub(Message, "[\n]+", " "), % collapse whitespace to one space |
| 60 | |
| 61 | Type1 = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 62 | case Type of |
| 63 | "" -> ""; |
| 64 | _ -> sformat("~p ", [Type]) |
| 65 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 66 | |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 67 | Banner = |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 68 | case config(show_pid) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 69 | true -> |
| 70 | sformat("~s ~s ~s", [Symbol, Pid, Type1]); |
| 71 | false -> |
| 72 | sformat("~s~i ~s", [Symbol, Pid, Type1]) |
| 73 | end, |
| 74 | BannerLen = length(Banner), |
| 75 | |
| 76 | %% there's no way to see if Message is a string? just try |
| 77 | Output = sformat("~s", [Message]), |
| 78 | OutputSafe = sformat("~s", [MessageSafe]), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 79 | |
| 80 | Length = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 81 | case (length(OutputSafe) + BannerLen) < config(term_width) of |
| 82 | true -> short; |
| 83 | false -> long |
| 84 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 85 | |
| 86 | OneLine = |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 87 | case NL == 0 of |
| 88 | true -> oneliner; |
| 89 | false -> multiline |
| 90 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 91 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 92 | case { config(force_one_line), Length, OneLine } of |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 93 | %% one line and short ... print as is |
| 94 | {_, short, oneliner} -> |
| 95 | format("~s~s~n", [Banner, OutputSafe]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 96 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 97 | %% too long ... squash to one |
| 98 | {true, long, _} -> |
| 99 | O = Banner ++ OutputSafe, |
| 100 | Format = sformat("~~~ps >~n", [config(term_width)-2]), % e.g. "~80s >~n" |
| 101 | format(Format, [O]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 102 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 103 | %% short but multiline... collapse to one |
| 104 | {true, short, multiline} -> |
| 105 | format("~s~s~n", [Banner, OutputSafe]); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 106 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 107 | %% just print it |
| 108 | _ -> |
| 109 | format("~s~n~s~n~n", [Banner, Output]) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 110 | end. |
| 111 | |
| 112 | %% |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 113 | |
Christopher Piro | efd5eec | 2007-10-02 01:33:37 +0000 | [diff] [blame] | 114 | handle_event1({What, _Gleader, {Ref, Format, Data}}, State) when is_list(Format) -> |
Christopher Piro | 10522a7 | 2007-11-15 06:26:31 +0000 | [diff] [blame] | 115 | Symbol = |
| 116 | case What of |
| 117 | error -> "!!"; |
| 118 | warning_msg -> "**"; |
| 119 | info_msg -> ".."; |
| 120 | _Else -> "??" |
| 121 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 122 | |
Christopher Piro | 524c3ec | 2007-10-13 05:15:33 +0000 | [diff] [blame] | 123 | case {Format, Data} of |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 124 | {?GS_TERM_FORMAT, [Ref, LastMessage, Obj, {Kind, E}]} when Kind == timeout; Kind == thrift_exception -> |
| 125 | ok; |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 126 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 127 | {?GS_TERM_FORMAT, [Ref, LastMessage, Obj, Reason]} -> |
| 128 | Format1 = "** gen_server terminating in message ~p~n** State = ~s~n** Reason = ~p~n", |
| 129 | Message = sformat(Format1, [LastMessage, oop:inspect(Obj), Reason]), |
| 130 | handle_event2(Symbol, Ref, "", Message, State); |
| 131 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 132 | {?GS_TERM_FORMAT, _Dta} -> |
| 133 | Message = sformat("DATA DIDN'T MATCH: ~p~n", [Data]) ++ sformat(Format, Data), |
| 134 | handle_event2(Symbol, Ref, "", Message, State); |
| 135 | {_, _} -> |
| 136 | case lists:member(Format, config(omit_fmt)) of |
| 137 | false -> |
| 138 | Message = sformat(Format, Data), |
| 139 | handle_event2(Symbol, Ref, "", Message, State); |
| 140 | true -> |
| 141 | ok |
| 142 | end |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 143 | end, |
| 144 | {ok, State}; |
| 145 | |
| 146 | handle_event1({What, _Gleader, {Pid, Type, Report}}, State) -> |
Christopher Piro | 10522a7 | 2007-11-15 06:26:31 +0000 | [diff] [blame] | 147 | Symbol = |
| 148 | case What of |
| 149 | error_report -> "!!"; |
| 150 | warning_report -> "**"; |
| 151 | info_report -> ".."; |
| 152 | _Else -> "??" |
| 153 | end, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 154 | |
| 155 | case Type of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 156 | crash_report -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 157 | print_crash_report(Report); |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 158 | progress -> |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 159 | ok; |
| 160 | _ -> |
| 161 | Message = sformat("|| ~s", [oop:inspect(Report)]), |
| 162 | handle_event2(Symbol, Pid, Type, Message, State) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 163 | end, |
| 164 | {ok, State}; |
| 165 | |
| 166 | handle_event1(_Event, State) -> |
| 167 | handle_event2("??", "<?.?.?>", "", _Event, State), |
| 168 | {ok, State}. |
| 169 | |
| 170 | handle_event(Event, State) -> |
| 171 | try |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 172 | handle_event1(Event, State) |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 173 | catch |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 174 | _:E -> |
| 175 | format("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n error logger error:~n ~p~n Event = ~p~n State = ~p~n ~p~n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n", |
| 176 | [E, Event, State, erlang:get_stacktrace()]), |
| 177 | {ok, State} |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 178 | end. |
| 179 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 180 | %%% |
| 181 | %%% call, info, terminate, code_change |
| 182 | %%% |
| 183 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 184 | handle_call(_Request, State) -> |
| 185 | Reply = ok, |
| 186 | {ok, Reply, State}. |
| 187 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 188 | handle_info(_Info, State) -> |
| 189 | {ok, State}. |
| 190 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 191 | terminate(normal, _State) -> |
| 192 | ok; |
| 193 | terminate(Reason, _State) -> |
| 194 | format("*****************~n~n frick, error logger terminating: ~p~n~n*****************~n~n", [Reason]), |
| 195 | ok. |
| 196 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 197 | code_change(_OldVsn, State, _Extra) -> |
| 198 | {ok, State}. |
| 199 | |
| 200 | %%==================================================================== |
| 201 | %%% Internal functions |
| 202 | %%==================================================================== |
Christopher Piro | 6894029 | 2007-10-02 00:35:12 +0000 | [diff] [blame] | 203 | |
| 204 | %% how to output |
| 205 | format(Format, Data) -> |
| 206 | io:format(Format, Data). |
| 207 | |
| 208 | %% convenience |
| 209 | sformat(Format, Data) -> |
| 210 | thrift_utils:sformat(Format, Data). |
| 211 | |
Christopher Piro | 6c46f1a | 2007-10-23 09:47:15 +0000 | [diff] [blame] | 212 | config(Item) -> |
| 213 | thrift:config(Item). |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 214 | |
Christopher Piro | c2e37c7 | 2007-11-15 06:26:30 +0000 | [diff] [blame] | 215 | print_crash_report(Report) -> |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 216 | case Report of |
Christopher Piro | a89cd7b | 2007-12-19 00:00:39 +0000 | [diff] [blame] | 217 | %% for R12B0 |
| 218 | [[_, _, {error_info, {exit, {thrift_exception, _}, _}} | _] | _] -> |
| 219 | ok; |
| 220 | [[_, _, {error_info, {exit, {timeout, _}, _}} | _] | _] -> |
| 221 | ok; |
| 222 | |
| 223 | %% for R11B5 |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 224 | [[_,_,{error_info, {thrift_exception, _}}|_] | _] -> |
| 225 | ok; |
| 226 | [[_,_,{error_info, {timeout, _}}|_] | _] -> |
| 227 | ok; |
Christopher Piro | a89cd7b | 2007-12-19 00:00:39 +0000 | [diff] [blame] | 228 | |
| 229 | %% else |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 230 | _ -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 231 | io:format("~~~~ crash report: ~w~n", [Report]) |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 232 | end. |