-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.hpp
117 lines (100 loc) · 3.02 KB
/
error.hpp
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
#pragma once
#include <stdint.h>
#include <vector>
#include <sstream>
#include "utils.hpp"
namespace bristol
{
//----------------------------------------------------------------------------------
enum LogLevel
{
LogLevelNone,
LogLevelDebug,
LogLevelInfo,
LogLevelWarning,
LogLevelError,
};
enum LogFlags
{
LogFlagsNaked = 1 << 0,
};
struct LogEntry;
typedef std::function<void (const LogEntry& entry)> fnLogCallback;
void SetLogCallback(const fnLogCallback& cb);
void SetBreakOnError(bool value);
struct LogEntry
{
LogLevel level;
uint32_t flags;
const char* file;
uint32_t line;
const char* msg;
};
//----------------------------------------------------------------------------------
struct LogSink
{
LogSink();
virtual ~LogSink();
virtual void Log(const LogEntry& entry) = 0;
};
//----------------------------------------------------------------------------------
struct LogSinkFile : public LogSink
{
LogSinkFile();
~LogSinkFile();
bool Open(const char* filename);
virtual void Log(const LogEntry& entry) override;
FILE* _file;
};
//----------------------------------------------------------------------------------
struct LogSinkConsole : public LogSink
{
virtual void Log(const LogEntry& entry) override;
};
//----------------------------------------------------------------------------------
struct LogSinkApp : public LogSink
{
virtual void Log(const LogEntry& entry) override;
};
//----------------------------------------------------------------------------------
struct LogStream
{
LogStream(LogLevel level, const char* file, uint32_t line, uint32_t flags);
~LogStream();
template <typename T>
void Log(const T& t)
{
std::ostringstream str;
str << t;
_curMessage.append(str.str());
}
template <typename T, typename... Args>
void Log(const T& head, Args... tail)
{
Log(head);
Log(tail...);
}
std::string _curMessage;
LogLevel _level;
const char* _file;
uint32_t _line;
uint32_t _flags;
};
// The minimum level at which we log
LogLevel GetLogLevel();
void SetLogLevel(LogLevel level);
#define LOG_DEBUG(...) \
bristol::LogStream (bristol::LogLevelDebug, __FILE__, __LINE__, 0).Log(__VA_ARGS__);
#define LOG_INFO(...) \
bristol::LogStream(bristol::LogLevelInfo, __FILE__, __LINE__, 0).Log(__VA_ARGS__);
#define LOG_WARN(...) \
bristol::LogStream(bristol::LogLevelWarning, __FILE__, __LINE__, 0).Log(__VA_ARGS__);
#define LOG_ERROR(...) \
bristol::LogStream(bristol::LogLevelError, __FILE__, __LINE__, 0).Log(__VA_ARGS__);
#define LOG_INFO_NAKED(...) \
bristol::LogStream(bristol::LogLevelInfo, __FILE__, __LINE__, LogFlagsNaked).Log(__VA_ARGS__);
#define LOG_WARN_NAKED(...) \
bristol::LogStream(bristol::LogLevelWarning, __FILE__, __LINE__, LogFlagsNaked).Log(__VA_ARGS__);
#define LOG_ERROR_NAKED(...) \
bristol::LogStream(bristol::LogLevelError, __FILE__, __LINE__, LogFlagsNaked).Log(__VA_ARGS__);
}