-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.h
221 lines (201 loc) · 6.23 KB
/
Log.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
ArduinoLog 1.0.0
Copyright (c) 2011 Michael C Dillon. All rights reserved.
Simple logging utility for the Arduino platform.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdarg.h"
/**
* Provides a simple and lightweight logging enviroment for
* debugging and development purposes. Probably should not
* be left enabled when flying.
*
* Supported formatters:
* %b - binary value
* %d - int / byte
* %f - double / float
* %s - char string
* %x - hex value
*
* To enable the Log, place the following define statement in
* the class you are including everthing in:
* #define DEVELOPMENT_LOG
*
* To set the log level place the following define statement in
* the class you are including everything in:
* #define LOG_LEVEL <Log_Level>
*
* Make sure you have the include statements setup properly after
* the Log define statements.
*
* Example Usage:
*
* #define DEVELOPMENT_LOG
* #define LOG_LEVEL INFO_LEVEL
* #include Log.h
*
* Log::fine ("Fine statement");
* Log::debug ("Debug statement");
* Log::info ("Info statement ");
* Log::warn ("Warn statement");
* Log::error ("Error Statement");
*
* Log::info ("Int: %d", 5);
* Log::info ("Double: %f", 5.5);
* Log::info ("Chars: %s", "test string");
* Log::info ("Binary of 5: %b", 5);
* Log::info ("Hex of 47: %x", 47);
*
* Log::info ("Unrecognized formatter: %p", 1);
*
*/
#define LOG_ALL 0
#define FINE_LEVEL 0
#define DEBUG_LEVEL 1
#define INFO_LEVEL 2
#define WARN_LEVEL 3
#define ERROR_LEVEL 4
#define LOG_OFF 5
class Log {
private:
static const char fine_prefix[];
static const char debug_prefix[];
static const char info_prefix[];
static const char warn_prefix[];
static const char error_prefix[];
/**
* Prints the prefix of the message and the actual message
* to the standard Serial port.
*/
static void print_msg (const char prefix[], char msg[], va_list ap) {
#ifdef DEVELOPMENT_LOG
float temp_float;
int temp_int;
char* temp_chars;
int i = 0;
char cur;
Serial.print (prefix);
while ((cur = msg[i]) != '\0') {
if (cur != '%') {
Serial.print (cur);
i++;
}
else {
i++;
cur = msg[i];
switch (cur) {
case 'b':
temp_int = va_arg (ap, int);
Serial.print (temp_int, BIN);
break;
case 'd':
temp_int = va_arg (ap, int);
Serial.print (temp_int);
break;
case 'f':
temp_float = va_arg (ap, double);
Serial.print (temp_float);
break;
case 's':
temp_chars = va_arg (ap, char*);
Serial.print (temp_chars);
break;
case 'x':
temp_int = va_arg (ap, int);
Serial.print (temp_int, HEX);
break;
default:
Serial.print ("<Log Error: Unrecognized Formatter: %");
Serial.print (cur);
Serial.print (" >");
break;
}
i++;
}
}
Serial.print ('\n');
#endif
}
public:
/**
* Prints a prefixed fine statment message
*/
static void fine (char msg[], ...) {
#ifdef DEVELOPMENT_LOG
if (LOG_LEVEL <= FINE_LEVEL) {
va_list ap;
va_start (ap, msg);
print_msg (fine_prefix, msg, ap);
va_end (ap);
}
#endif
}
/**
* Prints a prefixed debug statment message
*/
static void debug (char msg[], ...) {
#ifdef DEVELOPMENT_LOG
if (LOG_LEVEL <= DEBUG_LEVEL) {
va_list ap;
va_start (ap, msg);
print_msg (debug_prefix, msg, ap);
va_end (ap);
}
#endif
}
/**
* Prints a prefixed info statment message
*/
static void info (char msg[], ...) {
#ifdef DEVELOPMENT_LOG
if (LOG_LEVEL <= INFO_LEVEL) {
va_list ap;
va_start (ap, msg);
print_msg (info_prefix, msg, ap);
va_end (ap);
}
#endif
}
/**
* Prints a prefixed warn statment message
*/
static void warn (char msg[], ...) {
#ifdef DEVELOPMENT_LOG
if (LOG_LEVEL <= WARN_LEVEL) {
va_list ap;
va_start (ap, msg);
print_msg (warn_prefix, msg, ap);
va_end (ap);
}
#endif
}
/**
* Prints a prefixed error statment message
*/
static void error (char msg[], ...) {
#ifdef DEVELOPMENT_LOG
if (LOG_LEVEL <= ERROR_LEVEL) {
va_list ap;
va_start (ap, msg);
print_msg (error_prefix, msg, ap);
va_end (ap);
}
#endif
}
};
#ifdef DEVELOPMENT_LOG
const char Log::fine_prefix[] = "[FINE] ";
const char Log::debug_prefix[] = "[DEBUG] ";
const char Log::info_prefix[] = "[INFO] ";
const char Log::warn_prefix[] = "[WARN] ";
const char Log::error_prefix[] = "[ERROR] ";
#endif