|
| 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