00001 /* 00002 This file is part of Kompex SQLite Wrapper. 00003 Copyright (c) 2008-2012 Sven Broeske 00004 00005 Kompex SQLite Wrapper is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU Lesser General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Kompex SQLite Wrapper is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public License 00016 along with Kompex SQLite Wrapper. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #ifndef KompexSQLiteCerrRedirection_H 00020 #define KompexSQLiteCerrRedirection_H 00021 00022 #include <fstream> 00023 #include <iostream> 00024 #include "KompexSQLitePrerequisites.h" 00025 00026 namespace Kompex 00027 { 00029 class _SQLiteWrapperExport Redirection 00030 { 00031 public: 00033 Redirection() 00034 { 00035 pOutputFile = new std::ofstream(); 00036 } 00038 virtual ~Redirection() 00039 { 00040 delete pOutputFile; 00041 } 00042 00043 protected: 00045 std::ofstream *pOutputFile; 00047 std::streambuf *mBuffer; 00048 00049 private: 00051 Redirection(const Redirection& r); 00053 Redirection& operator=(const Redirection& r) {return *this;} 00054 }; 00055 00057 class _SQLiteWrapperExport CerrRedirection : public Redirection 00058 { 00059 public: 00061 CerrRedirection(const std::string &filename) 00062 { 00063 pOutputFile->open(filename.c_str(), std::ios_base::out); 00064 std::streambuf *errbuf = pOutputFile->rdbuf(); 00065 mBuffer = std::cerr.rdbuf(); 00066 std::cerr.rdbuf(errbuf); 00067 }; 00069 virtual ~CerrRedirection() 00070 { 00071 std::cerr.rdbuf(mBuffer); 00072 } 00073 00074 private: 00076 CerrRedirection(const CerrRedirection& cr) {} 00078 CerrRedirection& operator=(const CerrRedirection& cr) {return *this;} 00079 }; 00080 00082 class _SQLiteWrapperExport CoutRedirection : public Redirection 00083 { 00084 public: 00086 CoutRedirection(const std::string &filename) 00087 { 00088 pOutputFile->open(filename.c_str(), std::ios_base::out); 00089 std::streambuf *buf = pOutputFile->rdbuf(); 00090 mBuffer = std::cout.rdbuf(); 00091 std::cout.rdbuf(buf); 00092 }; 00094 virtual ~CoutRedirection() 00095 { 00096 std::cerr.rdbuf(mBuffer); 00097 } 00098 00099 private: 00101 CoutRedirection(const CoutRedirection& cr) {} 00103 CoutRedirection& operator=(const CoutRedirection& cr) {return *this;} 00104 }; 00105 00106 }; 00107 00108 #endif // KompexSQLiteCerrRedirection_H