LoggerTest.cpp 8.3 KB

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