Skip to content

Commit 5cb4de3

Browse files
author
Gunter Kniesel
committed
ADD: record_stout_in_file/4 and .../2 for duplicating textual output to a file (typically for logging everything that appears on the console).
1 parent 41685b6 commit 5cb4de3

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

prolog.library/pl/files.pl

+42-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
with_output_to_file/2, % (File,Goal) Mode = write
1919
with_output_to_file/3, % (File,Mode,Goal) Encoding = utf8
2020
with_output_to_file/4, % (File,Mode,Goal,Encoding)
21+
record_stout_in_file/4, % (+FullFilePath,+OutPreGoal,+Goal,+OutPostGoal)
22+
record_stout_in_file/2, % (+FullFilePath,+Goal)
23+
2124
with_output_to_folder/2, % (+Folder,+Call) FileName=Functor+Time
2225
with_output_to_folder/3, % (+Folder,-FileName,+Call) FileName=Functor+Time
2326
report_to_file_ctc/1, % (CallLiteral) As above but in CTC_HOME directory
@@ -221,11 +224,33 @@
221224
close(Stream) % cleanup
222225
).
223226

224-
% -- OBSOLETE, use with_output_to_file/3 instead:
225227
:- module_transparent with_output_to_file/2, export_goal_output/2.
226228
with_output_to_file(File,Goal) :- with_output_to_file(File,write,Goal).
227229
export_goal_output(File,Goal) :- with_output_to_file(File,write,Goal).
228230

231+
232+
233+
% record_stout_in_file(+FullFilePath,+Goal)
234+
%
235+
% Run a Goal that writes to stout or other Stream and
236+
% additionally record the output in File. File is an
237+
% absolute file path.
238+
:- meta_predicate(record_stout_in_file(+,0)).
239+
record_stout_in_file(File,OutGoal) :-
240+
record_stout_in_file(File,true,true,Goal).
241+
242+
% record_stout_in_file(+FullFilePath,+OutPreGoal,+Goal,+OutPostGoal)
243+
%
244+
%:- public(record_stout_in_file/4). % (File,OutPreGoal,Goal,OutPostGoal)
245+
:- meta_predicate(record_stout_in_file(+,0,0,0)).
246+
record_stout_in_file(File,OutPreGoal,Goal,OutPostGoal) :-
247+
call(OutPreGoal), % <- must write to stdout without other side-effects
248+
ctc_util_files:with_output_to_file(File,append,OutPreGoal), % replicates pre output in File
249+
call(Goal),
250+
ctc_util_files:with_output_to_file(File,append,OutPostGoal), % replicates post output in File
251+
call(OutPostGoal). % <- must write to stdout without other side-effects
252+
253+
229254

230255
% Portable implementation of the above predicate
231256
% (without SWI-specific "with_otput_to"):
@@ -271,20 +296,22 @@
271296
format(atom(TimeStampAtom), '~a.~a.~a ~a:~a:~d', [D,M,Y,H,Mn,Seconds]).
272297

273298
/*
274-
* Determine the absolute path to the root directory of the current
275-
* workspace ASSUNIMG that this file is three levels deeper:
276-
* --> WorkspaceDirPath/ProjectDir/FileDir/thisfile
299+
* Determine the absolute path to the root directory of the PDT
300+
* repository, ASSUMIMG that this file is three levels deeper:
301+
* --> PDTRepoDirPath/prolog.library/pl/files.pl
277302
*/
278-
workspace_root(WorkspaceDirPath) :-
279-
file_search_path('worspace_root', WorkspaceDirPath),
303+
pdt_repository_root(WorkspaceDirPath) :-
304+
file_search_path('pdt_repository_root', WorkspaceDirPath),
280305
!.
281-
workspace_root(WorkspaceDirPath) :-
282-
source_file(workspace_root(_), CurrentFile), % get absolute path of current file
283-
file_directory_name(CurrentFile, FileDirPath), % get path to its containing directory
284-
file_base_name(FileDirPath, FileDir), % get name of containing directory
285-
concat(ProjectDirPath, FileDir, FileDirPath), % get path to its containing project
286-
file_base_name(ProjectDirPath, ProjectDir), % get name of containing project
287-
concat(WorkspaceDirPath, ProjectDir, ProjectDirPath), % get path of its containing worksapace
288-
assert(file_search_path('worspace_root', WorkspaceDirPath)).
289-
306+
pdt_repository_root(WorkspaceDirPath) :-
307+
% get absolute path of current file
308+
source_file(pdt_repository_root(_), CurrentFile),
309+
% get the Workspace directory by removing the
310+
% Filename,FileDirectory and ProjectDirectory
311+
atomic_list_concat(L,'/',CurrentFile),
312+
reverse(L,[_FileName,_FileDir,_ProjDir|WorkspaceDirListRev]),
313+
reverse(WorkspaceDirListRev,WorkspaceDirList),
314+
atomic_list_concat(WorkspaceDirList,'/',WorkspaceDirPath),
315+
% remember it as 'workspace_root' alias
316+
assert(file_search_path('pdt_repository_root', WorkspaceDirPath)).
290317

0 commit comments

Comments
 (0)