00001 /* 00002 This file is part of Kompex SQLite Wrapper. 00003 Copyright (c) 2008-2010 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 { 00028 class _SQLiteWrapperExport Redirection 00029 { 00030 public: 00032 Redirection() 00033 { 00034 pOutputFile = new std::ofstream(); 00035 } 00037 virtual ~Redirection() 00038 { 00039 delete pOutputFile; 00040 } 00041 00042 protected: 00044 std::ofstream *pOutputFile; 00046 std::streambuf *mBuffer; 00047 00048 private: 00050 Redirection(const Redirection& r); 00052 Redirection& operator=(const Redirection& r) {return *this;} 00053 }; 00054 00055 00056 class _SQLiteWrapperExport CerrRedirection : public Redirection 00057 { 00058 public: 00060 CerrRedirection(const std::string &filename) 00061 { 00062 pOutputFile->open(filename.c_str(), std::ios_base::out); 00063 std::streambuf *errbuf = pOutputFile->rdbuf(); 00064 mBuffer = std::cerr.rdbuf(); 00065 std::cerr.rdbuf(errbuf); 00066 }; 00068 virtual ~CerrRedirection() 00069 { 00070 std::cerr.rdbuf(mBuffer); 00071 } 00072 00073 private: 00075 CerrRedirection(const CerrRedirection& cr) {} 00077 CerrRedirection& operator=(const CerrRedirection& cr) {return *this;} 00078 }; 00079 00080 class _SQLiteWrapperExport CoutRedirection : public Redirection 00081 { 00082 public: 00084 CoutRedirection(const std::string &filename) 00085 { 00086 pOutputFile->open(filename.c_str(), std::ios_base::out); 00087 std::streambuf *buf = pOutputFile->rdbuf(); 00088 mBuffer = std::cout.rdbuf(); 00089 std::cout.rdbuf(buf); 00090 }; 00092 virtual ~CoutRedirection() 00093 { 00094 std::cerr.rdbuf(mBuffer); 00095 } 00096 00097 private: 00099 CoutRedirection(const CoutRedirection& cr) {} 00101 CoutRedirection& operator=(const CoutRedirection& cr) {return *this;} 00102 }; 00103 00104 }; 00105 00106 #endif // KompexSQLiteCerrRedirection_H