<commit_before>-module(data_logger).

-behaviour(gen_server).

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

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

-record(state, {port, err_log, raw_log}).

start_link() ->
    gen_server:start_link(?MODULE, [], []).

stop(Ref) ->
    gen_server:cast(Ref, stop).

init(_Args) ->
    {ok, Binary} = application:get_env(?MODULE, binary),
    Executable = code:priv_dir(?MODULE) ++ "/" ++ Binary,
    {ok, Dir} = application:get_env(?MODULE, data_dir),
    {ok, Errlog} = file:open(Dir ++ "/err.log", [write]),
    {ok, Log} = file:open(Dir ++ "/raw.txt", [append]),
    Port = open_port({spawn_executable, Executable}, [{packet, 2}]),
    case erlang:port_info(Port) of
        undefined -> {stop, can_not_open_port};
        _ ->
            State = #state{port = Port,
                           err_log = Errlog,
                           raw_log = Log},
            {ok, State}
    end.

terminate({port_terminated, _Reason}, _State) ->
    ok;
terminate(_Reason, _State = #state{port=Port,<commit_msg>Add trap_exit<commit_after>-module(data_logger).

-behaviour(gen_server).

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

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

-record(state, {port, err_log, raw_log}).

start_link() ->
    gen_server:start_link(?MODULE, [], []).

stop(Ref) ->
    gen_server:cast(Ref, stop).

init(_Args) ->
    process_flag(trap_exit, true),
    {ok, Binary} = application:get_env(?MODULE, binary),
    Executable = code:priv_dir(?MODULE) ++ "/" ++ Binary,
    {ok, Dir} = application:get_env(?MODULE, data_dir),
    {ok, Errlog} = file:open(Dir ++ "/err.log", [write]),
    {ok, Log} = file:open(Dir ++ "/raw.txt", [append]),
    Port = open_port({spawn_executable, Executable}, [{packet, 2}]),
    case erlang:port_info(Port) of
        undefined -> {stop, can_not_open_port};
        _ ->
            State = #state{port = Port,
                           err_log = Errlog,
                           raw_log = Log},
            {ok, State}
    end.

terminate({port_terminated, _Reason}, _State) ->
    ok;
terminate(_Reason, _State = #state{port=Port,
{'fraction_non_alphanumeric': 0.18323925314806774, 'fraction_numerical': 0.007815892314372557, 'mean_word_length': 3.056338028169014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '5406379', 'n_tokens_mistral': 827, 'n_tokens_neox': 808, 'n_words': 146}