Skip to content

Commit 119b82a

Browse files
committed
First Commit of library
1 parent 13e4360 commit 119b82a

File tree

104 files changed

+608
-5340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+608
-5340
lines changed

ArduinoLog.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
_ ___ ___ _ _ ___ _ _ ___ _ ___ ___
3+
/_\ | _ \ \| | | |_ _| \| |/ _ \| | / _ \ / __|
4+
/ _ \| / |) | |_| || || .` | (_) | |_| (_) | (_ |
5+
/_/ \_\_|_\___/ \___/|___|_|\_|\___/|____\___/ \___|
6+
7+
Log library for Arduino
8+
version 1.0.0
9+
https://github.com/thijse/Arduino-Log
10+
11+
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a copy
14+
of this software and associated documentation files (the "Software"), to deal
15+
in the Software without restriction, including without limitation the rights
16+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
copies of the Software, and to permit persons to whom the Software is
18+
furnished to do so, subject to the following conditions:
19+
20+
The above copyright notice and this permission notice shall be included in all
21+
copies or substantial portions of the Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
SOFTWARE.
30+
*/
31+
32+
#include "ArduinoLog.h"
33+
34+
void Logging::begin(int level, Print* logOutput, bool showLevel){
35+
_level = constrain(level,LOG_LEVEL_SILENT,LOG_LEVEL_VERBOSE);
36+
_showLevel = showLevel;
37+
_logOutput = logOutput;
38+
}
39+
40+
void Logging::print(const __FlashStringHelper *format, va_list args) {
41+
#ifndef DISABLE_LOGGING
42+
PGM_P p = reinterpret_cast<PGM_P>(format);
43+
char c = pgm_read_byte(p++);
44+
for(;c != 0; c = pgm_read_byte(p++)){
45+
if (c == '%') {
46+
c = pgm_read_byte(p++);
47+
printFormat(c, &args);
48+
} else {
49+
_logOutput->print(c);
50+
}
51+
}
52+
#endif
53+
}
54+
55+
void Logging::print(const char *format, va_list args) {
56+
#ifndef DISABLE_LOGGING
57+
for (; *format != 0; ++format) {
58+
if (*format == '%') {
59+
++format;
60+
printFormat(*format, &args);
61+
} else {
62+
_logOutput->print(*format);
63+
}
64+
}
65+
#endif
66+
}
67+
68+
void Logging::printFormat(const char format, va_list *args) {
69+
#ifndef DISABLE_LOGGING
70+
if (format == '\0') return;
71+
72+
if (format == '%') {
73+
_logOutput->print(format);
74+
return;
75+
}
76+
77+
if( format == 's' ) {
78+
register char *s = (char *)va_arg( *args, int );
79+
_logOutput->print(s);
80+
return;
81+
}
82+
83+
if( format == 'd' || format == 'i') {
84+
_logOutput->print(va_arg( *args, int ),DEC);
85+
return;
86+
}
87+
88+
if( format == 'x' ) {
89+
_logOutput->print(va_arg( *args, int ),HEX);
90+
return;
91+
}
92+
93+
if( format == 'X' ) {
94+
_logOutput->print("0x");
95+
_logOutput->print(va_arg( *args, int ),HEX);
96+
return;
97+
}
98+
99+
if( format == 'b' ) {
100+
_logOutput->print(va_arg( *args, int ),BIN);
101+
return;
102+
}
103+
104+
if( format == 'B' ) {
105+
_logOutput->print("0b");
106+
_logOutput->print(va_arg( *args, int ),BIN);
107+
return;
108+
}
109+
110+
if( format == 'l' ) {
111+
_logOutput->print(va_arg( *args, long ),DEC);
112+
return;
113+
}
114+
115+
if( format == 'c' ) {
116+
_logOutput->print(va_arg( *args, int ));
117+
return;
118+
}
119+
120+
if( format == 't' ) {
121+
if (va_arg( *args, int ) == 1) {
122+
_logOutput->print("T");
123+
}
124+
else {
125+
_logOutput->print("F");
126+
}
127+
return;
128+
}
129+
130+
if( format == 'T' ) {
131+
if (va_arg( *args, int ) == 1) {
132+
_logOutput->print(F("true"));
133+
}
134+
else {
135+
_logOutput->print(F("false"));
136+
}
137+
return;
138+
}
139+
#endif
140+
}
141+
142+
Logging Log = Logging();
143+
144+
145+
146+
147+
148+
149+
150+

ArduinoLog.h

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
_ ___ ___ _ _ ___ _ _ ___ _ ___ ___
3+
/_\ | _ \ \| | | |_ _| \| |/ _ \| | / _ \ / __|
4+
/ _ \| / |) | |_| || || .` | (_) | |_| (_) | (_ |
5+
/_/ \_\_|_\___/ \___/|___|_|\_|\___/|____\___/ \___|
6+
7+
Log library for Arduino
8+
version 1.0.0
9+
https://github.com/thijse/Arduino-Log
10+
11+
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
12+
13+
*/
14+
15+
#ifndef LOGGING_H
16+
#define LOGGING_H
17+
#include <inttypes.h>
18+
#include <stdarg.h>
19+
#if defined(ARDUINO) && ARDUINO >= 100
20+
#include "Arduino.h"
21+
#else
22+
#include "WProgram.h"
23+
#endif
24+
25+
//#include <stdint.h>
26+
//#include <stddef.h>
27+
// *************************************************************************
28+
// Uncomment line below to fully disable logging, and reduce project size
29+
// ************************************************************************
30+
//#define DISABLE_LOGGING
31+
32+
33+
#define LOG_LEVEL_SILENT 0
34+
#define LOG_LEVEL_FATAL 1
35+
#define LOG_LEVEL_ERROR 2
36+
#define LOG_LEVEL_WARNING 3
37+
#define LOG_LEVEL_NOTICE 4
38+
#define LOG_LEVEL_VERBOSE 5
39+
40+
#define CR "\n"
41+
#define LOGGING_VERSION 1_0_0
42+
43+
/*!
44+
Logging is a helper class to output informations over
45+
RS232. If you know log4j or log4net, this logging class
46+
is more or less similar ;-) <br>
47+
Different loglevels can be used to extend or reduce output
48+
All methods are able to handle any number of output parameters.
49+
All methods print out a formated string (like printf).<br>
50+
To reduce output and program size, reduce loglevel.
51+
52+
Output format string can contain below wildcards. Every wildcard
53+
must be start with percent sign (\%)
54+
55+
**** Wildcards
56+
57+
* %s replace with an string (char*)
58+
* %c replace with an character
59+
* %d replace with an integer value
60+
* %l replace with an long value
61+
* %x replace and convert integer value into hex
62+
* %X like %x but combine with 0x123AB
63+
* %b replace and convert integer value into binary
64+
* %B like %x but combine with 0b10100011
65+
* %t replace and convert boolean value into "t" or "f"
66+
* %T like %t but convert into "true" or "false"
67+
68+
**** Loglevels
69+
70+
* 0 - LOG_LEVEL_SILENT no output
71+
* 1 - LOG_LEVEL_FATAL fatal errors
72+
* 2 - LOG_LEVEL_ERROR all errors
73+
* 3 - LOG_LEVEL_WARNING errors, and warnings
74+
* 4 - LOG_LEVEL_NOTICE errors, warnings and notices
75+
* 5 - LOG_LEVEL_VERBOSE all
76+
*/
77+
78+
class Logging {
79+
private:
80+
int _level;
81+
bool _showLevel;
82+
Print* _logOutput;
83+
public:
84+
/*!
85+
* default Constructor
86+
*/
87+
Logging()
88+
: _level(LOG_LEVEL_SILENT),
89+
_showLevel(true),
90+
_logOutput(NULL) {}
91+
92+
93+
/**
94+
* Initializing, must be called as first. Note that if you use
95+
* this variant of Init, you need to initialize the baud rate
96+
* yourself, if printer happens to be a serial port.
97+
* \param level - logging levels <= this will be logged.
98+
* \param printer - place that logging output will be sent to.
99+
* \return void
100+
*
101+
*/
102+
void begin(int level, Print *output, bool showLevel = true);
103+
104+
/**
105+
* Output an error message. Output message contains
106+
* ERROR: followed by original msg
107+
* Error messages are printed out, at every loglevel
108+
* except 0 ;-)
109+
* \param msg format string to output
110+
* \param ... any number of variables
111+
* \return void
112+
*/
113+
template <class T> void fatal(T msg, ...){
114+
#ifndef DISABLE_LOGGING
115+
if (LOG_LEVEL_FATAL <= _level) {
116+
if (_showLevel) _logOutput->print("F: ");
117+
va_list args;
118+
va_start(args, msg);
119+
print(msg,args);
120+
}
121+
#endif
122+
}
123+
124+
/**
125+
* Output an error message. Output message contains
126+
* ERROR: followed by original msg
127+
* Error messages are printed out, at every loglevel
128+
* except 0 ;-)
129+
* \param msg format string to output
130+
* \param ... any number of variables
131+
* \return void
132+
*/
133+
template <class T> void error(T msg, ...){
134+
#ifndef DISABLE_LOGGING
135+
if (LOG_LEVEL_ERROR <= _level) {
136+
if (_showLevel) _logOutput->print("E: ");
137+
va_list args;
138+
va_start(args, msg);
139+
print(msg,args);
140+
}
141+
#endif
142+
}
143+
/**
144+
* Output an info message. Output message contains
145+
* Info messages are printed out at l
146+
* loglevels >= LOG_LEVEL_INFOS
147+
*
148+
* \param msg format string to output
149+
* \param ... any number of variables
150+
* \return void
151+
*/
152+
153+
template <class T> void warning(T msg, ...){
154+
#ifndef DISABLE_LOGGING
155+
if (LOG_LEVEL_WARNING <= _level) {
156+
if (_showLevel) _logOutput->print("W: ");
157+
va_list args;
158+
va_start(args, msg);
159+
print(msg,args);
160+
}
161+
#endif
162+
}
163+
/**
164+
* Output an debug message. Output message contains
165+
* Debug messages are printed out at l
166+
* loglevels >= LOG_LEVEL_DEBUG
167+
*
168+
* \param msg format string to output
169+
* \param ... any number of variables
170+
* \return void
171+
*/
172+
173+
template <class T> void notice(T msg, ...){
174+
#ifndef DISABLE_LOGGING
175+
if (LOG_LEVEL_NOTICE <= _level) {
176+
if (_showLevel) _logOutput->print("N: ");
177+
va_list args;
178+
va_start(args, msg);
179+
print(msg,args);
180+
}
181+
#endif
182+
}
183+
/**
184+
* Output an verbose message. Output message contains
185+
* Debug messages are printed out at l
186+
* loglevels >= LOG_LEVEL_VERBOSE
187+
*
188+
* \param msg format string to output
189+
* \param ... any number of variables
190+
* \return void
191+
*/
192+
template <class T> void verbose(T msg, ...){
193+
#ifndef DISABLE_LOGGING
194+
if (LOG_LEVEL_VERBOSE <= _level) {
195+
if (_showLevel) _logOutput->print("V: ");
196+
va_list args;
197+
va_start(args, msg);
198+
print(msg,args);
199+
}
200+
#endif
201+
}
202+
203+
private:
204+
void print(const char *format, va_list args);
205+
void print(const __FlashStringHelper *format, va_list args);
206+
void printFormat(const char format, va_list *args);
207+
};
208+
209+
extern Logging Log;
210+
#endif
211+
212+
213+
214+

0 commit comments

Comments
 (0)