log.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_LOG_H_
  15. #define SOURCE_OPT_LOG_H_
  16. #include <cstdio>
  17. #include <cstdlib>
  18. #include <utility>
  19. #include <vector>
  20. #include "spirv-tools/libspirv.hpp"
  21. // Asserts the given condition is true. Otherwise, sends a message to the
  22. // consumer and exits the problem with failure code. Accepts the following
  23. // formats:
  24. //
  25. // SPIRV_ASSERT(<message-consumer>, <condition-expression>);
  26. // SPIRV_ASSERT(<message-consumer>, <condition-expression>, <message>);
  27. // SPIRV_ASSERT(<message-consumer>, <condition-expression>,
  28. // <message-format>, <variable-arguments>);
  29. //
  30. // In the third format, the number of <variable-arguments> cannot exceed (5 -
  31. // 2). If more arguments are wanted, grow PP_ARG_N and PP_NARGS in the below.
  32. #if !defined(NDEBUG)
  33. #define SPIRV_ASSERT(consumer, ...) SPIRV_ASSERT_IMPL(consumer, __VA_ARGS__)
  34. #else
  35. #define SPIRV_ASSERT(consumer, ...)
  36. #endif
  37. // Logs a debug message to the consumer. Accepts the following formats:
  38. //
  39. // SPIRV_DEBUG(<message-consumer>, <message>);
  40. // SPIRV_DEBUG(<message-consumer>, <message-format>, <variable-arguments>);
  41. //
  42. // In the second format, the number of <variable-arguments> cannot exceed (5 -
  43. // 1). If more arguments are wanted, grow PP_ARG_N and PP_NARGS in the below.
  44. #if !defined(NDEBUG) && defined(SPIRV_LOG_DEBUG)
  45. #define SPIRV_DEBUG(consumer, ...) SPIRV_DEBUG_IMPL(consumer, __VA_ARGS__)
  46. #else
  47. #define SPIRV_DEBUG(consumer, ...)
  48. #endif
  49. // Logs an error message to the consumer saying the given feature is
  50. // unimplemented.
  51. #define SPIRV_UNIMPLEMENTED(consumer, feature) \
  52. do { \
  53. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  54. {static_cast<size_t>(__LINE__), 0, 0}, \
  55. "unimplemented: " feature); \
  56. } while (0)
  57. // Logs an error message to the consumer saying the code location
  58. // should be unreachable.
  59. #define SPIRV_UNREACHABLE(consumer) \
  60. do { \
  61. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  62. {static_cast<size_t>(__LINE__), 0, 0}, "unreachable"); \
  63. } while (0)
  64. // Helper macros for concatenating arguments.
  65. #define SPIRV_CONCATENATE(a, b) SPIRV_CONCATENATE_(a, b)
  66. #define SPIRV_CONCATENATE_(a, b) a##b
  67. // Helper macro to force expanding __VA_ARGS__ to satisfy MSVC compiler.
  68. #define PP_EXPAND(x) x
  69. namespace spvtools {
  70. // Calls the given |consumer| by supplying the |message|. The |message| is from
  71. // the given |source| and |location| and of the given severity |level|.
  72. inline void Log(const MessageConsumer& consumer, spv_message_level_t level,
  73. const char* source, const spv_position_t& position,
  74. const char* message) {
  75. if (consumer != nullptr) consumer(level, source, position, message);
  76. }
  77. // Calls the given |consumer| by supplying the message composed according to the
  78. // given |format|. The |message| is from the given |source| and |location| and
  79. // of the given severity |level|.
  80. template <typename... Args>
  81. void Logf(const MessageConsumer& consumer, spv_message_level_t level,
  82. const char* source, const spv_position_t& position,
  83. const char* format, Args&&... args) {
  84. #if defined(_MSC_VER) && _MSC_VER < 1900
  85. // Sadly, snprintf() is not supported until Visual Studio 2015!
  86. #define snprintf _snprintf
  87. #endif
  88. enum { kInitBufferSize = 256 };
  89. char message[kInitBufferSize];
  90. const int size =
  91. snprintf(message, kInitBufferSize, format, std::forward<Args>(args)...);
  92. if (size >= 0 && size < kInitBufferSize) {
  93. Log(consumer, level, source, position, message);
  94. return;
  95. }
  96. if (size >= 0) {
  97. // The initial buffer is insufficient. Allocate a buffer of a larger size,
  98. // and write to it instead. Force the size to be unsigned to avoid a
  99. // warning in GCC 7.1.
  100. std::vector<char> longer_message(size + 1u);
  101. snprintf(longer_message.data(), longer_message.size(), format,
  102. std::forward<Args>(args)...);
  103. Log(consumer, level, source, position, longer_message.data());
  104. return;
  105. }
  106. Log(consumer, level, source, position, "cannot compose log message");
  107. #if defined(_MSC_VER) && _MSC_VER < 1900
  108. #undef snprintf
  109. #endif
  110. }
  111. // Calls the given |consumer| by supplying the given error |message|. The
  112. // |message| is from the given |source| and |location|.
  113. inline void Error(const MessageConsumer& consumer, const char* source,
  114. const spv_position_t& position, const char* message) {
  115. Log(consumer, SPV_MSG_ERROR, source, position, message);
  116. }
  117. // Calls the given |consumer| by supplying the error message composed according
  118. // to the given |format|. The |message| is from the given |source| and
  119. // |location|.
  120. template <typename... Args>
  121. inline void Errorf(const MessageConsumer& consumer, const char* source,
  122. const spv_position_t& position, const char* format,
  123. Args&&... args) {
  124. Logf(consumer, SPV_MSG_ERROR, source, position, format,
  125. std::forward<Args>(args)...);
  126. }
  127. } // namespace spvtools
  128. #define SPIRV_ASSERT_IMPL(consumer, ...) \
  129. PP_EXPAND(SPIRV_CONCATENATE(SPIRV_ASSERT_, PP_NARGS(__VA_ARGS__))( \
  130. consumer, __VA_ARGS__))
  131. #define SPIRV_DEBUG_IMPL(consumer, ...) \
  132. PP_EXPAND(SPIRV_CONCATENATE(SPIRV_DEBUG_, PP_NARGS(__VA_ARGS__))( \
  133. consumer, __VA_ARGS__))
  134. #define SPIRV_ASSERT_1(consumer, condition) \
  135. do { \
  136. if (!(condition)) { \
  137. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  138. {static_cast<size_t>(__LINE__), 0, 0}, \
  139. "assertion failed: " #condition); \
  140. std::exit(EXIT_FAILURE); \
  141. } \
  142. } while (0)
  143. #define SPIRV_ASSERT_2(consumer, condition, message) \
  144. do { \
  145. if (!(condition)) { \
  146. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  147. {static_cast<size_t>(__LINE__), 0, 0}, \
  148. "assertion failed: " message); \
  149. std::exit(EXIT_FAILURE); \
  150. } \
  151. } while (0)
  152. #define SPIRV_ASSERT_more(consumer, condition, format, ...) \
  153. do { \
  154. if (!(condition)) { \
  155. spvtools::Logf(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  156. {static_cast<size_t>(__LINE__), 0, 0}, \
  157. "assertion failed: " format, __VA_ARGS__); \
  158. std::exit(EXIT_FAILURE); \
  159. } \
  160. } while (0)
  161. #define SPIRV_ASSERT_3(consumer, condition, format, ...) \
  162. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  163. #define SPIRV_ASSERT_4(consumer, condition, format, ...) \
  164. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  165. #define SPIRV_ASSERT_5(consumer, condition, format, ...) \
  166. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  167. #define SPIRV_DEBUG_1(consumer, message) \
  168. do { \
  169. spvtools::Log(consumer, SPV_MSG_DEBUG, __FILE__, \
  170. {static_cast<size_t>(__LINE__), 0, 0}, message); \
  171. } while (0)
  172. #define SPIRV_DEBUG_more(consumer, format, ...) \
  173. do { \
  174. spvtools::Logf(consumer, SPV_MSG_DEBUG, __FILE__, \
  175. {static_cast<size_t>(__LINE__), 0, 0}, format, \
  176. __VA_ARGS__); \
  177. } while (0)
  178. #define SPIRV_DEBUG_2(consumer, format, ...) \
  179. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  180. #define SPIRV_DEBUG_3(consumer, format, ...) \
  181. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  182. #define SPIRV_DEBUG_4(consumer, format, ...) \
  183. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  184. #define SPIRV_DEBUG_5(consumer, format, ...) \
  185. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  186. // Macros for counting the number of arguments passed in.
  187. #define PP_NARGS(...) PP_EXPAND(PP_ARG_N(__VA_ARGS__, 5, 4, 3, 2, 1, 0))
  188. #define PP_ARG_N(_1, _2, _3, _4, _5, N, ...) N
  189. // Tests for making sure that PP_NARGS() behaves as expected.
  190. static_assert(PP_NARGS(0) == 1, "PP_NARGS macro error");
  191. static_assert(PP_NARGS(0, 0) == 2, "PP_NARGS macro error");
  192. static_assert(PP_NARGS(0, 0, 0) == 3, "PP_NARGS macro error");
  193. static_assert(PP_NARGS(0, 0, 0, 0) == 4, "PP_NARGS macro error");
  194. static_assert(PP_NARGS(0, 0, 0, 0, 0) == 5, "PP_NARGS macro error");
  195. static_assert(PP_NARGS(1 + 1, 2, 3 / 3) == 3, "PP_NARGS macro error");
  196. static_assert(PP_NARGS((1, 1), 2, (3, 3)) == 3, "PP_NARGS macro error");
  197. #endif // SOURCE_OPT_LOG_H_