Fillwave  10.0.0
Log.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <flw/Config.h>
25 #include <flw/PlatformSW.h>
26 #include <string>
27 
28 #if defined(FILLWAVE_COMPILATION_RELEASE) || defined(FILLWAVE_BENCHMARK_TESTS)
29 
30 #define FLOGINIT_DEFAULT()
31 #define FLOGINIT_MASK(mask)
32 #define FLOGINIT(tag, mask)
33 #define FLOGINIT_NONE()
34 
35 #define fLog()
36 #define fLogF(...) { }
37 #define fLogI(...) { }
38 #define fLogD(...) { }
39 #define fLogU(...) { }
40 #define fLogW(...) { }
41 #define fLogE(...) { }
42 #define fLogC(...) { }
43 
44 #else /* FILLWAVE_COMPILATION_RELEASE */
45 
46 #define FBIT(offset) (1 << offset)
47 #define FERROR FBIT(0)
48 #define FINFO FBIT(1)
49 #define FDEBUG FBIT(2)
50 #define FFATAL FBIT(3)
51 #define FUSER FBIT(4)
52 #define FWARNING FBIT(5)
53 #define FBIT_MAX (FERROR | FINFO | FDEBUG | FFATAL | FUSER | FWARNING)
54 #define FIF(mask) (::_mask_ & mask)
55 #define FSTR_HELPER(x) #x
56 #define FTO_STRING(x) FSTR_HELPER(x)
57 
58 #ifdef __ANDROID__
59 #include <android/log.h>
60 #else /* __ANDROID__ */
61 
62 #include <sstream>
63 #include <iostream>
64 
65 #ifdef _WIN32
66 constexpr auto FLOG_BLACK = "";
67 constexpr auto FLOG_RED = "";
68 constexpr auto FLOG_GREEN = "";
69 constexpr auto FLOG_YELLOW = "";
70 constexpr auto FLOG_BLUE = "";
71 constexpr auto FLOG_MAGENTA = "";
72 constexpr auto FLOG_CYAN = "";
73 constexpr auto FLOG_WHITE = "";
74 constexpr auto FLOG_END = "";
75 #else
76 constexpr auto FLOG_BLACK = "\033[0;30m";
77 constexpr auto FLOG_RED = "\033[0;31m";
78 constexpr auto FLOG_GREEN = "\033[0;32m";
79 constexpr auto FLOG_YELLOW = "\033[0;33m";
80 constexpr auto FLOG_BLUE = "\033[0;34m";
81 constexpr auto FLOG_MAGENTA = "\033[0;35m";
82 constexpr auto FLOG_CYAN = "\033[0;36m";
83 constexpr auto FLOG_WHITE = "\033[0;37m";
84 constexpr auto FLOG_END = "\033[0m";
85 #endif /* _WIN32 */
86 
87 #endif /* __ANDROID__ */
88 
89 #define FLOGINIT_DEFAULT() \
90  static const std::string _tag_ = ( \
91  std::string(__FILE__).find(FILLWAVE_OS_SEPRATOR) == std::string::npos ? std::string(__FILE__) : \
92  std::string(__FILE__).substr(std::string(__FILE__).find_last_of( \
93  FILLWAVE_OS_SEPRATOR) + 1, std::string(__FILE__).size())); \
94  static const char _mask_ = FBIT_MAX;
95 
96 #define FLOGINIT_MASK(mask) \
97  static const std::string _tag_ = ( \
98  std::string(__FILE__).find(FILLWAVE_OS_SEPRATOR) == std::string::npos ? std::string(__FILE__) : \
99  std::string(__FILE__).substr(std::string(__FILE__).find_last_of( \
100  FILLWAVE_OS_SEPRATOR) + 1, std::string(__FILE__).size())); \
101  static const char _mask_ = (mask);
102 
103 #define FLOGINIT(tag, mask) \
104  static const std::string _tag_ = (tag); \
105  static const char _mask_ = (mask);
106 
107 #define FLOGINIT_NONE() \
108  static const std::string _tag_ = ""; \
109  static const char _mask_ = 0;
110 
111 #ifdef __ANDROID__
112 
113 #define GPU_FATAL ANDROID_LOG_FATAL
114 #define GPU_ERROR ANDROID_LOG_ERROR
115 #define GPU_INFO ANDROID_LOG_INFO
116 #define GPU_DEBUG ANDROID_LOG_DEBUG
117 #define GPU_WARN ANDROID_LOG_WARN
118 #define GPU_USER ANDROID_LOG_WARN
119 
120 #else /* __ANDROID__ */
121 
122 #define GPU_FATAL FLOG_MAGENTA
123 #define GPU_ERROR FLOG_RED
124 #define GPU_INFO FLOG_WHITE
125 #define GPU_DEBUG FLOG_GREEN
126 #define GPU_WARN FLOG_YELLOW
127 #define GPU_USER FLOG_BLUE
128 
129 #endif /* __ANDROID__ */
130 
131 /* Log */
132 #ifdef __ANDROID__
133 
134 #define fLog() __android_log_print(LOG_FLAG, ::_tag_.c_str(), "")
135 #define fLogBase(LOG_CONDITION, LOG_FLAG, ...)\
136  do { if ( FIF(LOG_CONDITION) ) (void)__android_log_print(LOG_FLAG, ::_tag_.c_str(), __VA_ARGS__); } while(0)
137 
138 #else /* __ANDROID__ */
139 
140 template<typename T>
141 static void fLogWithStream(std::stringstream& s, const T& t) {
142  std::cout << s.str() << t << FLOG_END << "\n";
143 }
144 template<typename T, typename... Args>
145 static void fLogWithStream(std::stringstream& s, const T& t, Args... args) {
146  s << t;
147  fLogWithStream(s, args...);
148 }
149 
150 #define fLogBase(COLOR, COND, ...)\
151 do {\
152  if ( FIF(COND) ) {\
153  std::stringstream s;\
154  s << COLOR;\
155  fLogWithStream(s, ::_tag_,":", __LINE__, " ", __VA_ARGS__);\
156  }\
157 } while(0)
158 
159 #endif /* __ANDROID__ */
160 
161 /* Check */
162 #ifdef __ANDROID__
163 
164 #define fLogC(...)\
165  do {\
166  (void)_mask_;\
167  GLenum errorCode = glGetError();\
168  if ( errorCode != GL_NO_ERROR) {\
169  (void)__android_log_print(ANDROID_LOG_ERROR, ::_tag_.c_str(), "[%s 0x%04x] ", "CORE ERROR:", errorCode);\
170  (void)__android_log_print(ANDROID_LOG_ERROR, ::_tag_.c_str(), __VA_ARGS__);\
171  if (errorCode == 0x0506) { /*Framebuffer error*/ \
172  GLenum status = getFramebufferStatus();\
173  (void)__android_log_print(ANDROID_LOG_ERROR, ::_tag_.c_str(), "[%s 0x%04x] ", "FRAMEBUFFER_STATUS:", status);\
174  }\
175  }\
176  }while(0)
177 #else /* __ANDROID__ */
178 
179 #ifdef _WIN32
180 #define FILLWAVE_SPRINTF sprintf_s
181 #else
182 #define FILLWAVE_SPRINTF sprintf
183 #endif /* _WIN32 */
184 
185 #define fLogC(...) do {\
186  (void)_mask_;\
187  GLenum errorCode = getGlError();\
188  if (0 != errorCode) { /* GL_NO_ERROR */\
189  char glBuffer[256];\
190  int n = FILLWAVE_SPRINTF(glBuffer, "[%s 0x%04x] ", "CORE ERROR:", errorCode);\
191  if (n > 0) {\
192  fLogF(glBuffer);\
193  }\
194  if (errorCode == 0x0506) { /* GL_INVALID_FRAMEBUFFER_OPERATION */\
195  GLenum status = getFramebufferStatus();\
196  n = FILLWAVE_SPRINTF(glBuffer, "[%s 0x%04x] ", "FRAMEBUFFER_STATUS:", status);\
197  if (n > 0) {\
198  fLogF(glBuffer);\
199  }\
200  }\
201  fLogF(__VA_ARGS__);\
202  abort();\
203  }\
204 \
205 } while (0)
206 
207 #endif /* __ANDROID__ */
208 
209 #define fLogF(...) fLogBase(FLOG_MAGENTA, FFATAL, __VA_ARGS__)
210 #define fLogE(...) fLogBase(FLOG_RED, FERROR, __VA_ARGS__)
211 #define fLogI(...) fLogBase(FLOG_WHITE, FINFO, __VA_ARGS__)
212 #define fLogU(...) fLogBase(FLOG_BLUE, FUSER, __VA_ARGS__)
213 #define fLogD(...) fLogBase(FLOG_GREEN, FDEBUG, __VA_ARGS__)
214 #define fLogW(...) fLogBase(FLOG_YELLOW, FWARNING, __VA_ARGS__)
215 
216 #endif /* FILLWAVE_COMPILATION_RELEASE */