LoggerTest.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2021-07-16
  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. static ls_std::String getContentFromLogFile(const std::string &_logName)
  35. {
  36. ls_std::File file{TestHelper::getResourcesFolderLocation() + _logName};
  37. ls_std::FileReader reader{file};
  38. ls_std::String content{reader.read()};
  39. file.remove();
  40. return content;
  41. }
  42. };
  43. TEST_F(LoggerTest, constructor_no_writer_reference)
  44. {
  45. EXPECT_THROW({
  46. try
  47. {
  48. ls_std::Logger logger{nullptr};
  49. }
  50. catch (const ls_std::IllegalArgumentException &_exception)
  51. {
  52. throw;
  53. }
  54. }, ls_std::IllegalArgumentException);
  55. }
  56. TEST_F(LoggerTest, debug)
  57. {
  58. // write to log file
  59. std::string logName = "output_debug.log";
  60. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  61. ls_std::Logger logger{writer};
  62. logger.setLogLevel(ls_std::LogLevelValue::DEBUG);
  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. // get content and check
  70. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  71. ls_std::String content = getContentFromLogFile(logName);
  72. ASSERT_TRUE(content.contains("1. line!"));
  73. ASSERT_TRUE(content.contains("2. line!"));
  74. ASSERT_TRUE(content.contains("3. line!"));
  75. ASSERT_TRUE(content.contains("4. line!"));
  76. ASSERT_TRUE(content.contains("5. line!"));
  77. ASSERT_FALSE(content.contains("6. line!"));
  78. }
  79. TEST_F(LoggerTest, error)
  80. {
  81. // write to log file
  82. std::string logName = "output_error.log";
  83. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  84. ls_std::Logger logger{writer};
  85. logger.setLogLevel(ls_std::LogLevelValue::ERR);
  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. // get content and check
  93. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  94. ls_std::String content = getContentFromLogFile(logName);
  95. ASSERT_FALSE(content.contains("1. line!"));
  96. ASSERT_FALSE(content.contains("2. line!"));
  97. ASSERT_TRUE(content.contains("3. line!"));
  98. ASSERT_TRUE(content.contains("4. line!"));
  99. ASSERT_FALSE(content.contains("5. line!"));
  100. ASSERT_FALSE(content.contains("6. line!"));
  101. }
  102. TEST_F(LoggerTest, fatal)
  103. {
  104. // write to log file
  105. std::string logName = "output_fatal.log";
  106. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  107. ls_std::Logger logger{writer};
  108. logger.setLogLevel(ls_std::LogLevelValue::FATAL);
  109. logger.debug("1. line!");
  110. logger.info("2. line!");
  111. logger.error("3. line!");
  112. logger.fatal("4. line!");
  113. logger.warn("5. line!");
  114. logger.trace("6. line!");
  115. // get content and check
  116. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  117. ls_std::String content = getContentFromLogFile(logName);
  118. ASSERT_FALSE(content.contains("1. line!"));
  119. ASSERT_FALSE(content.contains("2. line!"));
  120. ASSERT_FALSE(content.contains("3. line!"));
  121. ASSERT_TRUE(content.contains("4. line!"));
  122. ASSERT_FALSE(content.contains("5. line!"));
  123. ASSERT_FALSE(content.contains("6. line!"));
  124. }
  125. TEST_F(LoggerTest, getLogLevel)
  126. {
  127. ls_std::Logger logger{createFileLogger("output.log")};
  128. ASSERT_EQ(ls_std::LogLevelValue::INFO, logger.getLogLevel());
  129. }
  130. TEST_F(LoggerTest, info)
  131. {
  132. // write to log file
  133. std::string logName = "output_info.log";
  134. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  135. ls_std::Logger logger{writer};
  136. logger.setLogLevel(ls_std::LogLevelValue::INFO);
  137. logger.fatal("1. line!");
  138. logger.error("2. line!");
  139. logger.warn("3. line!");
  140. logger.info("4. line!");
  141. logger.debug("5. line!");
  142. logger.trace("6. line!");
  143. // get content and check
  144. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  145. ls_std::String content = getContentFromLogFile(logName);
  146. ASSERT_TRUE(content.contains("1. line!"));
  147. ASSERT_TRUE(content.contains("2. line!"));
  148. ASSERT_TRUE(content.contains("3. line!"));
  149. ASSERT_TRUE(content.contains("4. line!"));
  150. ASSERT_FALSE(content.contains("5. line!"));
  151. ASSERT_FALSE(content.contains("6. line!"));
  152. }
  153. TEST_F(LoggerTest, setLogLevel)
  154. {
  155. ls_std::Logger logger{createFileLogger("output.log")};
  156. logger.setLogLevel(ls_std::LogLevelValue::ERR);
  157. ASSERT_EQ(ls_std::LogLevelValue::ERR, logger.getLogLevel());
  158. }
  159. TEST_F(LoggerTest, trace)
  160. {
  161. // write to log file
  162. std::string logName = "output_trace.log";
  163. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  164. ls_std::Logger logger{writer};
  165. logger.setLogLevel(ls_std::LogLevelValue::TRACE);
  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. // get content and check
  173. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  174. ls_std::String content = getContentFromLogFile(logName);
  175. ASSERT_TRUE(content.contains("1. line!"));
  176. ASSERT_TRUE(content.contains("2. line!"));
  177. ASSERT_TRUE(content.contains("3. line!"));
  178. ASSERT_TRUE(content.contains("4. line!"));
  179. ASSERT_TRUE(content.contains("5. line!"));
  180. ASSERT_TRUE(content.contains("6. line!"));
  181. }
  182. TEST_F(LoggerTest, warn)
  183. {
  184. // write to log file
  185. std::string logName = "output_warn.log";
  186. std::shared_ptr<ls_std::IWriter> writer = createFileLogger(logName);
  187. ls_std::Logger logger{writer};
  188. logger.setLogLevel(ls_std::LogLevelValue::WARN);
  189. logger.fatal("1. line!");
  190. logger.error("2. line!");
  191. logger.warn("3. line!");
  192. logger.info("4. line!");
  193. logger.debug("5. line!");
  194. logger.trace("6. line!");
  195. // get content and check
  196. std::dynamic_pointer_cast<ls_std::FileOutputStream>(writer)->close();
  197. ls_std::String content = getContentFromLogFile(logName);
  198. ASSERT_TRUE(content.contains("1. line!"));
  199. ASSERT_TRUE(content.contains("2. line!"));
  200. ASSERT_TRUE(content.contains("3. line!"));
  201. ASSERT_FALSE(content.contains("4. line!"));
  202. ASSERT_FALSE(content.contains("5. line!"));
  203. ASSERT_FALSE(content.contains("6. line!"));
  204. }
  205. }