LoggerTest.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-04-12
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using ls::std::core::IllegalArgumentException;
  14. using ls::std::core::interface_type::IWriter;
  15. using ls::std::io::File;
  16. using ls::std::io::FileOutputStream;
  17. using ls::std::io::FileReader;
  18. using ls::std::io::Logger;
  19. using ls::std::io::LogLevelValue;
  20. using ls::std::test::TestHelper;
  21. using std::dynamic_pointer_cast;
  22. using std::make_shared;
  23. using std::shared_ptr;
  24. using std::string;
  25. using testing::Test;
  26. namespace
  27. {
  28. class LoggerTest : public Test
  29. {
  30. public:
  31. LoggerTest() = default;
  32. ~LoggerTest() override = default;
  33. static shared_ptr<IWriter> createFileLogger(const string &_logName)
  34. {
  35. string path = TestHelper::getResourcesFolderLocation() + _logName;
  36. File file{path};
  37. if (!file.exists())
  38. {
  39. file.createNewFile();
  40. }
  41. shared_ptr<IWriter> writer = make_shared<FileOutputStream>(file);
  42. return writer;
  43. }
  44. static string getContentFromLogFile(const string &_logName)
  45. {
  46. File file{TestHelper::getResourcesFolderLocation() + _logName};
  47. FileReader reader{file};
  48. string content{reader.read()};
  49. file.remove();
  50. return content;
  51. }
  52. };
  53. TEST_F(LoggerTest, constructor_no_writer_reference)
  54. {
  55. EXPECT_THROW(
  56. {
  57. try
  58. {
  59. Logger logger{nullptr};
  60. }
  61. catch (const IllegalArgumentException &_exception)
  62. {
  63. throw;
  64. }
  65. },
  66. IllegalArgumentException);
  67. }
  68. TEST_F(LoggerTest, debug)
  69. {
  70. // write to log file
  71. string logName = "output-debug.log";
  72. shared_ptr<IWriter> writer = createFileLogger(logName);
  73. Logger logger{writer};
  74. logger.setLogLevel(LogLevelValue::DEBUG);
  75. logger.showLogLevel();
  76. logger.debug("1. line!");
  77. logger.info("2. line!");
  78. logger.error("3. line!");
  79. logger.fatal("4. line!");
  80. logger.warn("5. line!");
  81. logger.trace("6. line!");
  82. // get content and check
  83. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  84. string content = getContentFromLogFile(logName);
  85. ASSERT_TRUE(content.find("1. line!") != string::npos);
  86. ASSERT_TRUE(content.find("2. line!") != string::npos);
  87. ASSERT_TRUE(content.find("3. line!") != string::npos);
  88. ASSERT_TRUE(content.find("4. line!") != string::npos);
  89. ASSERT_TRUE(content.find("5. line!") != string::npos);
  90. ASSERT_FALSE(content.find("6. line!") != string::npos);
  91. }
  92. TEST_F(LoggerTest, error)
  93. {
  94. // write to log file
  95. string logName = "output-error.log";
  96. shared_ptr<IWriter> writer = createFileLogger(logName);
  97. Logger logger{writer};
  98. logger.setLogLevel(LogLevelValue::ERR);
  99. logger.showLogLevel();
  100. logger.debug("1. line!");
  101. logger.info("2. line!");
  102. logger.error("3. line!");
  103. logger.fatal("4. line!");
  104. logger.warn("5. line!");
  105. logger.trace("6. line!");
  106. // get content and check
  107. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  108. string content = getContentFromLogFile(logName);
  109. ASSERT_FALSE(content.find("1. line!") != string::npos);
  110. ASSERT_FALSE(content.find("2. line!") != string::npos);
  111. ASSERT_TRUE(content.find("3. line!") != string::npos);
  112. ASSERT_TRUE(content.find("4. line!") != string::npos);
  113. ASSERT_FALSE(content.find("5. line!") != string::npos);
  114. ASSERT_FALSE(content.find("6. line!") != string::npos);
  115. }
  116. TEST_F(LoggerTest, fatal)
  117. {
  118. // write to log file
  119. string logName = "output-fatal.log";
  120. shared_ptr<IWriter> writer = createFileLogger(logName);
  121. Logger logger{writer};
  122. logger.setLogLevel(LogLevelValue::FATAL);
  123. logger.showLogLevel();
  124. logger.debug("1. line!");
  125. logger.info("2. line!");
  126. logger.error("3. line!");
  127. logger.fatal("4. line!");
  128. logger.warn("5. line!");
  129. logger.trace("6. line!");
  130. // get content and check
  131. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  132. string content = getContentFromLogFile(logName);
  133. ASSERT_FALSE(content.find("1. line!") != string::npos);
  134. ASSERT_FALSE(content.find("2. line!") != string::npos);
  135. ASSERT_FALSE(content.find("3. line!") != string::npos);
  136. ASSERT_TRUE(content.find("4. line!") != string::npos);
  137. ASSERT_FALSE(content.find("5. line!") != string::npos);
  138. ASSERT_FALSE(content.find("6. line!") != string::npos);
  139. }
  140. TEST_F(LoggerTest, getLogLevel)
  141. {
  142. Logger logger{createFileLogger("output.log")};
  143. ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel().getValue());
  144. }
  145. TEST_F(LoggerTest, hideLogLevel)
  146. {
  147. string logName = "hide-log-level-output-fatal.log";
  148. shared_ptr<IWriter> writer = createFileLogger(logName);
  149. Logger logger{writer};
  150. logger.setLogLevel(LogLevelValue::DEBUG);
  151. logger.hideLogLevel();
  152. logger.fatal("test message");
  153. // get content and check
  154. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  155. string content = getContentFromLogFile(logName);
  156. ASSERT_TRUE(content.find("FATAL") == string::npos);
  157. }
  158. TEST_F(LoggerTest, info)
  159. {
  160. // write to log file
  161. string logName = "output-info.log";
  162. shared_ptr<IWriter> writer = createFileLogger(logName);
  163. Logger logger{writer};
  164. logger.setLogLevel(LogLevelValue::INFO);
  165. logger.showLogLevel();
  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. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  174. string content = getContentFromLogFile(logName);
  175. ASSERT_TRUE(content.find("1. line!") != string::npos);
  176. ASSERT_TRUE(content.find("2. line!") != string::npos);
  177. ASSERT_TRUE(content.find("3. line!") != string::npos);
  178. ASSERT_TRUE(content.find("4. line!") != string::npos);
  179. ASSERT_FALSE(content.find("5. line!") != string::npos);
  180. ASSERT_FALSE(content.find("6. line!") != string::npos);
  181. }
  182. TEST_F(LoggerTest, setLogLevel)
  183. {
  184. Logger logger{createFileLogger("output.log")};
  185. logger.setLogLevel(LogLevelValue::ERR);
  186. ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel().getValue());
  187. }
  188. TEST_F(LoggerTest, showLogLevel)
  189. {
  190. string logName = "show-log-level-output-fatal.log";
  191. shared_ptr<IWriter> writer = createFileLogger(logName);
  192. Logger logger{writer};
  193. logger.setLogLevel(LogLevelValue::DEBUG);
  194. logger.showLogLevel();
  195. logger.fatal("test message");
  196. // get content and check
  197. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  198. string content = getContentFromLogFile(logName);
  199. ASSERT_TRUE(content.find("FATAL") != string::npos);
  200. }
  201. TEST_F(LoggerTest, trace)
  202. {
  203. // write to log file
  204. string logName = "output-trace.log";
  205. shared_ptr<IWriter> writer = createFileLogger(logName);
  206. Logger logger{writer};
  207. logger.setLogLevel(LogLevelValue::TRACE);
  208. logger.showLogLevel();
  209. logger.fatal("1. line!");
  210. logger.error("2. line!");
  211. logger.warn("3. line!");
  212. logger.info("4. line!");
  213. logger.debug("5. line!");
  214. logger.trace("6. line!");
  215. // get content and check
  216. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  217. string content = getContentFromLogFile(logName);
  218. ASSERT_TRUE(content.find("1. line!") != string::npos);
  219. ASSERT_TRUE(content.find("2. line!") != string::npos);
  220. ASSERT_TRUE(content.find("3. line!") != string::npos);
  221. ASSERT_TRUE(content.find("4. line!") != string::npos);
  222. ASSERT_TRUE(content.find("5. line!") != string::npos);
  223. ASSERT_TRUE(content.find("6. line!") != string::npos);
  224. }
  225. TEST_F(LoggerTest, warn)
  226. {
  227. // write to log file
  228. string logName = "output-warn.log";
  229. shared_ptr<IWriter> writer = createFileLogger(logName);
  230. Logger logger{writer};
  231. logger.setLogLevel(LogLevelValue::WARN);
  232. logger.showLogLevel();
  233. logger.fatal("1. line!");
  234. logger.error("2. line!");
  235. logger.warn("3. line!");
  236. logger.info("4. line!");
  237. logger.debug("5. line!");
  238. logger.trace("6. line!");
  239. // get content and check
  240. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  241. string content = getContentFromLogFile(logName);
  242. ASSERT_TRUE(content.find("1. line!") != string::npos);
  243. ASSERT_TRUE(content.find("2. line!") != string::npos);
  244. ASSERT_TRUE(content.find("3. line!") != string::npos);
  245. ASSERT_FALSE(content.find("4. line!") != string::npos);
  246. ASSERT_FALSE(content.find("5. line!") != string::npos);
  247. ASSERT_FALSE(content.find("6. line!") != string::npos);
  248. }
  249. }