,register_with_logger/0
,add_handler/2
,delete_handler/0
- ,log_generation/3
+ ,log_generation/4
]).
%% Callbacks
gen_event:delete_handler(?EVENT_MGR_REF, ?HANDLER, Args).
-log_generation(GenID, Dead, Alive) ->
- Event = {generation, GenID, Dead, Alive},
+log_generation(GenID, GenDuration, Dead, Alive) ->
+ Event = {generation, GenID, GenDuration, Dead, Alive},
gen_event:notify(?EVENT_MGR_REF, Event).
ok = validate_makedir(file:make_dir(?PATH_DIR__DATA)),
{ok, LogFile} = file:open(FilePath, write),
- CSVHeaders = ["Generation", "Dead", "Alive"],
+ CSVHeaders = ["Generation", "Duration", "Dead", "Alive"],
ok = do_write(LogFile, list_to_csvline(CSVHeaders)),
{ok, #state{log_file=LogFile}}.
{ok, State}.
-handle_event({generation, GenID, Dead, Alive},
+handle_event({generation, GenID, GenDuration, Dead, Alive},
#state{log_file=LogFile}=State) ->
- Values = [integer_to_list(I) || I <- [GenID, Dead, Alive]],
+ Values = [integer_to_list(GenID)
+ ,float_to_string(GenDuration)
+ ,integer_to_list(Dead)
+ ,integer_to_list(Alive)
+ ],
ok = do_write(LogFile, list_to_csvline(Values)),
{ok, State}.
list_to_csvline(List) ->
string:join(List, ?CSV_DELIMITER).
+
+
+float_to_string(Float) ->
+ io_lib:format("~f", [Float]).
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
,gen_id :: integer()
+ ,gen_began :: erlang:timestamp()
}).
,gen_id=GenID
}=State) ->
+ GenBegan = os:timestamp(),
NewGenID = GenID + 1,
ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
NewState = State#state{replies_pending=NumCells
,gen_id=NewGenID
+ ,gen_began=GenBegan
,num_dead=0
,num_alive=0
},
,state_pairs=StatePairs
,replies_pending=RepliesPending
,gen_id=GenID
+ ,gen_began=GenBegan
,num_cells=NumCells
}=State) ->
),
StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
- ok = life_observer:log_generation(GenID, NewNDead, NewNAlive),
+ GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000,
+
+ ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive),
ok = io:format(
- "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
- [X, Y, NumCells, NewNDead, NewNAlive, GenID]
+ "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n",
+ [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration]
),
ok = do_print_bar(X),
ok = do_print_state_chars(X, StateChars),