Back to home page

EIC code displayed by LXR

 
 

    


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

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 #include "SDirect.hh"
0022 #include <iomanip>
0023 #include <iostream>
0024 #include <fstream>
0025 #include <sstream>
0026 #include <vector>
0027 #include <string>
0028 
0029 #include "SSys.hh"
0030 #include "OPTICKS_LOG.hh"
0031 
0032 
0033 void test_cout_cerr_redirect(const char* msg)
0034 {
0035     std::stringstream coutbuf;
0036     std::stringstream cerrbuf;
0037     {
0038         cout_redirect out_(coutbuf.rdbuf());
0039         cerr_redirect err_(cerrbuf.rdbuf());
0040         
0041         SSys::Dump(msg); 
0042         
0043         // dtors of the redirect structs reset back to standard cout/cerr streams  
0044     }        
0045 
0046     std::string out = coutbuf.str(); 
0047     std::string err = cerrbuf.str(); 
0048 
0049     LOG(info) << " captured cout " << out.size()  ; 
0050     std::cout << "[" << std::endl << out << "]" << std::endl  ; 
0051 
0052     LOG(info) << " captured cerr " << err.size() ; 
0053     std::cout << "[" << std::endl << err << "]" << std::endl  ; 
0054 
0055     SSys::Dump(msg); 
0056 }
0057 
0058 
0059 void method_expecting_to_write_to_file( std::ofstream& fp, std::vector<std::string>& msgv )
0060 {
0061     for(unsigned i=0 ; i < msgv.size() ; i++ )
0062     {
0063         const char* pt = msgv[i].c_str() ;
0064         fp.write( const_cast<char*>(pt) , sizeof(pt)); 
0065     }
0066 } 
0067 
0068 void test_stream_redirect()
0069 {
0070     std::ofstream fp("/dev/null", std::ios::out); 
0071     std::stringstream ss ;          
0072 
0073     stream_redirect rdir(ss,fp); // stream_redirect such that writes to the file instead go to the stringstream 
0074 
0075     std::vector<std::string> msgv ; 
0076     msgv.push_back("hello"); 
0077     msgv.push_back("world"); 
0078  
0079     method_expecting_to_write_to_file(fp, msgv);
0080 
0081     std::cout <<  ss.str() << std::endl ; 
0082 }
0083 
0084 
0085 int main(int argc, char** argv)
0086 {
0087     OPTICKS_LOG(argc, argv);
0088 
0089 
0090     LOG(info) << argv[0] ; 
0091 
0092     SSys::Dump(argv[0]); 
0093 
0094     //test_cout_cerr_redirect(argv[0]); 
0095     test_stream_redirect(); 
0096 
0097 
0098     return 0 ; 
0099 }
0100