Anonymous avatar Anonymous committed f849271

Initial commit

Comments (0)

Files changed (5)

+glob:*~

mod_report_handler.erl

+%%%-------------------------------------------------------------------
+%%% File    : mod_report_handler.erl
+%%% Author  : Andreas Stenius <andreas.stenius@astekk.se>
+%%% Description : 
+%%%
+%%% Created : 23 Apr 2011 by Andreas Stenius <andreas.stenius@astekk.se>
+%%%-------------------------------------------------------------------
+
+%% Copyright 2011 Andreas Stenius
+%%
+%% 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.
+
+-module(mod_report_handler).
+
+%% API
+-export([
+	 init/1	 
+	]).
+
+%%====================================================================
+%% API
+%%====================================================================
+
+init(Context) ->
+    ok = error_logger:add_report_handler(mod_report_handler_callback, Context).
+
+
+%%====================================================================
+%% Internal functions
+%%====================================================================

support/mod_report_handler_callback.erl

+%%%-------------------------------------------------------------------
+%%% File    : mod_report_handler.erl
+%%% Author  : Andreas Stenius <andreas.stenius@astekk.se>
+%%% Description : 
+%%%
+%%% Created : 23 Apr 2011 by Andreas Stenius <andreas.stenius@astekk.se>
+%%%-------------------------------------------------------------------
+
+%% Copyright 2011 Andreas Stenius
+%%
+%% 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.
+
+-module(mod_report_handler_callback).
+
+%% API
+-export([
+	 init/1,
+	 handle_event/2,
+	 handle_call/2,
+	 handle_info/2,
+	 terminate/2,
+	 code_change/2
+	 %format_status/2 optinal, see doc: http://www.erlang.org/doc/man/gen_event.html
+	]).
+
+-include_lib("zotonic.hrl").
+-record(state, {
+	  context
+	 }).
+
+%%====================================================================
+%% API
+%%====================================================================
+
+init({Args, _Term}) ->
+    init(Args);
+init(Context) ->
+    {ok, #state{ context=Context }}.
+
+%    Generated when error_msg/1,2 or format is called.
+handle_event({error, _Gleader, {__Pid, Format, Data}}, #state{context=Context}=State) ->
+    emit_report("Error Report", erlang:localtime(), {Format, Data}, Context),
+    {ok, State};
+
+%    Generated when error_report/1 is called.
+handle_event({error_report, _Gleader, {__Pid, std_error, Report}}, #state{context=Context}=State) ->
+    emit_report("Error Report", erlang:localtime(), {"~p", [Report]}, Context),
+    {ok, State};
+
+%    Generated when error_report/2 is called.
+handle_event({error_report, _Gleader, {_Pid, Type, Report}}, #state{context=Context}=State) ->
+    emit_report("Error Report", erlang:localtime(), {"~p: ~p", [Type, Report]}, Context),
+    {ok, State};
+
+%    Generated when warning_msg/1,2 is called, provided that warnings are set to be tagged as warnings.
+handle_event({warning_msg, _Gleader, {_Pid, Format, Data}}, #state{context=Context}=State) ->
+    emit_report("Warning Report", erlang:localtime(), {Format, Data}, Context),
+    {ok, State};
+
+%    Generated when warning_report/1 is called, provided that warnings are set to be tagged as warnings.
+handle_event({warning_report, _Gleader, {_Pid, std_warning, Report}}, #state{context=Context}=State) ->
+    emit_report("Warning Report", erlang:localtime(), {"~p", [Report]}, Context),
+    {ok, State};
+
+%    Generated when warning_report/2 is called, provided that warnings are set to be tagged as warnings.
+handle_event({warning_report, _Gleader, {_Pid, Type, Report}}, #state{context=Context}=State) ->
+    emit_report("Warning Report", erlang:localtime(), {"~p: ~p", [Type, Report]}, Context),
+    {ok, State};
+
+%    Generated when info_msg/1,2 is called.
+handle_event({info_msg, _Gleader, {_Pid, Format, Data}}, #state{context=Context}=State) ->
+    emit_report("Info Report", erlang:localtime(), {Format, Data}, Context),
+    {ok, State};
+
+%    Generated when info_report/1 is called.
+handle_event({info_report, _Gleader, {_Pid, std_info, Report}}, #state{context=Context}=State) ->
+    emit_report("Info Report", erlang:localtime(), {"~p", [Report]}, Context),
+    {ok, State};
+
+%    Generated when info_report/2 is called.
+handle_event({info_report, _Gleader, {_Pid, Type, Report}}, #state{context=Context}=State) ->    
+    emit_report("Info Report", erlang:localtime(), {"~p: ~p", [Type, Report]}, Context),
+    {ok, State};
+
+handle_event(_evt, State) ->
+    {ok, State}.
+
+handle_call(_req, State) ->
+    {ok, State}.
+
+handle_info(_info, State) ->
+    {ok, State}.
+
+terminate(_arg, _state) ->
+    ok.
+
+code_change(_OldVsn, State) ->
+    {ok, State}.
+
+
+% optional
+%format_status(_Opt, [_PDict, _State]) ->
+%    ok.
+
+
+%%====================================================================
+%% Internal functions
+%%====================================================================
+
+
+emit_report(Caption, Timestamp, {Format, Args}, Context) ->
+    emit_report(Caption, Timestamp, io_lib:format(Format, Args), Context);
+emit_report(Caption, Timestamp, Message, Context) ->
+%    io:format("~n~p> ~p ~p ~p~n", [?MODULE, Caption, Timestamp, Message]),
+    mod_signal:emit(
+      {error_report, 
+       [
+	{caption, Caption}, 
+	{timestamp, Timestamp},
+	{message, Message}
+       ]},
+      Context
+     ).
+

templates/_error_report.tpl

+{% with m.signal[signal] as s %}
+<tr>
+	<td>{{ s.timestamp|date:"Y-m-d H:i:s" }}</td>
+	<td>{{ s.caption }}</td>
+	<td>{{ s.message }}</td>
+	{#<td>{% print s %}</td>#}
+</tr>
+{% endwith %}

templates/_error_reports.tpl

+<table id="error_reports" style="width: 100%">
+    <tr>
+    	<th style="width: 15%" align="left">{_ Date _}</th>
+	<th style="width: 15%" align="left">{_ Caption _}</th>
+	<th style="width: 70%" align="left">{_ Message _}</th>
+    </tr>
+</table>
+
+{% wire action={connect signal={error_report} action={insert_bottom target="error_reports" template="_error_report.tpl"}} %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.