-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.h
126 lines (101 loc) · 2.32 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
#ifndef LOGGER_H
#define LOGGER_H
#include <inttypes.h>
#include <stdarg.h>
#include <string.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
typedef void (*printfunc)(Print*);
/*
* %s string (char*)
* %c char
* %d int
* %f double
* %t boole
*/
// LOG_LEVEL
// 0 disable
// 1 output error info debug
// 2 output error info
// 3 output error
#define LOG_OFF 0
#define LOG_DEBUG 1
#define LOG_TEST 2
#define LOG_RUN 3
// #define LOG_LEVEL LOG_OFF
#define LOG_LEVEL LOG_DEBUG
// #define LOG_LEVEL LOG_TEST
// #define LOG_LEVEL LOG_RUN
#define debug(msg, args...) _debug(__FILE__,__LINE__,msg, args)
#define error(msg, args...) _error(__FILE__,__LINE__,msg, args)
class Logger
{
public:
Logger()
#if LOG_LEVEL > 0
:_logOutput(NULL)
#endif
{
}
Logger(Print *logOutput){
#if LOG_LEVEL > 0
_logOutput = logOutput;
#endif
}
template <class T,typename... Args> void _debug(const char *_file,int line,T msg, Args... args)
{
#if LOG_LEVEL > 0 && LOG_LEVEL < 2
char file[20];
strcpy(file,_file);
printLog(file,line,"DEBUG",msg, args...);
_logOutput->print(F(" FreeMemory: "));
_logOutput->println(freeMemory());
#endif
}
template <class T,typename... Args> void _error(const char *_file,int line,T msg,Args... args)
{
#if LOG_LEVEL > 0
char file[20];
strcpy(file,_file);
printLog(file,line,"ERROR",msg, args...);
_logOutput->println();
#endif
}
template <class T,typename... Args> void info(T msg, ...)
{
#if LOG_LEVEL > 0 && LOG_LEVEL < 3
va_list args;
va_start(args,msg);
print(msg, args);
#endif
}
void setPrefix(const char *_prefix);
private:
#if LOG_LEVEL > 0
Print* _logOutput;
char prefix[20];
#endif
void print(const __FlashStringHelper *format, va_list args);
void print(const char *format,va_list args);
void printf(const char format,va_list *args);
template <class T> void printLog(const char *file,int line,const char* level,T msg, ...){
#if LOG_LEVEL > 0
if(prefix != NULL) _logOutput->print(prefix);
_logOutput->print(" ");
va_list args;
va_start(args,msg);
print(msg, args);
_logOutput->print(F(" file: "));
_logOutput->print(file);
_logOutput->print(F(" line: "));
_logOutput->print(line);
_logOutput->print(F(" level: "));
_logOutput->print(level);
#endif
}
int freeMemory();
};
#endif