00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsg/nvsgapi.h"
00016
00017 #include "nvutil/nvutil.h"
00018
00019 #include <string>
00020
00021 #ifndef LINUX
00022 # include <conio.h>
00023 # include "nvutil/Registry.h"
00024 #else
00025 # include <stdio.h>
00026 # include <stdarg.h>
00027 #endif
00028
00030 extern NVSG_API const char * REG_NV_TRACE_SUBKEY;
00031
00032 namespace nvutil {
00033
00034
00036 #define TRACE_LOG_FILE "QuadroVR.log"
00037
00038
00039
00040
00041
00043 struct traceDebugOutput
00044 {
00046 void operator()(const char * str)
00047 {
00048 #ifdef _WIN32
00049 #ifndef UNICODE
00050 OutputDebugStringA(str);
00051 #endif
00052 #elif defined(LINUX)
00053 printf("%s", str);
00054 #endif
00055 }
00056 };
00057
00059 struct traceConsoleOutput
00060 {
00062 void operator()(const char * str)
00063 {
00064 #ifdef _WIN32
00065 _cputs(str);
00066 #endif
00067 }
00068 };
00069
00071 struct traceConsoleDebugOutput
00072 {
00074 void operator()(const char * str)
00075 {
00076 #ifdef _WIN32
00077 traceDebugOutput()(str);
00078 traceConsoleOutput()(str);
00079 #endif
00080 }
00081 };
00082
00084 struct traceFileOutput
00085 {
00087 void operator()(const char * str)
00088 {
00089 #ifdef _WIN32
00090 FILE * fp = fopen(TRACE_LOG_FILE, "a");
00091 if ( fp )
00092 {
00093 fputs(str, fp);
00094 fclose(fp);
00095 }
00096 #endif
00097 }
00098 };
00099
00101 struct traceFileDebugOutput
00102 {
00104 void operator()(const char * str)
00105 {
00106 #ifdef _WIN32
00107 traceFileOutput()(str);
00108 traceDebugOutput()(str);
00109 #endif
00110 }
00111 };
00112
00114 struct traceFileConsoleOutput
00115 {
00117 void operator()(const char * str)
00118 {
00119 #ifdef _WIN32
00120 traceFileOutput()(str);
00121 traceConsoleOutput()(str);
00122 #endif
00123 }
00124 };
00125
00127 template <typename Functor>
00128 class Trace
00129 {
00130 public:
00132 Trace(const char * fn, bool traceOut) : m_fn(fn), m_traceOut(traceOut) { if (m_traceOut) { enter(); } }
00133
00135 ~Trace() { if (m_traceOut) { leave(); } }
00136
00137 private:
00138 void enter()
00139 {
00140 std::string outstr(">>>> Enter ");
00141 outstr += m_fn.c_str();
00142 outstr += "\n";
00143 Functor()(outstr.c_str());
00144 }
00145
00146 void leave()
00147 {
00148 std::string outstr("<<<< Leave ");
00149 outstr += m_fn.c_str();
00150 outstr += "\n";
00151 Functor()(outstr.c_str());
00152 }
00153
00154 std::string m_fn;
00155 bool m_traceOut;
00156 };
00157
00159 template <typename OutFunctor>
00160 struct TraceFunctor
00161 {
00163 void operator()(const char* str) { OutFunctor()(str); }
00164
00166 void format(const char* fmt, ...)
00167 {
00168 const int bufSize = 1024;
00169 char outBuf[bufSize];
00170
00171 va_list ap;
00172 va_start(ap, fmt);
00173 vsnprintf(outBuf, bufSize-1, fmt, ap);
00174 va_end(ap);
00175
00176 OutFunctor()(outBuf);
00177 }
00178 };
00179
00180
00181 #if defined(_DEBUG) && defined(_WIN32) && !defined(UNICODE)
00182
00183 # define OutputString traceDebugOutput
00184
00185
00186
00187
00188
00189 # define __TRACE() \
00190 static DWORD dwTraceEnabled = RegVal<DWORD>( __FUNCTION__ \
00191 , REG_NV_TRACE_SUBKEY \
00192 , REG_DWORD_LITTLE_ENDIAN \
00193 , HKEY_LOCAL_MACHINE \
00194 , DWORD(0) ); \
00195 Trace<TraceFunctor<OutputString> > traceObject(__FUNCTION__, dwTraceEnabled!=0);
00196
00197
00198 # define __TRACE_OUT(s) \
00199 if ( dwTraceEnabled ) { TraceFunctor<OutputString>()(s); }
00200
00201
00202
00203
00204 # define __TRACE_OUT_F(s) \
00205 if ( dwTraceEnabled ) { TraceFunctor<OutputString>().format s; }
00206
00207 #else // defined(_DEBUG) && defined(_WIN32)
00208
00209 # define __TRACE()
00210 # define __TRACE_OUT(s)
00211 # define __TRACE_OUT_F(s)
00212
00213 #endif // _DEBUG
00214
00215 }
00216
00217
00218 #if defined(_DEBUG) && defined(_WIN32)
00219
00220 using nvutil::RegVal;
00221 using nvutil::traceConsoleDebugOutput;
00222 using nvutil::traceConsoleOutput;
00223 using nvutil::traceDebugOutput;
00224 using nvutil::traceFileConsoleOutput;
00225 using nvutil::traceFileDebugOutput;
00226 using nvutil::traceFileOutput;
00227 using nvutil::TraceFunctor;
00228 using nvutil::Trace;
00229
00230 #endif // defined(_DEBUG) && defined(_WIN32)
00231