-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
155 lines (132 loc) · 4.07 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
147
148
149
150
151
152
153
154
155
#pragma once
#include <sstream>
#include "global_include.h"
#if defined(ANDROID)
#include <android/log.h>
#else
#include <iostream>
#include <ctime>
#endif
#define LogDebug() LogDebugDetailed(__FILENAME__, __LINE__)
#define LogInfo() LogInfoDetailed(__FILENAME__, __LINE__)
#define LogWarn() LogWarnDetailed(__FILENAME__, __LINE__)
#define LogErr() LogErrDetailed(__FILENAME__, __LINE__)
namespace mavsdk {
enum class Color { Red, Green, Yellow, Blue, Gray, Reset };
void set_color(Color color);
class LogDetailed {
public:
LogDetailed(const char* filename, int filenumber) :
_s(),
_caller_filename(filename),
_caller_filenumber(filenumber)
{}
template<typename T> LogDetailed& operator<<(const T& x)
{
_s << x;
return *this;
}
virtual ~LogDetailed()
{
#if ANDROID
switch (_log_level) {
case LogLevel::Debug:
__android_log_print(ANDROID_LOG_DEBUG, "Mavsdk", "%s", _s.str().c_str());
break;
case LogLevel::Info:
__android_log_print(ANDROID_LOG_INFO, "Mavsdk", "%s", _s.str().c_str());
break;
case LogLevel::Warn:
__android_log_print(ANDROID_LOG_WARN, "Mavsdk", "%s", _s.str().c_str());
break;
case LogLevel::Err:
__android_log_print(ANDROID_LOG_ERROR, "Mavsdk", "%s", _s.str().c_str());
break;
}
// Unused:
(void)_caller_filename;
(void)_caller_filenumber;
#else
switch (_log_level) {
case LogLevel::Debug:
#ifdef NDEBUG
return;
#endif
set_color(Color::Green);
break;
case LogLevel::Info:
set_color(Color::Blue);
break;
case LogLevel::Warn:
set_color(Color::Yellow);
break;
case LogLevel::Err:
set_color(Color::Red);
break;
}
// Time output taken from:
// https://stackoverflow.com/questions/16357999#answer-16358264
time_t rawtime;
time(&rawtime);
struct tm* timeinfo = localtime(&rawtime);
char time_buffer[10]{}; // We need 8 characters + \0
strftime(time_buffer, sizeof(time_buffer), "%I:%M:%S", timeinfo);
std::cout << "[" << time_buffer;
switch (_log_level) {
case LogLevel::Debug:
std::cout << "|Debug] ";
break;
case LogLevel::Info:
std::cout << "|Info ] ";
break;
case LogLevel::Warn:
std::cout << "|Warn ] ";
break;
case LogLevel::Err:
std::cout << "|Error] ";
break;
}
set_color(Color::Reset);
std::cout << _s.str();
std::cout << " (" << _caller_filename << ":" << std::dec << _caller_filenumber << ")";
std::cout << '\n';
#endif
}
LogDetailed(const mavsdk::LogDetailed&) = delete;
void operator=(const mavsdk::LogDetailed&) = delete;
protected:
enum LogLevel { Debug, Info, Warn, Err } _log_level = LogLevel::Debug;
private:
std::stringstream _s;
const char* _caller_filename;
int _caller_filenumber;
};
class LogDebugDetailed : public LogDetailed {
public:
LogDebugDetailed(const char* filename, int filenumber) : LogDetailed(filename, filenumber)
{
_log_level = LogLevel::Debug;
}
};
class LogInfoDetailed : public LogDetailed {
public:
LogInfoDetailed(const char* filename, int filenumber) : LogDetailed(filename, filenumber)
{
_log_level = LogLevel::Info;
}
};
class LogWarnDetailed : public LogDetailed {
public:
LogWarnDetailed(const char* filename, int filenumber) : LogDetailed(filename, filenumber)
{
_log_level = LogLevel::Warn;
}
};
class LogErrDetailed : public LogDetailed {
public:
LogErrDetailed(const char* filename, int filenumber) : LogDetailed(filename, filenumber)
{
_log_level = LogLevel::Err;
}
};
} // namespace mavsdk