LoggerTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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::io::NewLine;
  21. using ls::std::test::TestHelper;
  22. using std::dynamic_pointer_cast;
  23. using std::make_shared;
  24. using std::shared_ptr;
  25. using std::string;
  26. using testing::Test;
  27. namespace
  28. {
  29. class LoggerTest : public Test
  30. {
  31. public:
  32. LoggerTest() = default;
  33. ~LoggerTest() override = default;
  34. static shared_ptr<IWriter> createFileLogger(const string &_logName)
  35. {
  36. string path = TestHelper::getResourcesFolderLocation() + _logName;
  37. File file{path};
  38. if (!file.exists())
  39. {
  40. file.createNewFile();
  41. }
  42. shared_ptr<IWriter> writer = make_shared<FileOutputStream>(file);
  43. return writer;
  44. }
  45. static string getContentFromLogFile(const string &_logName)
  46. {
  47. File file{TestHelper::getResourcesFolderLocation() + _logName};
  48. FileReader reader{file};
  49. string content{reader.read()};
  50. file.remove();
  51. return content;
  52. }
  53. };
  54. TEST_F(LoggerTest, constructor_no_writer_reference)
  55. {
  56. EXPECT_THROW(
  57. {
  58. try
  59. {
  60. Logger logger{nullptr};
  61. }
  62. catch (const IllegalArgumentException &_exception)
  63. {
  64. throw;
  65. }
  66. },
  67. IllegalArgumentException);
  68. }
  69. TEST_F(LoggerTest, debug)
  70. {
  71. // write to log file
  72. string logName = "output-debug.log";
  73. shared_ptr<IWriter> writer = createFileLogger(logName);
  74. Logger logger{writer};
  75. logger.setLogLevel(LogLevelValue::DEBUG);
  76. logger.showLogLevel();
  77. logger.showTimestamp();
  78. logger.debug("1. line!");
  79. logger.info("2. line!");
  80. logger.error("3. line!");
  81. logger.fatal("4. line!");
  82. logger.warn("5. line!");
  83. logger.trace("6. line!");
  84. // get content and check
  85. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  86. string content = getContentFromLogFile(logName);
  87. ASSERT_TRUE(content.find("1. line!") != string::npos);
  88. ASSERT_TRUE(content.find("2. line!") != string::npos);
  89. ASSERT_TRUE(content.find("3. line!") != string::npos);
  90. ASSERT_TRUE(content.find("4. line!") != string::npos);
  91. ASSERT_TRUE(content.find("5. line!") != string::npos);
  92. ASSERT_FALSE(content.find("6. line!") != string::npos);
  93. }
  94. TEST_F(LoggerTest, error)
  95. {
  96. // write to log file
  97. string logName = "output-error.log";
  98. shared_ptr<IWriter> writer = createFileLogger(logName);
  99. Logger logger{writer};
  100. logger.setLogLevel(LogLevelValue::ERR);
  101. logger.showLogLevel();
  102. logger.showTimestamp();
  103. logger.debug("1. line!");
  104. logger.info("2. line!");
  105. logger.error("3. line!");
  106. logger.fatal("4. line!");
  107. logger.warn("5. line!");
  108. logger.trace("6. line!");
  109. // get content and check
  110. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  111. string content = getContentFromLogFile(logName);
  112. ASSERT_FALSE(content.find("1. line!") != string::npos);
  113. ASSERT_FALSE(content.find("2. line!") != string::npos);
  114. ASSERT_TRUE(content.find("3. line!") != string::npos);
  115. ASSERT_TRUE(content.find("4. line!") != string::npos);
  116. ASSERT_FALSE(content.find("5. line!") != string::npos);
  117. ASSERT_FALSE(content.find("6. line!") != string::npos);
  118. }
  119. TEST_F(LoggerTest, fatal)
  120. {
  121. // write to log file
  122. string logName = "output-fatal.log";
  123. shared_ptr<IWriter> writer = createFileLogger(logName);
  124. Logger logger{writer};
  125. logger.setLogLevel(LogLevelValue::FATAL);
  126. logger.showLogLevel();
  127. logger.showTimestamp();
  128. logger.debug("1. line!");
  129. logger.info("2. line!");
  130. logger.error("3. line!");
  131. logger.fatal("4. line!");
  132. logger.warn("5. line!");
  133. logger.trace("6. line!");
  134. // get content and check
  135. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  136. string content = getContentFromLogFile(logName);
  137. ASSERT_FALSE(content.find("1. line!") != string::npos);
  138. ASSERT_FALSE(content.find("2. line!") != string::npos);
  139. ASSERT_FALSE(content.find("3. line!") != string::npos);
  140. ASSERT_TRUE(content.find("4. line!") != string::npos);
  141. ASSERT_FALSE(content.find("5. line!") != string::npos);
  142. ASSERT_FALSE(content.find("6. line!") != string::npos);
  143. }
  144. TEST_F(LoggerTest, getLogLevel)
  145. {
  146. Logger logger{createFileLogger("output.log")};
  147. ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel().getValue());
  148. }
  149. TEST_F(LoggerTest, hideLogLevel)
  150. {
  151. string logName = "hide-log-level-output-fatal.log";
  152. shared_ptr<IWriter> writer = createFileLogger(logName);
  153. Logger logger{writer};
  154. logger.setLogLevel(LogLevelValue::DEBUG);
  155. logger.hideLogLevel();
  156. logger.fatal("test message");
  157. // get content and check
  158. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  159. string content = getContentFromLogFile(logName);
  160. ASSERT_TRUE(content.find("FATAL") == string::npos);
  161. }
  162. TEST_F(LoggerTest, hideTimestamp)
  163. {
  164. string logName = "hide-time-stamp-output-fatal.log";
  165. shared_ptr<IWriter> writer = createFileLogger(logName);
  166. Logger logger{writer};
  167. logger.setLogLevel(LogLevelValue::DEBUG);
  168. logger.hideTimestamp();
  169. logger.fatal("test message");
  170. // get content and check
  171. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  172. string content = getContentFromLogFile(logName);
  173. ASSERT_TRUE(content.find('[') == string::npos);
  174. }
  175. TEST_F(LoggerTest, hideInformation)
  176. {
  177. string logName = "hide-info-output-fatal.log";
  178. shared_ptr<IWriter> writer = createFileLogger(logName);
  179. string message = "test message";
  180. Logger logger{writer};
  181. logger.setLogLevel(LogLevelValue::DEBUG);
  182. logger.hideLogLevel();
  183. logger.hideTimestamp();
  184. logger.fatal(message.c_str());
  185. // get content and check
  186. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  187. string content = getContentFromLogFile(logName);
  188. ASSERT_STREQ(string(message + NewLine::get()).c_str(), content.c_str());
  189. }
  190. TEST_F(LoggerTest, info)
  191. {
  192. // write to log file
  193. string logName = "output-info.log";
  194. shared_ptr<IWriter> writer = createFileLogger(logName);
  195. Logger logger{writer};
  196. logger.setLogLevel(LogLevelValue::INFO);
  197. logger.showLogLevel();
  198. logger.showTimestamp();
  199. logger.fatal("1. line!");
  200. logger.error("2. line!");
  201. logger.warn("3. line!");
  202. logger.info("4. line!");
  203. logger.debug("5. line!");
  204. logger.trace("6. line!");
  205. // get content and check
  206. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  207. string content = getContentFromLogFile(logName);
  208. ASSERT_TRUE(content.find("1. line!") != string::npos);
  209. ASSERT_TRUE(content.find("2. line!") != string::npos);
  210. ASSERT_TRUE(content.find("3. line!") != string::npos);
  211. ASSERT_TRUE(content.find("4. line!") != string::npos);
  212. ASSERT_FALSE(content.find("5. line!") != string::npos);
  213. ASSERT_FALSE(content.find("6. line!") != string::npos);
  214. }
  215. TEST_F(LoggerTest, setLogLevel)
  216. {
  217. Logger logger{createFileLogger("output.log")};
  218. logger.setLogLevel(LogLevelValue::ERR);
  219. ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel().getValue());
  220. }
  221. TEST_F(LoggerTest, showLogLevel)
  222. {
  223. string logName = "show-log-level-output-fatal.log";
  224. shared_ptr<IWriter> writer = createFileLogger(logName);
  225. Logger logger{writer};
  226. logger.setLogLevel(LogLevelValue::DEBUG);
  227. logger.showLogLevel();
  228. logger.fatal("test message");
  229. // get content and check
  230. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  231. string content = getContentFromLogFile(logName);
  232. ASSERT_TRUE(content.find("FATAL") != string::npos);
  233. }
  234. TEST_F(LoggerTest, showTimestamp)
  235. {
  236. string logName = "hide-time-stamp-output-fatal.log";
  237. shared_ptr<IWriter> writer = createFileLogger(logName);
  238. Logger logger{writer};
  239. logger.setLogLevel(LogLevelValue::DEBUG);
  240. logger.showTimestamp();
  241. logger.fatal("test message");
  242. // get content and check
  243. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  244. string content = getContentFromLogFile(logName);
  245. ASSERT_TRUE(content.find('[') != string::npos);
  246. }
  247. TEST_F(LoggerTest, trace)
  248. {
  249. // write to log file
  250. string logName = "output-trace.log";
  251. shared_ptr<IWriter> writer = createFileLogger(logName);
  252. Logger logger{writer};
  253. logger.setLogLevel(LogLevelValue::TRACE);
  254. logger.showLogLevel();
  255. logger.showTimestamp();
  256. logger.fatal("1. line!");
  257. logger.error("2. line!");
  258. logger.warn("3. line!");
  259. logger.info("4. line!");
  260. logger.debug("5. line!");
  261. logger.trace("6. line!");
  262. // get content and check
  263. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  264. string content = getContentFromLogFile(logName);
  265. ASSERT_TRUE(content.find("1. line!") != string::npos);
  266. ASSERT_TRUE(content.find("2. line!") != string::npos);
  267. ASSERT_TRUE(content.find("3. line!") != string::npos);
  268. ASSERT_TRUE(content.find("4. line!") != string::npos);
  269. ASSERT_TRUE(content.find("5. line!") != string::npos);
  270. ASSERT_TRUE(content.find("6. line!") != string::npos);
  271. }
  272. TEST_F(LoggerTest, warn)
  273. {
  274. // write to log file
  275. string logName = "output-warn.log";
  276. shared_ptr<IWriter> writer = createFileLogger(logName);
  277. Logger logger{writer};
  278. logger.setLogLevel(LogLevelValue::WARN);
  279. logger.showLogLevel();
  280. logger.showTimestamp();
  281. logger.fatal("1. line!");
  282. logger.error("2. line!");
  283. logger.warn("3. line!");
  284. logger.info("4. line!");
  285. logger.debug("5. line!");
  286. logger.trace("6. line!");
  287. // get content and check
  288. dynamic_pointer_cast<FileOutputStream>(writer)->close();
  289. string content = getContentFromLogFile(logName);
  290. ASSERT_TRUE(content.find("1. line!") != string::npos);
  291. ASSERT_TRUE(content.find("2. line!") != string::npos);
  292. ASSERT_TRUE(content.find("3. line!") != string::npos);
  293. ASSERT_FALSE(content.find("4. line!") != string::npos);
  294. ASSERT_FALSE(content.find("5. line!") != string::npos);
  295. ASSERT_FALSE(content.find("6. line!") != string::npos);
  296. }
  297. }