Back to home page

EIC code displayed by LXR

 
 

    


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

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 // https://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdo
0021 
0022 /**
0023 S_freopen_redirect
0024 ===================
0025 
0026 struct for file descriptor gymnastics. 
0027 
0028 Used from :doc:`/optixrap/OContext` to redirect OptiX kernel debug stdout to a file.
0029 
0030 **/
0031 
0032 
0033 #include <cstdio>
0034 #include <cstring>
0035 #include <unistd.h>
0036 
0037 struct S_freopen_redirect
0038 {
0039     FILE*       _old    ;
0040     int         _old_fd ; 
0041     const char* _path    ; 
0042 
0043     S_freopen_redirect( FILE* curr, const char* path )
0044         :
0045         _old(curr),
0046         _old_fd(dup(fileno(curr))),
0047         _path(strdup(path))
0048     {
0049         freopen( _path, "w", curr );
0050     }
0051 
0052     ~S_freopen_redirect()
0053     {
0054         fclose(_old);
0055         FILE *fp2 = fdopen(_old_fd, "w");
0056         *_old = *fp2;  // Unreliable!
0057     }                      
0058 };
0059