Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Trace.h

Go to the documentation of this file.
00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
00011 
00012 #pragma once
00013 
00015 #include "nvsg/nvsgapi.h"  // storage-class defines
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; // "Software\\NVIDIA Corporation\\QuadroVR\\Debug"
00031 
00032 namespace nvutil {
00033 
00034 
00036 #define TRACE_LOG_FILE       "QuadroVR.log" /* log file for debug printouts */
00037 
00038   //
00039   // a certain output functors
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   // uncomment one of the following for desired tracing
00183 # define OutputString traceDebugOutput
00184   //# define OutputString traceConsoleOutput
00185   //# define OutputString traceConsoleDebugOutput 
00186   //# define OutputString traceFileOutput
00187   //# define OutputString traceFileDebugOutput
00188   //# define OutputString traceFileConsoleOutput
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   // __TRACE_OUT(s) - string output 
00197   // requires TRACE() macro to be expanded before within the same function
00198 # define __TRACE_OUT(s)   \
00199   if ( dwTraceEnabled ) { TraceFunctor<OutputString>()(s); }
00200   // __TRACE_OUT_F(s) - formatted string output 
00201   // USAGE: __TRACE_OUT_F((const char* fmt, ...)) 
00202   //                      ^                    ^
00203   // requires TRACE() macro to be expanded before within the same function
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 } // namespace nvutil
00216 
00217 // avoid tedious, repeated qualification of namespace scope
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 

Generated on Tue Mar 1 13:19:19 2005 for NVSGSDK by NVIDIA