| 1 |
|
| 2 |
|
| 3 |
#ifndef U8_TRACE_H |
| 4 |
#define U8_TRACE_H |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <time.h> |
| 22 |
|
| 23 |
#define U8_TRACE_OUT_STREAM stdout |
| 24 |
|
| 25 |
#define U8_TRACE_INDENT_MOD (32u) |
| 26 |
#define U8_TRACE_INDENT_MAX (31u) |
| 27 |
#define U8_TRACE_INDENT_STEP (2u) |
| 28 |
#define U8_TRACE_NULLTERM_SIZE (1u) |
| 29 |
extern __thread unsigned int u8_trace_indent_depth; |
| 30 |
extern const char u8_trace_indent_pattern_begin[ U8_TRACE_INDENT_STEP * U8_TRACE_INDENT_MAX + U8_TRACE_NULLTERM_SIZE ]; |
| 31 |
extern const char u8_trace_indent_pattern_end[ U8_TRACE_INDENT_STEP * U8_TRACE_INDENT_MAX + U8_TRACE_NULLTERM_SIZE ]; |
| 32 |
extern const char u8_trace_indent_pattern_info[ U8_TRACE_INDENT_STEP * U8_TRACE_INDENT_MAX + U8_TRACE_NULLTERM_SIZE ]; |
| 33 |
#define U8_TRACE_INDENT_BEGIN \ |
| 34 |
(&(u8_trace_indent_pattern_begin[((U8_TRACE_INDENT_MAX-u8_trace_indent_depth)%U8_TRACE_INDENT_MOD)*U8_TRACE_INDENT_STEP])) |
| 35 |
#define U8_TRACE_INDENT_END \ |
| 36 |
(&(u8_trace_indent_pattern_end[((U8_TRACE_INDENT_MAX-u8_trace_indent_depth)%U8_TRACE_INDENT_MOD)*U8_TRACE_INDENT_STEP])) |
| 37 |
#define U8_TRACE_INDENT_INFO \ |
| 38 |
(&(u8_trace_indent_pattern_info[((U8_TRACE_INDENT_MAX-u8_trace_indent_depth)%U8_TRACE_INDENT_MOD)*U8_TRACE_INDENT_STEP])) |
| 39 |
#define U8_TRACE_RED_ "\033[37;1;41m" |
| 40 |
#define U8_TRACE_NORM_ "\033[0m" |
| 41 |
|
| 42 |
#ifndef NDEBUG |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define U8_TRACE_ACTIVE (true) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define U8_TRACE_INFO(x) { const char *string_test = (x); fprintf(U8_TRACE_OUT_STREAM,"%s%s\n",U8_TRACE_INDENT_INFO,string_test); } |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#define U8_TRACE_INFO_INT(x,i) { const char *string_test = (x); const int int_test = (i); fprintf(U8_TRACE_OUT_STREAM,"%s%s %i\n",U8_TRACE_INDENT_INFO,string_test,int_test); } |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#define U8_TRACE_INFO_INT_INT(x,i,j) { const char *string_test = (x); const int int_test = (i); const int int_test2 = (j); fprintf(U8_TRACE_OUT_STREAM,"%s%s %i, %i\n",U8_TRACE_INDENT_INFO,string_test,int_test,int_test2); } |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define U8_TRACE_INFO_INT_STR(x,i,s) { const char *string_test = (x); const int int_test = (i); const char *string2_test = (s); fprintf(U8_TRACE_OUT_STREAM,"%s%s %i, %s\n",U8_TRACE_INDENT_INFO,string_test,int_test,string2_test); } |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#define U8_TRACE_INFO_HEX(x,i) { const char *string_test = (x); const unsigned int int_test = (i); fprintf(U8_TRACE_OUT_STREAM,"%s%s 0x%x\n",U8_TRACE_INDENT_INFO,string_test,int_test); } |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#define U8_TRACE_INFO_PTR(x,p) { const char *string_test = (x); const void *ptr_test = (p); fprintf(U8_TRACE_OUT_STREAM,"%s%s @:%p\n",U8_TRACE_INDENT_INFO,string_test,ptr_test); } |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#define U8_TRACE_INFO_STR(x,s) { const char *string_test = (x); const char *string2_test = (s); fprintf(U8_TRACE_OUT_STREAM,"%s%s %s\n",U8_TRACE_INDENT_INFO,string_test,string2_test); } |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#define U8_TRACE_INFO_VIEW(x,v) { const char *string_test = (x); const int int_test = (v.length); const char *string2_test = (v.start); fprintf(U8_TRACE_OUT_STREAM,"%s%s %.*s\n",U8_TRACE_INDENT_INFO,string_test,int_test,string2_test); } |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#define U8_TRACE_INFO_FLT(x,r) { const char *string_test = (x); const double real_test = (r); fprintf(U8_TRACE_OUT_STREAM,"%s%s %f\n",U8_TRACE_INDENT_INFO,string_test,real_test); } |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#define U8_TRACE_BEGIN() const unsigned int u8_trace_symmetry_test=u8_trace_indent_depth; { fprintf(U8_TRACE_OUT_STREAM,"%s%s [begin]\n",U8_TRACE_INDENT_BEGIN,__func__); u8_trace_indent_depth++; } |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#define U8_TRACE_END() { u8_trace_indent_depth--; fprintf(U8_TRACE_OUT_STREAM,"%s%s [end]\n",U8_TRACE_INDENT_END,__func__); (void)u8_trace_symmetry_test; } |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#define U8_TRACE_END_ERR(e) { const int int_test = (e); if ( int_test==0 ) {U8_TRACE_END();} else { u8_trace_indent_depth--; fprintf(U8_TRACE_OUT_STREAM,"%s%s [end] "U8_TRACE_RED_"ERR="U8_TRACE_NORM_"0x%x\n",U8_TRACE_INDENT_END,__func__,int_test); } (void)u8_trace_symmetry_test; } |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#define U8_TRACE_FLUSH() { fflush(U8_TRACE_OUT_STREAM); } |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#ifdef _WIN32 |
| 122 |
#define U8_TRACE_TIMESTAMP() { const clock_t now = clock(); fprintf(U8_TRACE_OUT_STREAM,"%s[%i+%i/CLOCKS_PER_SEC]\n",U8_TRACE_INDENT_INFO,(int)(now/CLOCKS_PER_SEC),(int)(now%CLOCKS_PER_SEC)); } |
| 123 |
#else |
| 124 |
#define U8_TRACE_TIMESTAMP() { struct timespec tp; const int err = clock_gettime(CLOCK_MONOTONIC,&tp); fprintf(U8_TRACE_OUT_STREAM,"%s[%i.%06i %s]\n",U8_TRACE_INDENT_INFO,(int)(tp.tv_sec),(int)(tp.tv_nsec/1000),(err!=0)?"?":"sec"); } |
| 125 |
#endif |
| 126 |
|
| 127 |
#else |
| 128 |
|
| 129 |
#ifdef __GNUC__ |
| 130 |
#define ATTR_UNUSED __attribute__((unused)) |
| 131 |
#else |
| 132 |
#define ATTR_UNUSED |
| 133 |
#endif |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#define U8_TRACE_ACTIVE (false) |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#define U8_TRACE_INFO(x) { const char *string_test ATTR_UNUSED = (x); } |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
#define U8_TRACE_INFO_INT(x,i) { const char *string_test ATTR_UNUSED = (x); const int int_test ATTR_UNUSED = (i); } |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#define U8_TRACE_INFO_INT_INT(x,i,j) { const char *string_test ATTR_UNUSED = (x); const int int_test ATTR_UNUSED = (i); const int int_test2 ATTR_UNUSED = (j); } |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#define U8_TRACE_INFO_INT_STR(x,i,s) { const char *string_test ATTR_UNUSED = (x); const int int_test ATTR_UNUSED = (i); const char *string2_test ATTR_UNUSED = (s); } |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#define U8_TRACE_INFO_HEX(x,i) { const char *string_test ATTR_UNUSED = (x); const int int_test ATTR_UNUSED = (i); } |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#define U8_TRACE_INFO_PTR(x,p) { const char *string_test ATTR_UNUSED = (x); const void *ptr_test ATTR_UNUSED = (p); } |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#define U8_TRACE_INFO_STR(x,s) { const char *string_test ATTR_UNUSED = (x); const char *string2_test ATTR_UNUSED = (s); } |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#define U8_TRACE_INFO_VIEW(x,v) { const char *string_test ATTR_UNUSED = (x); const int int_test ATTR_UNUSED = (v.length); const char *string2_test ATTR_UNUSED = (v.start); } |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#define U8_TRACE_INFO_FLT(x,r) { const char *string_test ATTR_UNUSED = (x); const double real_test ATTR_UNUSED = (r); } |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#define U8_TRACE_BEGIN() {} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
#define U8_TRACE_END() {} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#define U8_TRACE_END_ERR(e) { const int int_test ATTR_UNUSED = (e); } |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#define U8_TRACE_FLUSH() {} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#define U8_TRACE_TIMESTAMP() {} |
| 213 |
|
| 214 |
#endif |
| 215 |
|
| 216 |
#endif |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|