Changeset 3369 in Sophya
- Timestamp:
- Oct 31, 2007, 11:47:58 PM (18 years ago)
- Location:
- trunk/SophyaPI/PI
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaPI/PI/piapplgen.cc
r3216 r3369 124 124 // vers l'objet PIConsole "cons". si "cons==NULL", redirection 125 125 // 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. 126 138 //-- 127 139 -
trunk/SophyaPI/PI/piapplgen.h
r2607 r3369 38 38 virtual void RedirectErrStream(PIConsole* cons, unsigned char va= PIVA_Red) = 0; 39 39 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 40 45 protected: 41 46 PIContainer *topcont; -
trunk/SophyaPI/PI/piapplx.cc
r3216 r3369 17 17 #include <unistd.h> 18 18 #include <fcntl.h> 19 #include <sys/types.h> 20 #include <sys/stat.h> 19 21 20 22 #include <iostream> … … 325 327 static XtInputId inputid[2]; 326 328 static int origfiledes[2]={-1, -1}; // descripteurs de fichiers de depart 327 329 static bool fgcopieos[2]={false, false}; // Si true, recopie sur descripteurs de depart, en plus de redirection 330 static int readfiledes[2]={-1, -1}; // descripteurs de fichiers ouvert en lecture , si redirection a travers fichier 331 static XtIntervalId intervid[2]; 332 static 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 328 337 static void redirectstream_callback(XtPointer cld, int * fd, XtInputId* /*iid*/) 329 338 { 330 339 char buff[512]; 331 int nr ;340 int nr, tnr; 332 341 333 342 int idx = *((int*)cld); 334 343 if (idx != 1) idx = 0; 335 344 if (!consstream[idx]) return; 336 while ( (nr=read(*fd, buff, 512)) > 0 ) { 345 tnr = 0; 346 while ( (nr=read(*fd, buff, 511)) > 0 ) { 337 347 buff[nr] = '\0'; consstream[idx]->AddStr(buff, streamva[idx], false); 338 } 339 consstream[idx]->Refresh(); 348 tnr += nr; 349 } 350 if (tnr > 0) consstream[idx]->Refresh(); 351 } 352 353 // call-back pour gestion de redirection stdout/stderr avec pipe 354 static void redirectstream_timeout_callback(XtPointer cld, XtIntervalId * /*tid*/) 355 { 356 char buff[512]; 357 int nr, tnr; 358 359 int idx = *((int*)cld); 360 if (idx != 1) idx = 0; 361 if (!consstream[idx]) return; 362 tnr = 0; 363 while ( (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 } 368 if (tnr > 0) consstream[idx]->Refresh(); 369 int szx, szy, szf; 370 XtAppContext * appctx = PIXtAppCtx(szx, szy, szf); 371 intervid[idx] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback, (XtPointer)(streamno+idx)); 340 372 } 341 373 … … 343 375 void PIApplicationX::RedirectOutStream(PIConsole* cons, unsigned char va) 344 376 { 345 if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD); 346 if ( cons == consstream[0]) return; 377 if ( origfiledes[0]<0 ) origfiledes[0] = fcntl(1, F_DUPFD, 0); 378 if ( cons == consstream[0]) { 379 streamva[0] = va; 380 return; 381 } 347 382 if ( (consstream[0]) && (cons) ) { consstream[0] = cons; streamva[0] = va; return; } 348 383 else if (!cons) { … … 372 407 inputid[0] = XtAppAddInput(*appctx, p[0], (XtPointer) XtInputReadMask, 373 408 redirectstream_callback, (XtPointer) streamno); 409 //DBG if (dbgfip == NULL) dbgfip = fopen("debug.log","w"); 374 410 } 375 411 … … 377 413 void PIApplicationX::RedirectErrStream(PIConsole* cons, unsigned char va) 378 414 { 379 if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD); 380 if ( cons == consstream[1]) return; 415 if ( origfiledes[1]<0 ) origfiledes[1] = fcntl(2, F_DUPFD, 0); 416 if ( cons == consstream[1]) { 417 streamva[1] = va; 418 return; 419 } 381 420 if ( (consstream[1]) && (cons) ) { consstream[1] = cons; streamva[1] = va; return; } 382 421 else if (!cons) { … … 408 447 } 409 448 449 /* --Methode-- */ 450 void PIApplicationX::RedirectOutStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va) 451 { 452 if (origfiledes[0] < 0) { 453 origfiledes[0] = dup(1); 454 if (origfiledes[0] < 0) { perror("RedirectOutStream()/ERROR ofd[0]<0 "); return; } 455 } 456 if (readfiledes[0] > -1) { close(readfiledes[0]); readfiledes[0] = -1;} 457 if (!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 464 int fidso = open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR); 465 if (fidso < 0) { 466 printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str()); 467 return; 468 } 469 int rdfid = open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0); 470 if (rdfid < 0) { 471 printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str()); 472 return; 473 } 474 readfiledes[0] = rdfid; 475 dup2(fidso, 1); // close(1=stdout), recopie fidso en fd=1 476 close(fidso); // on ferme le fidso (on l'a recopie en fd=1) 477 consstream[0] = cons; streamva[0] = va; fgcopieos[0] = fgcpos; 478 479 #if (!defined(__GNUG__) && !defined(HPUX)) 480 setlinebuf(stdout); 481 #endif 482 ios::sync_with_stdio(); 483 484 int szx, szy, szf; 485 XtAppContext * appctx = PIXtAppCtx(szx, szy, szf); 486 intervid[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-- */ 493 void PIApplicationX::RedirectErrStream(PIConsole* cons, string const & flnm, bool fgcpos, unsigned char va) 494 { 495 if (origfiledes[1] < 0) { 496 origfiledes[1] = dup(2); 497 if (origfiledes[1] < 0) { perror("RedirectOutStream()/ERROR ofd[1]<0 "); return; } 498 } 499 if (readfiledes[1] > -1) { close(readfiledes[1]); readfiledes[1] = -1;} 500 if (!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 507 int fidso = open(flnm.c_str(), O_CREAT|O_WRONLY|O_NONBLOCK, S_IRUSR|S_IWUSR); 508 if (fidso < 0) { 509 printf("PIApplicationX::RedirectOutStream()/ERROR creating file %s \n", flnm.c_str()); 510 return; 511 } 512 int rdfid = open(flnm.c_str(), O_RDONLY|O_NONBLOCK, 0); 513 if (rdfid < 0) { 514 printf("PIApplicationX::RedirectOutStream()/ERROR opening file %s for read \n", flnm.c_str()); 515 return; 516 } 517 readfiledes[1] = rdfid; 518 dup2(fidso, 2); // close(2=stderr), recopie fidso en fd=2 519 close(fidso); // on ferme le fidso (on l'a recopie en fd=2) 520 consstream[1] = cons; streamva[1] = va; fgcopieos[1] = fgcpos; 521 522 #if (!defined(__GNUG__) && !defined(HPUX)) 523 setlinebuf(stderr); 524 #endif 525 ios::sync_with_stdio(); 526 527 int szx, szy, szf; 528 XtAppContext * appctx = PIXtAppCtx(szx, szy, szf); 529 intervid[1] = XtAppAddTimeOut(*appctx, tm_interv, redirectstream_timeout_callback, 530 (XtPointer)(streamno+1)); 531 } 532 -
trunk/SophyaPI/PI/piapplx.h
r2607 r3369 26 26 virtual void RedirectOutStream(PIConsole* cons, unsigned char va= PIVA_Def); 27 27 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); 28 32 29 33 PIContainer* MBCont() { return intcont; }
Note:
See TracChangeset
for help on using the changeset viewer.