-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.h
204 lines (189 loc) · 6.24 KB
/
logger.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
#pragma once // Please format this file with clang before check-in to GitHub
/*
File: logger.h
Software: Barry Hansen, K7BWH, barry@k7bwh.com, Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: "class Logger" prints to the USB port and allows the
listener at the other end to select what messages are sent.
******************************************************************************
Licensed under the GNU General Public License v3.0
Permissions of this strong copyleft license are conditioned on making available
complete source code of licensed works and modifications, which include larger
works using a licensed work, under the same license. Copyright and license
notices must be preserved. Contributors provide an express grant of patent rights.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.en.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
enum {
NMEA = 1,
DEBUG,
INFO,
WARNING,
ERROR,
}
*/
#include <Arduino.h> // for "strncpy" and others
#include "logger.h" // conditional printing to Serial port
// extern
void floatToCharArray(char *result, int maxlen, double fValue, int decimalPlaces); // Griduino.ino
class Logger {
public:
bool print_nmea = true; // set TRUE for NmeaTime2 by www.visualgps.net
bool print_gmt = false; // the time reports are frequent (1 per second) so by default it's off
bool print_fencepost = true;
bool print_debug = true;
bool print_info = true; // set FALSE for NmeaTime2 by www.visualgps.net
bool print_warning = true;
bool print_error = true;
// NMEA messages, such as $GPRMC
// this has frequent output messages (1 per second) so by default it's off
void nmea(const char *pText) {
if (print_nmea) {
//
// ---GPRMC--- ---GPGGA---
// hh:mm:ss hh:mm:ss
// Sat status Latitude
// Latitude Longitude
// Longitude Fix quality
// Ground speed (knots) No. of sats in use
// Track angle Altitude (m)
// DD:MM:YY
//
// this GPS info is required by NMEATime2 by www.visualgps
const char haystack[] = "$GPRMC $GPGGA $GPGSA $GPGSV ";
char needle[7];
strncpy(needle, pText, 6);
needle[6] = 0; // null terminated
Serial.print(pText);
if (strstr("$GPRMC", needle)) {
Serial.println();
}
}
}
// GMT time reports
void gmt(const char *pText) {
if (print_gmt) {
Serial.print(pText);
}
}
void fencepost(const char *pModule, const int lineno) {
// example output: "Griduino.ino[123]"
if (print_fencepost) {
Serial.print(pModule);
Serial.print("[");
Serial.print(lineno);
Serial.println("]");
}
}
void fencepost(const char *pModule, const char *pSubroutine, const int lineno) {
// This is used extensively in unittest.cpp
// example input: logger.fencepost("unittest.cpp", "subroutineName()", __LINE__);
// example output: "----- subroutineName(), unit_test.cpp[123]"
if (print_fencepost) {
Serial.print("----- ");
Serial.print(pSubroutine);
Serial.print(", ");
Serial.print(pModule);
Serial.print("[");
Serial.print(lineno);
Serial.println("] ");
}
}
void info(const char *pText) { // one string arg
if (print_info) {
Serial.println(pText);
}
}
void info(const char *pText, const int value) { // one format string containing %d, one number
if (print_info) {
char msg[256];
snprintf(msg, sizeof(msg), pText, value);
Serial.println(msg);
}
}
void info(const char *pText, const float value1, const int decimalPlaces) { // one format string containing %s, one float, one int
if (print_info) {
char msg[256];
char sFloat[8];
floatToCharArray(sFloat, sizeof(sFloat), value1, decimalPlaces);
snprintf(msg, sizeof(msg), pText, sFloat);
Serial.println(msg);
}
}
void info(const char *pText, const int value1, const int value2) { // one format string, two numbers
if (print_info) {
char msg[256];
snprintf(msg, sizeof(msg), pText, value1, value2);
Serial.println(msg);
}
}
void info(const char *pText1, const char *pText2) { // two string args
if (print_info) {
Serial.print(pText1);
Serial.println(pText2);
}
}
void info(const int int1, const char *pText2) { // linenumber, one string arg
if (print_info) {
Serial.print(int1);
Serial.println(pText2);
}
}
void info(const char *pText1, const char *pText2, const char *pText3) { // three string args
if (print_info) {
Serial.print(pText1);
Serial.print(pText2);
Serial.println(pText3);
}
}
void info(const int int1, const char *pText2, const char *pText3) { // linenumber, two string args
if (print_info) {
Serial.print(int1);
Serial.print(pText2);
Serial.println(pText3);
}
}
void debug(const char *pText) {
if (print_debug) {
Serial.println(pText);
}
}
void warning(const char *pText) {
if (print_warning) {
Serial.println(pText);
}
}
void error(const char *pText) { // one string arg
if (print_error) {
Serial.println(pText);
}
}
void error(const char *pText1, const char *pText2) { // two string args
if (print_error) {
Serial.print(pText1);
Serial.println(pText2);
}
}
void error(const char *pFormatString, const int val1, const int val2 = 0) { // format string, one or two numbers
if (print_error) {
char msg[256];
snprintf(msg, sizeof(msg), pFormatString, val1, val2);
Serial.println(msg);
}
}
void error(const char *pText1, const char *pText2, const char *pText3) { // three string args
if (print_error) {
Serial.print(pText1);
Serial.print(pText2);
Serial.println(pText3);
}
}
protected:
// nothing yet
}; // end class Logger