Source

mod_report_handler / support / mod_report_handler_callback.erl

Full commit
%%%-------------------------------------------------------------------
%%% 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
     ).