LoggerTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2020-10-29
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include "../../../TestHelper.hpp"
  12. namespace {
  13. class LoggerTest : public ::testing::Test {
  14. protected:
  15. LoggerTest() = default;
  16. ~LoggerTest() override = default;
  17. void SetUp() override {}
  18. void TearDown() override {}
  19. };
  20. TEST_F(LoggerTest, debug)
  21. {
  22. std::string path = TestHelper::getResourcesFolderLocation() + "output_debug.log";
  23. ls_std::Logger logger {path};
  24. logger.setLogLevel(ls_std::LogLevelValue::DEBUG);
  25. logger.debug("1. line!");
  26. logger.info("2. line!");
  27. logger.error("3. line!");
  28. logger.fatal("4. line!");
  29. logger.warn("5. line!");
  30. logger.trace("6. line!");
  31. logger.close();
  32. ls_std::File file {path};
  33. ls_std::FileReader reader {file};
  34. ls_std::String content {reader.read()};
  35. ASSERT_TRUE(content.contains("1. line!"));
  36. ASSERT_TRUE(content.contains("2. line!"));
  37. ASSERT_TRUE(content.contains("3. line!"));
  38. ASSERT_TRUE(content.contains("4. line!"));
  39. ASSERT_TRUE(content.contains("5. line!"));
  40. ASSERT_FALSE(content.contains("6. line!"));
  41. file.remove();
  42. }
  43. TEST_F(LoggerTest, error)
  44. {
  45. std::string path = TestHelper::getResourcesFolderLocation() + "output_error.log";
  46. ls_std::Logger logger {path};
  47. logger.setLogLevel(ls_std::LogLevelValue::ERR);
  48. logger.debug("1. line!");
  49. logger.info("2. line!");
  50. logger.error("3. line!");
  51. logger.fatal("4. line!");
  52. logger.warn("5. line!");
  53. logger.trace("6. line!");
  54. logger.close();
  55. ls_std::File file {path};
  56. ls_std::FileReader reader {file};
  57. ls_std::String content {reader.read()};
  58. ASSERT_FALSE(content.contains("1. line!"));
  59. ASSERT_FALSE(content.contains("2. line!"));
  60. ASSERT_TRUE(content.contains("3. line!"));
  61. ASSERT_TRUE(content.contains("4. line!"));
  62. ASSERT_FALSE(content.contains("5. line!"));
  63. ASSERT_FALSE(content.contains("6. line!"));
  64. file.remove();
  65. }
  66. TEST_F(LoggerTest, fatal)
  67. {
  68. std::string path = TestHelper::getResourcesFolderLocation() + "output_fatal.log";
  69. ls_std::Logger logger {path};
  70. logger.setLogLevel(ls_std::LogLevelValue::FATAL);
  71. logger.debug("1. line!");
  72. logger.info("2. line!");
  73. logger.error("3. line!");
  74. logger.fatal("4. line!");
  75. logger.warn("5. line!");
  76. logger.trace("6. line!");
  77. logger.close();
  78. ls_std::File file {path};
  79. ls_std::FileReader reader {file};
  80. ls_std::String content {reader.read()};
  81. ASSERT_FALSE(content.contains("1. line!"));
  82. ASSERT_FALSE(content.contains("2. line!"));
  83. ASSERT_FALSE(content.contains("3. line!"));
  84. ASSERT_TRUE(content.contains("4. line!"));
  85. ASSERT_FALSE(content.contains("5. line!"));
  86. ASSERT_FALSE(content.contains("6. line!"));
  87. file.remove();
  88. }
  89. TEST_F(LoggerTest, getLogLevel)
  90. {
  91. ls_std::Logger logger {TestHelper::getResourcesFolderLocation() + "output.log"};
  92. ASSERT_EQ(ls_std::LogLevelValue::INFO, logger.getLogLevel());
  93. }
  94. TEST_F(LoggerTest, info)
  95. {
  96. std::string path = TestHelper::getResourcesFolderLocation() + "output_info.log";
  97. ls_std::Logger logger {path};
  98. logger.setLogLevel(ls_std::LogLevelValue::INFO);
  99. logger.fatal("1. line!");
  100. logger.error("2. line!");
  101. logger.warn("3. line!");
  102. logger.info("4. line!");
  103. logger.debug("5. line!");
  104. logger.trace("6. line!");
  105. logger.close();
  106. ls_std::File file {path};
  107. ls_std::FileReader reader {file};
  108. ls_std::String content {reader.read()};
  109. ASSERT_TRUE(content.contains("1. line!"));
  110. ASSERT_TRUE(content.contains("2. line!"));
  111. ASSERT_TRUE(content.contains("3. line!"));
  112. ASSERT_TRUE(content.contains("4. line!"));
  113. ASSERT_FALSE(content.contains("5. line!"));
  114. ASSERT_FALSE(content.contains("6. line!"));
  115. file.remove();
  116. }
  117. TEST_F(LoggerTest, setLogLevel)
  118. {
  119. ls_std::Logger logger {TestHelper::getResourcesFolderLocation() + "output.log"};
  120. logger.setLogLevel(ls_std::LogLevelValue::ERR);
  121. ASSERT_EQ(ls_std::LogLevelValue::ERR, logger.getLogLevel());
  122. }
  123. TEST_F(LoggerTest, trace)
  124. {
  125. std::string path = TestHelper::getResourcesFolderLocation() + "output_trace.log";
  126. ls_std::Logger logger {path};
  127. logger.setLogLevel(ls_std::LogLevelValue::TRACE);
  128. logger.fatal("1. line!");
  129. logger.error("2. line!");
  130. logger.warn("3. line!");
  131. logger.info("4. line!");
  132. logger.debug("5. line!");
  133. logger.trace("6. line!");
  134. logger.close();
  135. ls_std::File file {path};
  136. ls_std::FileReader reader {file};
  137. ls_std::String content {reader.read()};
  138. ASSERT_TRUE(content.contains("1. line!"));
  139. ASSERT_TRUE(content.contains("2. line!"));
  140. ASSERT_TRUE(content.contains("3. line!"));
  141. ASSERT_TRUE(content.contains("4. line!"));
  142. ASSERT_TRUE(content.contains("5. line!"));
  143. ASSERT_TRUE(content.contains("6. line!"));
  144. file.remove();
  145. }
  146. TEST_F(LoggerTest, warn)
  147. {
  148. std::string path = TestHelper::getResourcesFolderLocation() + "output_warn.log";
  149. ls_std::Logger logger {path};
  150. logger.setLogLevel(ls_std::LogLevelValue::WARN);
  151. logger.fatal("1. line!");
  152. logger.error("2. line!");
  153. logger.warn("3. line!");
  154. logger.info("4. line!");
  155. logger.debug("5. line!");
  156. logger.trace("6. line!");
  157. logger.close();
  158. ls_std::File file {path};
  159. ls_std::FileReader reader {file};
  160. ls_std::String content {reader.read()};
  161. ASSERT_TRUE(content.contains("1. line!"));
  162. ASSERT_TRUE(content.contains("2. line!"));
  163. ASSERT_TRUE(content.contains("3. line!"));
  164. ASSERT_FALSE(content.contains("4. line!"));
  165. ASSERT_FALSE(content.contains("5. line!"));
  166. ASSERT_FALSE(content.contains("6. line!"));
  167. file.remove();
  168. }
  169. }