00001 //---------------------------------------------------------------------------- 00002 /** @file SgDebug.cpp 00003 See SgDebug.h 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #include "SgSystem.h" 00008 #include "SgDebug.h" 00009 00010 #include <fstream> 00011 #include <iostream> 00012 #include <memory> 00013 00014 using namespace std; 00015 00016 //---------------------------------------------------------------------------- 00017 00018 /** Null stream. 00019 This file stream will never be opened and acts as a null stream 00020 for SgDebug(). 00021 */ 00022 static ofstream s_nullStream; 00023 00024 static auto_ptr<ofstream> s_fileStream; 00025 00026 00027 ostream* g_debugStrPtr(&cerr); 00028 00029 std::ostream& SgDebug() 00030 { 00031 if (! g_debugStrPtr->good()) 00032 { 00033 // does not just use a direct SG_ASSERT(g_debugStrPtr->good()) 00034 // in order to allow a breakpoint to be set on the line below. 00035 SG_ASSERT(false); 00036 } 00037 return *g_debugStrPtr; 00038 } 00039 00040 std::ostream& SgWarning() 00041 { 00042 SgDebug() << "WARNING: "; 00043 return SgDebug(); 00044 } 00045 00046 //---------------------------------------------------------------------------- 00047 00048 void SgDebugToWindow() 00049 { 00050 g_debugStrPtr = &cerr; 00051 } 00052 00053 void SgDebugToFile(const char* filename) 00054 { 00055 if (s_fileStream.get() == 0) 00056 s_fileStream.reset(new ofstream(filename, ios::app)); 00057 g_debugStrPtr = s_fileStream.get(); 00058 } 00059 00060 void SgDebugToNull() 00061 { 00062 g_debugStrPtr = &s_nullStream; 00063 } 00064 00065 ostream* SgSwapDebugStr(ostream* newStr) 00066 { 00067 ostream* t = g_debugStrPtr; 00068 g_debugStrPtr = newStr; 00069 return t; 00070 } 00071 00072 //---------------------------------------------------------------------------- 00073 00074 SgDebugToNewFile::SgDebugToNewFile(const char* filename) 00075 : m_old(SgSwapDebugStr(new ofstream(filename, ios::app))) 00076 { 00077 } 00078 00079 SgDebugToNewFile::SgDebugToNewFile() 00080 : m_old(NULL) 00081 { 00082 } 00083 00084 void SgDebugToNewFile::SetFile(const char* filename) 00085 { 00086 m_old = SgSwapDebugStr(new ofstream(filename, ios::app)); 00087 } 00088 00089 SgDebugToNewFile::~SgDebugToNewFile() 00090 { 00091 if (m_old) 00092 { 00093 ostream* t = SgSwapDebugStr(m_old); 00094 delete t; 00095 } 00096 } 00097 00098 //---------------------------------------------------------------------------- 00099 00100 SgDebugToString::SgDebugToString(bool writeToOldDebugStr) 00101 : m_writeToOldDebugStr(writeToOldDebugStr) 00102 { 00103 m_old = SgSwapDebugStr(&m_str); 00104 } 00105 00106 SgDebugToString::~SgDebugToString() 00107 { 00108 if (m_writeToOldDebugStr) 00109 (*m_old) << GetString(); 00110 SgSwapDebugStr(m_old); 00111 } 00112 00113 //---------------------------------------------------------------------------- 00114