Thomas Allen
2008-Jun-19 06:47 UTC
[R] Appending diagnostic information to all lines sent to stdout and stderr
Dear All I'm logging the stdout and stderr of an R program into two separate files (stderr.txt and stdout.txt) using sink() I would like to append extra information such as "date", "memory usage" etc to every line of output that goes to stdout or stderr. For example> cat("hello \n")should give output that looks something like: "hello --- Thu Jun 19 18:39:15 2008 | cel_norm_expm | 389.6 Mb used |" instead of just: "hello" I don't want to replace functions like "print", "cat", "stop" and "warning" with my own special functions. Instead, I want to somehow catch the messages that these functions return and alter them before writing to my logging files. Any code that writes output by making calls to cat etc should be completely isolated from the mechanism that is adding this extra diagnostic information. Is this actually possible? I don't really like the idea of rewriting the "cat" function and creating output for stdout from stderr using "kludges" like:> options(error=function(x=geterrmessage()){cat(x)})Thank you for any help and please ask me to explain my situation in more detail if necessary -- Thomas Allen Department of Biochemistry University of Otago 710 Cumberland street Box 56 Dunedin New Zealand email1: hedbag at gmail.com email2: allth025 at student.otago.ac.nz Only the insane have strength enough to prosper and only those who prosper can truly judge what is sane
Prof Brian Ripley
2008-Jun-19 07:26 UTC
[R] Appending diagnostic information to all lines sent to stdout and stderr
> Is this actually possible?Yes. All R output to stdout/stderr that will get redirected by sink() passes through a connection. You can modify the sources to make a connection which adds info. Whether the information you want is available at that point is another matter -- and for things like the date you could do this by using a pipe() connections and writing a suitable filter. On Thu, 19 Jun 2008, Thomas Allen wrote:> Dear All > > I'm logging the stdout and stderr of an R program into two separate > files (stderr.txt and stdout.txt) using sink() > I would like to append extra information such as "date", "memory > usage" etc to every line of output that goes to stdout or stderr. > > For example > >> cat("hello \n") > > should give output that looks something like: > "hello --- Thu Jun 19 18:39:15 2008 | cel_norm_expm | 389.6 Mb used |" > instead of just: > "hello" > > I don't want to replace functions like "print", "cat", "stop" and > "warning" with my own special functions. > Instead, I want to somehow catch the messages that these functions > return and alter them before writing to my > logging files. > Any code that writes output by making calls to cat etc should be > completely isolated from the mechanism that is adding this extra > diagnostic information. > > Is this actually possible? > I don't really like the idea of rewriting the "cat" function and > creating output for stdout from stderr using "kludges" like: > >> options(error=function(x=geterrmessage()){cat(x)}) > > Thank you for any help and please ask me to explain my situation in > more detail if necessary > > -- > Thomas Allen > Department of Biochemistry > University of Otago > 710 Cumberland street > Box 56 > Dunedin > New Zealand > email1: hedbag at gmail.com > email2: allth025 at student.otago.ac.nz > > Only the insane have strength enough to prosper and only those who > prosper can truly judge what is sane > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595