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 // Class Definition
00051 
00060 class Debug {
00061 public:
00063         inline static bool              getEnabled() { return mDebug; };
00064         
00066         inline static int               getVerbosity() { return mVerbosity; };
00067         
00069         inline static void              setEnabled(bool Enable) { mDebug = Enable; };
00070         
00072         inline static void              setLog(std::string const & LogFile);
00073         
00075         inline static void              setVerbosity(int Verbosity) { mVerbosity = Verbosity; };
00076         
00077         inline friend const Debug&      operator << (const Debug & dbg, const char * s);
00078         inline friend const Debug&      operator << (const Debug & dbg, const std::string & s);
00079         inline friend const Debug&      operator << (const Debug & dbg, const char c);
00080         inline friend const Debug&      operator << (const Debug & dbg, const int d);
00081         inline friend const Debug&      operator << (const Debug & dbg, const unsigned int d);
00082         inline friend const Debug&      operator << (const Debug & dbg, const long l);
00083         inline friend const Debug&      operator << (const Debug & dbg, const bool b);
00084         inline friend const Debug&      operator << (const Debug & dbg, const unsigned long l);
00085         inline friend const Debug&      operator << (const Debug & dbg, const double lf);
00086         inline friend const Debug&      operator << (const Debug & dbg, std::ostream&(*f)(std::ostream&)); // for endl
00087 
00088         Debug(int ThisVerbosity);
00089         virtual ~Debug();
00090         
00091 private:
00092         inline static bool              testPrint(const Debug& dbg) { return ((dbg.mDebug) && (dbg.mVerbosity >= dbg.mThisVerbosity)); };
00093 
00094         static bool                     mDebug;                 
00095         static std::ofstream            mLogFile;               
00096         static std::string              mLogPath;               
00097         static bool                     mLogToFile;             
00098         int                             mThisVerbosity;         
00099         static int                      mVerbosity;             
00100 };
00101 
00103 
00107 inline void Debug::setLog(std::string const & LogFile) {
00108         Debug::mLogFile.open(LogFile.c_str(), std::ios::app);
00109         if (!Debug::mLogFile.is_open()) {
00110                 Debug::mLogPath = "";
00111                 Debug::mLogToFile = false;
00112                 throw H::Exception("Invalid Debug Log [" + LogFile + "] Specified! -- Logging to File Disabled", __FILE__, __FUNCTION__, __LINE__);
00113         }
00114         Debug::mLogPath = LogFile;
00115         Debug::mLogToFile = true;
00116         Debug::mLogFile.close();
00117 }
00118 
00122 inline const Debug& operator << (const Debug & dbg, const std::string & s) {
00123         if (Debug::testPrint(dbg)) {
00124                 std::cout << s;
00125                 if (Debug::mLogToFile) {
00126                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00127                         if (Debug::mLogFile.is_open()) {
00128                                 Debug::mLogFile << s;
00129                                 Debug::mLogFile.close();
00130                         }
00131                 }
00132         }
00133         return dbg;
00134 }
00135 
00139 inline const Debug& operator << (const Debug & dbg, const char * s) {
00140         if (Debug::testPrint(dbg)) {
00141                 std::cout << s;
00142                 if (Debug::mLogToFile) {
00143                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00144                         if (Debug::mLogFile.is_open()) {
00145                                 Debug::mLogFile << s;
00146                                 Debug::mLogFile.close();
00147                         }
00148                 }
00149         }
00150         return dbg;
00151 }
00152 
00156 inline const Debug& operator << (const Debug & dbg, const char c) {
00157         if (Debug::testPrint(dbg)) {
00158                 std::cout << c;
00159                 if (Debug::mLogToFile) {
00160                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00161                         if (Debug::mLogFile.is_open()) {
00162                                 Debug::mLogFile << c;
00163                                 Debug::mLogFile.close();
00164                         }
00165                 }
00166         }       
00167         return dbg;
00168 }
00169 
00173 inline const Debug& operator << (const Debug & dbg, const int d) {
00174         if (Debug::testPrint(dbg)) {
00175                 std::cout << d;
00176                 if (Debug::mLogToFile) {
00177                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00178                         if (Debug::mLogFile.is_open()) {
00179                                 Debug::mLogFile << d;
00180                                 Debug::mLogFile.close();
00181                         }
00182                 }
00183         }       
00184         return dbg;
00185 }
00186 
00190 inline const Debug& operator << (const Debug & dbg, const unsigned int d) {
00191         if (Debug::testPrint(dbg)) {
00192                 std::cout << d;
00193                 if (Debug::mLogToFile) {
00194                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00195                         if (Debug::mLogFile.is_open()) {
00196                                 Debug::mLogFile << d;
00197                                 Debug::mLogFile.close();
00198                         }
00199                 }
00200         }       
00201         return dbg;
00202 }
00203 
00207 inline const Debug& operator << (const Debug & dbg, const long l) {
00208         if (Debug::testPrint(dbg)) {
00209                 std::cout << l;
00210                 if (Debug::mLogToFile) {
00211                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00212                         if (Debug::mLogFile.is_open()) {
00213                                 Debug::mLogFile << l;
00214                                 Debug::mLogFile.close();
00215                         }
00216                 }
00217         }       
00218         return dbg;
00219 }
00220 
00224 inline const Debug& operator << (const Debug & dbg, const bool b) {
00225         if (Debug::testPrint(dbg)) {
00226                 std::cout << b;
00227                 if (Debug::mLogToFile) {
00228                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00229                         if (Debug::mLogFile.is_open()) {
00230                                 Debug::mLogFile << b;
00231                                 Debug::mLogFile.close();
00232                         }
00233                 }
00234         }       
00235         return dbg;
00236 }
00237 
00241 inline const Debug& operator << (const Debug & dbg, const unsigned long l) {
00242         if (Debug::testPrint(dbg)) {
00243                 std::cout << l;
00244                 if (Debug::mLogToFile) {
00245                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00246                         if (Debug::mLogFile.is_open()) {
00247                                 Debug::mLogFile << l;
00248                                 Debug::mLogFile.close();
00249                         }
00250                 }
00251         }       
00252         return dbg;
00253 }
00254 
00258 inline const Debug& operator << (const Debug & dbg, const double lf) {
00259         if (Debug::testPrint(dbg)) {
00260                 std::cout << lf;
00261                 if (Debug::mLogToFile) {
00262                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00263                         if (Debug::mLogFile.is_open()) {
00264                                 Debug::mLogFile << lf;
00265                                 Debug::mLogFile.close();
00266                         }
00267                 }
00268         }       
00269         return dbg;
00270 }
00271 
00275 inline const Debug& operator << (const Debug & dbg, std::ostream&(* f)(std::ostream &)) {
00276         if (Debug::testPrint(dbg)) {
00277                 std::cout << f;
00278                 if (Debug::mLogToFile) {
00279                         Debug::mLogFile.open(Debug::mLogPath.c_str(), std::ios::app);
00280                         if (Debug::mLogFile.is_open()) {
00281                                 Debug::mLogFile << f;
00282                                 Debug::mLogFile.close();
00283                         }
00284                 }
00285         }       
00286         return dbg;
00287 }
00288 
00289 static const Debug cdbg(0);
00290 static const Debug cdbg1(1);
00291 static const Debug cdbg2(2);
00292 static const Debug cdbg3(3);
00293 static const Debug cdbg4(4);
00294 static const Debug cdbg5(5);
00295 
00296 } // H namespace
00297 
00299 
00300 #endif // __Debug_h

Generated on Wed Jun 27 12:41:53 2007 for gizmod by  doxygen 1.4.7