00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsg/nvsgapi.h"
00016
00017 #include <stdarg.h>
00018 #include <string>
00019
00020 #include "nvutil/Singleton.h"
00021
00023 extern NVSG_API std::string __LOG_FILE__;
00024
00025 namespace nvutil {
00026
00031
00032 typedef enum
00033 {
00034 TM_NONE = 0
00035 , TM_TEST
00036 , TM_BENCH_TEST
00037 , TM_SHADER_TEST
00038 } TESTMODE;
00039
00041 typedef void LOG_OUTPUT_FUNCTION(const char *);
00042 NVSG_API LOG_OUTPUT_FUNCTION logFileOutput;
00043
00044
00046
00049 template <TESTMODE TestMode, LOG_OUTPUT_FUNCTION * OutputFn>
00050 class Log
00051 {
00052 public:
00053
00055
00058 Log() : m_enabled(false) {}
00060
00062 void init( bool enable
00063 );
00065
00067 void format( const char * fmt
00068 , ...
00069 ) const;
00071
00073 bool isEnabled() const;
00074
00075 private:
00076 bool m_enabled;
00077 };
00078
00079
00080
00081 template <TESTMODE TestMode, LOG_OUTPUT_FUNCTION * OutputFn>
00082 inline void Log<TestMode, OutputFn>::init(bool enable)
00083 {
00084 m_enabled = enable;
00085 }
00086
00087 template <TESTMODE TestMode, LOG_OUTPUT_FUNCTION * OutputFn>
00088 inline void Log<TestMode, OutputFn>::format(const char * fmt, ...) const
00089 {
00090 if ( !m_enabled )
00091 {
00092 return;
00093 }
00094
00095 const int bufSize = 1024;
00096 char outBuf[bufSize];
00097
00098 va_list ap;
00099 va_start(ap, fmt);
00100 vsnprintf(outBuf, bufSize-1, fmt, ap);
00101 va_end(ap);
00102
00103 OutputFn(outBuf);
00104 }
00105
00106 template <TESTMODE TestMode, LOG_OUTPUT_FUNCTION * OutputFn>
00107 inline bool Log<TestMode, OutputFn>::isEnabled() const
00108 {
00109 return m_enabled;
00110 }
00111
00112 typedef Singleton< Log<TM_TEST, logFileOutput> > LogTest;
00113 typedef Singleton< Log<TM_BENCH_TEST, logFileOutput> > LogBenchTest;
00114 typedef Singleton< Log<TM_SHADER_TEST, logFileOutput> > LogShaderTest;
00115
00116
00117
00118
00129
00130
00132 #define __LOG_TEST_INIT(TM) nvutil::LogTest::instance()->init(TM!=nvutil::TM_NONE)
00133
00134
00136 #define __LOG_BENCH_TEST_INIT(TM) nvutil::LogBenchTest::instance()->init(TM==nvutil::TM_BENCH_TEST)
00137
00138
00140 #define __LOG_SHADER_TEST_INIT(TM) nvutil::LogShaderTest::instance()->init(TM==nvutil::TM_SHADER_TEST)
00141
00154
00155 #define __LOG_TEST(s) { if ( nvutil::LogTest::instance()->isEnabled() ) { nvutil::LogTest::instance()->format s; } }
00156
00157 #define __LOG_BENCH_TEST(s) { if ( nvutil::LogBenchTest::instance()->isEnabled() ) { nvutil::LogBenchTest::instance()->format s; } }
00158
00159 #define __LOG_SHADER_TEST(s) { if ( nvutil::LogShaderTest::instance()->isEnabled() ) { nvutil::LogShaderTest::instance()->format s; } }
00160
00173
00174 #define __LOG_TEST_ENABLED() nvutil::LogTest::instance()->isEnabled()
00175
00176 #define __LOG_BENCH_TEST_ENABLED() nvutil::LogBenchTest::instance()->isEnabled()
00177
00178 #define __LOG_SHADER_TEST_ENABLED() nvutil::LogShaderTest::instance()->isEnabled()
00179
00182 }