Changeset 3369 in Sophya


Ignore:
Timestamp:
Oct 31, 2007, 11:47:58 PM (18 years ago)
Author:
ansari
Message:

Ajout methodes redirect stdout/err to PIConsole a travers un fichier, Reza 31/10/2007

Location:
trunk/SophyaPI/PI
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaPI/PI/piapplgen.cc

    r3216 r3369  
    124124//      vers l'objet PIConsole "cons". si "cons==NULL", redirection
    125125//       sur le flot original.
     126//
     127// void  RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos=false, unsigned char va= PIVA_Def)
     128//      Redirige le flot de sortie ("stdout" sous Unix) vers l'objet
     129//      PIConsole "cons", a travers le fichier "flnm".
     130//      Si "cons==NULL", redirection sur le flot original.
     131//      Si "fgcpos==true", recopie sur le flot original, en plus de la redirection.
     132//
     133// void  RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos=false, unsigned char va= PIVA_Def)
     134//      Redirige le flot des messages d'erreur ("stderr" sous Unix)
     135//      vers l'objet PIConsole "cons", a travers le fichier "flnm". 
     136//      Si "cons==NULL", redirection sur le flot original.
     137//      Si "fgcpos==true", recopie sur le flot original, en plus de la redirection.
    126138//--
    127139
  • trunk/SophyaPI/PI/piapplgen.h

    r2607 r3369  
    3838        virtual void              RedirectErrStream(PIConsole* cons, unsigned char va= PIVA_Red) = 0;
    3939
     40        virtual void              RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos=false,
     41                                                    unsigned char va= PIVA_Def)  = 0;
     42        virtual void              RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos=false,
     43                                                    unsigned char va= PIVA_Def)  = 0;
     44
    4045protected:
    4146    PIContainer               *topcont;
  • trunk/SophyaPI/PI/piapplx.cc

    r3216 r3369  
    1717#include <unistd.h>
    1818#include <fcntl.h>
     19#include <sys/types.h>
     20#include <sys/stat.h>
    1921
    2022#include <iostream>
     
    325327static XtInputId inputid[2];
    326328static int origfiledes[2]={-1, -1};   // descripteurs de fichiers de depart
    327 
     329static bool fgcopieos[2]={false, false};    // Si true, recopie sur descripteurs de depart, en plus de redirection
     330static int  readfiledes[2]={-1, -1};   // descripteurs de fichiers ouvert en lecture , si redirection a travers fichier
     331static XtIntervalId intervid[2];
     332static unsigned long tm_interv = 40;   // intervalle timer = 40 ms
     333
     334//DBG static FILE * dbgfip = NULL;
     335
     336// call-back pour gestion de redirection stdout/stderr avec pipe
    328337static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/)
    329338{
    330339char buff[512];
    331 int nr;
     340int nr, tnr;
    332341
    333342int idx = *((int*)cld);
    334343if (idx != 1) idx = 0;
    335344if (!consstream[idx]) return;
    336 while ( (nr=read(*fd, buff, 512)) > 0 ) {
     345tnr = 0;
     346while ( (nr=read(*fd, buff, 511)) > 0 ) {
    337347  buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
    338   }
    339 consstream[idx]->Refresh();
     348  tnr += nr;
     349  }
     350if (tnr > 0) consstream[idx]->Refresh();
     351}
     352
     353// call-back pour gestion de redirection stdout/stderr avec pipe
     354static void redirectstream_timeout_callback(XtPointer cld, XtIntervalId * /*tid*/)
     355{
     356char buff[512];
     357int nr, tnr;
     358
     359int idx = *((int*)cld);
     360if (idx != 1) idx = 0;
     361if (!consstream[idx]) return;
     362tnr = 0;
     363while ( (nr=read(readfiledes[idx], buff, 511)) > 0 ) {
     364  buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false);
     365  tnr += nr;
     366  if (fgcopieos[idx]) write(origfiledes[idx], buff, nr);   // recopie sur filedesc original
     367  }
     368if (tnr > 0) consstream[idx]->Refresh();
     369int szx, szy, szf;
     370XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
     371intervid[idx] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback, (XtPointer)(streamno+idx));
    340372}
    341373
     
    343375void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va)
    344376{
    345 if ( origfiledes[0]<0 )  origfiledes[0] = fcntl(1, F_DUPFD);     
    346 if ( cons == consstream[0]) return;
     377if ( origfiledes[0]<0 )  origfiledes[0] = fcntl(1, F_DUPFD, 0);     
     378if ( cons == consstream[0]) {
     379  streamva[0] = va;
     380  return;
     381}
    347382if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; }
    348383else if (!cons) {
     
    372407inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask,
    373408                           redirectstream_callback, (XtPointer) streamno);
     409//DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w");
    374410}
    375411
     
    377413void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va)
    378414{
    379 if ( origfiledes[1]<0 )  origfiledes[1] = fcntl(2, F_DUPFD);     
    380 if ( cons == consstream[1]) return;
     415if ( origfiledes[1]<0 )  origfiledes[1] = fcntl(2, F_DUPFD, 0);     
     416if ( cons == consstream[1]) {
     417  streamva[1] = va;
     418  return;
     419}
    381420if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; }
    382421else if (!cons) {
     
    408447}
    409448
     449/* --Methode-- */
     450void PIApplicationX::RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va)
     451{
     452if (origfiledes[0] < 0)  {
     453  origfiledes[0] = dup(1);
     454  if (origfiledes[0] < 0) { perror("RedirectOutStream()/ERROR ofd[0]<0 "); return; }
     455}
     456if (readfiledes[0] > -1)  { close(readfiledes[0]); readfiledes[0] = -1;}
     457if (!cons) { // requete d'annulation de redirection
     458  if (origfiledes[0] > -1) dup2(origfiledes[0], 1);
     459  XtRemoveTimeOut(intervid[0]);
     460  consstream[0] = NULL; 
     461  return;
     462  }
     463
     464int fidso =  open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR);
     465if (fidso < 0) {
     466  printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str());
     467  return;
     468}
     469int rdfid =  open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0);
     470if (rdfid < 0) {
     471  printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str());
     472  return;
     473}
     474readfiledes[0] = rdfid;
     475dup2(fidso, 1);   // close(1=stdout), recopie fidso en fd=1
     476close(fidso);    // on ferme le fidso (on l'a recopie en fd=1)
     477consstream[0] = cons; streamva[0] = va;  fgcopieos[0] = fgcpos;
     478
     479#if (!defined(__GNUG__) && !defined(HPUX))
     480setlinebuf(stdout);
     481#endif
     482ios::sync_with_stdio();
     483
     484int szx, szy, szf;
     485XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
     486intervid[0] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback,
     487                              (XtPointer)(streamno+0));
     488
     489//DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w");
     490}
     491
     492/* --Methode-- */
     493void PIApplicationX::RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va)
     494{
     495if (origfiledes[1] < 0)  {
     496  origfiledes[1] = dup(2);
     497  if (origfiledes[1] < 0) { perror("RedirectOutStream()/ERROR ofd[1]<0 "); return; }
     498}
     499if (readfiledes[1] > -1)  { close(readfiledes[1]); readfiledes[1] = -1;}
     500if (!cons) { // requete d'annulation de redirection
     501  if (origfiledes[1] > -1) dup2(origfiledes[1], 1);
     502  XtRemoveTimeOut(intervid[1]);
     503  consstream[1] = NULL; 
     504  return;
     505  }
     506
     507int fidso =  open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR);
     508if (fidso < 0) {
     509  printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str());
     510  return;
     511}
     512int rdfid =  open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0);
     513if (rdfid < 0) {
     514  printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str());
     515  return;
     516}
     517readfiledes[1] = rdfid;
     518dup2(fidso, 2);   // close(2=stderr), recopie fidso en fd=2
     519close(fidso);    // on ferme le fidso (on l'a recopie en fd=2)
     520consstream[1] = cons; streamva[1] = va;  fgcopieos[1] = fgcpos;
     521
     522#if (!defined(__GNUG__) && !defined(HPUX))
     523setlinebuf(stderr);
     524#endif
     525ios::sync_with_stdio();
     526
     527int szx, szy, szf;
     528XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
     529intervid[1] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback,
     530                              (XtPointer)(streamno+1));
     531}
     532
  • trunk/SophyaPI/PI/piapplx.h

    r2607 r3369  
    2626  virtual void              RedirectOutStream(PIConsole* cons, unsigned char va= PIVA_Def);
    2727  virtual void              RedirectErrStream(PIConsole* cons, unsigned char va= PIVA_Red);
     28  virtual void              RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos=false,
     29                                              unsigned char va= PIVA_Def);
     30  virtual void              RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos=false,
     31                                              unsigned char va= PIVA_Def);
    2832
    2933  PIContainer*              MBCont() { return intcont; }
Note: See TracChangeset for help on using the changeset viewer.