csp_debug.h
Go to the documentation of this file.
1 /*
2 Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3 Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
4 Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
27 #ifndef _CSP_DEBUG_H_
28 #define _CSP_DEBUG_H_
29 
30 #include <inttypes.h>
31 #include <string.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
38 typedef enum {
39  CSP_ERROR = 0,
40  CSP_WARN = 1,
41  CSP_INFO = 2,
45  CSP_LOCK = 6,
47 
48 extern unsigned char csp_debug_level_enabled[];
49 
50 /* Extract filename component from path */
51 #define BASENAME(_file) ((strrchr(_file, '/') ? : (strrchr(_file, '\\') ? : _file))+1)
52 
53 /* Implement csp_assert_fail_action to override default failure action */
54 extern void __attribute__((weak)) csp_assert_fail_action(char *assertion, const char *file, int line);
55 
56 #ifndef NDEBUG
57  #define csp_assert(exp) \
58  do { \
59  if (!(exp)) { \
60  char *assertion = #exp; \
61  const char *file = BASENAME(__FILE__); \
62  int line = __LINE__; \
63  printf("\E[1;31m[%02"PRIu8"] Assertion \'%s\' failed in %s:%d\E[0m\r\n", \
64  csp_get_address(), assertion, file, line); \
65  if (csp_assert_fail_action) \
66  csp_assert_fail_action(assertion, file, line); \
67  } \
68  } while (0)
69 #else
70  #define csp_assert(...) do {} while (0)
71 #endif
72 
73 #ifdef __AVR__
74  #include <avr/pgmspace.h>
75  #define CONSTSTR(data) PSTR(data)
76  #undef printf
77  #undef sscanf
78  #undef scanf
79  #undef sprintf
80  #undef snprintf
81  #define printf(s, ...) printf_P(PSTR(s), ## __VA_ARGS__)
82  #define sscanf(buf, s, ...) sscanf_P(buf, PSTR(s), ## __VA_ARGS__)
83  #define scanf(s, ...) scanf_P(PSTR(s), ## __VA_ARGS__)
84  #define sprintf(buf, s, ...) sprintf_P(buf, PSTR(s), ## __VA_ARGS__)
85  #define snprintf(buf, size, s, ...) snprintf_P(buf, size, PSTR(s), ## __VA_ARGS__)
86 #else
87  #define CONSTSTR(data) data
88 #endif
89 
90 #ifdef CSP_DEBUG
91 #ifdef CSP_VERBOSE
92  #define csp_debug(level, format, ...) do { if (csp_debug_level_enabled[level]) { do_csp_debug(level, CONSTSTR("[%02"PRIu8"] %s:%d " format), csp_get_address(), BASENAME(__FILE__), __LINE__, ##__VA_ARGS__);}} while(0)
93 #else
94  #define csp_debug(level, format, ...) do { if (csp_debug_level_enabled[level]) { do_csp_debug(level, CONSTSTR(format), ##__VA_ARGS__);}} while(0)
95 #endif
96 #else
97  #define csp_debug(...) do {} while (0)
98 #endif
99 
100 #ifdef CSP_LOG_LEVEL_ERROR
101  #define csp_log_error(format, ...) csp_debug(CSP_ERROR, format, ##__VA_ARGS__)
102 #else
103  #define csp_log_error(...) do {} while (0)
104 #endif
105 
106 #ifdef CSP_LOG_LEVEL_WARN
107  #define csp_log_warn(format, ...) csp_debug(CSP_WARN, format, ##__VA_ARGS__)
108 #else
109  #define csp_log_warn(...) do {} while (0)
110 #endif
111 
112 #ifdef CSP_LOG_LEVEL_INFO
113  #define csp_log_info(format, ...) csp_debug(CSP_INFO, format, ##__VA_ARGS__)
114 #else
115  #define csp_log_info(...) do {} while (0)
116 #endif
117 
118 #ifdef CSP_LOG_LEVEL_DEBUG
119  #define csp_log_buffer(format, ...) csp_debug(CSP_BUFFER, format, ##__VA_ARGS__)
120  #define csp_log_packet(format, ...) csp_debug(CSP_PACKET, format, ##__VA_ARGS__)
121  #define csp_log_protocol(format, ...) csp_debug(CSP_PROTOCOL, format, ##__VA_ARGS__)
122  #define csp_log_lock(format, ...) csp_debug(CSP_LOCK, format, ##__VA_ARGS__)
123 #else
124  #define csp_log_buffer(...) do {} while (0)
125  #define csp_log_packet(...) do {} while (0)
126  #define csp_log_protocol(...) do {} while (0)
127  #define csp_log_lock(...) do {} while (0)
128 #endif
129 
135 void do_csp_debug(csp_debug_level_t level, const char *format, ...);
136 
142 
148 void csp_debug_set_level(csp_debug_level_t level, bool value);
149 
156 
157 #ifdef __cplusplus
158 } /* extern "C" */
159 #endif
160 
161 #endif // _CSP_DEBUG_H_
162 
163 /* @} */
Definition: csp_debug.h:45
int csp_debug_get_level(csp_debug_level_t level)
Get current debug level value.
Definition: csp_debug.h:42
void csp_debug_set_level(csp_debug_level_t level, bool value)
Set debug level.
Definition: csp_debug.h:44
csp_debug_level_t
Debug levels.
Definition: csp_debug.h:38
void csp_debug_toggle_level(csp_debug_level_t level)
Toggle debug level on/off.
Definition: csp_debug.h:41
Definition: csp_debug.h:43
void do_csp_debug(csp_debug_level_t level, const char *format,...)
This function should not be used directly, use csp_log_<level>() macro instead.
Definition: csp_debug.h:40
void csp_assert_fail_action(char *assertion, const char *file, int line)
Definition: csp_debug.h:39
unsigned char csp_debug_level_enabled[]