aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-12-12 17:06:16 +0000
committerMike Buland <eichlan@xagasoft.com>2007-12-12 17:06:16 +0000
commit7eb00b6d3037a7e30c326b43e23502f777b92eb6 (patch)
tree60013b086b3682c68192ad2ac8337e221ec9eba9 /src
parentba4167f75c8f7ba8726132eed94af3ae6cd38eee (diff)
downloadlibbu++-7eb00b6d3037a7e30c326b43e23502f777b92eb6.tar.gz
libbu++-7eb00b6d3037a7e30c326b43e23502f777b92eb6.tar.bz2
libbu++-7eb00b6d3037a7e30c326b43e23502f777b92eb6.tar.xz
libbu++-7eb00b6d3037a7e30c326b43e23502f777b92eb6.zip
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.
Diffstat (limited to '')
-rw-r--r--src/fstring.cpp8
-rw-r--r--src/fstring.h4
-rw-r--r--src/tests/tracer.cpp3
-rw-r--r--src/trace.cpp116
-rw-r--r--src/trace.h96
5 files changed, 181 insertions, 46 deletions
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 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#define BU_TRACE
9#include "bu/trace.h"
10
8#include "fstring.h" 11#include "fstring.h"
9#include "hash.h" 12#include "hash.h"
10 13
@@ -34,3 +37,8 @@ std::basic_ostream<char>& operator<< (std::basic_ostream<char> &os, const Bu::FS
34 return os; 37 return os;
35} 38}
36 39
40
41template<> void Bu::__tracer_format<Bu::FString>( const Bu::FString &v )
42{
43 printf("(%ld)\"%s\"", v.getSize(), v.getStr() );
44}
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
1011 1011
1012 template<> uint32_t __calcHashCode<FString>( const FString &k ); 1012 template<> uint32_t __calcHashCode<FString>( const FString &k );
1013 template<> bool __cmpHashKeys<FString>( const FString &a, const FString &b ); 1013 template<> bool __cmpHashKeys<FString>( const FString &a, const FString &b );
1014
1015#ifdef BU_TRACE
1016 template<> void __tracer_format<FString>( const FString &v );
1017#endif
1014} 1018}
1015 1019
1016#include <ostream> 1020#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 )
12 TRACE( x, bob ); 12 TRACE( x, bob );
13} 13}
14 14
15void doThing( int x ) 15void doThing( int8_t x )
16{ 16{
17 TRACE( x ); 17 TRACE( x );
18} 18}
19 19
20int main( int argc, char *argv[] ) 20int main( int argc, char *argv[] )
21{ 21{
22 TRACE( argc, argv );
22 doThing( 54 ); 23 doThing( 54 );
23 doThing2( 128, "Hello" ); 24 doThing2( 128, "Hello" );
24 doThing3( 266, "Goodbye", argv ); 25 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 @@
1#define BU_TRACE
2#include "bu/trace.h"
3
4void Bu::__tracer( const char *pf )
5{
6 printf("trace: %s\n", pf );
7}
8
9template<> void Bu::__tracer_format<int8_t>( const int8_t &v )
10{
11 printf("%hhd", v );
12}
13
14template<> void Bu::__tracer_format<uint8_t>( const uint8_t &v )
15{
16 printf("%hhu", v );
17}
18
19template<> void Bu::__tracer_format<int16_t>( const int16_t &v )
20{
21 printf("%hd", v );
22}
23
24template<> void Bu::__tracer_format<uint16_t>( const uint16_t &v )
25{
26 printf("%hu", v );
27}
28
29template<> void Bu::__tracer_format<int32_t>( const int32_t &v )
30{
31 printf("%d", v );
32}
33
34template<> void Bu::__tracer_format<uint32_t>( const uint32_t &v )
35{
36 printf("%u", v );
37}
38
39template<> void Bu::__tracer_format<int64_t>( const int64_t &v )
40{
41 printf("%lld", v );
42}
43
44template<> void Bu::__tracer_format<uint64_t>( const uint64_t &v )
45{
46 printf("%llu", v );
47}
48
49template<> void Bu::__tracer_format<bool>( const bool &v )
50{
51 if( v )
52 printf("true");
53 else
54 printf("false");
55}
56
57template<> void Bu::__tracer_format<char>( const char &v )
58{
59 printf("%hhd", v );
60}
61
62template<> void Bu::__tracer_format<long>( const long &v )
63{
64 printf("%ld", v );
65}
66
67template<> void Bu::__tracer_format<unsigned long>( const unsigned long &v )
68{
69 printf("%lu", v );
70}
71
72template<> void Bu::__tracer_format<float>( const float &v )
73{
74 printf("%f", v );
75}
76
77template<> void Bu::__tracer_format<double>( const double &v )
78{
79 printf("%f", v );
80}
81
82template<> void Bu::__tracer_format<void *>( void * const &v )
83{
84 printf("0x%08X", (unsigned int)v );
85}
86
87template<> void Bu::__tracer_format<char *>( char * const &v )
88{
89 printf("\"%s\"", v );
90}
91
92template<> void Bu::__tracer_format<char **>( char ** const &v )
93{
94 printf("[");
95 for( int j = 0; v[j]; j++ )
96 printf("\"%s\"", v[j] );
97 printf("]");
98}
99
100template<> void Bu::__tracer_format<void const *>( void const * const &v )
101{
102 printf("0x%08X", (unsigned int)v );
103}
104
105template<> void Bu::__tracer_format<char const *>( char const * const &v )
106{
107 printf("\"%s\"", v );
108}
109
110template<> void Bu::__tracer_format<char const **>( char const ** const &v )
111{
112 printf("[");
113 for( int j = 0; v[j]; j++ )
114 printf("\"%s\"", v[j] );
115 printf("]");
116}
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 @@
8#ifndef BU_TRACE_H 8#ifndef BU_TRACE_H
9#define BU_TRACE_H 9#define BU_TRACE_H
10 10
11#ifdef BU_TRACE 11#include <stdio.h>
12#include "bu/fstring.h" 12#include <stdint.h>
13 13
14namespace Nango 14namespace Bu
15{ 15{
16 template<typename t> void __tracer_format( t &v ); 16/* template<typename t> void __tracer_format( t &v )
17
18 void __tracer( const char *pf )
19 { 17 {
20 printf("trace: %s\n", pf ); 18 __tracer_format( *const_cast<const t *>(&v) );
21 } 19 }
20*/
21 template<typename t> void __tracer_format( const t &v );
22 22
23#define looper( vv ) \ 23 void __tracer( const char *pf );
24
25 #define looper( vv ) \
24 for( ; *n; n++ ) \ 26 for( ; *n; n++ ) \
25 { \ 27 { \
26 if( *n == ',' || *n == ')' ) \ 28 if( *n == ',' || *n == ')' ) \
@@ -33,6 +35,7 @@ namespace Nango
33 break; \ 35 break; \
34 } \ 36 } \
35 } 37 }
38
36 template<typename t1> void __tracer( const char *pf, t1 &v1 ) 39 template<typename t1> void __tracer( const char *pf, t1 &v1 )
37 { 40 {
38 printf("trace: "); 41 printf("trace: ");
@@ -42,9 +45,9 @@ namespace Nango
42 fwrite( s, (int)n-(int)s, 1, stdout ); 45 fwrite( s, (int)n-(int)s, 1, stdout );
43 fwrite( "\n", 1, 1, stdout ); 46 fwrite( "\n", 1, 1, stdout );
44 } 47 }
45 48
46 template<typename t1, typename t2> void __tracer( const char *pf, 49 template<typename t1, typename t2> void __tracer( const char *pf,
47 t1 &v1, t2 &v2 ) 50 t1 &v1, t2 &v2 )
48 { 51 {
49 printf("trace: "); 52 printf("trace: ");
50 const char *s = pf; 53 const char *s = pf;
@@ -54,9 +57,9 @@ namespace Nango
54 fwrite( s, (int)n-(int)s, 1, stdout ); 57 fwrite( s, (int)n-(int)s, 1, stdout );
55 fwrite( "\n", 1, 1, stdout ); 58 fwrite( "\n", 1, 1, stdout );
56 } 59 }
57 60
58 template<typename t1, typename t2, typename t3> 61 template<typename t1, typename t2, typename t3>
59 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3 ) 62 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3 )
60 { 63 {
61 printf("trace: "); 64 printf("trace: ");
62 const char *s = pf; 65 const char *s = pf;
@@ -67,9 +70,9 @@ namespace Nango
67 fwrite( s, (int)n-(int)s, 1, stdout ); 70 fwrite( s, (int)n-(int)s, 1, stdout );
68 fwrite( "\n", 1, 1, stdout ); 71 fwrite( "\n", 1, 1, stdout );
69 } 72 }
70 73
71 template<typename t1, typename t2, typename t3, typename t4> 74 template<typename t1, typename t2, typename t3, typename t4>
72 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4 ) 75 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4 )
73 { 76 {
74 printf("trace: "); 77 printf("trace: ");
75 const char *s = pf; 78 const char *s = pf;
@@ -81,9 +84,9 @@ namespace Nango
81 fwrite( s, (int)n-(int)s, 1, stdout ); 84 fwrite( s, (int)n-(int)s, 1, stdout );
82 fwrite( "\n", 1, 1, stdout ); 85 fwrite( "\n", 1, 1, stdout );
83 } 86 }
84 87
85 template<typename t1, typename t2, typename t3, typename t4, typename t5> 88 template<typename t1, typename t2, typename t3, typename t4, typename t5>
86 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5 ) 89 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5 )
87 { 90 {
88 printf("trace: "); 91 printf("trace: ");
89 const char *s = pf; 92 const char *s = pf;
@@ -96,11 +99,11 @@ namespace Nango
96 fwrite( s, (int)n-(int)s, 1, stdout ); 99 fwrite( s, (int)n-(int)s, 1, stdout );
97 fwrite( "\n", 1, 1, stdout ); 100 fwrite( "\n", 1, 1, stdout );
98 } 101 }
99 102
100 template<typename t1, typename t2, typename t3, typename t4, typename t5, 103 template<typename t1, typename t2, typename t3, typename t4, typename t5,
101 typename t6> 104 typename t6>
102 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, 105 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5,
103 t6 &v6) 106 t6 &v6)
104 { 107 {
105 printf("trace: "); 108 printf("trace: ");
106 const char *s = pf; 109 const char *s = pf;
@@ -114,11 +117,11 @@ namespace Nango
114 fwrite( s, (int)n-(int)s, 1, stdout ); 117 fwrite( s, (int)n-(int)s, 1, stdout );
115 fwrite( "\n", 1, 1, stdout ); 118 fwrite( "\n", 1, 1, stdout );
116 } 119 }
117 120
118 template<typename t1, typename t2, typename t3, typename t4, typename t5, 121 template<typename t1, typename t2, typename t3, typename t4, typename t5,
119 typename t6, typename t7> 122 typename t6, typename t7>
120 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5, 123 void __tracer( const char *pf, t1 &v1, t2 &v2, t3 &v3, t4 &v4, t5 &v5,
121 t6 &v6, t7 &v7 ) 124 t6 &v6, t7 &v7 )
122 { 125 {
123 printf("trace: "); 126 printf("trace: ");
124 const char *s = pf; 127 const char *s = pf;
@@ -133,29 +136,32 @@ namespace Nango
133 fwrite( s, (int)n-(int)s, 1, stdout ); 136 fwrite( s, (int)n-(int)s, 1, stdout );
134 fwrite( "\n", 1, 1, stdout ); 137 fwrite( "\n", 1, 1, stdout );
135 } 138 }
136 139#undef looper
137 template<> void __tracer_format<const int>( const int &v ) 140
138 { 141 template<> void __tracer_format<int8_t>( const int8_t &v );
139 printf("%d", v ); 142 template<> void __tracer_format<uint8_t>( const uint8_t &v );
140 } 143 template<> void __tracer_format<int16_t>( const int16_t &v );
141 144 template<> void __tracer_format<uint16_t>( const uint16_t &v );
142 template<> void __tracer_format<int>( int &v ) 145 template<> void __tracer_format<int32_t>( const int32_t &v );
143 { 146 template<> void __tracer_format<uint32_t>( const uint32_t &v );
144 printf("%d", v ); 147 template<> void __tracer_format<int64_t>( const int64_t &v );
145 } 148 template<> void __tracer_format<uint64_t>( const uint64_t &v );
146 149 template<> void __tracer_format<bool>( const bool &v );
147 template<> void __tracer_format<void *>( void * &v ) 150 template<> void __tracer_format<char>( const char &v );
148 { 151 template<> void __tracer_format<long>( const long &v );
149 printf("0x%08X", (unsigned int)v ); 152 template<> void __tracer_format<unsigned long>( const unsigned long &v );
150 } 153 template<> void __tracer_format<float>( const float &v );
151 154 template<> void __tracer_format<double>( const double &v );
152 template<> void __tracer_format<const char *>( const char * &v ) 155 template<> void __tracer_format<void *>( void * const &v );
153 { 156 template<> void __tracer_format<char *>( char * const &v );
154 printf("\"%s\"", v ); 157 template<> void __tracer_format<char **>( char ** const &v );
155 } 158 template<> void __tracer_format<void const *>( void const * const &v );
159 template<> void __tracer_format<char const *>( char const * const &v );
160 template<> void __tracer_format<char const **>( char const ** const &v );
156} 161}
157 162
158# define TRACE(args...) Nango::__tracer( __PRETTY_FUNCTION__, ##args ) 163#ifdef BU_TRACE
164# define TRACE(args...) Bu::__tracer( __PRETTY_FUNCTION__, ##args )
159#else 165#else
160# define TRACE(args...) {} 166# define TRACE(args...) {}
161#endif 167#endif