acc_wrap_printf.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2019
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6
7 #include <printf.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13
14 /**
15 * This file contains wrapper functions used to replace the default
16 * printf and friends implementation with a minimal which is thread safe
17 * and memory efficient when used on embedded targets.
18 */
19
20 #define BUF_SIZE 200
21
22
23 int _write(int file, const char *ptr, int len);
24
25
26 typedef struct
27 {
28 char buffer[BUF_SIZE];
29 uint16_t position;
30 } print_buffer_t;
31
32
33 static void out_func(char character, void *arg)
34 {
35 print_buffer_t *buf = arg;
36
37 buf->buffer[buf->position++] = character;
38 if (buf->position == BUF_SIZE)
39 {
40 _write(0, buf->buffer, BUF_SIZE);
41 buf->position = 0;
42 }
43 }
44
45
46 int __wrap_puts(const char *str)
47 {
48 size_t len = strlen(str);
49
50 _write(0, str, len);
51 _write(0, "\n", 1);
52 return 0;
53 }
54
55
56 int __wrap_fputs(const char *str, FILE *stream)
57 {
58 (void)stream;
59 size_t len = strlen(str);
60
61 _write(0, str, len);
62 _write(0, "\n", 1);
63 return 0;
64 }
65
66
67 int __wrap_printf(const char *format, ...)
68 {
69 print_buffer_t buf = {
70 .position = 0,
71 .buffer = {0}
72 };
73 va_list va;
74
75 va_start(va, format);
76 int ret = fctvprintf(out_func, &buf, format, va);
77 if (buf.position != 0)
78 {
79 _write(0, buf.buffer, buf.position);
80 }
81
82 va_end(va);
83
84 return ret;
85 }
86
87
88 int __wrap_sprintf(char *buffer, const char *format, ...)
89 {
90 va_list va;
91
92 va_start(va, format);
93 const int ret = vsnprintf_(buffer, (size_t)-1, format, va);
94 va_end(va);
95 return ret;
96 }
97
98
99 int __wrap_snprintf(char *buffer, size_t count, const char *format, ...)
100 {
101 va_list va;
102
103 va_start(va, format);
104 const int ret = vsnprintf_(buffer, count, format, va);
105 va_end(va);
106 return ret;
107 }
108
109
110 int __wrap_vsnprintf(char *buffer, size_t count, const char *format, va_list va)
111 {
112 return vsnprintf_(buffer, count, format, va);
113 }
__wrap_puts
int __wrap_puts(const char *str)
Definition: acc_wrap_printf.c:46
__wrap_fputs
int __wrap_fputs(const char *str, FILE *stream)
Definition: acc_wrap_printf.c:56
__wrap_printf
int __wrap_printf(const char *format,...)
Definition: acc_wrap_printf.c:67
print_buffer_t::position
uint16_t position
Definition: acc_wrap_printf.c:29
_write
int _write(int file, const char *ptr, int len)
fctvprintf
int fctvprintf(void(*out)(char character, void *arg), void *arg, const char *format, va_list va)
Definition: printf.c:915
BUF_SIZE
#define BUF_SIZE
Definition: acc_wrap_printf.c:20
printf.h
out_func
static void out_func(char character, void *arg)
Definition: acc_wrap_printf.c:33
print_buffer_t
Definition: acc_wrap_printf.c:26
__wrap_sprintf
int __wrap_sprintf(char *buffer, const char *format,...)
Definition: acc_wrap_printf.c:88
__wrap_snprintf
int __wrap_snprintf(char *buffer, size_t count, const char *format,...)
Definition: acc_wrap_printf.c:99
vsnprintf_
int vsnprintf_(char *buffer, size_t count, const char *format, va_list va)
Definition: printf.c:899
print_buffer_t::buffer
char buffer[200]
Definition: acc_wrap_printf.c:28
__wrap_vsnprintf
int __wrap_vsnprintf(char *buffer, size_t count, const char *format, va_list va)
Definition: acc_wrap_printf.c:110