Debian Codegraph @

u8stream/include/u8/u8_trace.h

raw symbols SCIP
1 /* File: u8_trace.h; Copyright and License: see below */
2
3 #ifndef U8_TRACE_H
4 #define U8_TRACE_H
5
6 /* public file for the doxygen documentation: */
7 /*!
8 * \file
9 * \brief Prints traces to a byte (u8) stream
10 *
11 * Target audience is the developer of the application.
12 *
13 * Use logs to address the operator
14 * or to explain the history of events in bug reports to the developer.
15 *
16 * Return error codes to address the user of the application
17 * or to defer the decision to the calling function.
18 */
19
20 #include <stdio.h>
21 #include <time.h>
22
23 #define U8_TRACE_OUT_STREAM stdout
24 /* #define U8_TRACE_OUT_STREAM stderr */ /* stderr helps to locate sudden termination errors */
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 /* SWITCH: CASE DEBUG */
43
44 /*!
45 * \brief a condition to determine if tracing is active
46 */
47 #define U8_TRACE_ACTIVE (true)
48
49 /*!
50 * \brief traces a string
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 * \brief traces a string and an integer
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 * \brief traces a string and two integers
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 * \brief traces a string and an integer and another string
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 * \brief traces a string and a hexadecimal integer
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 * \brief traces a string and a pointer
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 * \brief traces a string and an information string
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 * \brief traces a string and a stringview (a struct with members start and length)
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 * \brief traces a string and a double
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 * \brief traces a function start
96 *
97 * Note: For every U8_TRACE_BEGIN, one U8_TRACE_END shall be called to create a nicely indented trace output
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 * \brief traces a function return
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 * \brief traces a function return and an error code if the error does not equal 0
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 * \brief ensures to flush all data.
113 *
114 * Call this macro before fork() or when you expect a process crash (e.g. SIGSEGV).
115 */
116 #define U8_TRACE_FLUSH() { fflush(U8_TRACE_OUT_STREAM); }
117
118 /*!
119 * \brief traces a timestamp
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 /* SWITCH: CASE RELEASE */
128
129 #ifdef __GNUC__
130 #define ATTR_UNUSED __attribute__((unused))
131 #else
132 #define ATTR_UNUSED
133 #endif
134
135 /*!
136 * \brief a condition to determine if tracing is active
137 */
138 #define U8_TRACE_ACTIVE (false)
139
140 /*!
141 * \brief traces a string
142 */
143 #define U8_TRACE_INFO(x) { const char *string_test ATTR_UNUSED = (x); }
144
145 /*!
146 * \brief traces a string and an integer
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 * \brief traces a string and two integers
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 * \brief traces a string and an integer and another string
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 * \brief traces a string and a hexadecimal integer
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 * \brief traces a string and a pointer
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 * \brief traces a string and an information string
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 * \brief traces a string and a stringview (a struct with members start and length)
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 * \brief traces a string and a double
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 * \brief traces a function start
187 *
188 * Note: For every U8_TRACE_BEGIN, one U8_TRACE_END shall be called to create a nicely indented trace output
189 */
190 #define U8_TRACE_BEGIN() {}
191
192 /*!
193 * \brief traces a function return
194 */
195 #define U8_TRACE_END() {}
196
197 /*!
198 * \brief traces a function return and an error code if the error does not equal 0
199 */
200 #define U8_TRACE_END_ERR(e) { const int int_test ATTR_UNUSED = (e); }
201
202 /*!
203 * \brief ensures to flush all data.
204 *
205 * Call this macro before fork() or when you expect a process crash (e.g. SIGSEGV).
206 */
207 #define U8_TRACE_FLUSH() {}
208
209 /*!
210 * \brief traces a timestamp
211 */
212 #define U8_TRACE_TIMESTAMP() {}
213
214 #endif /* SWITCH DEBUG/RELEASE */
215
216 #endif /* U8_TRACE_H */
217
218
219 /*
220 Copyright 2016-2026 Andreas Warnke
221
222 Licensed under the Apache License, Version 2.0 (the "License");
223 you may not use this file except in compliance with the License.
224 You may obtain a copy of the License at
225
226 http://www.apache.org/licenses/LICENSE-2.0
227
228 Unless required by applicable law or agreed to in writing, software
229 distributed under the License is distributed on an "AS IS" BASIS,
230 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
231 See the License for the specific language governing permissions and
232 limitations under the License.
233 */