redefine logStream := outputStream_file::create("log.txt"), for many different files

Discussions related to Visual Prolog
mdosnov2016
VIP Member
Posts: 73
Joined: 19 Nov 2016 7:58

redefine logStream := outputStream_file::create("log.txt"), for many different files

Unread post by mdosnov2016 » 2 Mar 2018 21:36

Is it possible to reassign the logStream variable as above, to arbitrary number of many different log files,
in the same VP8 project?
e.g. log.txt, log1.txt, code1.ada, code2.vhdl, etc.

I have the impression that VP8 forbides this. Is it true?
PrologUser

Harrison Pratt
VIP Member
Posts: 323
Joined: 5 Nov 2000 0:01

Re: redefine logStream := outputStream_file::create("log.txt"), for many different files

Unread post by Harrison Pratt » 2 Mar 2018 23:21

The code below works as expected. It would be good practice to keep track of the output streams which need to be closed when you are finished so I put it a rudimentary way of doing that, too.

Code: Select all

class facts     logStream : outputStream := erroneous.     logStreamsToClose : outputStream* := [].   class predicates     testlog : (). clauses     testlog() :-         logStream := outputStream_file::create("File1.txt"),         logStreamsToClose := [logStream | logStreamsToClose],         logStream:write("Some text to stream 1"),           logStream := outputStream_file::create("File2.txt"),         logStreamsToClose := [logStream | logStreamsToClose],         logStream:write("Different text to stream 2"),           foreach OS in logStreamsToClose do             try                 OS:close()             catch Err do                 stdio::write("\nError #", Err, " closing stream")             end try         end foreach.

Post Reply