changeset 47:e487c33c89ae

Fixed exponential history file growth bug.
author samer
date Wed, 25 Mar 2015 12:40:42 +0000
parents 0f96024b657e
children 7117e6ef0445
files .plrc
diffstat 1 files changed, 21 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/.plrc	Wed Mar 25 12:09:47 2015 +0000
+++ b/.plrc	Wed Mar 25 12:40:42 2015 +0000
@@ -1,27 +1,29 @@
 :- op(200,fy,@).
 
+:- dynamic persistent_history_file/1.
 persistent_history(H) :-
-   debug(history,'Will use persistent history in "~s".',[H]),
-   prolog_history(disable),
-   (exists_file(H) -> rl_read_history(H); true),
-   history_sign_on(H),
-   periodic_save_history(H),
-   at_halt(history_sign_off(H)).
+   (  persistent_history_file(H) -> true
+   ;  persistent_history_file(H1) -> throw(persistent_history_mismatch(H1,H))
+   ;  debug(history,'Will use persistent history in "~s".',[H]),
+      prolog_history(disable),
+      (exists_file(H) -> rl_read_history(H); true),
+      assert(persistent_history_file(H)),
+      current_prolog_flag(os_argv,ARGV),
+      atomics_to_string(ARGV," ",Command),
+      history_event('Start: ~s',[Command]),
+      periodic_save_history,
+      at_halt(history_event('Halt',[]))
+   ).
 
-history_sign_on(H) :-
-   current_prolog_flag(os_argv,ARGV),
+history_event(Msg,Args) :-
+   persistent_history_file(H),
    get_time(Now),
-   format_time(atom(Time),'%+',Now),
-   atomics_to_string(ARGV," ",Command),
-   format(atom(Header),'% Start: ~w - ~s\n',[Time,Command]),
-   rl_add_history(Header).
-
-history_sign_off(H) :-
-   get_time(Now),
-   format_time(atom(Time),'%+',Now),
-   atomics_to_string(ARGV," ",Command),
-   format(atom(Header),'% Halt: ~w\n',[Time]),
-   rl_add_history(Header).
+   format_time(string(Time),'%+',Now),
+   format(string(Info),Msg,Args),
+   format(atom(Line),'% ~w | ~s',[Time,Info]),
+   debug(history,'History event: ~s',[Line]),
+   rl_add_history(Line),
+   rl_write_history(H).
 
 periodic_save_history(H) :-
    debug(history,'Saving history to "~s"...',[H]),