From 7eb00b6d3037a7e30c326b43e23502f777b92eb6 Mon Sep 17 00:00:00 2001
From: Mike Buland <eichlan@xagasoft.com>
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

(limited to 'src')

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<char>& operator<< (std::basic_ostream<char> &os, const Bu::FS
 	return os;
 }
 
+
+template<> void Bu::__tracer_format<Bu::FString>( 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<FString>( const FString &k );
 	template<> bool __cmpHashKeys<FString>( const FString &a, const FString &b );
+	
+#ifdef BU_TRACE
+	template<> void __tracer_format<FString>( const FString &v );
+#endif
 }
 
 #include <ostream>
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<int8_t>( const int8_t &v )
+{
+	printf("%hhd", v );
+}
+
+template<> void Bu::__tracer_format<uint8_t>( const uint8_t &v )
+{
+	printf("%hhu", v );
+}
+
+template<> void Bu::__tracer_format<int16_t>( const int16_t &v )
+{
+	printf("%hd", v );
+}
+
+template<> void Bu::__tracer_format<uint16_t>( const uint16_t &v )
+{
+	printf("%hu", v );
+}
+
+template<> void Bu::__tracer_format<int32_t>( const int32_t &v )
+{
+	printf("%d", v );
+}
+
+template<> void Bu::__tracer_format<uint32_t>( const uint32_t &v )
+{
+	printf("%u", v );
+}
+
+template<> void Bu::__tracer_format<int64_t>( const int64_t &v )
+{
+	printf("%lld", v );
+}
+
+template<> void Bu::__tracer_format<uint64_t>( const uint64_t &v )
+{
+	printf("%llu", v );
+}
+
+template<> void Bu::__tracer_format<bool>( const bool &v )
+{
+	if( v )
+		printf("true");
+	else
+		printf("false");
+}
+
+template<> void Bu::__tracer_format<char>( const char &v )
+{
+	printf("%hhd", v );
+}
+
+template<> void Bu::__tracer_format<long>( const long &v )
+{
+	printf("%ld", v );
+}
+
+template<> void Bu::__tracer_format<unsigned long>( const unsigned long &v )
+{
+	printf("%lu", v );
+}
+
+template<> void Bu::__tracer_format<float>( const float &v )
+{
+	printf("%f", v );
+}
+
+template<> void Bu::__tracer_format<double>( const double &v )
+{
+	printf("%f", v );
+}
+
+template<> void Bu::__tracer_format<void *>( void * const &v )
+{
+	printf("0x%08X", (unsigned int)v );
+}
+
+template<> void Bu::__tracer_format<char *>( char * const &v )
+{
+	printf("\"%s\"", v );
+}
+
+template<> void Bu::__tracer_format<char **>( char ** const &v )
+{
+	printf("[");
+	for( int j = 0; v[j]; j++ )
+		printf("\"%s\"", v[j] );
+	printf("]");
+}
+
+template<> void Bu::__tracer_format<void const *>( void const * const &v )
+{
+	printf("0x%08X", (unsigned int)v );
+}
+
+template<> void Bu::__tracer_format<char const *>( char const * const &v )
+{
+	printf("\"%s\"", v );
+}
+
+template<> void Bu::__tracer_format<char const **>( 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 <stdio.h>
+#include <stdint.h>
 
-namespace Nango
+namespace Bu
 {
-	template<typename t> void __tracer_format( t &v );
-	
-	void __tracer( const char *pf )
+/*	template<typename t> void __tracer_format( t &v )
 	{
-		printf("trace: %s\n", pf );
+		__tracer_format( *const_cast<const t *>(&v) );
 	}
+*/
+	template<typename t> 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<typename t1> 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<typename t1, typename t2> 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<typename t1, typename t2, typename t3>
-		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<typename t1, typename t2, typename t3, typename t4>
-		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<typename t1, typename t2, typename t3, typename t4, typename t5>
-		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<typename t1, typename t2, typename t3, typename t4, typename t5,
-		typename t6>
-		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<typename t1, typename t2, typename t3, typename t4, typename t5,
-		typename t6, typename t7>
-		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>( const int &v )
-	{
-		printf("%d", v );
-	}
-
-	template<> void __tracer_format<int>( int &v )
-	{
-		printf("%d", v );
-	}
-
-	template<> void __tracer_format<void *>( void * &v )
-	{
-		printf("0x%08X", (unsigned int)v );
-	}
-
-	template<> void __tracer_format<const char *>( const char * &v )
-	{
-		printf("\"%s\"", v );
-	}
+#undef looper
+	
+	template<> void __tracer_format<int8_t>( const int8_t &v );
+	template<> void __tracer_format<uint8_t>( const uint8_t &v );
+	template<> void __tracer_format<int16_t>( const int16_t &v );
+	template<> void __tracer_format<uint16_t>( const uint16_t &v );
+	template<> void __tracer_format<int32_t>( const int32_t &v );
+	template<> void __tracer_format<uint32_t>( const uint32_t &v );
+	template<> void __tracer_format<int64_t>( const int64_t &v );
+	template<> void __tracer_format<uint64_t>( const uint64_t &v );
+	template<> void __tracer_format<bool>( const bool &v );
+	template<> void __tracer_format<char>( const char &v );
+	template<> void __tracer_format<long>( const long &v );
+	template<> void __tracer_format<unsigned long>( const unsigned long &v );
+	template<> void __tracer_format<float>( const float &v );
+	template<> void __tracer_format<double>( const double &v );
+	template<> void __tracer_format<void *>( void * const &v );
+	template<> void __tracer_format<char *>( char * const &v );
+	template<> void __tracer_format<char **>( char ** const &v );
+	template<> void __tracer_format<void const *>( void const * const &v );
+	template<> void __tracer_format<char const *>( char const * const &v );
+	template<> void __tracer_format<char const **>( 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