Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

test_misc.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "includes.h"
  2. #include "test_sink.h"
  3. template<class T>
  4. std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
  5. {
  6. std::ostringstream oss;
  7. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  8. spdlog::logger oss_logger("oss", oss_sink);
  9. oss_logger.set_level(logger_level);
  10. oss_logger.set_pattern("%v");
  11. oss_logger.info(what);
  12. return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
  13. }
  14. TEST_CASE("basic_logging ", "[basic_logging]")
  15. {
  16. // const char
  17. REQUIRE(log_info("Hello") == "Hello");
  18. REQUIRE(log_info("") == "");
  19. // std::string
  20. REQUIRE(log_info(std::string("Hello")) == "Hello");
  21. REQUIRE(log_info(std::string()) == std::string());
  22. // Numbers
  23. REQUIRE(log_info(5) == "5");
  24. REQUIRE(log_info(5.6) == "5.6");
  25. // User defined class
  26. // REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
  27. }
  28. TEST_CASE("log_levels", "[log_levels]")
  29. {
  30. REQUIRE(log_info("Hello", spdlog::level::err) == "");
  31. REQUIRE(log_info("Hello", spdlog::level::critical) == "");
  32. REQUIRE(log_info("Hello", spdlog::level::info) == "Hello");
  33. REQUIRE(log_info("Hello", spdlog::level::debug) == "Hello");
  34. REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello");
  35. }
  36. TEST_CASE("to_c_str", "[convert_to_c_str]")
  37. {
  38. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::trace)) == "trace");
  39. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::debug)) == "debug");
  40. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::info)) == "info");
  41. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::warn)) == "warning");
  42. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::err)) == "error");
  43. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::critical)) == "critical");
  44. REQUIRE(std::string(spdlog::level::to_c_str(spdlog::level::off)) == "off");
  45. }
  46. TEST_CASE("to_short_c_str", "[convert_to_short_c_str]")
  47. {
  48. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::trace)) == "T");
  49. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::debug)) == "D");
  50. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::info)) == "I");
  51. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::warn)) == "W");
  52. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::err)) == "E");
  53. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::critical)) == "C");
  54. REQUIRE(std::string(spdlog::level::to_short_c_str(spdlog::level::off)) == "O");
  55. }
  56. TEST_CASE("to_level_enum", "[convert_to_level_enum]")
  57. {
  58. REQUIRE(spdlog::level::from_str("trace") == spdlog::level::trace);
  59. REQUIRE(spdlog::level::from_str("debug") == spdlog::level::debug);
  60. REQUIRE(spdlog::level::from_str("info") == spdlog::level::info);
  61. REQUIRE(spdlog::level::from_str("warning") == spdlog::level::warn);
  62. REQUIRE(spdlog::level::from_str("error") == spdlog::level::err);
  63. REQUIRE(spdlog::level::from_str("critical") == spdlog::level::critical);
  64. REQUIRE(spdlog::level::from_str("off") == spdlog::level::off);
  65. REQUIRE(spdlog::level::from_str("null") == spdlog::level::off);
  66. }
  67. TEST_CASE("periodic flush", "[periodic_flush]")
  68. {
  69. using namespace spdlog;
  70. auto logger = spdlog::create<sinks::test_sink_mt>("periodic_flush");
  71. auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(logger->sinks()[0]);
  72. spdlog::flush_every(std::chrono::seconds(1));
  73. std::this_thread::sleep_for(std::chrono::milliseconds(1100));
  74. REQUIRE(test_sink->flush_counter() == 1);
  75. spdlog::flush_every(std::chrono::seconds(0));
  76. spdlog::drop_all();
  77. }
  78. TEST_CASE("clone", "[clone]")
  79. {
  80. using namespace spdlog;
  81. auto logger = spdlog::create<sinks::test_sink_mt>("orig");
  82. auto cloned = logger->clone("clone");
  83. REQUIRE(cloned->name() == "clone");
  84. REQUIRE(logger->sinks() == cloned->sinks());
  85. REQUIRE(logger->level() == cloned->level());
  86. REQUIRE(logger->flush_level() == cloned->flush_level());
  87. logger->info("Some message 1");
  88. cloned->info("Some message 2");
  89. auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
  90. REQUIRE(test_sink->msg_counter() == 2);
  91. spdlog::drop_all();
  92. }
  93. TEST_CASE("clone async", "[clone]")
  94. {
  95. using namespace spdlog;
  96. auto logger = spdlog::create_async<sinks::test_sink_mt>("orig");
  97. auto cloned = logger->clone("clone");
  98. REQUIRE(cloned->name() == "clone");
  99. REQUIRE(logger->sinks() == cloned->sinks());
  100. REQUIRE(logger->level() == cloned->level());
  101. REQUIRE(logger->flush_level() == cloned->flush_level());
  102. logger->info("Some message 1");
  103. cloned->info("Some message 2");
  104. spdlog::details::os::sleep_for_millis(10);
  105. auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
  106. REQUIRE(test_sink->msg_counter() == 2);
  107. spdlog::drop_all();
  108. }
  109. #include "spdlog/fmt/bin_to_hex.h"
  110. TEST_CASE("to_hex", "[to_hex]")
  111. {
  112. std::ostringstream oss;
  113. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  114. spdlog::logger oss_logger("oss", oss_sink);
  115. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  116. oss_logger.info("{}", spdlog::to_hex(v));
  117. auto output = oss.str();
  118. REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
  119. }
  120. TEST_CASE("to_hex_upper", "[to_hex]")
  121. {
  122. std::ostringstream oss;
  123. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  124. spdlog::logger oss_logger("oss", oss_sink);
  125. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  126. oss_logger.info("{:X}", spdlog::to_hex(v));
  127. auto output = oss.str();
  128. REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
  129. }
  130. TEST_CASE("to_hex_no_delimiter", "[to_hex]")
  131. {
  132. std::ostringstream oss;
  133. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  134. spdlog::logger oss_logger("oss", oss_sink);
  135. std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
  136. oss_logger.info("{:sX}", spdlog::to_hex(v));
  137. auto output = oss.str();
  138. REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
  139. }
  140. TEST_CASE("message_counter", "[message_counter]")
  141. {
  142. std::ostringstream oss;
  143. auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
  144. spdlog::logger oss_logger("oss", oss_sink);
  145. oss_logger.set_pattern("%i %v");
  146. oss_logger.info("Hello");
  147. REQUIRE(oss.str() == "000001 Hello" + std::string(spdlog::details::os::default_eol));
  148. oss.str("");
  149. oss_logger.info("Hello again");
  150. REQUIRE(oss.str() == "000002 Hello again" + std::string(spdlog::details::os::default_eol));
  151. }