MeterLogger
tinyprintf.h
Go to the documentation of this file.
1 /*
2 File: tinyprintf.h
3 
4 Copyright (C) 2004 Kustaa Nyholm
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10 
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 This library is really just two files: 'tinyprintf.h' and 'tinyprintf.c'.
21 
22 They provide a simple and small (+400 loc) printf functionality to
23 be used in embedded systems.
24 
25 I've found them so useful in debugging that I do not bother with a
26 debugger at all.
27 
28 They are distributed in source form, so to use them, just compile them
29 into your project.
30 
31 Two printf variants are provided: printf and the 'sprintf' family of
32 functions ('snprintf', 'sprintf', 'vsnprintf', 'vsprintf').
33 
34 The formats supported by this implementation are:
35 'c' 'd' 'i' 'o' 'p' 'u' 's' 'x' 'X'.
36 
37 Zero padding and field width are also supported.
38 
39 If the library is compiled with 'PRINTF_SUPPORT_LONG' defined, then
40 the long specifier is also supported. Note that this will pull in some
41 long math routines (pun intended!) and thus make your executable
42 noticeably longer. Likewise with 'PRINTF_LONG_LONG_SUPPORT' for the
43 long long specifier, and with 'PRINTF_SIZE_T_SUPPORT' for the size_t
44 specifier.
45 
46 The memory footprint of course depends on the target CPU, compiler and
47 compiler options, but a rough guesstimate (based on a H8S target) is about
48 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
49 Not too bad. Your mileage may vary. By hacking the source code you can
50 get rid of some hundred bytes, I'm sure, but personally I feel the balance of
51 functionality and flexibility versus code size is close to optimal for
52 many embedded systems.
53 
54 To use the printf, you need to supply your own character output function,
55 something like :
56 
57 void putc ( void* p, char c)
58 {
59  while (!SERIAL_PORT_EMPTY) ;
60  SERIAL_PORT_TX_REGISTER = c;
61 }
62 
63 Before you can call printf, you need to initialize it to use your
64 character output function with something like:
65 
66 init_printf(NULL,putc);
67 
68 Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
69 the NULL (or any pointer) you pass into the 'init_printf' will eventually be
70 passed to your 'putc' routine. This allows you to pass some storage space (or
71 anything really) to the character output function, if necessary.
72 This is not often needed but it was implemented like that because it made
73 implementing the sprintf function so neat (look at the source code).
74 
75 The code is re-entrant, except for the 'init_printf' function, so it is safe
76 to call it from interrupts too, although this may result in mixed output.
77 If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
78 
79 The printf and sprintf functions are actually macros that translate to
80 'tfp_printf' and 'tfp_sprintf' when 'TINYPRINTF_OVERRIDE_LIBC' is set
81 (default). Setting it to 0 makes it possible to use them along with
82 'stdio.h' printf's in a single source file. When
83 'TINYPRINTF_OVERRIDE_LIBC' is set, please note that printf/sprintf are
84 not function-like macros, so if you have variables or struct members
85 with these names, things will explode in your face. Without variadic
86 macros this is the best we can do to wrap these function. If it is a
87 problem, just give up the macros and use the functions directly, or
88 rename them.
89 
90 It is also possible to avoid defining tfp_printf and/or tfp_sprintf by
91 clearing 'TINYPRINTF_DEFINE_TFP_PRINTF' and/or
92 'TINYPRINTF_DEFINE_TFP_SPRINTF' to 0. This allows for example to
93 export only tfp_format, which is at the core of all the other
94 functions.
95 
96 For further details see source code.
97 
98 regs Kusti, 23.10.2004
99 */
100 
101 #ifndef __TFP_PRINTF__
102 #define __TFP_PRINTF__
103 
104 #include <stdarg.h>
105 
106 /* Global configuration */
107 
108 /* Set this to 0 if you do not want to provide tfp_printf */
109 #ifndef TINYPRINTF_DEFINE_TFP_PRINTF
110 # define TINYPRINTF_DEFINE_TFP_PRINTF 1
111 #endif
112 
113 /* Set this to 0 if you do not want to provide
114  tfp_sprintf/snprintf/vsprintf/vsnprintf */
115 #ifndef TINYPRINTF_DEFINE_TFP_SPRINTF
116 # define TINYPRINTF_DEFINE_TFP_SPRINTF 1
117 #endif
118 
119 /* Set this to 0 if you do not want tfp_printf and
120  tfp_{vsn,sn,vs,s}printf to be also available as
121  printf/{vsn,sn,vs,s}printf */
122 #ifndef TINYPRINTF_OVERRIDE_LIBC
123 # define TINYPRINTF_OVERRIDE_LIBC 0
124 #endif
125 
126 /* Optional external types dependencies */
127 
128 #if TINYPRINTF_DEFINE_TFP_SPRINTF
129 # include <sys/types.h> /* size_t */
130 #endif
131 
132 /* Declarations */
133 
134 #ifdef __GNUC__
135 # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx) \
136  __attribute__((format (printf, fmt_idx, arg1_idx)))
137 #else
138 # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx)
139 #endif
140 
141 #ifdef __cplusplus
142 extern "C" {
143 #endif
144 
145 typedef void (*putcf) (void *, char);
146 
147 /*
148  'tfp_format' really is the central function for all tinyprintf. For
149  each output character after formatting, the 'putf' callback is
150  called with 2 args:
151  - an arbitrary void* 'putp' param defined by the user and
152  passed unmodified from 'tfp_format',
153  - the character.
154  The 'tfp_printf' and 'tfp_sprintf' functions simply define their own
155  callback and pass to it the right 'putp' it is expecting.
156 */
157 ICACHE_FLASH_ATTR void tfp_format(void *putp, putcf putf, const char *fmt, va_list va);
158 
159 #if TINYPRINTF_DEFINE_TFP_SPRINTF
160 ICACHE_FLASH_ATTR int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
161 ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *fmt, ...) \
162  _TFP_SPECIFY_PRINTF_FMT(3, 4);
163 ICACHE_FLASH_ATTR int tfp_vsprintf(char *str, const char *fmt, va_list ap);
164 ICACHE_FLASH_ATTR int tfp_sprintf(char *str, const char *fmt, ...) \
165  _TFP_SPECIFY_PRINTF_FMT(2, 3);
166 # if TINYPRINTF_OVERRIDE_LIBC
167 # define vsnprintf tfp_vsnprintf
168 # define snprintf tfp_snprintf
169 # define vsprintf tfp_vsprintf
170 # define sprintf tfp_sprintf
171 # endif
172 #endif
173 
174 #if TINYPRINTF_DEFINE_TFP_PRINTF
175 ICACHE_FLASH_ATTR void init_printf(void *putp, putcf putf);
176 ICACHE_FLASH_ATTR void tfp_printf(char *fmt, ...) _TFP_SPECIFY_PRINTF_FMT(1, 2);
177 # if TINYPRINTF_OVERRIDE_LIBC
178 # define printf tfp_printf
179 # endif
180 #endif
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(3
ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR void init_printf(void *putp, putcf putf)
Definition: tinyprintf.c:429
#define _TFP_SPECIFY_PRINTF_FMT(fmt_idx, arg1_idx)
Definition: tinyprintf.h:138
ICACHE_FLASH_ATTR void tfp_printf(char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(1
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
ICACHE_FLASH_ATTR int tfp_sprintf(char *str, const char *fmt,...) _TFP_SPECIFY_PRINTF_FMT(2
ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR int tfp_vsprintf(char *str, const char *fmt, va_list ap)
Definition: tinyprintf.c:503
ICACHE_FLASH_ATTR void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
Definition: tinyprintf.c:255
void(* putcf)(void *, char)
Definition: tinyprintf.h:145
ICACHE_FLASH_ATTR int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
Definition: tinyprintf.c:460