logger.cpp 1.57 KB
Newer Older
louiz’'s avatar
louiz’ committed
1 2 3 4 5 6 7 8 9 10 11 12
#include "catch.hpp"

#include <logger/logger.hpp>
#include <config/config.hpp>

#include "io_tester.hpp"
#include <iostream>

using namespace std::string_literals;

TEST_CASE("Basic logging")
{
13 14
  const std::string debug_header = "[DEBUG]: ";
  const std::string error_header = "[ERROR]: ";
louiz’'s avatar
louiz’ committed
15 16 17 18 19 20
  Logger::instance().reset();
  GIVEN("A logger with log_level 0")
    {
      Config::set("log_level", "0");
      WHEN("we log some debug text")
        {
21
          IoTester<std::ostream> out(std::cout);
22
          log_debug("deb", "ug");
louiz’'s avatar
louiz’ committed
23
          THEN("debug logs are written")
24
            CHECK(out.str() == debug_header + "tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\tdebug\n");
louiz’'s avatar
louiz’ committed
25 26 27
        }
      WHEN("we log some errors")
        {
28
          IoTester<std::ostream> out(std::cout);
louiz’'s avatar
louiz’ committed
29
          log_error("err", 12, "or");
louiz’'s avatar
louiz’ committed
30
          THEN("error logs are written")
louiz’'s avatar
louiz’ committed
31
            CHECK(out.str() == error_header + "tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\terr12or\n");
louiz’'s avatar
louiz’ committed
32 33 34 35 36 37 38
        }
    }
  GIVEN("A logger with log_level 3")
    {
      Config::set("log_level", "3");
      WHEN("we log some debug text")
        {
39
          IoTester<std::ostream> out(std::cout);
louiz’'s avatar
louiz’ committed
40
          log_debug(123, "debug");
louiz’'s avatar
louiz’ committed
41 42 43 44 45
          THEN("nothing is written")
            CHECK(out.str().empty());
        }
      WHEN("we log some errors")
        {
46
          IoTester<std::ostream> out(std::cout);
louiz’'s avatar
louiz’ committed
47
          log_error(123, " errors");
louiz’'s avatar
louiz’ committed
48
          THEN("error logs are still written")
louiz’'s avatar
louiz’ committed
49
            CHECK(out.str() == error_header + "tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\t123 errors\n");
louiz’'s avatar
louiz’ committed
50 51 52
        }
    }
}