diag-trace 4.2.1
The µOS++ Tracing Infrastructure
Loading...
Searching...
No Matches
trace.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2015-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef MICRO_OS_PLUS_DIAG_TRACE_H_
13#define MICRO_OS_PLUS_DIAG_TRACE_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18#include <cstdint>
19#include <cstddef>
20#include <cstdarg>
21#include <cstdlib>
22#else
23#include <stdint.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#endif // __cplusplus
27
28#include <sys/types.h>
29
30#if defined(__cplusplus)
31
32// To be effective, <stdio.h> must be included *before* this patch.
33#include <stdio.h>
34
35// This is a very annoying issue, some very old libraries still
36// define putchar() as a macro. This is abusive, since it prevents
37// the use of putchar() in other name spaces.
38#if defined(putchar)
39#undef putchar
40#endif // defined(putchar)
41
42#endif // __cplusplus
43
44// ----------------------------------------------------------------------------
45
46#if defined(MICRO_OS_PLUS_TRACE) || defined(MICRO_OS_PLUS_TRACE_TESTING)
47
48#if defined(MICRO_OS_PLUS_TRACE_TESTING)
49#define MICRO_OS_PLUS_TRACE_NAME_TESTING(name) name##_testing
50#else
51#define MICRO_OS_PLUS_TRACE_NAME_TESTING(name) name
52#endif
53
54#if defined(__cplusplus)
55
84#if defined(__clang__)
85#pragma clang diagnostic push
86#pragma clang diagnostic ignored "-Wpre-c++17-compat"
87#endif
88
89// For separation, use a separate naming space while testing.
90namespace micro_os_plus::MICRO_OS_PLUS_TRACE_NAME_TESTING(trace)
91
92#if defined(__clang__)
93#pragma clang diagnostic pop
94#endif
95{
96 // --------------------------------------------------------------------------
97
109 void
111
128 ssize_t
129 write (const void* buf, std::size_t nbyte);
130
148 void
149 flush (void);
150
151 // ------------------------------------------------------------------------
152
160 int
161 printf (const char* format, ...);
162
172 int
173 vprintf (const char* format, std::va_list arguments);
174
182 int
183 puts (const char* s = "");
184
192 int
193 putchar (int c);
194
203 void
204 dump_args (int argc, char* argv[], const char* name = "main");
205
206 // --------------------------------------------------------------------------
207} // namespace micro_os_plus::trace
208
209#endif // defined(__cplusplus)
210
211#if defined(__cplusplus)
212extern "C"
213{
214#endif // defined(__cplusplus)
215
216 /*
217 * Called from startup.
218 */
228 void
230
240 const void* buf, size_t nbyte);
241
252
261 const char* format, ...);
262
273 const char* format, va_list arguments);
274
283 const char* s);
284
293
302 int argc, char* argv[]);
303
304#if defined(__cplusplus)
305}
306#endif // defined(__cplusplus)
307
308#endif /* defined(MICRO_OS_PLUS_TRACE) */
309
310// When TRACE is not enabled, define empty definitions.
311#if !defined(MICRO_OS_PLUS_TRACE)
312
313#if defined(__cplusplus)
314
315#pragma GCC diagnostic push
316#if defined(__clang__)
317#pragma clang diagnostic ignored "-Wc++98-compat"
318#pragma clang diagnostic ignored "-Wc++98-c++11-c++14-compat"
319#endif
320
321// The void definitions are always in the regular namespace.
322namespace micro_os_plus::trace
323{
324 // ------------------------------------------------------------------------
325
326 inline void
327 initialize (void);
328
329 inline ssize_t
330 write (const void* buf, std::size_t nbyte);
331
332 // ------------------------------------------------------------------------
333
334 inline int
335 printf (const char* format, ...);
336
337 inline int
338 vprintf (const char* format, std::va_list arguments);
339
340 inline int
341 puts (const char* s = "");
342
343 inline int
344 putchar (int c);
345
346 inline void
347 dump_args (int argc, char* argv[], const char* name = "main");
348
349 // ------------------------------------------------------------------------
350
351 inline __attribute__ ((always_inline)) void
352 initialize (void)
353 {
354 }
355
356#pragma GCC diagnostic push
357#pragma GCC diagnostic ignored "-Wunused-parameter"
358
359 inline __attribute__ ((always_inline)) ssize_t
360 write (const void* buf, std::size_t nbyte)
361 {
362 return static_cast<ssize_t> (nbyte);
363 }
364
365 inline __attribute__ ((always_inline)) void
366 flush (void)
367 {
368 }
369
370 inline __attribute__ ((always_inline)) int
371 printf (const char* format, ...)
372 {
373 return 0;
374 }
375
376 inline __attribute__ ((always_inline)) int
377 vprintf (const char* format, std::va_list arguments)
378 {
379 return 0;
380 }
381
382 inline __attribute__ ((always_inline)) int
383 puts (const char* s)
384 {
385 return 0;
386 }
387
388 inline __attribute__ ((always_inline)) int
389 putchar (int c)
390 {
391 return c;
392 }
393
394 inline __attribute__ ((always_inline)) void
395 dump_args (int argc, char* argv[], const char* name)
396 {
397 }
398
399#pragma GCC diagnostic pop
400
401} // namespace micro_os_plus::trace
402
403#pragma GCC diagnostic pop
404
405#endif // defined(__cplusplus)
406
407#if defined(__cplusplus)
408extern "C"
409{
410#endif // defined(__cplusplus)
411
412 // The void definitions are always in the regular namespace.
413
414 inline void
416
417 // Implementation dependent
418 inline ssize_t
419 micro_os_plus_trace_write (const void* buf, size_t nbyte);
420
421 inline void
423
424 inline int
425 micro_os_plus_trace_printf (const char* format, ...);
426
427 inline int
428 micro_os_plus_trace_vprintf (const char* format, va_list arguments);
429
430 inline int
431 micro_os_plus_trace_puts (const char* s);
432
433 inline int
435
436 inline void
437 micro_os_plus_trace_dump_args (int argc, char* argv[]);
438
439#if defined(__cplusplus)
440}
441#endif // defined(__cplusplus)
442
443inline __attribute__ ((always_inline)) void
445{
446}
447
448#pragma GCC diagnostic push
449#pragma GCC diagnostic ignored "-Wunused-parameter"
450
451#pragma GCC diagnostic push
452#if defined(__cplusplus)
453#pragma GCC diagnostic ignored "-Wold-style-cast"
454#endif // defined(__cplusplus)
455
456inline __attribute__ ((always_inline)) ssize_t
457micro_os_plus_trace_write (const void* buf, size_t nbyte)
458{
459 return (ssize_t)(nbyte);
460}
461
462inline __attribute__ ((always_inline)) void
464{
465}
466
467#pragma GCC diagnostic pop
468
469inline __attribute__ ((always_inline)) int
470micro_os_plus_trace_printf (const char* format, ...)
471{
472 return 0;
473}
474
475inline __attribute__ ((always_inline)) int
476micro_os_plus_trace_vprintf (const char* format, va_list arguments)
477{
478 return 0;
479}
480
481inline __attribute__ ((always_inline)) int
482micro_os_plus_trace_puts (const char* s)
483{
484 return 0;
485}
486
487inline __attribute__ ((always_inline)) int
489{
490 return c;
491}
492
493inline __attribute__ ((always_inline)) void
494micro_os_plus_trace_dump_args (int argc, char* argv[])
495{
496}
497
498#pragma GCC diagnostic pop
499
500#endif // defined(MICRO_OS_PLUS_TRACE)
501
502// ----------------------------------------------------------------------------
503// Compatibility definitions.
504
505#define trace_initialize micro_os_plus_trace_initialize
506#define trace_write micro_os_plus_trace_write
507#define trace_flush micro_os_plus_trace_flush
508
509#define trace_printf micro_os_plus_trace_printf
510#define trace_vprintf micro_os_plus_trace_vprintf
511#define trace_puts micro_os_plus_trace_puts
512#define trace_putchar micro_os_plus_trace_putchar
513#define trace_dump_args micro_os_plus_trace_dump_args
514
515// ----------------------------------------------------------------------------
516
517#endif // MICRO_OS_PLUS_DIAG_TRACE_H_
518
519// ----------------------------------------------------------------------------
void micro_os_plus_trace_dump_args(int argc, char *argv[])
Write the argv[] array to the trace output channel.
Definition trace.cpp:249
void micro_os_plus_trace_initialize(void)
Initialize the trace output channel.
Definition trace.cpp:172
ssize_t micro_os_plus_trace_write(const void *buf, size_t nbyte)
Write the given number of bytes to the trace output channel.
void micro_os_plus_trace_flush(void)
Flush the trace output channel.
Definition trace.cpp:185
int micro_os_plus_trace_printf(const char *format,...)
Write a formatted string to the trace output channel.
Definition trace.cpp:196
int micro_os_plus_trace_puts(const char *s)
Write the string and a line terminator to the trace output channel.
Definition trace.cpp:227
int micro_os_plus_trace_vprintf(const char *format, va_list arguments)
Write a formatted variable arguments list to the trace output channel.
Definition trace.cpp:215
int micro_os_plus_trace_putchar(int c)
Write the single character to the trace output channel.
Definition trace.cpp:238
void dump_args(int argc, char *argv[], const char *name="main")
Send the argv[] array to the trace output channel.
Definition trace.cpp:146
void flush(void)
Flush the trace output channel.
void initialize(void)
Initialize the trace output channel.
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
int printf(const char *format,...)
Write a formatted string to the trace output channel.
Definition trace.cpp:46
int putchar(int c)
Write the single character to the trace output channel.
Definition trace.cpp:123
int puts(const char *s="")
Write the string and a line terminator to the trace output channel.
Definition trace.cpp:94
int vprintf(const char *format, std::va_list arguments)
Write a formatted variable arguments list to the trace output channel.
Definition trace.cpp:63
Tracing support namespace.
Definition trace.h:95
#define MICRO_OS_PLUS_TRACE_NAME_TESTING(name)
Definition trace.h:51