Skip to content
This repository
Browse code

Reformat configs to conform with Erlang Emacs mode + correct ISO shor…

…tcuts for Turkish/Polish translations + move z_logger to webzmachine + add config options for changes in webzmachine
  • Loading branch information...
commit 5bd1e0c3ede175a87be47a03f087a28961b05050 1 parent 3ba6028
amiramix authored August 27, 2012
1  deps/z_logger/Emakefile
... ...
@@ -1 +0,0 @@
1  
-{"src/*", [debug_info, warn_unused_vars, warn_unused_import, {outdir, "ebin"}]}.
6  deps/z_logger/Makefile
... ...
@@ -1,6 +0,0 @@
1  
-all:
2  
-	@test -d ebin || mkdir ebin
3  
-	@erl -make
4  
-
5  
-clean:
6  
-	@rm -rf ebin/* erl_crash.dump
300  deps/z_logger/src/z_logger.erl
... ...
@@ -1,300 +0,0 @@
1  
-%% @author Atilla Erdodi <atilla@maximonster.com>
2  
-%% @copyright 2010 Maximonster Interactive Things
3  
-%% @doc Lightweight logging framework (for Zotonic)
4  
-%%      Several logger processes are expected to run at the same time 
5  
-%%      so fine-grained overload and flush protection can be implemented.
6  
-%%      In a basic configuration, there should be one for each WM request,
7  
-%%      one for the Zotonic core and optionally one for 'z_tracer' messages.
8  
-%%      Note, this module should be used to log lower level events.
9  
-%%      High level log messages (e.g. events by Zotonic modules) should be
10  
-%%      handled by 'mod_logging' and logged into the database.
11  
-%%      General log messages, such as those emitted by this module
12  
-%%      should be logged by the standard error_logger (todo).
13  
-
14  
-%% Copyright 2010 Maximonster Interactive Things
15  
-%%
16  
-%% Licensed under the Apache License, Version 2.0 (the "License");
17  
-%% you may not use this file except in compliance with the License.
18  
-%% You may obtain a copy of the License at
19  
-%% 
20  
-%%     http://www.apache.org/licenses/LICENSE-2.0
21  
-%% 
22  
-%% Unless required by applicable law or agreed to in writing, software
23  
-%% distributed under the License is distributed on an "AS IS" BASIS,
24  
-%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  
-%% See the License for the specific language governing permissions and
26  
-%% limitations under the License.
27  
-
28  
--module(z_logger).
29  
-
30  
--behaviour(gen_server).
31  
-
32  
--export([start/1, stop/2, flush_and_stop/1, drop_and_stop/1, log/3, log/4, set_loglevel/2]).
33  
--export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2, code_change/3]).
34  
-
35  
--record(state, {output, loglevel, log_timestamp, format, eagerness, buffer = [], wm_mref}).
36  
-
37  
-%% Configuration defaults
38  
--define(DEFAULT_LOGLEVEL, 0).
39  
--define(DEFAULT_LOGTS, false).
40  
--define(DEFAULT_FORMAT, text).
41  
--define(DEFAULT_POLICY, immediate).
42  
--define(DEFAULT_OUTPUT, {file, "zotonic_" ++ pid_to_list(self()) ++ ".log"}).
43  
-
44  
-%% Timestamp format string - obviously only applicable when format == text
45  
--define(TS_FORMATSTR, "~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B ").
46  
-
47  
-%% The limit of the buffer size after it should be flushed
48  
--define(BUFFER_SIZE_N, 8). %% TODO: shouldn't be hardcoded!
49  
-
50  
-%% Overload protection settings
51  
--define(MEM_SOFTLIMIT,  268435456). %% TODO: shouldn't be hardcoded!
52  
--define(MEM_HARDLIMIT, 1073741824). %% TODO: shouldn't be hardcoded!
53  
--define(MEM_SOFTL_CHKINTERVAL, 1000).
54  
--define(MEM_HARDL_CHKINTERVAL, 5000).
55  
-
56  
-
57  
-%% 
58  
-%% API functions
59  
-%%
60  
-
61  
-%% @doc Spawns a new logger process
62  
-start(Args) ->
63  
-    Args1 = [{wm_pid, self()} | Args],
64  
-    gen_server:start(?MODULE, Args1, []).
65  
-
66  
-%% @doc Stops a logger process.
67  
-stop(LoggerProc, PolicyFunParam) ->
68  
-    gen_server:call(LoggerProc, {stop, PolicyFunParam}).
69  
-
70  
-flush_and_stop(LoggerProc) ->
71  
-    gen_server:call(LoggerProc, flush_and_stop).
72  
-
73  
-drop_and_stop(LoggerProc) ->
74  
-    gen_server:call(LoggerProc, drop_and_stop).
75  
-
76  
-log(LoggerProc, LogLevel, TextOrBin) ->
77  
-    gen_server:cast(LoggerProc, {log, LogLevel, TextOrBin, []}).
78  
-
79  
-log(LoggerProc, LogLevel, Text, Data) ->
80  
-    gen_server:cast(LoggerProc, {log, LogLevel, Text, Data}).
81  
-
82  
-set_loglevel(LoggerProc, LogLevel) ->
83  
-    gen_server:cast(LoggerProc, {set_loglevel, LogLevel}).
84  
-
85  
-%%
86  
-%% gen_server callbacks
87  
-%%
88  
-
89  
-init(Args) ->
90  
-    [LogLevel, LogTimestamp, Format, Eagerness, Output] =
91  
-        [proplists:get_value(Key, Args, Default) 
92  
-         || {Key, Default} <- [{loglevel, ?DEFAULT_LOGLEVEL}, 
93  
-                               {log_timestamp, ?DEFAULT_LOGTS},
94  
-                               {format, ?DEFAULT_FORMAT}, 
95  
-                               {eagerness, ?DEFAULT_POLICY},
96  
-                               {output, ?DEFAULT_OUTPUT}]],
97  
-    
98  
-    Output2 = case Eagerness of
99  
-                  {at_once, _PolicyFun} ->
100  
-                      Output; % do not open the file until it needed!
101  
-                  _ ->
102  
-                      open(Output)
103  
-              end,
104  
-              
105  
-    timer:send_interval(?MEM_SOFTL_CHKINTERVAL, check_softlimit),
106  
-    timer:send_interval(?MEM_HARDL_CHKINTERVAL, check_hardlimit),
107  
-
108  
-    {wm_pid, WMPid} = proplists:lookup(wm_pid, Args),
109  
-    WMRef = erlang:monitor(process, WMPid),
110  
-
111  
-    {ok, #state{loglevel=LogLevel, log_timestamp=LogTimestamp,
112  
-                format=Format, eagerness=Eagerness, output=Output2,
113  
-                wm_mref=WMRef}}.
114  
-
115  
-terminate(_Reason, _State) ->
116  
-    ok.
117  
-
118  
-handle_call({stop, PolicyFunParam}, _From, State=#state{eagerness=Eagerness}) ->
119  
-    WhatToDo = case Eagerness of
120  
-                   {at_once, PolicyFun} ->
121  
-                       PolicyFun(PolicyFunParam);
122  
-                   _ -> flush
123  
-               end,
124  
-    case  WhatToDo of
125  
-        flush -> do_handle_flush(State);
126  
-        drop -> drop
127  
-    end,    
128  
-    {stop, normal, ok, State};
129  
-handle_call(flush_and_stop, _From, State=#state{}) ->
130  
-    do_handle_flush(State),
131  
-    {stop, normal, ok, State};
132  
-handle_call(drop_and_stop, _From, State=#state{output=Output}) ->
133  
-    close(Output),
134  
-    {stop, normal, ok, State}.    
135  
-
136  
-handle_cast({log, MsgLogLevel, _Text, _Data}, 
137  
-            State=#state{loglevel=LogLevel}) when MsgLogLevel > LogLevel ->
138  
-    {noreply, State};
139  
-handle_cast({log, _MsgLogLevel, Text, Data}, State) ->
140  
-    State2 = do_handle_log(Text, Data, State),
141  
-    {noreply, State2};
142  
-handle_cast({set_loglevel, LogLevel}, State) ->
143  
-    {noreply, State#state{loglevel=LogLevel}}.
144  
-    
145  
-
146  
-%% Overload protection
147  
-handle_info(check_softlimit, State) ->
148  
-    {memory, Mem} = process_info(self(), memory),
149  
-    z_utils:flush_message(check_softlimit), 
150  
-    if 
151  
-        Mem < ?MEM_SOFTLIMIT ->
152  
-            {noreply, State};
153  
-        Mem >= ?MEM_SOFTLIMIT ->
154  
-            % it should be logged properly...
155  
-            io:format("Logger process ~p exceeded its soft memory limit. Cleaning up...\n", [self()]),
156  
-            % flush the process mailbox
157  
-            clear_mbox(),
158  
-            % force garbage collection
159  
-            % (we can't clean up the buffer yet, as the gen_server 
160  
-            %  process still holds a reference to it)
161  
-            garbage_collect(),
162  
-            % clear the buffer, hopefully it will be garbage collected soon
163  
-            {noreply, State#state{buffer=[]}}
164  
-    end;
165  
-handle_info(check_hardlimit, State=#state{output=Output}) ->
166  
-    {memory, Mem} = process_info(self(), memory),
167  
-    z_utils:flush_message(check_hardlimit), 
168  
-    if 
169  
-        Mem < ?MEM_HARDLIMIT ->
170  
-            {noreply, State};
171  
-        Mem >= ?MEM_HARDLIMIT -> %% very unlikely...
172  
-            close(Output),
173  
-            {stop, mem_hardlimit_exceeded, State}
174  
-    end;
175  
-%% TODO: check CPU/IO/etc usage
176  
-
177  
-% Webmachine left without saying goodbye.
178  
-handle_info({'DOWN', Ref, process, _Pid, Reason}, State = #state{wm_mref=Ref}) ->
179  
-    State2 = do_handle_log("Unexpected Webmachine DOWN: ~p~n", [Reason], State),
180  
-    do_handle_flush(State2),
181  
-    {stop, normal, State2}.
182  
-
183  
-code_change(_OldVsn, State, _Extra) -> {ok, State}.
184  
-
185  
-%% 
186  
-%% Internal functions
187  
-%%
188  
-
189  
-do_handle_log(Text, Data, 
190  
-            State=#state{output=Output,
191  
-                         log_timestamp=LogTimestamp,
192  
-                         format=Format, 
193  
-                         eagerness=Eagerness, 
194  
-                         buffer=Buffer}) ->   
195  
-    ToLog = fmt_log(Format, LogTimestamp, {Text, Data}),
196  
-    Buffer2 = do_log(Output, Eagerness, ToLog, Buffer),
197  
-    State#state{buffer=Buffer2}.
198  
-
199  
-do_handle_flush(#state{output=Output, buffer=Buffer}) ->
200  
-    Output2 = open(Output),
201  
-    flush(Output2, Buffer),
202  
-    close(Output2).
203  
-
204  
-
205  
-
206  
-fmt_timestamp(text) ->
207  
-    {{Y, M, D}, {H, Mm, S}} = calendar:local_time(),
208  
-    io_lib:format(?TS_FORMATSTR,
209  
-                  [Y, M, D, H, Mm, S]);
210  
-fmt_timestamp(binary) ->
211  
-    Now = now(),
212  
-    term_to_binary(Now).
213  
-    
214  
-fmt_timestamp(text, Format) ->
215  
-    {{Y, M, D}, {H, Mm, S}} = calendar:local_time(),
216  
-    io_lib:format(Format,
217  
-                  [Y, M, D, H, Mm, S]).
218  
-
219  
-fmt_log(binary, LogTimestamp, Binary) when is_binary(Binary) ->
220  
-    case LogTimestamp of
221  
-        true ->
222  
-            Timestamp = fmt_timestamp(binary),
223  
-            <<Timestamp/binary, Binary/binary>>;
224  
-        false ->
225  
-            Binary
226  
-    end;
227  
-fmt_log(text, LogTimestamp, {Text, Data}) ->
228  
-    case LogTimestamp of
229  
-        true ->
230  
-            Timestamp = fmt_timestamp(text),
231  
-            io_lib:format(Timestamp ++ Text, Data);
232  
-        {true, Format} ->
233  
-            Timestamp = fmt_timestamp(text, Format),
234  
-            io_lib:format(Timestamp ++ Text, Data);
235  
-        false ->
236  
-            io_lib:format(Text, Data)
237  
-    end.
238  
-
239  
-
240  
-do_log(_Output, {at_once, _Fun}, ToLog, Buffer) ->
241  
-    [ToLog | Buffer];
242  
-do_log(_Output, delayed, ToLog, Buffer) when length(Buffer) < ?BUFFER_SIZE_N ->
243  
-    [ToLog | Buffer];
244  
-do_log(Output, delayed, ToLog, Buffer) ->
245  
-    flush(Output, [ToLog | Buffer]),
246  
-    [];
247  
-do_log({file, {io_device, File}}, immediate, ToLog, Buffer) ->
248  
-    ok = file:write(File, ToLog),
249  
-    Buffer;
250  
-do_log({udp, {Socket, Address, Port}}, immediate, ToLog, Buffer) ->
251  
-    gen_udp:send(Socket, Address, Port, ToLog),
252  
-    Buffer.
253  
-
254  
-
255  
-
256  
-open(Output) ->
257  
-    case Output of
258  
-        {file, {io_device, File}} ->
259  
-            {file, {io_device, File}};
260  
-        {file, FileName} ->
261  
-            {ok, File} = file:open(FileName, [write, raw]),
262  
-            {file, {io_device, File}};
263  
-        {udp, {Socket, Address, Port}} ->
264  
-            {udp, {Socket, Address, Port}};
265  
-        {udp, {Address, Port}} ->
266  
-            {ok, Socket} = gen_udp:open(0),
267  
-            {udp, {Socket, Address, Port}}
268  
-    end.
269  
-
270  
-flush(Output, Buffer) ->
271  
-    Buffer2 = lists:reverse(Buffer),
272  
-    [do_log(Output, immediate, ToLog, []) || ToLog <- Buffer2].
273  
-    
274  
-close({file, {io_device, File}}) ->
275  
-    file:close(File);
276  
-close({udp, {_Socket, _, _}}) ->
277  
-    ok; % TODO: the udp connection is shared???
278  
-close(_) ->
279  
-    ok.
280  
-
281  
-%%
282  
-%% Overload protection - Internal functions
283  
-%%
284  
-
285  
-clear_mbox() ->
286  
-    receive _ -> clear_mbox() after 0 -> ok end.
287  
-
288  
-overload_test() ->
289  
-    MessagesAtOnce = 50000,
290  
-    N = 10000,
291  
-    {ok, P} = z_logger:start([{eagerness, {at_once, fun(_) -> drop end}}]),
292  
-    [begin
293  
-         [z_logger:log(P, 0, "text ~p ~p ~n",
294  
-                [atom, I]) || I <- lists:seq(1, MessagesAtOnce)],
295  
-            
296  
-         io:format("[~p bytes]", [element(2, process_info(P, memory))])
297  
-     end
298  
-     || _ <- lists:seq(1, N)].
299  
-
300  
-
4  modules/mod_translation/mod_translation.erl
@@ -67,8 +67,8 @@ init(Context) ->
67 67
                             {fr, [ {language, <<"Français">>}, {is_enabled, true}]},
68 68
                             {de, [ {language, <<"Deutsch">>}, {is_enabled, true}]},
69 69
                             {nl, [ {language, <<"Nederlands">>}, {is_enabled, true}]},
70  
-                            {pl, [ {language, <<"Türkçe">>}, {is_enabled, true}]},
71  
-                            {tr, [ {language, <<"Polski">>}, {is_enabled, true}]}
  70
+                            {tr, [ {language, <<"Türkçe">>}, {is_enabled, true}]},
  71
+                            {pl, [ {language, <<"Polski">>}, {is_enabled, true}]}
72 72
                         ], Context);
73 73
                 _Exists ->
74 74
                     ok
21  priv/erlang.config.in
... ...
@@ -1,12 +1,17 @@
1  
-[
2  
- {lager, [
3  
-    {handlers, [
4  
-      {lager_console_backend, info},
5  
-      {lager_file_backend, [
6  
-        {"priv/sasl/error.log", error, 10485760, "$D0", 5},
7  
-        {"priv/sasl/console.log", info, 10485760, "$D0", 5}
  1
+%% -*- mode: erlang -*-
  2
+[{lager,
  3
+  [{handlers,
  4
+    [{lager_console_backend, info},
  5
+     {lager_file_backend,
  6
+      [{"priv/sasl/error.log", error, 10485760, "$D0", 5},
  7
+       {"priv/sasl/console.log", info, 10485760, "$D0", 5}
8 8
       ]}
9 9
     ]},
10  
-    {crash_log, "priv/sasl/crash.log"}
  10
+   {crash_log, "priv/sasl/crash.log"}
  11
+  ]},
  12
+
  13
+ {webzmachine,
  14
+  [{wmtrace_dir, "priv/wmtrace"},
  15
+   {silent_console, true}
11 16
   ]}
12 17
 ].
11  src/support/z_config.erl
@@ -216,11 +216,12 @@ write_config(Config) ->
216 216
     {ok, Dev} = file:open(config_file(), [write]),
217 217
     try
218 218
         ok = file:write(Dev, <<
219  
-            "% THIS FILE IS AUTOMATICALLY GENERATED, DO NOT CHANGE WHILE ZOTONIC IS RUNNING.",10,
220  
-            "%", 10,
221  
-            "% You can change these options when Zotonic is not running.",10,
222  
-            "% Delete this file to reset Zotonic to its defaults (when Zotonic is not running).",10,
223  
-            "% Consult the config.in file for available options and their defaults.",10,
  219
+            "%% -*- mode: erlang -*-",10,
  220
+            "%% THIS FILE IS AUTOMATICALLY GENERATED, DO NOT CHANGE WHILE ZOTONIC IS RUNNING.",10,
  221
+            "%%", 10,
  222
+            "%% You can change these options when Zotonic is not running.",10,
  223
+            "%% Delete this file to reset Zotonic to its defaults (when Zotonic is not running).",10,
  224
+            "%% Consult the config.in file for available options and their defaults.",10,
224 225
             10>>),
225 226
         Data = io_lib:format("~p.~n", [Config]),
226 227
         ok = file:write(Dev, iolist_to_binary(Data))
9  src/zotonic.app
... ...
@@ -1,12 +1,15 @@
  1
+%% -*- mode: erlang -*-
1 2
 {application, zotonic,
2 3
  [{description, "zotonic"},
3 4
   {vsn, "0.9-dev"},
4  
-  {modules, [
  5
+  {modules,
  6
+   [
5 7
     zotonic,
6 8
     zotonic_app,
7 9
     zotonic_sup,
8  
-    zotonic_deps
9  
-  ]},
  10
+    zotonic_deps,
  11
+    z
  12
+   ]},
10 13
   {registered, []},
11 14
   {mod, {zotonic_app, []}},
12 15
   {env, []},

0 notes on commit 5bd1e0c

Please sign in to comment.
Something went wrong with that request. Please try again.