LoggerTest.cpp 6.8 KB

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