<commit_before>%%%----------------------------------------------------------------------
%%% File    : gui.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : Graphical User Interface
%%%
%%% Created : 31 Mar 2010 by <NAME> <<EMAIL>>
%%%
%%% @doc: Graphical User Interface
%%% @end
%%%----------------------------------------------------------------------

-module(gui).

%% UI exports.
-export([start/0]).
%% Log server callback exports.
-export([init/1, terminate/2, handle_event/2]).

-include_lib("wx/include/wx.hrl").
-include("gen.hrl").
-include("gui.hrl").

%%%----------------------------------------------------------------------
%%% UI functions
%%%----------------------------------------------------------------------

%% @spec start() -> 'true'
%% @doc: Start the CED GUI.
-spec start() -> 'true'.

start() ->
    register(?RP_GUI, self()),
    wx:new(),
    %% Start the object reference mapping service.
    ref_start(),
    %% Set initial file load path (used by the module addition dialog).
    ref_add(?FILE_PATH, ""),
    Frame = setupFrame(),
    wxFrame:show(Frame),
    %% Start the log manager.
    log:start(?MODULE, wx:get_env()),
    %% Start the replay server.
    replay_server:start(),
    loop(),
    replay_server:stop(),
    log:stop(),
    ref_stop(),
    wx:destroy(),
    unregister(?RP_GUI).

%%%----------------------------------------------------------------------
%%% Log event handler callback functions
%%%----------------------------------------------------------------------

-spec init(term()) -> {'ok', log:state()}.

%% @doc: Initialize the event handler.
%% Note: The wx environment is set once in this function and is subsequently
%%       used by all callback functions. If any change is to happen to the
%%       environment (e.g. new elements added dynamically), `set_env' will have
%%       to be called again (by manually calling a special update_environment
%%       function for each update?).
init(Env) ->
    wx:set_env(Env),
    {ok, []}.

-spec terminate(term(), log:state()) -> 'ok'.

terminate(_Reason, _State) ->
    ok.

-spec handle_event(log:event(), log:state()) -> {'ok', log:state()}.

handle_event({msg, String}, State) ->
    LogText = ref_lookup(?LOG_TEXT),
    wxTextCtrl:appendText(LogText, String),
    {ok, State};
handle_event({result, {error, analysis, {Mod, Fun, Args}, ErrorStates}},
	     State) ->
    Errors = [error_to_string(Error) || {Error, _State} <- ErrorStates],
    setListItems(?ERROR_LIST, Errors),
    replay_server:register_errors(Mod, Fun, Args, ErrorStates),
    analysis_cleanup(),
    {ok, State};
handle_event({result, _Result}, State) ->
    analysis_cleanup(),
    {ok, State}.

%% To be moved.
error_to_string(assert) ->
    "Assertion violation";
error_to_string(deadlock) ->
    "Deadlock".

%%%----------------------------------------------------------------------
%%% Setup functions
%%%----------------------------------------------------------------------

setupFrame() ->
    Frame = wxFrame:new(wx:null(), ?FRAME, ?APP_STRING),
    ref_add(?FRAME, Frame),<commit_msg>Fixed gui log event handler state<commit_after>%%%----------------------------------------------------------------------
%%% File    : gui.erl
%%% Author  : <NAME> <<EMAIL>>
%%% Description : Graphical User Interface
%%%
%%% Created : 31 Mar 2010 by <NAME> <<EMAIL>>
%%%
%%% @doc: Graphical User Interface
%%% @end
%%%----------------------------------------------------------------------

-module(gui).

%% UI exports.
-export([start/0]).
%% Log server callback exports.
-export([init/1, terminate/2, handle_event/2]).

-include_lib("wx/include/wx.hrl").
-include("gen.hrl").
-include("gui.hrl").

%% Log event handler internal state.
-type state() :: [].

%%%----------------------------------------------------------------------
%%% UI functions
%%%----------------------------------------------------------------------

%% @spec start() -> 'true'
%% @doc: Start the CED GUI.
-spec start() -> 'true'.

start() ->
    register(?RP_GUI, self()),
    wx:new(),
    %% Start the object reference mapping service.
    ref_start(),
    %% Set initial file load path (used by the module addition dialog).
    ref_add(?FILE_PATH, ""),
    Frame = setupFrame(),
    wxFrame:show(Frame),
    %% Start the log manager.
    log:start(?MODULE, wx:get_env()),
    %% Start the replay server.
    replay_server:start(),
    loop(),
    replay_server:stop(),
    log:stop(),
    ref_stop(),
    wx:destroy(),
    unregister(?RP_GUI).

%%%----------------------------------------------------------------------
%%% Log event handler callback functions
%%%----------------------------------------------------------------------

-spec init(term()) -> {'ok', state()}.

%% @doc: Initialize the event handler.
%% Note: The wx environment is set once in this function and is subsequently
%%       used by all callback functions. If any change is to happen to the
%%       environment (e.g. new elements added dynamically), `set_env' will have
%%       to be called again (by manually calling a special update_environment
%%       function for each update?).
init(Env) ->
    wx:set_env(Env),
    {ok, []}.

-spec terminate(term(), state()) -> 'ok'.

terminate(_Reason, _State) ->
    ok.

-spec handle_event(log:event(), state()) -> {'ok', state()}.

handle_event({msg, String}, State) ->
    LogText = ref_lookup(?LOG_TEXT),
    wxTextCtrl:appendText(LogText, String),
    {ok, State};
handle_event({result, {error, analysis, {Mod, Fun, Args}, ErrorStates}},
	     State) ->
    Errors = [error_to_string(Error) || {Error, _State} <- ErrorStates],
    setListItems(?ERROR_LIST, Errors),
    replay_server:register_errors(Mod, Fun, Args, ErrorStates),
    analysis_cleanup(),
    {ok, State};
handle_event({result, _Result}, State) ->
    analysis_cleanup(),
    {ok, State}.

%% To be moved.
error_to_string(assert) ->
    "Assertion violation";
error_to_string(deadlock) ->
    "Deadlock".

%%%----------------------------------------------------------------------
%%% Setup functions
%%%----------------------------------------------------------------------

setupFrame() ->
    Frame = wxFrame:new(wx:null(), ?FRAME, ?APP_STRING),
    ref_add(?FRAME, Frame),
{'fraction_non_alphanumeric': 0.32840379360231475, 'fraction_numerical': 0.003214917215881691, 'mean_word_length': 1.8153846153846154, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, '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': '4570709', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1669, 'n_words': 459}