LoggerTest.cpp 5.9 KB

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