Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:14

0001 /*
0002  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0003  *
0004  * This file is part of Opticks
0005  * (see https://bitbucket.org/simoncblyth/opticks).
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License"); 
0008  * you may not use this file except in compliance with the License.  
0009  * You may obtain a copy of the License at
0010  *
0011  *   http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software 
0014  * distributed under the License is distributed on an "AS IS" BASIS, 
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0016  * See the License for the specific language governing permissions and 
0017  * limitations under the License.
0018  */
0019 
0020 
0021 // https://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdo
0022 
0023 //  https://stackoverflow.com/questions/5846691/how-to-restore-stdout-after-using-freopen
0024 
0025 #include <iostream>
0026 #include <cassert>
0027 #include <csignal>
0028 #include "S_freopen_redirect.hh"
0029 #include "spath.h"
0030 #include "sdirectory.h"
0031 #include "ssys.h"
0032 
0033 
0034 /**
0035 only the std::cerr appears in output, the rest is directed to the file
0036 **/
0037 
0038 void test_redirect()
0039 {
0040     const char* path = spath::Resolve("$TMP/S_freopen_redirect_test/redirect.log") ;
0041     sdirectory::MakeDirsForFile(path); 
0042     std::cout << "test_redirect writing to " << path << std::endl ;  
0043 
0044     ssys::Dump("before redirect, should appear in output"); 
0045 
0046     {
0047        S_freopen_redirect fr(stdout, path) ; 
0048        ssys::Dump("during redirect, should NOT appear in output, should be written to file : observe only std::cerr in output"); 
0049     }
0050 
0051     ssys::Dump("after redirect, should appear in output"); 
0052 }
0053 
0054 
0055 void test_runpython()
0056 {
0057     //int rc = ssys::run("python -c 'print \"hello\"' " );
0058     int rc = ssys::run("python -c 'import sys ; sys.stderr.write(\"hello stderr\\n\")' " );
0059     //int rc = ssys::run("python -c 'import sys ; sys.stdout.write(\"hello stdout\\n\")' " );
0060     if(rc) std::raise(SIGINT); 
0061     assert( rc == 0 );
0062 }
0063 
0064 
0065 
0066 int main(void)
0067 {
0068     test_runpython(); 
0069     test_redirect(); 
0070 
0071     return 0;
0072 }
0073 
0074 
0075 
0076 /*
0077 
0078 close failed in file object destructor:
0079 sys.excepthook is missing
0080 lost sys.stderr
0081 
0082 ::
0083 
0084     double OContext::launch_redirected_(unsigned entry, unsigned width, unsigned height)
0085     {
0086         assert( m_llogpath ) ; 
0087         S_freopen_redirect sfr(stdout, m_llogpath );
0088         double dt = launch_( entry, width, height ) ;
0089         return dt ;                              
0090     }
0091 
0092 OContext::launch_redirected_ succeeds to write kernel rtPrintf 
0093 logging to file BUT a subsequent same process "system" invokation 
0094 of python has problems indicating that cleanup is not complete
0095 ::
0096 
0097     Traceback (most recent call last):
0098       File "/Users/blyth/opticks/ana/tboolean.py", line 62, in <module>
0099         print ab
0100     IOError: [Errno 9] Bad file descriptor
0101     2017-12-13 15:33:13.436 INFO  [321569] [SSys::run@50] tboolean.py --tag 1 --tagoffset 0 --det tboolean-box --src torch   rc_raw : 256 rc : 1
0102     2017-12-13 15:33:13.436 WARN  [321569] [SSys::run@57] SSys::run FAILED with  cmd tboolean.py --tag 1 --tagoffset 0 --det tboolean-box --src torch  
0103     2017-12-13 15:33:13.436 INFO  [321569] [OpticksAna::run@79] OpticksAna::run anakey tboolean cmdline tboolean.py --tag 1 --tagoffset 0 --det tboolean-box --src torch   rc 1 rcmsg OpticksAna::run non-zero RC from ana script
0104     2017-12-13 15:33:13.436 FATAL [321569] [Opticks::dumpRC@186]  rc 1 rcmsg : OpticksAna::run non-zero RC from ana script
0105     2017-12-13 15:33:13.436 INFO  [321569] [SSys::WaitForInput@151] SSys::WaitForInput OpticksAna::run paused : hit RETURN to continue...
0106 
0107 
0108 This issue is reproduced by this test
0109 
0110 Perhaps dup2 can put humpty back together again
0111   
0112 * https://msdn.microsoft.com/en-us/library/8syseb29.aspx
0113 
0114 */
0115