/*************************************************************************** debug.h - sets up debugging sinks inspired by http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=4v50ta%246a7%40netlab.cs.rpi.edu&rnum=8 Basically, dout(LEVEL) returns clog if the debug level is higher than LEVEL or a dummy ostream if lower The level can be set by dout.setLevel(LEVEL) ------------------- begin : 10-20-2003 copyright : (C) 2003 by Andrew O'Neill,HiRes Grad Student email : oneill@phys.columbia.edu ***************************************************************************/ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef AON_DEBUG_H #define AON_DEBUG_H #include #include #include using namespace std; #ifdef NDEBUG // define this dummy macro #define dout(X) 1 ? (void)0 : (void) cerr #else // create dummy stream which just dumps all output // this is what we use if the DebugLevel is higher than the message importance // this stream sets a failbit and hence won't attempt to process it's input static ostream nullstream(NULL); class teebuf: public std::streambuf { public: teebuf(std::streambuf* sbuf1, std::streambuf* sbuf2): m_sbuf1(sbuf1), m_sbuf2(sbuf2) {} private: int_type overflow(int_type c) { if (m_sbuf1->sputc(c) == traits_type::eof()) return traits_type::eof(); if (m_sbuf2->sputc(c) == traits_type::eof()) return traits_type::eof(); return traits_type::not_eof(c); } std::streambuf* m_sbuf1; std::streambuf* m_sbuf2; }; // describing the debug levels //ALL show all messages // TIMING - timing profile messages // MEM - ultra-detailed memory leak debugging //LINFO msgs that may be in loops //LFUNC function announcements that might be in loops //FUNC function announcement //BLANK same level as INFO - does not write " INFO:" prior to message //INFO general info //RELEASE stuff that you'd want to stick in a log file even after release //WARN somethings wrong, e.g. input. Beeps too! //ERR DISASTER Beeps too! enum DebugLevelType { ALL, MEM, TIMING, LFUNC, LINFO, FUNC, INFO, BLANK, PROGRESS, RELEASE, WARN, ERR, OFF }; class Debug { public: Debug (); ~Debug(); ostream& operator()(const DebugLevelType& Level); static void setLevel(const DebugLevelType& Level); static string printLevel(); // static void LogToFile(const char* LogFileName, const DebugLevelType& Level); private: static DebugLevelType itsLevel; }; static Debug dout; #endif // NDEBUG #endif // AON_DEBUG_H