Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 170 lines (147 sloc) 6.203 kb
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
-module(ernie_access_logger).
-behaviour(gen_server).

%% api
-export([start_link/1, start/1, acc/1, err/3, reopen/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-include_lib("ernie.hrl").

-record(lstate, {access_file_name = undefined,
                 access_file = undefined}).

%%====================================================================
%% API
%%====================================================================

start_link(Args) ->
  gen_server:start_link({global, ?MODULE}, ?MODULE, Args, []).

start(Args) ->
  gen_server:start({global, ?MODULE}, ?MODULE, Args, []).

acc(Request) ->
  gen_server:cast({global, ?MODULE}, {acc, Request}).

err(Request, Msg, Args) ->
  gen_server:cast({global, ?MODULE}, {err, Request, Msg, Args}).

reopen() ->
  gen_server:cast({global, ?MODULE}, reopen).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([undefined]) ->
  error_logger:info_msg("~p starting~n", [?MODULE]),
  {ok, #lstate{}};
init([AccessFileName]) ->
  error_logger:info_msg("~p starting~n", [?MODULE]),
  case file:open(AccessFileName, [append]) of
    {ok, AccessFile} ->
      {ok, _T} = timer:apply_interval(10000, ernie_access_logger, reopen, []),
      {ok, #lstate{access_file_name = AccessFileName,
                   access_file = AccessFile}};
    {error, Error} ->
      error_logger:error_msg("Error opening access log ~p: ~p.~n", [AccessFileName, Error]),
      {ok, #lstate{}}
  end.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
  {reply, ok, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast({acc, Request}, State) ->
  case State#lstate.access_file_name of
    undefined -> ok;
    _AccessFilename -> acc(Request, State)
  end,
  {noreply, State};
handle_cast({err, Request, Msg, Args}, State) ->
  case State#lstate.access_file_name of
    undefined -> ok;
    _AccessFilename -> err(Request, Msg, Args, State)
  end,
  {noreply, State};
handle_cast(reopen, State) ->
  case State#lstate.access_file_name of
    undefined ->
      {noreply, State};
    AccessFileName ->
      case file:read_file_info(AccessFileName) of
        {ok, _FileInfo} ->
          {noreply, State};
        {error, enoent} ->
          ok = file:close(State#lstate.access_file),
          {ok, AccessFile} = file:open(AccessFileName, [append]),
          {noreply, State#lstate{access_file = AccessFile}};
        _OtherError ->
          {noreply, #lstate{}}
      end
  end;
handle_cast(_Msg, State) ->
  {noreply, State}.

handle_info(Msg, State) ->
  error_logger:error_msg("Unexpected message: ~p~n", [Msg]),
  {noreply, State}.

terminate(_Reason, _State) -> ok.
code_change(_OldVersion, State, _Extra) -> {ok, State}.

%%====================================================================
%% Internal
%%====================================================================

acc(Request, State) ->
  StatString = stat_string(Request),
  ActionString = action_string(Request),
  Line = io_lib:fwrite("ACC ~s - ~s~n", [StatString, ActionString]),
  file:write(State#lstate.access_file, Line).

err(Request, Msg, Args, State) ->
  StatString = stat_string(Request),
  ActionString = action_string(Request),
  ErrString = io_lib:fwrite(Msg, Args),
  Line = io_lib:fwrite("ERR ~s - ~s : ~s~n", [StatString, ErrString, ActionString]),
  file:write(State#lstate.access_file, Line).

stat_string(Request) ->
  Log = Request#request.log,
  TAccept = time_tuple_to_iso_8601_date(Log#log.taccept),
  D1 = time_difference_in_seconds(Log#log.taccept, Log#log.tprocess),
  D2 = time_difference_in_seconds(Log#log.tprocess, Log#log.tdone),
  Type = Log#log.type,
  HQ = Log#log.hq,
  LQ = Log#log.lq,
  Prio = Request#request.priority,
  Args = [TAccept, D1, D2, HQ, LQ, Type, Prio],
  io_lib:fwrite("[~s] ~f ~f - ~B ~B ~3s ~p", Args).

action_string(Request) ->
  TermAction = binary_to_term(Request#request.action),
  RawAction = lists:flatten(io_lib:fwrite("~1000000000.0.0p", [TermAction])),
  case string:len(RawAction) > 150 of
    true ->
      Action = re:replace(RawAction, "\n", "", [global, {return, list}]),
      [string:sub_string(Action, 1, 150), "..."];
    false ->
      RawAction
  end.

time_tuple_to_iso_8601_date(TimeTuple) ->
  {{YY, MM, DD}, {H, M, S}} = calendar:now_to_local_time(TimeTuple),
  {_MegaSecs, _Secs, MicroSecs} = TimeTuple,
  Args = [YY, MM, DD, H, M, S, MicroSecs],
  io_lib:fwrite("~4B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B.~-6.10.0B", Args).

time_difference_in_seconds(T1, T2) ->
  {_, _, MS1} = T1,
  {_, _, MS2} = T2,
  S1 = calendar:datetime_to_gregorian_seconds(calendar:now_to_local_time(T1)),
  S2 = calendar:datetime_to_gregorian_seconds(calendar:now_to_local_time(T2)),
  F1 = S1 + (MS1 / 1000000),
  F2 = S2 + (MS2 / 1000000),
  F2 - F1.
Something went wrong with that request. Please try again.