diag-trace 5.0.1
µOS++ Tracing Infrastructure
Loading...
Searching...
No Matches
cpp-api.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-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * 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 be
9 * obtained from https://opensource.org/licenses/mit.
10 */
11
12// ----------------------------------------------------------------------------
13
28
29// ----------------------------------------------------------------------------
30
31#if !defined(MICRO_OS_PLUS_DIAG_TRACE_H_)
32#error "Do not include this file directly; use micro-os-plus/diag/trace.h."
33#endif // MICRO_OS_PLUS_DIAG_TRACE_H_
34
35// ----------------------------------------------------------------------------
36
37#ifndef MICRO_OS_PLUS_DIAG_TRACE_CPP_API_H_
38#define MICRO_OS_PLUS_DIAG_TRACE_CPP_API_H_
39
40// ----------------------------------------------------------------------------
41
42#if defined(__cplusplus)
43
44// ----------------------------------------------------------------------------
45
46#include <sys/types.h>
47
48#include <cstddef>
49#include <cstdarg>
50#include <concepts>
51
52// ----------------------------------------------------------------------------
53
96
97#if defined(__GNUC__)
98#pragma GCC diagnostic push
99
100#if defined(__clang__)
101#pragma clang diagnostic ignored "-Wpre-c++17-compat"
102#endif // defined(__clang__)
103#endif // defined(__GNUC__)
104
106{
107#if defined(__GNUC__)
108#pragma GCC diagnostic push
109
110#if defined(__clang__)
111#pragma clang diagnostic ignored "-Wc++98-compat"
112#pragma clang diagnostic ignored "-Wc++98-c++11-c++14-compat"
113#endif // defined(__clang__)
114#endif // defined(__GNUC__)
115
116 // Free functions declarations.
117
132 void
133 initialise (void) noexcept;
134
147 ssize_t
148 write (const void* buf, std::size_t nbyte) noexcept;
149
162 void
163 flush (void) noexcept;
164
165 // --------------------------------------------------------------------------
166
181 int
182 printf (const char* format, ...) noexcept;
183
199 int
200 vprintf (const char* format, std::va_list arguments) noexcept;
201
214 int
215 puts (const char* s = "") noexcept;
216
229 int
230 putchar (int c) noexcept;
231
247 void
248 dump_args (int argc, char* argv[], const char* name = "main") noexcept;
249
250#if defined(__GNUC__)
251#pragma GCC diagnostic pop
252#endif // defined(__GNUC__)
253} // namespace micro_os_plus::trace
254
255// ----------------------------------------------------------------------------
256
257#if defined(MICRO_OS_PLUS_DIAG_TRACE_ENABLED)
258
259// ----------------------------------------------------------------------------
260
261namespace micro_os_plus::trace
262{
263#if defined(__GNUC__)
264#pragma GCC diagnostic push
265
266#if defined(__clang__)
267#pragma clang diagnostic ignored "-Wc++98-compat"
268#endif // defined(__clang__)
269#endif // defined(__GNUC__)
270
271 // --------------------------------------------------------------------------
272
285 namespace detail
286 {
302 {
303 public:
316 static void
317 initialise (void) noexcept;
318
332 static ssize_t
333 write (const void* buf, std::size_t nbyte) noexcept;
334
349 static void
350 flush (void) noexcept;
351 };
352
353 // ------------------------------------------------------------------------
354
372 template <typename T>
373 concept trace_policy = requires (const void* buf, std::size_t n) {
374 { T::initialise () } noexcept -> std::same_as<void>;
375 { T::write (buf, n) } noexcept -> std::same_as<ssize_t>;
376 { T::flush () } noexcept -> std::same_as<void>;
377 };
378
379 // ------------------------------------------------------------------------
380
402 template <trace_policy T>
403 class tracer
404 {
405 public:
414 tracer () = delete;
415
431 static void
432 initialise (void) noexcept
433 {
434 T::initialise ();
435 }
436
452 static ssize_t
453 write (const void* buf, std::size_t nbyte) noexcept
454 {
455 return T::write (buf, nbyte);
456 }
457
476 static void
477 flush (void) noexcept
478 {
479 T::flush ();
480 }
481
482 // ----------------------------------------------------------------------
483
499 static int
500 printf (const char* format, ...) noexcept
501 __attribute__ ((format (printf, 1, 2)));
502
521 static int
522 vprintf (const char* format, std::va_list arguments) noexcept;
523
538 static int
539 puts (const char* s = "") noexcept;
540
552 static int
553 putchar (int c) noexcept;
554
572 static void
573 dump_args (int argc, char* argv[], const char* name = "main") noexcept;
574 };
575
576 // ------------------------------------------------------------------------
577 // Suppress implicit instantiation of the tracer member function bodies
578 // in every TU that includes this header. The explicit instantiations in
579 // trace.cpp are the sole ODR-defining instances.
580 // ------------------------------------------------------------------------
581
582#if defined(__GNUC__)
583#pragma GCC diagnostic push
584
585#if defined(__clang__)
586#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
587#endif // defined(__clang__)
588#endif // defined(__GNUC__)
589
590 extern template class tracer<implementation>;
591
592#if defined(__GNUC__)
593#pragma GCC diagnostic pop
594#endif // defined(__GNUC__)
595 } // namespace detail
596
597#if defined(__GNUC__)
598#pragma GCC diagnostic pop
599#endif // defined(__GNUC__)
600} // namespace micro_os_plus::trace
601
602// ----------------------------------------------------------------------------
603
604#endif // defined(MICRO_OS_PLUS_DIAG_TRACE_ENABLED)
605
606// ----------------------------------------------------------------------------
607
608#if defined(__GNUC__)
609#pragma GCC diagnostic pop
610#endif // defined(__GNUC__)
611
612// ----------------------------------------------------------------------------
613
614#endif // defined(__cplusplus)
615
616// ============================================================================
617// Templates, inlines & constexpr implementations.
618
620
621// ----------------------------------------------------------------------------
622
623#endif // MICRO_OS_PLUS_DIAG_TRACE_CPP_API_H_
624
625// ----------------------------------------------------------------------------
Policy class for the production trace instance.
Definition cpp-api.h:302
static void initialise(void) noexcept
Initialise the trace output channel.
static ssize_t write(const void *buf, std::size_t nbyte) noexcept
Write the given number of bytes to the trace output channel.
static void flush(void) noexcept
Flush the trace output channel.
Tracing API bound to a given policy class.
Definition cpp-api.h:404
static void initialise(void) noexcept
Initialise the trace output channel.
Definition cpp-api.h:432
tracer()=delete
Deleted default constructor.
static int printf(const char *format,...) noexcept
Write a formatted string to the trace output channel.
static ssize_t write(const void *buf, std::size_t nbyte) noexcept
Write the given number of bytes to the trace output channel.
Definition cpp-api.h:453
static void flush(void) noexcept
Flush the trace output channel.
Definition cpp-api.h:477
Concept constraining the policy class T used by tracer<T>.
Definition cpp-api.h:373
C++ header file with the inline implementations for the tracer class template.
void dump_args(int argc, char *argv[], const char *name="main") noexcept
Send the argv[] array to the trace output channel.
void initialise(void) noexcept
Initialise the trace output channel.
ssize_t write(const void *buf, std::size_t nbyte) noexcept
Write the given number of bytes to the trace output channel.
void flush(void) noexcept
Flush the trace output channel.
int printf(const char *format,...) noexcept
Write a formatted string to the trace output channel.
int puts(const char *s="") noexcept
Write the string and a line terminator to the trace output channel.
int putchar(int c) noexcept
Write the single character to the trace output channel.
int vprintf(const char *format, std::va_list arguments) noexcept
Write a formatted variable arguments list to the trace output channel.
Implementation details namespace.
Definition cpp-api.h:286
Tracing support namespace.
Definition cpp-api.h:106