MCU-Common
Modules useful for embedded (MCU) programming
logger.h
Go to the documentation of this file.
1 /*
2  * This file is part of MCU-Common.
3  *
4  * Copyright (C) 2017 Adam Heinrich <adam@adamh.cz>
5  *
6  * MCU-Common is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * MCU-Common 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MCU-Common. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  */
31 
32 #ifndef MCU_COMMON_LOGGER_H
33 #define MCU_COMMON_LOGGER_H
34 
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <mcu-common/macros.h>
38 #include <mcu-common/fifo.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #define LOGGER_PUT_IMPL_N(n) LOGGER_PUT_ARGC##n
45 
46 #define LOGGER_PUT_IMPL(log, argc, ...) \
47  LOGGER_PUT_IMPL_N(argc)(log, __VA_ARGS__)
48 
49 #define LOGGER_PUT_ARGC1(log, fmt) \
50  logger_put0((log), (fmt))
51 #define LOGGER_PUT_ARGC2(log, fmt, a0) \
52  logger_put1((log), (fmt), (int)(a0))
53 #define LOGGER_PUT_ARGC3(log, fmt, a0, a1) \
54  logger_put2((log), (fmt), (int)(a0), (int)(a1))
55 #define LOGGER_PUT_ARGC4(log, fmt, a0, a1, a2) \
56  logger_put3((log), (fmt), (int)(a0), (int)(a1), (int)(a2))
57 #define LOGGER_PUT_ARGC5(log, fmt, a0, a1, a2, a3) \
58  logger_put4((log), (fmt), (int)(a0), (int)(a1), (int)(a2), (int)(a3))
59 #define LOGGER_PUT_ARGC6(log, fmt, a0, a1, a2, a3, a4) \
60  logger_put5((log), (fmt), (int)(a0), (int)(a1), (int)(a2), (int)(a3), (int)(a4))
61 #define LOGGER_PUT_ARGC7(log, fmt, a0, a1, a2, a3, a4, a5) \
62  logger_put6((log), (fmt), (int)(a0), (int)(a1), (int)(a2), (int)(a3), (int)(a4), (int)(a5))
63 
64 
72 #ifndef LOGGER_MAX_ARGC
73 #define LOGGER_MAX_ARGC 6
74 #endif
75 
77 /* TODO: Packed, uint8_t for argc? */
78 /* __attribute__((packed)) */
79 struct logger_entry {
81  int argc;
83  const char *fmt;
86 };
87 
92 struct logger {
104  void (*write_cb)(const char *str, size_t length);
106  struct fifo *fifo;
108  char *str;
110  size_t str_size;
113 };
114 
128 #define LOGGER_INIT(log, log_write_cb, log_capacity, str_capacity) \
129  do { \
130  static struct fifo logger_fifo; \
131  FIFO_INIT(&logger_fifo, sizeof(struct logger_entry), \
132  (log_capacity)); \
133  static char str[(str_capacity)]; \
134  (log)->fifo = &logger_fifo; \
135  (log)->write_cb = (log_write_cb); \
136  (log)->str = (str); \
137  (log)->str_size = (str_capacity); \
138  (log)->write_cb = (log_write_cb); \
139  logger_init((log)); \
140  } while (0)
141 
153 #define LOGGER_PUT(log, ...) \
154  logger_put(log, VA_ARGC(__VA_ARGS__), __VA_ARGS__)
155 // LOGGER_PUT_IMPL(log, VA_ARGC(__VA_ARGS__), __VA_ARGS__)
156 
157 
158 bool logger_init(struct logger *log);
159 bool logger_put(const struct logger *log, int argc, const char *fmt, ...)
160  __attribute__((format (printf, 3, 4)));
161 bool logger_process(const struct logger *log);
162 
165 bool logger_put0(const struct logger *log, const char *fmt);
166 bool logger_put1(const struct logger *log, const char *fmt, int arg0);
167 bool logger_put2(const struct logger *log, const char *fmt, int arg0, int arg1);
168 bool logger_put3(const struct logger *log, const char *fmt,
169  int arg0, int arg1, int arg2);
170 bool logger_put4(const struct logger *log, const char *fmt,
171  int arg0, int arg1, int arg2, int arg3);
172 bool logger_put5(const struct logger *log, const char *fmt,
173  int arg0, int arg1, int arg2, int arg3, int arg4);
174 bool logger_put6(const struct logger *log, const char *fmt,
175  int arg0, int arg1, int arg2, int arg3, int arg4, int arg5);
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* MCU_COMMON_LOGGER_H */
bool logger_process(const struct logger *log)
Processes a single logged message from the buffer.
Definition: logger.c:180
bool logger_put(const struct logger *log, int argc, const char *fmt,...)
Logs a message (puts it into internal buffer for later processing).
Definition: logger.c:135
bool logger_init(struct logger *log)
Initializes logger.
Definition: logger.c:102
#define LOGGER_MAX_ARGC
The maximum number of logger_put() arguments supported by the implementation.
Definition: logger.h:73
bool logger_put6(const struct logger *log, const char *fmt, int arg0, int arg1, int arg2, int arg3, int arg4, int arg5)
Inserts a message to the logger.
Definition: logger.c:360
bool logger_put1(const struct logger *log, const char *fmt, int arg0)
Inserts a message to the logger.
Definition: logger.c:233
bool logger_put0(const struct logger *log, const char *fmt)
Inserts a message to the logger.
Definition: logger.c:212
bool logger_put3(const struct logger *log, const char *fmt, int arg0, int arg1, int arg2)
Inserts a message to the logger.
Definition: logger.c:279
bool logger_put2(const struct logger *log, const char *fmt, int arg0, int arg1)
Inserts a message to the logger.
Definition: logger.c:255
bool logger_put5(const struct logger *log, const char *fmt, int arg0, int arg1, int arg2, int arg3, int arg4)
Inserts a message to the logger.
Definition: logger.c:332
bool logger_put4(const struct logger *log, const char *fmt, int arg0, int arg1, int arg2, int arg3)
Inserts a message to the logger.
Definition: logger.c:305
FIFO instance.
Definition: fifo.h:63
Logger entry (used internally)
Definition: logger.h:79
const char * fmt
Format string to be passed to sprintf
Definition: logger.h:83
int argc
Number of arguments in argv (0 to LOGGER_MAX_ARGC)
Definition: logger.h:81
int argv[LOGGER_MAX_ARGC]
Array of arguments to be passed to sprintf
Definition: logger.h:85
Logger instance.
Definition: logger.h:92
void(* write_cb)(const char *str, size_t length)
Pointer to write callback implemented by driver.
Definition: logger.h:104
size_t str_size
Size of the string buffer.
Definition: logger.h:110
struct fifo * fifo
Pointer to fifo instance for storing logger_entry entries.
Definition: logger.h:106
char * str
Pointer to a buffer used to store string composed by sprintf.
Definition: logger.h:108
bool initialized
Logger initialized flag (handled internally)
Definition: logger.h:112