<commit_before>-module(wsdemo_server_logger).
-behaviour(gen_server).
-define(SERVER, ?MODULE).

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------

-export([start_link/2, start_link/1, stop/0]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------

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

%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link(Host, Port) when is_integer(Port) ->
    HostAndPort = lists:flatten([Host, ":", integer_to_list(Port)]),
    start_link(HostAndPort).

-spec start_link(HostAndPort :: iolist()) -> {ok, pid()}.
start_link(HostAndPort) ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [HostAndPort], []).

stop() ->
    gen_server:call(?SERVER, stop).

%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------

init([HostAndPort]) ->
    erlang:send_after(timer:seconds(1), self(), log_stats),
    {ok, HostAndPort}.

handle_call(stop, _From, State) ->
    {stop, normal, ok, State};
handle_call(_Request, _From, State) ->
    {reply, ok, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(log_stats, HostAndPort) ->
    {ok, RSS} = wsdemo_server_manager:memusage(),
    {ok, Connections} = wsdemo_server_manager:connections(HostAndPort),

    wsdemo_logger:event({server, {rss, RSS}}),
    wsdemo_logger:event({server, {connections, Connections}}),

    erlang:send_after(timer:seconds(1), self(), log_stats),
    {noreply, HostAndPort}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------

<commit_msg>Made it so that the server logger does not crash if it can not log<commit_after>-module(wsdemo_server_logger).
-behaviour(gen_server).
-define(SERVER, ?MODULE).

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------

-export([start_link/2, start_link/1, stop/0]).

%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------

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

%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link(Host, Port) when is_integer(Port) ->
    HostAndPort = lists:flatten([Host, ":", integer_to_list(Port)]),
    start_link(HostAndPort).

-spec start_link(HostAndPort :: iolist()) -> {ok, pid()}.
start_link(HostAndPort) ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [HostAndPort], []).

stop() ->
    gen_server:call(?SERVER, stop).

%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------

init([HostAndPort]) ->
    erlang:send_after(timer:seconds(1), self(), log_stats),
    {ok, HostAndPort}.

handle_call(stop, _From, State) ->
    {stop, normal, ok, State};
handle_call(_Request, _From, State) ->
    {reply, ok, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(log_stats, HostAndPort) ->
    case wsdemo_server_manager:memusage() of
        {ok, RSS} ->
            wsdemo_logger:event({server, {rss, RSS}});
        O ->
            error_logger:warning_msg("Memusage: ~p~n", [O])
    end,
    case wsdemo_server_manager:connections(HostAndPort) of
        {ok, Connections} ->
            wsdemo_logger:event({server, {connections, Connections}});
        O2 ->
            error_logger:warning_msg("Connections: ~p~n", [O2])
    end,

    erlang:send_after(timer:seconds(1), self(), log_stats),
    {noreply, HostAndPort}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------


{'fraction_non_alphanumeric': 0.40170394036208734, 'fraction_numerical': 0.005111821086261981, 'mean_word_length': 1.3621730382293762, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '898714', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1201, 'n_words': 226}