test_switch.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #define TEST_LIBRARY_ID_zf_log 1
  3. #define TEST_LIBRARY_ID_spdlog 2
  4. #define TEST_LIBRARY_ID_easylog 3
  5. #define TEST_LIBRARY_ID_g3log 4
  6. #define TEST_LIBRARY_ID_glog 5
  7. #define _CONCAT(a, b) a##b
  8. #define CONCAT(a, b) _CONCAT(a, b)
  9. #if TEST_LIBRARY_ID_zf_log == CONCAT(TEST_LIBRARY_ID_, TEST_LIBRARY)
  10. #define TEST_LIBRARY_ZF_LOG
  11. #elif TEST_LIBRARY_ID_spdlog == CONCAT(TEST_LIBRARY_ID_, TEST_LIBRARY)
  12. #define TEST_LIBRARY_SPDLOG
  13. #elif TEST_LIBRARY_ID_easylog == CONCAT(TEST_LIBRARY_ID_, TEST_LIBRARY)
  14. #define TEST_LIBRARY_EASYLOG
  15. #elif TEST_LIBRARY_ID_g3log == CONCAT(TEST_LIBRARY_ID_, TEST_LIBRARY)
  16. #define TEST_LIBRARY_G3LOG
  17. #elif TEST_LIBRARY_ID_glog == CONCAT(TEST_LIBRARY_ID_, TEST_LIBRARY)
  18. #define TEST_LIBRARY_GLOG
  19. #else
  20. #error Unknown test library name
  21. #endif
  22. #define XLOG_STRING_LITERAL "A random string"
  23. #define XLOG_INT_LITERAL 42
  24. /* It's important that values are not const. Otherwise compilers will be able
  25. * to optimize out things that we care about.
  26. */
  27. extern const char *XLOG_STRING_VALUE;
  28. extern int XLOG_INT_VALUE;
  29. #ifndef TEST_SWITCH_MODULE
  30. const char *XLOG_STRING_VALUE = XLOG_STRING_LITERAL;
  31. int XLOG_INT_VALUE = XLOG_INT_LITERAL;
  32. #endif
  33. #ifdef TEST_FORMAT_SLOW_FUNC
  34. #include <chrono>
  35. #include <thread>
  36. static int XLOG_SLOW_FUNC()
  37. {
  38. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  39. return (int)std::hash<std::thread::id>()(std::this_thread::get_id());
  40. }
  41. #endif
  42. #define XLOG_MESSAGE_STR_LITERAL_PRINTF XLOG_STRING_LITERAL
  43. #define XLOG_MESSAGE_STR_LITERAL_CPPFMT XLOG_STRING_LITERAL
  44. #define XLOG_MESSAGE_STR_LITERAL_STREAM XLOG_STRING_LITERAL
  45. #define XLOG_MESSAGE_3INT_VALUES_PRINTF \
  46. "vA: %i, vB: %i, vC: %i", \
  47. XLOG_INT_VALUE, XLOG_INT_VALUE, XLOG_INT_VALUE
  48. #define XLOG_MESSAGE_3INT_VALUES_CPPFMT \
  49. "vA: {}, vB: {}, vC: {}", \
  50. XLOG_INT_VALUE, XLOG_INT_VALUE, XLOG_INT_VALUE
  51. #define XLOG_MESSAGE_3INT_VALUES_STREAM \
  52. "vA: " << XLOG_INT_VALUE << ", vB: " << XLOG_INT_VALUE << \
  53. ", vC: " << XLOG_INT_VALUE
  54. #define XLOG_MESSAGE_SLOW_FUNC_PRINTF "%i", XLOG_SLOW_FUNC()
  55. #define XLOG_MESSAGE_SLOW_FUNC_CPPFMT "{}", XLOG_SLOW_FUNC()
  56. #define XLOG_MESSAGE_SLOW_FUNC_STREAM XLOG_SLOW_FUNC()
  57. #ifdef TEST_LIBRARY_ZF_LOG
  58. #include <zf_log.h>
  59. #ifdef TEST_NULL_SINK
  60. #define _XLOG_INIT_SINK() \
  61. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, \
  62. [](const zf_log_message *, void *){})
  63. #else
  64. #define _XLOG_INIT_SINK()
  65. #endif
  66. #ifdef TEST_LOG_OFF
  67. #define _XLOG_INIT_LEVEL() \
  68. zf_log_set_output_level(ZF_LOG_ERROR)
  69. #else
  70. #define _XLOG_INIT_LEVEL()
  71. #endif
  72. static void XLOG_INIT()
  73. {
  74. _XLOG_INIT_SINK();
  75. _XLOG_INIT_LEVEL();
  76. }
  77. #if defined(TEST_FORMAT_INTS)
  78. #define XLOG_STATEMENT() ZF_LOGI(XLOG_MESSAGE_3INT_VALUES_PRINTF)
  79. #elif defined(TEST_FORMAT_SLOW_FUNC)
  80. #define XLOG_STATEMENT() ZF_LOGI(XLOG_MESSAGE_SLOW_FUNC_PRINTF)
  81. #else
  82. #define XLOG_STATEMENT() ZF_LOGI(XLOG_MESSAGE_STR_LITERAL_PRINTF)
  83. #endif
  84. #endif
  85. #ifdef TEST_LIBRARY_SPDLOG
  86. #include <spdlog/spdlog.h>
  87. extern const std::shared_ptr<spdlog::logger> g_logger;
  88. #ifdef TEST_NULL_SINK
  89. class null_sink: public spdlog::sinks::sink
  90. {
  91. public:
  92. void log(const spdlog::details::log_msg &) override {}
  93. void flush() override {}
  94. };
  95. #ifndef TEST_SWITCH_MODULE
  96. const std::shared_ptr<spdlog::logger> g_logger = spdlog::create<null_sink>("null");
  97. #endif
  98. #else
  99. #ifndef TEST_SWITCH_MODULE
  100. const std::shared_ptr<spdlog::logger> g_logger = spdlog::stderr_logger_st("stderr");
  101. #endif
  102. #endif
  103. #ifdef TEST_LOG_OFF
  104. #define _XLOG_INIT_LEVEL() spdlog::set_level(spdlog::level::err)
  105. #else
  106. #define _XLOG_INIT_LEVEL()
  107. #endif
  108. static void XLOG_INIT()
  109. {
  110. _XLOG_INIT_LEVEL();
  111. }
  112. #if defined(TEST_FORMAT_INTS)
  113. #define XLOG_STATEMENT() g_logger->info(XLOG_MESSAGE_3INT_VALUES_CPPFMT)
  114. #elif defined(TEST_FORMAT_SLOW_FUNC)
  115. #define XLOG_STATEMENT() g_logger->info(XLOG_MESSAGE_SLOW_FUNC_CPPFMT)
  116. #else
  117. #define XLOG_STATEMENT() g_logger->info(XLOG_MESSAGE_STR_LITERAL_CPPFMT)
  118. #endif
  119. #endif
  120. #ifdef TEST_LIBRARY_EASYLOG
  121. #ifdef TEST_NULL_SINK
  122. class null_stream {
  123. public:
  124. template<typename T>
  125. null_stream &operator<<(T) { return *this; }
  126. null_stream &operator<<(std::ostream& (*)(std::ostream&)) { return *this; }
  127. };
  128. extern null_stream g_null;
  129. #ifndef TEST_SWITCH_MODULE
  130. null_stream g_null;
  131. #endif
  132. #define ELPP_CUSTOM_COUT g_null
  133. #define _XLOG_INIT_SINK() \
  134. el::Loggers::reconfigureAllLoggers(el::ConfigurationType::ToFile, "false")
  135. #else
  136. #define _XLOG_INIT_SINK()
  137. #endif
  138. #ifdef TEST_LOG_OFF
  139. #define _XLOG_INIT_LEVEL() \
  140. el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Enabled, "false")
  141. #else
  142. #define _XLOG_INIT_LEVEL()
  143. #endif
  144. #define ELPP_THREAD_SAFE
  145. #include <easylogging++.h>
  146. #ifndef TEST_SWITCH_MODULE
  147. INITIALIZE_EASYLOGGINGPP
  148. #endif
  149. static void XLOG_INIT()
  150. {
  151. _XLOG_INIT_SINK();
  152. _XLOG_INIT_LEVEL();
  153. }
  154. #if defined(TEST_FORMAT_INTS)
  155. #define XLOG_STATEMENT() LOG(INFO) << XLOG_MESSAGE_3INT_VALUES_STREAM
  156. #elif defined(TEST_FORMAT_SLOW_FUNC)
  157. #define XLOG_STATEMENT() LOG(INFO) << XLOG_MESSAGE_SLOW_FUNC_STREAM
  158. #else
  159. #define XLOG_STATEMENT() LOG(INFO) << XLOG_MESSAGE_STR_LITERAL_STREAM
  160. #endif
  161. #endif
  162. #ifdef TEST_LIBRARY_G3LOG
  163. #include <g3log/g3log.hpp>
  164. #include <g3log/logworker.hpp>
  165. #ifdef TEST_NULL_SINK
  166. class null_sink
  167. {
  168. public:
  169. void log(const std::string) {}
  170. };
  171. #define _XLOG_INIT_SINK() \
  172. auto worker = g3::LogWorker::createLogWorker(); \
  173. g3::initializeLogging(worker.get()); \
  174. worker->addSink(std::unique_ptr<null_sink>(new null_sink), &null_sink::log);
  175. #else
  176. #define _XLOG_INIT_SINK() \
  177. auto worker = g3::LogWorker::createLogWorker(); \
  178. g3::initializeLogging(worker.get()); \
  179. worker->addDefaultLogger("g3log", "g3log.log");
  180. #endif
  181. #ifdef TEST_LOG_OFF
  182. #ifndef G3_DYNAMIC_LOGGING
  183. #error g3log must be built with G3_DYNAMIC_LOGGING defined
  184. #endif
  185. #define _XLOG_INIT_LEVEL() \
  186. g3::only_change_at_initialization::setLogLevel(INFO, false)
  187. #else
  188. #define _XLOG_INIT_LEVEL()
  189. #endif
  190. static void XLOG_INIT()
  191. {
  192. _XLOG_INIT_SINK();
  193. _XLOG_INIT_LEVEL();
  194. }
  195. #if defined(TEST_FORMAT_INTS)
  196. #define XLOG_STATEMENT() LOGF(INFO, XLOG_MESSAGE_3INT_VALUES_PRINTF)
  197. #elif defined(TEST_FORMAT_SLOW_FUNC)
  198. #define XLOG_STATEMENT() LOGF(INFO, XLOG_MESSAGE_SLOW_FUNC_PRINTF)
  199. #else
  200. #define XLOG_STATEMENT() LOGF(INFO, XLOG_MESSAGE_STR_LITERAL_PRINTF)
  201. #endif
  202. #endif
  203. #ifdef TEST_LIBRARY_GLOG
  204. #include <glog/logging.h>
  205. #ifdef TEST_NULL_SINK
  206. class null_sink: public google::LogSink
  207. {
  208. public:
  209. void send(google::LogSeverity, const char *, const char *, int,
  210. const struct ::tm *, const char *, size_t) override {}
  211. void WaitTillSent() override {}
  212. };
  213. extern null_sink g_sink;
  214. #ifndef TEST_SWITCH_MODULE
  215. null_sink g_sink;
  216. #endif
  217. #define _XLOG_LOG(lvl) LOG_TO_SINK_BUT_NOT_TO_LOGFILE(&g_sink, lvl)
  218. #else
  219. #define _XLOG_LOG(lvl) LOG(lvl)
  220. #endif
  221. #ifdef TEST_LOG_OFF
  222. #define _XLOG_INIT_LEVEL() FLAGS_minloglevel = google::ERROR
  223. #else
  224. #define _XLOG_INIT_LEVEL()
  225. #endif
  226. static void XLOG_INIT()
  227. {
  228. google::InitGoogleLogging("glog");
  229. _XLOG_INIT_LEVEL();
  230. }
  231. #if defined(TEST_FORMAT_INTS)
  232. #define XLOG_STATEMENT() _XLOG_LOG(INFO) << XLOG_MESSAGE_3INT_VALUES_STREAM
  233. #elif defined(TEST_FORMAT_SLOW_FUNC)
  234. #define XLOG_STATEMENT() _XLOG_LOG(INFO) << XLOG_MESSAGE_SLOW_FUNC_STREAM
  235. #else
  236. #define XLOG_STATEMENT() _XLOG_LOG(INFO) << XLOG_MESSAGE_STR_LITERAL_STREAM
  237. #endif
  238. #endif