-- When developing a program I often find myself writing the -- same diagnostic messages to a log file and to the console -- at once. -- -- It makes for tedious typing, or copying, and frequent -- typing or copying mistakes. -- -- I got sick of it. -- -- So I eventually wrote myself a set of output procedures -- paralleling print, printf, puts, close and open -- -- The syntax is exactly the same -- First, we open a multiple output like this: -- -- sequence out -- out = xopen({1,"LOG"}, "w") -- -- i.e. write to console and to file "LOG" function xopen(sequence out, sequence option) sequence f object fn integer wa -- write or append -- option can only be 'a' (append) or 'w' (write) -- do nothing if it is anything else if length(option) !=1 then return {} -- wrong option, no output end if wa = option[1] if wa != 'a' and wa != 'w' then return {} -- wrong option, no output end if f = {} for i=1 to length(out) by 1 do fn = out[i] if sequence(fn) then f = append(f, open(fn, option)) elsif fn = 1 or fn = 2 then f = append(f, fn) -- else trying to write to input or to a file -- already open, so ignore it end if end for return f end function -- The output produres themselves are similar to print, -- printf, puts, but are called xprint, xprintf, xputs -- (i.e. cross-print, cross-printf, cross-puts) -- -- for instance: -- -- xputs(out, "Hello there\n") global procedure xprint(sequence out, object what) for i=1 to length(out) by 1 do print(out[i], what) end for end procedure -- or: -- -- xprintf(out,"[%02d] %s\n", {i, names[i]}) global procedure xprintf(sequence out, sequence format, object what) for i=1 to length(out) by 1 do printf(out[i], format, what) end for end procedure -- or: -- -- xputs(out, "Hello there\n") global procedure xputs(sequence out, sequence text) for i=1 to length(out) by 1 do puts(out[i], text) end for end procedure -- and finally, don't forget to close the log file(s) -- using xclose(out) global procedure xclose(sequence out) for i = 1 to length(out) do if out[i]>2 then close(out[i]) end if end for end procedure -- And now for a full example -- (comment it out or delete it once done with testing it) sequence log, msg msg = "Hello there" log = xopen({1,"HELLO"}, "a") -- write to console and append to file "HELLO" xprint(log,msg) xputs (log,"\n\n"&msg&"\n") xprintf(log,"\n%3d %s has %d letters\n", {22,msg, length(msg)}) xclose(log)