00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library 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 GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef WT_TRACE_H 00022 #define WT_TRACE_H 00023 00024 #include <stdio.h> 00025 #include <stdarg.h> 00026 00027 #include <iostream> 00028 #if DEBUG 00029 #include <list> 00030 #include <string> 00031 #include <fstream> 00032 #include <sstream> 00033 #endif 00034 00035 #define SEPARATOR \ 00036 "----------------------------------------------------------------\n" 00037 #define SEPARATOR2 \ 00038 "================================================================\n" 00039 #define SEPARATOR3 \ 00040 "****************************************************************\n" 00041 00042 #define WARNPREFIX "Warning:[%s:%u]:" 00043 #define ERRORPREFIX "ERROR:[%s:%u]:" 00044 00045 namespace Wt { 00046 00047 /* vararg wrapper around fprintf */ 00048 static inline int stderr_print(const char *msg, ...) { 00049 int ret; 00050 va_list ap; 00051 va_start(ap, msg); 00052 ret = vfprintf(stderr, msg, ap); 00053 va_end(ap); 00054 return ret; 00055 } 00056 00057 #define __STDERR_PRINT(PREFIX) \ 00058 ( ! fprintf(stderr, PREFIX, __FILE__, __LINE__) ) ? (0) : \ 00059 Wt::stderr_print 00060 00061 #define Warn __STDERR_PRINT(WARNPREFIX) 00062 #define Error __STDERR_PRINT(ERRORPREFIX) 00063 00064 #if DEBUG 00065 extern std::list<std::string> debugRelay; 00066 00067 extern int enableTrace(const std::string &relay); 00068 extern int disableTrace(const std::string &relay); 00069 00070 static inline bool isTrace(const std::string &relay) { 00071 std::list<std::string>::const_iterator channel, end; 00072 00073 for (channel = debugRelay.begin(), end = debugRelay.end(); channel != end; ++channel) { 00074 if ((*channel == "all") || (*channel == relay)) { 00075 return true; 00076 } 00077 } 00078 00079 return false; 00080 } 00081 00082 class Trace { 00083 private: 00084 class Stream { 00085 public: 00086 00087 bool isActive() const { 00088 return active_; 00089 } 00090 00091 void setActive(bool active = true) { 00092 active_ = active; 00093 } 00094 00095 template<typename T> 00096 Stream& operator<<(const T& t) { 00097 if (isActive()) { 00098 std::cerr << t; 00099 } 00100 return *this; 00101 } 00102 00103 // this is for endl 00104 Stream& operator<<(std::ostream& (*p)(std::ostream&)) { 00105 if (isActive()) { 00106 p(std::cerr); 00107 } 00108 return *this; 00109 } 00110 00111 private: 00112 bool active_; 00113 }; 00114 00115 public: 00116 void setInfo(const char* file, int line) { 00117 buffer.str(""); 00118 buffer << "[" << file << ":" << line << "]:"; 00119 } 00120 00121 /* vararg wrapper around trace */ 00122 inline int operator()(const char *relay, const char *msg, ...) { 00123 int ret = 0; 00124 va_list ap; 00125 va_start(ap, msg); 00126 if (isTrace(relay)) { 00127 fprintf(stderr, "[%s]:%s", relay, buffer.str().c_str()); 00128 ret = vfprintf(stderr, msg, ap); 00129 } 00130 va_end(ap); 00131 return ret; 00132 } 00133 00134 inline Stream& operator()(const char *relay) { 00135 trstr.setActive(isTrace(relay)); 00136 trstr << "[" << relay << "]:" << buffer.str(); 00137 return trstr; 00138 } 00139 00140 private: 00141 std::stringstream buffer; 00142 static Stream trstr; 00143 }; 00144 00145 #else // no DEBUG 00146 00147 #define enableTrace(RELAY) 00148 #define disableTrace(RELAY) 00149 #define isTrace(RELAY) 0 00150 00151 static inline void do_nothing(const char *relay, const char *msg, ...) { 00152 if (0 && relay && msg) {} 00153 } 00154 00155 class Trace { 00156 private: 00157 class Stream { 00158 public: 00159 template<typename T> 00160 Stream& operator<<(const T&) { 00161 return *this; 00162 } 00163 00164 // this is for endl 00165 Stream& operator<<(std::ostream& (*)(std::ostream&)) { 00166 return *this; 00167 } 00168 }; 00169 00170 public: 00171 void setInfo(const char*, int) {} 00172 00173 /* vararg wrapper around trace */ 00174 inline int operator()(const char *, const char *, ...) { 00175 return 0; 00176 } 00177 00178 inline Stream& operator()(const char *) { 00179 return trstr; 00180 } 00181 00182 private: 00183 static Stream trstr; 00184 }; 00185 00186 #endif 00187 00188 static inline Trace& getTrace(const char* filename, int line) { 00189 static Trace trace_; 00190 trace_.setInfo(filename, line); 00191 return trace_; 00192 } 00193 00194 #define trace getTrace(__FILE__, __LINE__) 00195 00196 } // namespace 00197 00198 #endif /* _WT_TRACE_H */
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.