-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.h
146 lines (129 loc) · 5.27 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
/*
* Log.h
*
* Created on: Jul 3, 2016
* Author: lieven
*/
#ifndef LOG_H_
#define LOG_H_
#include <Sema.h>
#include <Sys.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <string>
#define myASSERT(xxx) \
if (!(xxx)) { \
WARN(" assertion " #xxx " failed."); \
};
extern std::string& string_format(std::string& str, const char* fmt, ...);
void bytesToHex(std::string& ret, uint8_t* input, uint32_t length,
char sep = 0);
typedef void (*LogFunction)(char* start, uint32_t length);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
using cstr = const char* const;
static constexpr cstr past_last_slash(cstr str, cstr last_slash) {
return *str == '\0' ? last_slash
: *str == '/' ? past_last_slash(str + 1, str + 1)
: past_last_slash(str + 1, last_slash);
}
static constexpr cstr past_last_slash(cstr str) {
return past_last_slash(str, str);
}
#define __SHORT_FILE__ \
({ \
constexpr cstr sf__{past_last_slash(__FILE__)}; \
sf__; \
})
//#pragma GCC diagnostic pop
class Log {
public:
typedef enum {
LOG_TRACE = 0,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_NONE
} LogLevel;
static char _logLevel[7];
std::string* _line;
private:
bool _enabled;
LogFunction _logFunction;
char _hostname[20];
char _application[20];
LogLevel _level;
#if !defined(ARDUINO)
Sema& _sema;
#endif
public:
Log(uint32_t size);
~Log();
bool enabled(LogLevel level);
void setLogLevel(char l);
void disable();
void enable();
void defaultOutput();
void writer(LogFunction function);
LogFunction writer();
void printf(const char* fmt, ...);
void log(char level, const char* file, uint32_t line, const char* function,
const char* fmt, ...);
void vprintf(const char* fmt, va_list args);
const char* time();
void host(const char* hostname);
void location(const char* module, uint32_t line);
void application(const char* applicationName);
void flush();
void level(LogLevel l);
void logLevel();
static void serialLog(char* start, uint32_t length);
LogLevel level();
};
extern Log logger;
//#include <cstdio>
#define LOGF(fmt, ...) \
logger.log(__FLE__, __SHORT_FILE__, __PRETTY_FUNCTION__, fmt, ##__VA_ARGS__)
/*
#define LOGF(fmt, ...) \ logger.log(__FLE__, __LINE__, __PRETTY_FUNCTION__,
fmt, ##__VA_ARGS__)
*/
#undef ASSERT
#define ASSERT(xxx) \
if (!(xxx)) { \
WARN(" ASSERT FAILED : %s", #xxx); \
while (1) { \
Sys::delay(1000); \
}; \
}
#define INFO(fmt, ...) \
if (logger.enabled(Log::LOG_INFO)) \
logger.log('I', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define ERROR(fmt, ...) \
if (logger.enabled(Log::LOG_ERROR)) \
logger.log('E', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define WARN(fmt, ...) \
if (logger.enabled(Log::LOG_WARN)) \
logger.log('W', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define FATAL(fmt, ...) \
if (logger.enabled(Log::LOG_FATAL)) \
logger.log('F', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define DEBUG(fmt, ...) \
if (logger.enabled(Log::LOG_DEBUG)) \
logger.log('D', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define TRACE(fmt, ...) \
if (logger.enabled(Log::LOG_TRACE)) \
logger.log('T', __SHORT_FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \
##__VA_ARGS__)
#define __FLE__ \
(__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 \
: __FILE__)
#endif /* LOG_H_ */