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