summaryrefslogtreecommitdiff
path: root/src/couch_log/src/couch_log_config.erl
blob: 92597317813391d83c7361159fe5af23601b4c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
%
% Based on Bob Ippolitto's mochiglobal.erl

-module(couch_log_config).

-export([
    init/0,
    reconfigure/0,
    get/1
]).

-define(MOD_NAME, couch_log_config_dyn).
-define(ERL_FILE, "couch_log_config_dyn.erl").

-spec init() -> ok.
init() ->
    reconfigure().

-spec reconfigure() -> ok.
reconfigure() ->
    {ok, ?MOD_NAME, Bin} = compile:forms(forms(), [verbose, report_errors]),
    code:purge(?MOD_NAME),
    {module, ?MOD_NAME} = code:load_binary(?MOD_NAME, ?ERL_FILE, Bin),
    ok.

-spec get(atom()) -> term().
get(Key) ->
    ?MOD_NAME:get(Key).

-spec entries() -> [string()].
entries() ->
    [
        {level, "level", "info"},
        {level_int, "level", "info"},
        {max_message_size, "max_message_size", "16000"},
        {strip_last_msg, "strip_last_msg", "true"},
        {filter_fields, "filter_fields", "[pid, registered_name, error_info, messages]"}
    ].

-spec forms() -> [erl_syntax:syntaxTree()].
forms() ->
    GetFunClauses = lists:map(
        fun({FunKey, CfgKey, Default}) ->
            FunVal = transform(FunKey, config:get("log", CfgKey, Default)),
            Patterns = [erl_syntax:abstract(FunKey)],
            Bodies = [erl_syntax:abstract(FunVal)],
            erl_syntax:clause(Patterns, none, Bodies)
        end,
        entries()
    ),

    Statements = [
        % -module(?MOD_NAME)
        erl_syntax:attribute(
            erl_syntax:atom(module),
            [erl_syntax:atom(?MOD_NAME)]
        ),

        % -export([lookup/1]).
        erl_syntax:attribute(
            erl_syntax:atom(export),
            [
                erl_syntax:list([
                    erl_syntax:arity_qualifier(
                        erl_syntax:atom(get),
                        erl_syntax:integer(1)
                    )
                ])
            ]
        ),

        % list(Key) -> Value.
        erl_syntax:function(erl_syntax:atom(get), GetFunClauses)
    ],
    [erl_syntax:revert(X) || X <- Statements].

transform(level, LevelStr) ->
    couch_log_util:level_to_atom(LevelStr);
transform(level_int, LevelStr) ->
    Level = couch_log_util:level_to_atom(LevelStr),
    couch_log_util:level_to_integer(Level);
transform(max_message_size, SizeStr) ->
    try list_to_integer(SizeStr) of
        Size -> Size
    catch
        _:_ ->
            16000
    end;
transform(strip_last_msg, "false") ->
    false;
transform(strip_last_msg, _) ->
    true;
transform(filter_fields, FieldsStr) ->
    Default = [pid, registered_name, error_info, messages],
    case parse_term(FieldsStr) of
        {ok, List} when is_list(List) ->
            case lists:all(fun erlang:is_atom/1, List) of
                true ->
                    List;
                false ->
                    Default
            end;
        _ ->
            Default
    end.

parse_term(List) ->
    {ok, Tokens, _} = erl_scan:string(List ++ "."),
    erl_parse:parse_term(Tokens).