forked from libcsp/libcsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsp_debug.c
133 lines (114 loc) · 3.16 KB
/
csp_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef __AVR__
#include <avr/pgmspace.h>
#endif
/* CSP includes */
#include <csp/csp.h>
#include <csp/arch/csp_system.h>
/* Custom debug function */
csp_debug_hook_func_t csp_debug_hook_func = NULL;
/* Debug levels */
static bool csp_debug_level_enabled[] = {
[CSP_ERROR] = true,
[CSP_WARN] = true,
[CSP_INFO] = false,
[CSP_BUFFER] = false,
[CSP_PACKET] = false,
[CSP_PROTOCOL] = false,
[CSP_LOCK] = false,
};
/* Some compilers do not support weak symbols, so this function
* can be used instead to set a custom debug hook */
void csp_debug_hook_set(csp_debug_hook_func_t f)
{
csp_debug_hook_func = f;
}
void do_csp_debug(csp_debug_level_t level, const char *format, ...)
{
int color = COLOR_RESET;
va_list args;
/* Don't print anything if log level is disabled */
if (level > CSP_LOCK || !csp_debug_level_enabled[level])
return;
switch(level) {
case CSP_INFO:
color = COLOR_GREEN | COLOR_BOLD;
break;
case CSP_ERROR:
color = COLOR_RED | COLOR_BOLD;
break;
case CSP_WARN:
color = COLOR_YELLOW | COLOR_BOLD;
break;
case CSP_BUFFER:
color = COLOR_MAGENTA;
break;
case CSP_PACKET:
color = COLOR_GREEN;
break;
case CSP_PROTOCOL:
color = COLOR_BLUE;
break;
case CSP_LOCK:
color = COLOR_CYAN;
break;
default:
return;
}
va_start(args, format);
/* If csp_debug_hook symbol is defined, pass on the message.
* Otherwise, just print with pretty colors ... */
if (csp_debug_hook_func) {
csp_debug_hook_func(level, format, args);
} else {
csp_sys_set_color(color);
#ifdef __AVR__
vfprintf_P(stdout, format, args);
#else
vprintf(format, args);
#endif
printf("\r\n");
csp_sys_set_color(COLOR_RESET);
}
va_end(args);
}
void csp_debug_set_level(csp_debug_level_t level, bool value)
{
if (level > CSP_LOCK)
return;
csp_debug_level_enabled[level] = value;
}
int csp_debug_get_level(csp_debug_level_t level)
{
if (level > CSP_LOCK)
return 0;
return csp_debug_level_enabled[level];
}
void csp_debug_toggle_level(csp_debug_level_t level)
{
if (level > CSP_LOCK) {
printf("Max level is 6\r\n");
return;
}
csp_debug_level_enabled[level] = (csp_debug_level_enabled[level]) ? false : true;
printf("Level %u: value %u\r\n", level, csp_debug_level_enabled[level]);
}