Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
cbprintf.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_H_
8#define ZEPHYR_INCLUDE_SYS_CBPRINTF_H_
9
10#include <stdarg.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <toolchain.h>
14
15#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
16#include <stdio.h>
17#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
18
19/* Determine if _Generic is supported.
20 * In general it's a C11 feature but it is supported in releases after:
21 * - GCC 4.9.0 https://gcc.gnu.org/gcc-4.9/changes.html
22 * - Clang 3.8 Introduced in 3.0 (https://releases.llvm.org/3.0/docs/ClangReleaseNotes.html)
23 * but with bug (http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_481)
24 * that was fixed in 3.8.
25 *
26 * @note Z_C_GENERIC is also set for C++ where functionality is implemented
27 * using overloading and templates.
28 */
29#ifndef Z_C_GENERIC
30#if defined(__cplusplus) || (((__STDC_VERSION__ >= 201112L) || \
31 ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40900) || \
32 ((__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) >= 30800)))
33#define Z_C_GENERIC 1
34#else
35#define Z_C_GENERIC 0
36#endif
37#endif
38
39/* Z_C_GENERIC is used there */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
53#ifdef __xtensa__
54#define CBPRINTF_PACKAGE_ALIGNMENT 16
55#elif defined(CONFIG_X86) && !defined(CONFIG_64BIT)
56/* sizeof(long double) is 12 on x86-32, which is not power of 2.
57 * So set it manually.
58 */
59#define CBPRINTF_PACKAGE_ALIGNMENT \
60 (IS_ENABLED(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE) ? \
61 16 : MAX(sizeof(double), sizeof(long long)))
62#else
63#define CBPRINTF_PACKAGE_ALIGNMENT \
64 (IS_ENABLED(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE) ? \
65 sizeof(long double) : MAX(sizeof(double), sizeof(long long)))
66#endif
67
78#define CBPRINTF_PACKAGE_ADD_STRING_IDXS BIT(0)
79
91#define CBPRINTF_MUST_RUNTIME_PACKAGE_CONST_CHAR BIT(0)
92
112typedef int (*cbprintf_cb)(/* int c, void *ctx */);
113
132typedef int (*cbvprintf_exteral_formatter_func)(cbprintf_cb out, void *ctx,
133 const char *fmt, va_list ap);
134
156#define CBPRINTF_MUST_RUNTIME_PACKAGE(skip, flags, ... /* fmt, ... */) \
157 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(skip, flags, __VA_ARGS__)
158
188#define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
189 ... /* fmt, ... */) \
190 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
191 align_offset, flags, __VA_ARGS__)
192
233__printf_like(4, 5)
234int cbprintf_package(void *packaged,
235 size_t len,
237 const char *format,
238 ...);
239
274int cbvprintf_package(void *packaged,
275 size_t len,
277 const char *format,
278 va_list ap);
279
306int cbprintf_fsc_package(void *in_packaged,
307 size_t in_len,
308 void *packaged,
309 size_t len);
310
333 void *ctx,
334 void *packaged);
335
362__printf_like(3, 4)
363int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
364
390int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
391
409static inline
410int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
411{
412 return cbpprintf_external(out, cbvprintf, ctx, packaged);
413}
414
415#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
416
435__printf_like(2, 3)
436int fprintfcb(FILE * stream, const char *format, ...);
437
455int vfprintfcb(FILE *stream, const char *format, va_list ap);
456
473__printf_like(1, 2)
474int printfcb(const char *format, ...);
475
491int vprintfcb(const char *format, va_list ap);
492
516__printf_like(3, 4)
517int snprintfcb(char *str, size_t size, const char *format, ...);
518
541int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
542
543#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
544
549#ifdef __cplusplus
550}
551#endif
552
553#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ */
int cbpprintf_external(cbprintf_cb out, cbvprintf_exteral_formatter_func formatter, void *ctx, void *packaged)
Generate the output for a previously captured format operation using an external formatter.
int cbprintf(cbprintf_cb out, void *ctx, const char *format,...)
*printf-like output through a callback.
static int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
Generate the output for a previously captured format operation.
Definition: cbprintf.h:410
int printfcb(const char *format,...)
printf using Zephyrs cbprintf infrastructure.
int vfprintfcb(FILE *stream, const char *format, va_list ap)
vfprintf using Zephyrs cbprintf infrastructure.
int fprintfcb(FILE *stream, const char *format,...)
fprintf using Zephyrs cbprintf infrastructure.
int vsnprintfcb(char *str, size_t size, const char *format, va_list ap)
vsnprintf using Zephyrs cbprintf infrastructure.
int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
varargs-aware *printf-like output through a callback.
int cbprintf_fsc_package(void *in_packaged, size_t in_len, void *packaged, size_t len)
Convert package to fully self-contained (fsc) package.
int snprintfcb(char *str, size_t size, const char *format,...)
snprintf using Zephyrs cbprintf infrastructure.
int vprintfcb(const char *format, va_list ap)
vprintf using Zephyrs cbprintf infrastructure.
int cbvprintf_package(void *packaged, size_t len, uint32_t flags, const char *format, va_list ap)
Capture state required to output formatted data later.
int(* cbprintf_cb)()
Signature for a cbprintf callback function.
Definition: cbprintf.h:112
int(* cbvprintf_exteral_formatter_func)(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
Signature for a external formatter function identical to cbvprintf.
Definition: cbprintf.h:132
int cbprintf_package(void *packaged, size_t len, uint32_t flags, const char *format,...)
Capture state required to output formatted data later.
flags
Definition: http_parser.h:131
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
int FILE
Definition: stdio.h:22
Macros to abstract toolchain specific capabilities.