LoggerTest.cpp 6.8 KB

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