From 7eb00b6d3037a7e30c326b43e23502f777b92eb6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 12 Dec 2007 17:06:16 +0000 Subject: Corrected some issues with the new trace system. It's now working fine. It's backword compatible, and the new features are a lot of fun. Since it uses template functions you can add any new variable types to be formatted. --- src/fstring.cpp | 8 ++++ src/fstring.h | 4 ++ src/tests/tracer.cpp | 3 +- src/trace.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/trace.h | 96 ++++++++++++++++++++++-------------------- 5 files changed, 181 insertions(+), 46 deletions(-) create mode 100644 src/trace.cpp diff --git a/src/fstring.cpp b/src/fstring.cpp index f1ddd2c..d278914 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -5,6 +5,9 @@ * terms of the license contained in the file LICENSE. */ +#define BU_TRACE +#include "bu/trace.h" + #include "fstring.h" #include "hash.h" @@ -34,3 +37,8 @@ std::basic_ostream& operator<< (std::basic_ostream &os, const Bu::FS return os; } + +template<> void Bu::__tracer_format( const Bu::FString &v ) +{ + printf("(%ld)\"%s\"", v.getSize(), v.getStr() ); +} diff --git a/src/fstring.h b/src/fstring.h index c7bbc5e..fd75892 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -1011,6 +1011,10 @@ namespace Bu template<> uint32_t __calcHashCode( const FString &k ); template<> bool __cmpHashKeys( const FString &a, const FString &b ); + +#ifdef BU_TRACE + template<> void __tracer_format( const FString &v ); +#endif } #include diff --git a/src/tests/tracer.cpp b/src/tests/tracer.cpp index 52ce0d7..a57edf6 100644 --- a/src/tests/tracer.cpp +++ b/src/tests/tracer.cpp @@ -12,13 +12,14 @@ void doThing2( int x, const char *bob ) TRACE( x, bob ); } -void doThing( int x ) +void doThing( int8_t x ) { TRACE( x ); } int main( int argc, char *argv[] ) { + TRACE( argc, argv ); doThing( 54 ); doThing2( 128, "Hello" ); doThing3( 266, "Goodbye", argv ); diff --git a/src/trace.cpp b/src/trace.cpp new file mode 100644 index 0000000..b328565 --- /dev/null +++ b/src/trace.cpp @@ -0,0 +1,116 @@ +#define BU_TRACE +#include "bu/trace.h" + +void Bu::__tracer( const char *pf ) +{ + printf("trace: %s\n", pf ); +} + +template<> void Bu::__tracer_format( const int8_t &v ) +{ + printf("%hhd", v ); +} + +template<> void Bu::__tracer_format( const uint8_t &v ) +{ + printf("%hhu", v ); +} + +template<> void Bu::__tracer_format( const int16_t &v ) +{ + printf("%hd", v ); +} + +template<> void Bu::__tracer_format( const uint16_t &v ) +{ + printf("%hu", v ); +} + +template<> void Bu::__tracer_format( const int32_t &v ) +{ + printf("%d", v ); +} + +template<> void Bu::__tracer_format( const uint32_t &v ) +{ + printf("%u", v ); +} + +template<> void Bu::__tracer_format( const int64_t &v ) +{ + printf("%lld", v ); +} + +template<> void Bu::__tracer_format( const uint64_t &v ) +{ + printf("%llu", v ); +} + +template<> void Bu::__tracer_format( const bool &v ) +{ + if( v ) + printf("true"); + else + printf("false"); +} + +template<> void Bu::__tracer_format( const char &v ) +{ + printf("%hhd", v ); +} + +template<> void Bu::__tracer_format( const long &v ) +{ + printf("%ld", v ); +} + +template<> void Bu::__tracer_format( const unsigned long &v ) +{ + printf("%lu", v ); +} + +template<> void Bu::__tracer_format( const float &v ) +{ + printf("%f", v ); +} + +template<> void Bu::__tracer_format( const double &v ) +{ + printf("%f", v ); +} + +template<> void Bu::__tracer_format( void * const &v ) +{ + printf("0x%08X", (unsigned int)v ); +} + +template<> void Bu::__tracer_format( char * const &v ) +{ + printf("\"%s\"", v ); +} + +template<> void Bu::__tracer_format( char ** const &v ) +{ + printf("["); + for( int j = 0; v[j]; j++ ) + printf("\"%s\"", v[j] ); + printf("]"); +} + +template<> void Bu::__tracer_format( void const * const &v ) +{ + printf("0x%08X", (unsigned int)v ); +} + +template<> void Bu::__tracer_format( char const * const &v ) +{ + printf("\"%s\"", v ); +} + +template<> void Bu::__tracer_format( char const ** const &v ) +{ + printf("["); + for( int j = 0; v[j]; j++ ) + printf("\"%s\"", v[j] ); + printf("]"); +} diff --git a/src/trace.h b/src/trace.h index 4f85c9b..fb54aaf 100644 --- a/src/trace.h +++ b/src/trace.h @@ -8,19 +8,21 @@ #ifndef BU_TRACE_H #define BU_TRACE_H -#ifdef BU_TRACE -#include "bu/fstring.h" +#include +#include -namespace Nango +namespace Bu { - template void __tracer_format( t &v ); - - void __tracer( const char *pf ) +/* template void __tracer_format( t &v ) { - printf("trace: %s\n", pf ); + __tracer_format( *const_cast(&v) ); } +*/ + template void __tracer_format( const t &v ); -#define looper( vv ) \ + void __tracer( const char *pf ); + + #define looper( vv ) \ for( ; *n; n++ ) \ { \ if( *n == ',' || *n == ')' ) \ @@ -33,6 +35,7 @@ namespace Nango break; \ } \ } + template void __tracer( const char *pf, t1 &v1 ) { printf("trace: "); @@ -42,9 +45,9 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template void __tracer( const char *pf, - t1 &v1, t2 &v2 ) + t1 &v1, t2 &v2 ) { printf("trace: "); const char *s = pf; @@ -54,9 +57,9 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template - void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3 ) + void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3 ) { printf("trace: "); const char *s = pf; @@ -67,9 +70,9 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template - void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4 ) + void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4 ) { printf("trace: "); const char *s = pf; @@ -81,9 +84,9 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template - void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5 ) + void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5 ) { printf("trace: "); const char *s = pf; @@ -96,11 +99,11 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template - void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, - t6 &v6) + typename t6> + void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, + t6 &v6) { printf("trace: "); const char *s = pf; @@ -114,11 +117,11 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - + template - void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, - t6 &v6, t7 &v7 ) + typename t6, typename t7> + void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, + t6 &v6, t7 &v7 ) { printf("trace: "); const char *s = pf; @@ -133,29 +136,32 @@ namespace Nango fwrite( s, (int)n-(int)s, 1, stdout ); fwrite( "\n", 1, 1, stdout ); } - - template<> void __tracer_format( const int &v ) - { - printf("%d", v ); - } - - template<> void __tracer_format( int &v ) - { - printf("%d", v ); - } - - template<> void __tracer_format( void * &v ) - { - printf("0x%08X", (unsigned int)v ); - } - - template<> void __tracer_format( const char * &v ) - { - printf("\"%s\"", v ); - } +#undef looper + + template<> void __tracer_format( const int8_t &v ); + template<> void __tracer_format( const uint8_t &v ); + template<> void __tracer_format( const int16_t &v ); + template<> void __tracer_format( const uint16_t &v ); + template<> void __tracer_format( const int32_t &v ); + template<> void __tracer_format( const uint32_t &v ); + template<> void __tracer_format( const int64_t &v ); + template<> void __tracer_format( const uint64_t &v ); + template<> void __tracer_format( const bool &v ); + template<> void __tracer_format( const char &v ); + template<> void __tracer_format( const long &v ); + template<> void __tracer_format( const unsigned long &v ); + template<> void __tracer_format( const float &v ); + template<> void __tracer_format( const double &v ); + template<> void __tracer_format( void * const &v ); + template<> void __tracer_format( char * const &v ); + template<> void __tracer_format( char ** const &v ); + template<> void __tracer_format( void const * const &v ); + template<> void __tracer_format( char const * const &v ); + template<> void __tracer_format( char const ** const &v ); } -# define TRACE(args...) Nango::__tracer( __PRETTY_FUNCTION__, ##args ) +#ifdef BU_TRACE +# define TRACE(args...) Bu::__tracer( __PRETTY_FUNCTION__, ##args ) #else # define TRACE(args...) {} #endif -- cgit v1.2.3