-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathILog.h
144 lines (112 loc) · 11.2 KB
/
ILog.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
#if !defined(ILOG_H)
#define ILOG_H
#if defined(__cplusplus)
extern "C" {
#endif
// Github repo link https://github.com/Immanuel-C/ILog
// This library is under the MIT licence
#include "ILogCore.h"
#include "DebugBreak.h"
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
#define _I_COLOUR_WHITE 1
#define _I_COLOUR_GREEN 2
#define _I_COLOUR_YELLOW 3
#define _I_COLOUR_RED 4
#define _I_COLOUR_FATAL_RED 5
#if defined(_MSC_VER) || defined(__clang__)
#define _I_VA_ARGS(...) , __VA_ARGS__
#else
#define _I_VA_ARGS(...) , ##__VA_ARGS__
#endif
#define __I_DEBUG_BREAK() debug_break()
#if !defined(NDEBUG)
#define I_DEBUG_LOG_TRACE(msg, ...) _i_log(stdout, "Debug Trace: ", msg, _I_COLOUR_GREEN _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_LOG_INFO(msg, ...) _i_log(stdout, "Debug Info: ", msg, _I_COLOUR_WHITE _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_LOG_WARNING(msg, ...) _i_log(stdout, "Debug Warning: ", msg, _I_COLOUR_YELLOW _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_LOG_ERROR(msg, ...) _i_log(stderr, "Debug Error: ", msg, _I_COLOUR_RED _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_LOG_FATAL_ERROR(msg, ...) _i_log(stderr, "Debug Fatal Error: ", msg, _I_COLOUR_FATAL_RED _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_FILE_LOG(fileName, msg, mode, ...) _f_i_log(fileName, msg, mode _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_FS_LOG(stream, msg, ...) _i_log(stream, "", msg, 0 _I_VA_ARGS( __VA_ARGS__ ))
#define I_DEBUG_ASSERT_ERROR(condition, msg, ...) \
{ \
if(condition) \
{ \
const char* prefixMsg = "Debug Error: Assertion Failed On Line: %u\nIn File: %s\n"; \
char* buf = (char*)malloc(strlen(prefixMsg) * sizeof(char) + strlen(__FILE__) * sizeof(char) + sizeof(unsigned int)); \
sprintf(buf, prefixMsg, (unsigned int)(__LINE__), __FILE__); \
_i_log(stderr, buf, msg, _I_COLOUR_RED _I_VA_ARGS( __VA_ARGS__ )); \
free(buf); \
__I_DEBUG_BREAK(); \
} \
}
#define I_DEBUG_ASSERT_FATAL_ERROR(condition, msg, ...) \
{ \
if(condition) \
{ \
const char* prefixMsg = "Debug Fatal Error: Assertion Failed On Line: %u\nIn File: %s\n"; \
char* buf = (char*)malloc(strlen(prefixMsg) * sizeof(char) + strlen(__FILE__) * sizeof(char) + sizeof(unsigned int)); \
sprintf(buf, prefixMsg, (unsigned int)(__LINE__), __FILE__); \
_i_log(stderr, buf, msg, _I_COLOUR_FATAL_RED _I_VA_ARGS( __VA_ARGS__ )); \
free(buf); \
__I_DEBUG_BREAK(); \
} \
}
#else
#define I_DEBUG_LOG_TRACE(msg, ...)
#define I_DEBUG_LOG_INFO(msg, ...)
#define I_DEBUG_LOG_WARNING(msg, ...)
#define I_DEBUG_LOG_ERROR(msg, ...)
#define I_DEBUG_LOG_FATAL_ERROR(msg, ...)
#define I_DEBUG_ASSERT_ERROR(condition, msg, ...)
#define I_DEBUG_ASSERT_FATAL_ERROR(condition, msg, ...)
#define I_DEBUG_FILE_LOG(fileName, msg, ...)
#define I_DEBUG_FS_LOG(stream, msg, ...)
#endif
#define I_LOG_TRACE(msg, ...) _i_log(stdout, "Trace: ", msg, _I_COLOUR_GREEN _I_VA_ARGS( __VA_ARGS__ ))
#define I_LOG_INFO(msg, ...) _i_log(stdout, "Info: ", msg, _I_COLOUR_WHITE _I_VA_ARGS( __VA_ARGS__ ))
#define I_LOG_WARNING(msg, ...) _i_log(stdout, "Warning: ", msg, _I_COLOUR_YELLOW _I_VA_ARGS( __VA_ARGS__ ))
#define I_LOG_ERROR(msg, ...) _i_log(stderr, "Error: ", msg, _I_COLOUR_RED _I_VA_ARGS( __VA_ARGS__ ))
#define I_LOG_FATAL_ERROR(msg, ...) _i_log(stderr, "Fatal Error: ", msg, _I_COLOUR_FATAL_RED, ##__VA_ARGS__)
#define I_FILE_LOG(fileName, msg, mode, ...) _f_i_log(fileName, msg, mode _I_VA_ARGS( __VA_ARGS__ ))
#define I_FS_LOG(stream, msg, ...) _i_log(stream, "", msg, 0 _I_VA_ARGS( __VA_ARGS__ ))
#define I_ASSERT_ERROR(condition, msg, ...) \
{ \
if(condition) \
{ \
const char* prefixMsg = "Error: Assertion Failed On Line: %u\nIn File: %s\n"; \
char* buf = (char*)malloc(strlen(prefixMsg) * sizeof(char) + strlen(__FILE__) * sizeof(char) + sizeof(unsigned int)); \
sprintf(buf, prefixMsg, (unsigned int)(__LINE__), __FILE__); \
_i_log(stderr, buf, msg, _I_COLOUR_RED _I_VA_ARGS( __VA_ARGS__ )); \
free(buf); \
__I_DEBUG_BREAK(); \
} \
}
#define I_ASSERT_FATAL_ERROR(condition, msg, ...) \
{ \
if(condition) \
{ \
const char* prefixMsg = "Fatal Error: Assertion Failed On Line: %u\nIn File: %s\n"; \
char* buf = (char*)malloc(strlen(prefixMsg) * sizeof(char) + strlen(__FILE__) * sizeof(char) + sizeof(unsigned int)); \
sprintf(buf, prefixMsg, (unsigned int)(__LINE__), __FILE__); \
_i_log(stderr, buf, msg, _I_COLOUR_FATAL_RED _I_VA_ARGS( __VA_ARGS__ )); \
free(buf); \
__I_DEBUG_BREAK(); \
} \
}
// DO NOT USE
ILOG_API void _platformLog(FILE* stream, const char* msg, int colour);
// DO NOT USE
ILOG_API void _i_log(FILE* stream, const char* prefix, const char* msg, int colour, ...);
// DO NOT USE
ILOG_API void _f_i_log(const char* fileName, const char* msg, const char* mode, ...);
#if defined(__cplusplus)
}
#endif
#endif