[ROOT] redirecting stdout, stderr

From: Frederic Sarazin (sarazin@triumf.ca)
Date: Wed Mar 20 2002 - 02:07:50 MET


Dear Rooters,

in my GUI, I would like to capture the Root messages and print them into

a "read-only" editor-type widget.

I tried to adapt:
http://root.cern.ch/lxr/source/proof/src/TProofServ.cxx#813
to my case so, I wrote the following subroutine:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

void MainWindow::RootDialog()
{
    char *logfile;
    char string[80];
    FILE *fLogFile;
    logfile ="RootLogFile";

    gROOT->ProcessLine(".! rm -f RootLogFile"); // delete old
RootLogFile

    if ((fLogFile=freopen(logfile, "w", stdout)) == 0) // redirect
stdout in logfile (???)
         printf("could not redirect stdout");
    if ((dup2(fileno(stdout), fileno(stderr))) < 0) // stdout and stderr
redirected to the same file.
         printf("could not redirect stderr");

    fprintf(fLogFile,"\n"); // add a new line at the end of the file
    fclose(fLogFile);

    if ((fLogFile = fopen(logfile, "r")) == 0) // open the newly created
file
         printf("could not open logfile");
    else
    {
         fgets(string,80,fLogFile); // get the 1st line
         while (!feof(fLogFile))  // test if EOF
         {
              RootSays->append(string); // write the line in my QTwidget

              fgets(string,80,fLogFile); // read the next line
         }
         fclose(fLogFile);
     }
}

I've got 2 problems that I haven't been able to solve:

1 - whatever I try, I don't get stdout written in my file and thus it is
not printed in my widget.
However, I have nothing also on the window where stdout would have been
printed if stdout
was not redirected... it just seems to be lost somewhere. But Where ???
2 - I've got a copy of stderr in logfile but nothing in my widget.

I tested the widget with a file that I created "by hand", no problem.

Thanks for your help.

Fred



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:46 MET