-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathLog.h
More file actions
72 lines (60 loc) · 1.87 KB
/
Log.h
File metadata and controls
72 lines (60 loc) · 1.87 KB
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
#ifndef UWS_LOG_H
#define UWS_LOG_H
#include <array>
#include <functional>
#include <iostream>
#include <string_view>
#define UWS_LOG_LEVEL 0 // override this to get more verbose logging
namespace uWS {
/*!
* \brief Log a message. The user of this lib is free to override this function object with a custom logger.
* \param [in] message The message to be logged.
* \param [in] logLevel The severity of the message. 0 is error, 1 is warning, 2 is info. With each increment the severity decrements.
*/
typedef std::function<void(std::string_view message, int logLevel)> LogFunction;
inline LogFunction log = [](std::string_view message, int logLevel) -> void {
if(logLevel <= 1) {
std::cerr << message << std::endl;
}
else {
std::cout << message << std::endl;
}
};
class LogBuffer
{
private:
std::array<char,1024> buf{0};
size_t cursor = 0;
LogBuffer& operator<< (std::string_view sv) {
const size_t bytesFree = buf.size() - cursor;
if(sv.size() > bytesFree)
throw std::length_error("log message too long");
memcpy(&buf[cursor], sv.data(), sv.size());
cursor += sv.size();
return *this;
}
public:
template <typename T>
void put(T msg) {
*this << msg;
}
template <typename T, typename... Ts>
void put(T t, Ts... ts) {
put(t);
put(ts...);
}
std::string_view get() {
std::string_view ret(buf.data(), cursor);
cursor = 0;
return ret;
}
};
thread_local LogBuffer logBuffer;
#define UWS_LOG_REQUEST(loglevel, ...) { \
if constexpr(loglevel <= UWS_LOG_LEVEL) { \
::uWS::logBuffer.put(__VA_ARGS__); \
::uWS::log(::uWS::logBuffer.get(), loglevel); \
} \
}
} // namespace uWS
#endif // UWS_LOG_H