libH/Debug.hpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #ifndef __Debug_h
00030 #define __Debug_h
00031 
00032 #if HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 #include "Exception.hpp"
00037 #include <iostream>
00038 #include <ostream>
00039 #include <fstream>
00040 #include <string>
00041 
00043 // Namespace
00045 
00046 namespace H {
00047         
00049 // Define's, typedefs
00051 
00056 #define debugOutput(out) \
00057         std::cout << out; \
00058         if (Debug::mLogToFile) { \
00059                 Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app); \
00060                 if (Debug::mLogFile.is_open()) { \
00061                         Debug::mLogFile << out; \
00062                         Debug::mLogFile.close(); \
00063                 } \
00064         } \
00065         return dbg
00066 
00068 // Class Definition
00070 
00079 class Debug {
00080 public:
00082         inline static bool              getEnabled() { return mDebug; };
00083         
00085         inline static int               getVerbosity() { return mVerbosity; };
00086         
00088         inline static void              setEnabled(bool Enable) { mDebug = Enable; };
00089         
00091         inline static void              setLog(std::string const & LogFile);
00092         
00094         inline static void              setVerbosity(int Verbosity) { mVerbosity = Verbosity; };
00095         
00096         inline friend const Debug&      operator << (const Debug & dbg, const char * s);
00097         inline friend const Debug&      operator << (const Debug & dbg, const std::string & s);
00098         inline friend const Debug&      operator << (const Debug & dbg, const char c);
00099         inline friend const Debug&      operator << (const Debug & dbg, const int d);
00100         inline friend const Debug&      operator << (const Debug & dbg, const unsigned int d);
00101         inline friend const Debug&      operator << (const Debug & dbg, const long l);
00102         inline friend const Debug&      operator << (const Debug & dbg, const long long ll);
00103         inline friend const Debug&      operator << (const Debug & dbg, const bool b);
00104         inline friend const Debug&      operator << (const Debug & dbg, const unsigned long l);
00105         inline friend const Debug&      operator << (const Debug & dbg, const unsigned long long ll);
00106         inline friend const Debug&      operator << (const Debug & dbg, const double lf);
00107         inline friend const Debug&      operator << (const Debug & dbg, std::ostream&(*f)(std::ostream&)); // for endl
00108 
00109         inline static bool              testPrint(const Debug& dbg) { return ((dbg.mDebug) && (dbg.mVerbosity >= dbg.mThisVerbosity)); };
00110         
00111         Debug(int ThisVerbosity);
00112         virtual ~Debug();
00113         
00114 private:
00115         static bool                     mDebug;                 
00116         static std::ofstream            mLogFile;               
00117         static std::string              mLogPath;               
00118         static bool                     mLogToFile;             
00119         int                             mThisVerbosity;         
00120         static int                      mVerbosity;             
00121 };
00122 
00124 
00128 inline void Debug::setLog(std::string const & LogFile) {
00129         Debug::mLogFile.open(LogFile.c_str(), std::ios::app);
00130         if (!Debug::mLogFile.is_open()) {
00131                 Debug::mLogPath = "";
00132                 Debug::mLogToFile = false;
00133                 throw H::Exception("Invalid Debug Log [" + LogFile + "] Specified! -- Logging to File Disabled", __FILE__, __FUNCTION__, __LINE__);
00134         }
00135         Debug::mLogPath = LogFile;
00136         Debug::mLogToFile = true;
00137         Debug::mLogFile.close();
00138 }
00139 
00143 inline const Debug& operator << (const Debug & dbg, const std::string & out) {
00144         debugOutput(out);
00145 }
00146 
00150 inline const Debug& operator << (const Debug & dbg, const char * out) {
00151         debugOutput(out);
00152 }
00153 
00157 inline const Debug& operator << (const Debug & dbg, const char out) {
00158         debugOutput(out);
00159 }
00160 
00164 inline const Debug& operator << (const Debug & dbg, const int out) {
00165         debugOutput(out);
00166 }
00167 
00171 inline const Debug& operator << (const Debug & dbg, const unsigned int out) {
00172         debugOutput(out);
00173 }
00174 
00178 inline const Debug& operator << (const Debug & dbg, const long out) {
00179         debugOutput(out);
00180 }
00181 
00185 inline const Debug& operator << (const Debug & dbg, const long long out) {
00186         debugOutput(out);
00187 }
00188 
00192 inline const Debug& operator << (const Debug & dbg, const bool out) {
00193         debugOutput(out);
00194 }
00195 
00199 inline const Debug& operator << (const Debug & dbg, const unsigned long out) {
00200         debugOutput(out);
00201 }
00202 
00206 inline const Debug& operator << (const Debug & dbg, const unsigned long long out) {
00207         debugOutput(out);
00208 }
00209 
00213 inline const Debug& operator << (const Debug & dbg, const double out) {
00214         debugOutput(out);
00215 }
00216 
00220 inline const Debug& operator << (const Debug & dbg, std::ostream&(* out)(std::ostream &)) {
00221         debugOutput(out);
00222 }
00223 
00224 static const Debug dbg0(0); 
00225 static const Debug dbg1(1); 
00226 static const Debug dbg2(2); 
00227 static const Debug dbg3(3); 
00228 static const Debug dbg4(4); 
00229 static const Debug dbg5(5); 
00230 
00231 #define cdbg    if (H::Debug::testPrint(dbg0)) H::dbg0
00232 #define cdbg1   if (H::Debug::testPrint(dbg1)) H::dbg1
00233 #define cdbg2   if (H::Debug::testPrint(dbg2)) H::dbg2
00234 #define cdbg3   if (H::Debug::testPrint(dbg3)) H::dbg3
00235 #define cdbg4   if (H::Debug::testPrint(dbg4)) H::dbg4
00236 #define cdbg5   if (H::Debug::testPrint(dbg5)) H::dbg5
00237                                 
00238 } // H namespace
00239 
00241 
00242 #endif // __Debug_h

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3