From 6c46f1a8c98113ff5951ae8974339cdeda0eb4bc Mon Sep 17 00:00:00 2001 From: Christopher Piro Date: Tue, 23 Oct 2007 09:47:15 +0000 Subject: [PATCH] [thrift] gut Erlang Thrift configuration; start using get_env Summary: I patterned some config stuff after iserve, but Erlang's built-in stuff is better Reviewed By: gopher Test Plan: works with recent ch server Revert Plan: ok Other Notes: default config given in thrift.app git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665307 13f79535-47bb-0310-9956-ffa450edef68 --- lib/erl/include/thrift.hrl | 2 - lib/erl/include/thrift.hrl.orig | 19 --------- lib/erl/priv/thrift.conf | 7 ---- lib/erl/src/thrift.app.src | 9 ++++- lib/erl/src/thrift.erl | 68 ++++++++++++++++----------------- lib/erl/src/thrift_logger.erl | 68 ++++++--------------------------- 6 files changed, 50 insertions(+), 123 deletions(-) delete mode 100644 lib/erl/include/thrift.hrl.orig delete mode 100644 lib/erl/priv/thrift.conf diff --git a/lib/erl/include/thrift.hrl b/lib/erl/include/thrift.hrl index efd2b432..395d45ea 100644 --- a/lib/erl/include/thrift.hrl +++ b/lib/erl/include/thrift.hrl @@ -4,8 +4,6 @@ %%% See accompanying file LICENSE or visit the Thrift site at: %%% http://developers.facebook.com/thrift/ --define(CONFIG_FILE, filename:join("conf", "thrift.conf")). - -define(ERROR(F, D), error_logger:format(F, D)). diff --git a/lib/erl/include/thrift.hrl.orig b/lib/erl/include/thrift.hrl.orig deleted file mode 100644 index 53128ced..00000000 --- a/lib/erl/include/thrift.hrl.orig +++ /dev/null @@ -1,19 +0,0 @@ -%%% Copyright (c) 2007- Facebook -%%% Distributed under the Thrift Software License -%%% -%%% See accompanying file LICENSE or visit the Thrift site at: -%%% http://developers.facebook.com/thrift/ - --define(APPLICATION, thrift). - --define(CONFIG_FILE, filename:join(code:priv_dir(?APPLICATION), - atom_to_list(?APPLICATION) ++ ".conf")). - --define(ERROR(F, D), - error_logger:format(F, D)). - --define(INFO(Type, Report), - error_logger:info_report({thrift_info, Type}, Report)). - --include("thrift_macros.hrl"). --include("thrift_constants.hrl"). diff --git a/lib/erl/priv/thrift.conf b/lib/erl/priv/thrift.conf deleted file mode 100644 index b32d21d8..00000000 --- a/lib/erl/priv/thrift.conf +++ /dev/null @@ -1,7 +0,0 @@ -{thrift_logger, { - {term_width, 110}, - {force_one_line, true}, - {omit, [oop_new]}, % req_processed - {gen_server_messages, false}, - {lookup, true} % DNS - }}. diff --git a/lib/erl/src/thrift.app.src b/lib/erl/src/thrift.app.src index 590e6a9b..e1d6be16 100644 --- a/lib/erl/src/thrift.app.src +++ b/lib/erl/src/thrift.app.src @@ -32,10 +32,15 @@ % configuration parameters similar to those in the config file specified % on the command line. can be fetched with gas:get_env - {env, []}, + {env, [ + {term_width, 110}, + {force_one_line, false}, + {omit_fmt, ["thrift ~p:new(~s) = ~s"]}, + {gen_server_messages, true}, + {lookup, false} % DNS + ]}, % The Module and Args used to start this application. {mod, {thrift_app, []}} ] }. - diff --git a/lib/erl/src/thrift.erl b/lib/erl/src/thrift.erl index 5dc89876..ef2c1fa0 100644 --- a/lib/erl/src/thrift.erl +++ b/lib/erl/src/thrift.erl @@ -6,7 +6,7 @@ -module(thrift). --export([start/0, stop/0, config/1, config/2]). +-export([start/0, stop/0, config/1, config/2, reconfig/1]). -include("thrift.hrl"). @@ -24,46 +24,42 @@ stop() -> %%% configuration %%% -%% go to the global file by default -config(Path) -> - config(Path, {file, ?CONFIG_FILE}). +config(Item) -> + config(?MODULE, Item). -config(Path, Source) -> - case config1(Path, Source) of - {error, file_error, R} -> - ?ERROR("error opening config ~p: ~p", [Source, R]), - false; - {error, bad_item, Tuple} -> - ?ERROR("malformed config item ~p found at ~p in ~p", [Tuple, Path, Source]), - false; - {error, not_found} -> - ?ERROR("config item ~p not found in ~p", [Path, Source]), - false; - {value, V} -> - {value, V} +config(App, Item) -> + case application:get_env(App, Item) of + {ok, Value} -> + Value; + undefined -> + ?ERROR("configuration for ~p is unavailable", [Item]), + unconfigured_item, + exit({missing_config, App, Item}) end. -%% go to a file -config1(Path, {file, File}) -> - case file:consult(File) of +reconfig(Config) -> + BFName = filename:basename(Config, ".config"), + FName = filename:join(filename:dirname(Config), + BFName ++ ".config"), + + case file:consult(FName) of {error, R={_,_,_}} -> {error, file_error, file:format_error(R)}; {error, Posix} -> {error, file_error, Posix}; - {ok, List} when is_list(List) -> - config1(Path, List) - end; + {ok, [List]} when is_list(List) -> + reconfig1(List) + end. + +reconfig1([]) -> + ok; +reconfig1([{App, List}|Tl]) -> + reconfig2(List, App, 0), + reconfig1(Tl). -%% go through a list from a file or a sublist -config1([P|Ps], List) when is_list(List) -> - case lists:keysearch(P, 1, List) of - {value, Tuple} when size(Tuple) == 2 -> - List1 = element(2, Tuple), %% either another list or, if Ps is [], the item itself - config1(Ps, List1); - {value, Tuple} -> - {error, bad_item, Tuple}; - false -> - {error, not_found} - end; -config1([], Item) -> {value, Item}; -config1(Item, Source) -> config1([Item], Source). +reconfig2([], App, Count) -> + ?INFO("application ~p reconfigured: ~p keys updated", [App, Count]), + ok; +reconfig2([{Par, Val}|Tl], App, Count) -> + application:set_env(App, Par, Val), + reconfig2(Tl, App, Count+1). diff --git a/lib/erl/src/thrift_logger.erl b/lib/erl/src/thrift_logger.erl index 8528da37..e266ef4c 100644 --- a/lib/erl/src/thrift_logger.erl +++ b/lib/erl/src/thrift_logger.erl @@ -8,10 +8,6 @@ -behaviour(gen_event). --record(state, {config}). - --define(CONFIG(Item), config(Item, State)). - %% TODO(cpiro): either %% make exceptions know whether they need to be displayed %% or not exit with tExecptions for non-errors @@ -25,12 +21,12 @@ -export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). --export([install/0, install/1, reconfig/0, reconfig/1]). +-export([install/0]). + +-record(state, {}). %% ensure the regular logger is out and ours is in install() -> - install([]). -install(Config) -> %% remove loggers io:format("starting logger~n"), lists:foreach(fun(Logger) -> @@ -44,17 +40,8 @@ install(Config) -> gen_event:add_handler(error_logger, ?MODULE, []), - reconfig(Config), - ok. -%% load our config file and amend the given stuff -reconfig() -> - reconfig([]). - -reconfig(Config) -> - gen_event:call(error_logger, ?MODULE, {reconfig, Config}). - %%==================================================================== %% gen_event callbacks %%==================================================================== @@ -67,17 +54,7 @@ reconfig(Config) -> %% @end %%-------------------------------------------------------------------- init([]) -> - BootConfig = [ - {term_width, 80}, - {force_one_line, false}, - {omit, []}, - {omit_fmt, []}, - {show_pid, false}, - {gen_server_messages, true}, - {lookup, false} - ], %% configuration before we try loading from file - - State = #state{config=BootConfig}, + State = #state{}, {ok, State}. %%-------------------------------------------------------------------- @@ -101,7 +78,7 @@ handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string end, Banner = - case ?CONFIG(show_pid) of + case config(show_pid) of true -> sformat("~s ~s ~s", [Symbol, Pid, Type1]); false -> @@ -114,7 +91,7 @@ handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string OutputSafe = sformat("~s", [MessageSafe]), Length = - case (length(OutputSafe) + BannerLen) < ?CONFIG(term_width) of + case (length(OutputSafe) + BannerLen) < config(term_width) of true -> short; false -> long end, @@ -125,7 +102,7 @@ handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string false -> multiline end, - case { ?CONFIG(force_one_line), Length, OneLine } of + case { config(force_one_line), Length, OneLine } of %% one line and short ... print as is {_, short, oneliner} -> format("~s~s~n", [Banner, OutputSafe]); @@ -133,7 +110,7 @@ handle_event2(Symbol, Pid, Type, Message, State) -> % Message must be a string %% too long ... squash to one {true, long, _} -> O = Banner ++ OutputSafe, - Format = sformat("~~~ps >~n", [?CONFIG(term_width)-2]), % e.g. "~80s >~n" + Format = sformat("~~~ps >~n", [config(term_width)-2]), % e.g. "~80s >~n" format(Format, [O]); %% short but multiline... collapse to one @@ -186,7 +163,7 @@ handle_event1({What, _Gleader, {Ref, Format, Data}}, State) when is_list(Format) Message = sformat("DATA DIDN'T MATCH: ~p~n", [Data]) ++ sformat(Format, Data), handle_event2(Symbol, Ref, "", Message, State); {_, _} -> - case lists:member(Format, ?CONFIG(omit_fmt)) of + case lists:member(Format, config(omit_fmt)) of false -> Message = sformat(Format, Data), handle_event2(Symbol, Ref, "", Message, State); @@ -253,11 +230,6 @@ handle_event(Event, State) -> %% handler to handle the request. %% @end %%-------------------------------------------------------------------- -handle_call({reconfig, Amendments}, State) -> - {OkOrError, State1} = reconfig1(State, Amendments), - format(".. reconfig was ~p~n", [OkOrError]), - {ok, OkOrError, State1}; - handle_call(_Request, State) -> Reply = ok, {ok, Reply, State}. @@ -313,26 +285,8 @@ format(Format, Data) -> sformat(Format, Data) -> thrift_utils:sformat(Format, Data). -reconfig1(State, Amendments) -> - case thrift:config(thrift_logger) of - {value, Config} -> - Config1 = lists:keysort(1, Config), - Amendments1 = lists:keysort(1, Amendments), - Config2 = lists:keymerge(1, Amendments1, Config1), - - State1 = State#state{config=Config2}, - {ok, State1}; - _ -> - {error, State} - end. - -config(Item, State) -> - case thrift:config(Item, State#state.config) of - {value, V} -> - V; - Else -> - ?ERROR("config for ~p is unavailable: ~p", [Item, Else]) - end. +config(Item) -> + thrift:config(Item). do_print_crash_report(Report) -> case Report of -- 2.17.1