2
0

log.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 program 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. // Adding a use to avoid errors in the release build related to unused
  36. // consumers.
  37. #define SPIRV_ASSERT(consumer, ...) (void)(consumer)
  38. #endif
  39. // Logs a debug message to the consumer. Accepts the following formats:
  40. //
  41. // SPIRV_DEBUG(<message-consumer>, <message>);
  42. // SPIRV_DEBUG(<message-consumer>, <message-format>, <variable-arguments>);
  43. //
  44. // In the second format, the number of <variable-arguments> cannot exceed (5 -
  45. // 1). If more arguments are wanted, grow PP_ARG_N and PP_NARGS in the below.
  46. #if !defined(NDEBUG) && defined(SPIRV_LOG_DEBUG)
  47. #define SPIRV_DEBUG(consumer, ...) SPIRV_DEBUG_IMPL(consumer, __VA_ARGS__)
  48. #else
  49. // Adding a use to avoid errors in the release build related to unused
  50. // consumers.
  51. #define SPIRV_DEBUG(consumer, ...) (void)(consumer)
  52. #endif
  53. // Helper macros for concatenating arguments.
  54. #define SPIRV_CONCATENATE(a, b) SPIRV_CONCATENATE_(a, b)
  55. #define SPIRV_CONCATENATE_(a, b) a##b
  56. // Helper macro to force expanding __VA_ARGS__ to satisfy MSVC compiler.
  57. #define PP_EXPAND(x) x
  58. namespace spvtools {
  59. // Calls the given |consumer| by supplying the |message|. The |message| is from
  60. // the given |source| and |location| and of the given severity |level|.
  61. inline void Log(const MessageConsumer& consumer, spv_message_level_t level,
  62. const char* source, const spv_position_t& position,
  63. const char* message) {
  64. if (consumer != nullptr) consumer(level, source, position, message);
  65. }
  66. // Calls the given |consumer| by supplying the message composed according to the
  67. // given |format|. The |message| is from the given |source| and |location| and
  68. // of the given severity |level|.
  69. template <typename... Args>
  70. void Logf(const MessageConsumer& consumer, spv_message_level_t level,
  71. const char* source, const spv_position_t& position,
  72. const char* format, Args&&... args) {
  73. #if defined(_MSC_VER) && _MSC_VER < 1900
  74. // Sadly, snprintf() is not supported until Visual Studio 2015!
  75. #define snprintf _snprintf
  76. #endif
  77. enum { kInitBufferSize = 256 };
  78. char message[kInitBufferSize];
  79. const int size =
  80. snprintf(message, kInitBufferSize, format, std::forward<Args>(args)...);
  81. if (size >= 0 && size < kInitBufferSize) {
  82. Log(consumer, level, source, position, message);
  83. return;
  84. }
  85. if (size >= 0) {
  86. // The initial buffer is insufficient. Allocate a buffer of a larger size,
  87. // and write to it instead. Force the size to be unsigned to avoid a
  88. // warning in GCC 7.1.
  89. std::vector<char> longer_message(size + 1u);
  90. snprintf(longer_message.data(), longer_message.size(), format,
  91. std::forward<Args>(args)...);
  92. Log(consumer, level, source, position, longer_message.data());
  93. return;
  94. }
  95. Log(consumer, level, source, position, "cannot compose log message");
  96. #if defined(_MSC_VER) && _MSC_VER < 1900
  97. #undef snprintf
  98. #endif
  99. }
  100. // Calls the given |consumer| by supplying the given error |message|. The
  101. // |message| is from the given |source| and |location|.
  102. inline void Error(const MessageConsumer& consumer, const char* source,
  103. const spv_position_t& position, const char* message) {
  104. Log(consumer, SPV_MSG_ERROR, source, position, message);
  105. }
  106. // Calls the given |consumer| by supplying the error message composed according
  107. // to the given |format|. The |message| is from the given |source| and
  108. // |location|.
  109. template <typename... Args>
  110. inline void Errorf(const MessageConsumer& consumer, const char* source,
  111. const spv_position_t& position, const char* format,
  112. Args&&... args) {
  113. Logf(consumer, SPV_MSG_ERROR, source, position, format,
  114. std::forward<Args>(args)...);
  115. }
  116. } // namespace spvtools
  117. #define SPIRV_ASSERT_IMPL(consumer, ...) \
  118. PP_EXPAND(SPIRV_CONCATENATE(SPIRV_ASSERT_, PP_NARGS(__VA_ARGS__))( \
  119. consumer, __VA_ARGS__))
  120. #define SPIRV_DEBUG_IMPL(consumer, ...) \
  121. PP_EXPAND(SPIRV_CONCATENATE(SPIRV_DEBUG_, PP_NARGS(__VA_ARGS__))( \
  122. consumer, __VA_ARGS__))
  123. #define SPIRV_ASSERT_1(consumer, condition) \
  124. do { \
  125. if (!(condition)) { \
  126. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  127. {static_cast<size_t>(__LINE__), 0, 0}, \
  128. "assertion failed: " #condition); \
  129. std::exit(EXIT_FAILURE); \
  130. } \
  131. } while (0)
  132. #define SPIRV_ASSERT_2(consumer, condition, message) \
  133. do { \
  134. if (!(condition)) { \
  135. spvtools::Log(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  136. {static_cast<size_t>(__LINE__), 0, 0}, \
  137. "assertion failed: " message); \
  138. std::exit(EXIT_FAILURE); \
  139. } \
  140. } while (0)
  141. #define SPIRV_ASSERT_more(consumer, condition, format, ...) \
  142. do { \
  143. if (!(condition)) { \
  144. spvtools::Logf(consumer, SPV_MSG_INTERNAL_ERROR, __FILE__, \
  145. {static_cast<size_t>(__LINE__), 0, 0}, \
  146. "assertion failed: " format, __VA_ARGS__); \
  147. std::exit(EXIT_FAILURE); \
  148. } \
  149. } while (0)
  150. #define SPIRV_ASSERT_3(consumer, condition, format, ...) \
  151. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  152. #define SPIRV_ASSERT_4(consumer, condition, format, ...) \
  153. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  154. #define SPIRV_ASSERT_5(consumer, condition, format, ...) \
  155. SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
  156. #define SPIRV_DEBUG_1(consumer, message) \
  157. do { \
  158. spvtools::Log(consumer, SPV_MSG_DEBUG, __FILE__, \
  159. {static_cast<size_t>(__LINE__), 0, 0}, message); \
  160. } while (0)
  161. #define SPIRV_DEBUG_more(consumer, format, ...) \
  162. do { \
  163. spvtools::Logf(consumer, SPV_MSG_DEBUG, __FILE__, \
  164. {static_cast<size_t>(__LINE__), 0, 0}, format, \
  165. __VA_ARGS__); \
  166. } while (0)
  167. #define SPIRV_DEBUG_2(consumer, format, ...) \
  168. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  169. #define SPIRV_DEBUG_3(consumer, format, ...) \
  170. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  171. #define SPIRV_DEBUG_4(consumer, format, ...) \
  172. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  173. #define SPIRV_DEBUG_5(consumer, format, ...) \
  174. SPIRV_DEBUG_more(consumer, format, __VA_ARGS__)
  175. // Macros for counting the number of arguments passed in.
  176. #define PP_NARGS(...) PP_EXPAND(PP_ARG_N(__VA_ARGS__, 5, 4, 3, 2, 1, 0))
  177. #define PP_ARG_N(_1, _2, _3, _4, _5, N, ...) N
  178. // Tests for making sure that PP_NARGS() behaves as expected.
  179. static_assert(PP_NARGS(0) == 1, "PP_NARGS macro error");
  180. static_assert(PP_NARGS(0, 0) == 2, "PP_NARGS macro error");
  181. static_assert(PP_NARGS(0, 0, 0) == 3, "PP_NARGS macro error");
  182. static_assert(PP_NARGS(0, 0, 0, 0) == 4, "PP_NARGS macro error");
  183. static_assert(PP_NARGS(0, 0, 0, 0, 0) == 5, "PP_NARGS macro error");
  184. static_assert(PP_NARGS(1 + 1, 2, 3 / 3) == 3, "PP_NARGS macro error");
  185. static_assert(PP_NARGS((1, 1), 2, (3, 3)) == 3, "PP_NARGS macro error");
  186. #endif // SOURCE_OPT_LOG_H_