Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

debug.h

00001 //
00002 //  The Worldforge Project
00003 //  Copyright (C) 1998,1999  The Worldforge Project
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU Lesser General Public License as 
00007 //  published by the Free Software Foundation; either version 2.1 of the
00008 //  License, or (at your option) any later version.
00009 //
00010 //  This program 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 General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, SA.
00018 //
00019 //  For information about Worldforge and its authors, please contact
00020 //  the Worldforge Web Site at http://www.worldforge.org.
00021 //
00022 
00023 // Originally written by Karsten Laux
00024 // C++ and streaming modifications by Ron Steinke
00025 
00026 #ifndef _WFTK_DEBUG_H_
00027 #define _WFTK_DEBUG_H_
00028 
00029 #ifdef DEBUG
00030 #include <iostream>
00031 #endif
00032 
00033 namespace wftk {
00034     
00046 class Debug {
00047  public:
00048 
00050 
00055   enum {
00056     INVALIDATE = 1 << 16,
00057     GENERIC = 1 << 17,
00058     DRAWING = 1 << 18,
00059     MAINLOOP = 1 << 19,
00060     STARTUP = 1 << 20, // and shutdown
00061     SOUND = 1 << 21,
00062     EVENTS = 1 << 22,
00063     WIDGET_CREATE = 1 << 23, // and destroy
00064     TEXT_WIDGETS = 1 << 24,
00065     FONTS = 1 << 25,
00066     PACKING = 1 << 26,
00067     OPENGL = 1 << 27,
00068     DRAW_TIMING = 1 << 28,
00069     APP_MASK = 0xffff,
00070     LIB_MASK = 0xffff0000
00071   };
00072 
00074   typedef unsigned long Mask;
00075 
00077   static void init(Mask mask)
00078   {
00079 #ifdef DEBUG
00080     if(mask)
00081       std::cerr<<"debugging enabled.("<<mask<<")"<<std::endl;
00082     else if(mask_)
00083       std::cerr<<"debugging disabled."<<std::endl;
00084     mask_ = mask;
00085     out.valid_ = (mask != 0);
00086 #endif
00087   }
00088 
00090   static void addChannels(Mask mask)
00091   {
00092 #ifdef DEBUG
00093     if(mask)
00094       out.valid_ = true;
00095     mask_ |= mask;
00096 #endif
00097   }
00098 
00100   static void removeChannels(Mask mask)
00101   {
00102 #ifdef DEBUG
00103     mask_ &= ~mask;
00104     if(!mask_)
00105       out.valid_ = false;
00106 #endif
00107   }
00108 
00110   Debug() : valid_(mask_ != 0) {}
00112   Debug(Mask mask) : valid_((mask_ & mask) != 0) {}
00113 
00115   static Debug out;
00117   static Debug& channel(Mask mask) {return (mask_ & mask) ? out : dummy_;}
00118 
00120   template<class C>
00121   Debug& operator<<(const C& c)
00122   {
00123 #ifdef DEBUG
00124     if(valid_)
00125       std::cerr << c;
00126 #endif // DEBUG
00127     return *this;
00128   }
00129 
00131   operator bool() const
00132   {
00133 #ifdef DEBUG
00134     return valid_;
00135 #else
00136     // we could return valid_ here too, but it wouldn't optimise as well
00137     return false;
00138 #endif
00139   }
00140 
00142   class Endl
00143   {
00144     // don't need this if DEBUG isn't defined, since
00145     // 'out << endl;' doesn't write to std::cerr in that case
00146 #ifdef DEBUG
00147     inline friend std::ostream& operator<<(std::ostream& os, const Endl& e)
00148     {
00149       os << std::endl;
00150       return os;
00151     }
00152 #endif
00153   };
00154 
00156   static Endl endl;
00157 
00159   class Flush
00160   {
00161     // don't need this if DEBUG isn't defined, since
00162     // 'out << flush;' doesn't write to std::cerr in that case
00163 #ifdef DEBUG
00164     inline friend std::ostream& operator<<(std::ostream& os, const Flush& e)
00165     {
00166       os << std::flush;
00167       return os;
00168     }
00169 #endif
00170   };
00171 
00173   static Flush flush;
00174 
00175  private:
00176   bool valid_;
00177   static Mask mask_;
00178   static Debug dummy_;
00179 };
00180 
00181 } // namespace
00182 
00183 #endif // _DEBUG_H_

Generated Tue Oct 26 19:02:12 2004.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.