Back to home page

EIC code displayed by LXR

 
 

    


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

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 /**
0022 SDirect
0023 ========
0024 
0025 Stream redirection experiments.
0026 
0027 
0028 TODO: replace all use of SDirect.hh with sdirect.h 
0029 
0030 
0031 **/
0032 
0033 
0034 // http://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string
0035 
0036 #include <iostream>
0037 #include <iomanip>
0038 #include <streambuf>
0039 #include <sstream>
0040 #include <string>
0041 
0042 struct cout_redirect {
0043     cout_redirect( std::streambuf * new_buffer ) 
0044         : old( std::cout.rdbuf( new_buffer ) ) 
0045     { } 
0046 
0047     ~cout_redirect( ) { 
0048         std::cout.rdbuf( old );
0049     }   
0050 
0051 private:
0052     std::streambuf * old;
0053 };
0054 
0055 
0056 struct cerr_redirect {
0057     cerr_redirect( std::streambuf * new_buffer ) 
0058         : old( std::cerr.rdbuf( new_buffer ) ) 
0059     { } 
0060 
0061     ~cerr_redirect( ) { 
0062         std::cerr.rdbuf( old );
0063     }   
0064 
0065 private:
0066     std::streambuf * old;
0067 };
0068 
0069 
0070 
0071 struct stream_redirect
0072 {
0073     // http://wordaligned.org/articles/cpp-streambufs
0074     stream_redirect(std::ostream & dst, std::ostream & src)
0075         :   
0076         src(src), 
0077         sbuf(src.rdbuf(dst.rdbuf())) 
0078     {   
0079     }   
0080 
0081     ~stream_redirect() 
0082     { 
0083        src.rdbuf(sbuf); 
0084     }
0085 
0086 private:
0087     std::ostream & src;
0088     std::streambuf * const sbuf;
0089 };
0090 
0091 
0092 inline std::string OutputMessage(const char* msg, const std::string& out, const std::string& err, bool verbose )  // static
0093 {
0094     std::stringstream ss ;
0095 
0096     ss << std::left << std::setw(30) << msg << std::right
0097        << " yielded chars : "
0098        << " cout " << std::setw(6) << out.size()
0099        << " cerr " << std::setw(6) << err.size()
0100        << " : set VERBOSE to see them "
0101        << std::endl
0102        ;   
0103 
0104     if(verbose)
0105     {   
0106         ss << "cout[" << std::endl << out << "]" << std::endl  ;
0107         ss << "cerr[" << std::endl << err << "]" << std::endl  ;
0108     }   
0109     std::string s = ss.str();
0110     return s ; 
0111 }
0112 
0113 
0114 
0115 
0116 
0117 
0118