mavtables  0.2.1
MAVLink router and firewall.
test_Logger.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <ctime>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include <catch.hpp>
23 
24 #include "Logger.hpp"
25 #include "utility.hpp"
26 
27 #include "common.hpp"
28 
29 
30 TEST_CASE("The Logger level can be set and retrieved with the static 'level' "
31  "method", "[Logger]")
32 {
33  REQUIRE(Logger::level() == 0);
34 
35  for (unsigned int i = 0; i <= 10; i++)
36  {
37  Logger::level(i);
38  REQUIRE(Logger::level() == i);
39  }
40 
41  Logger::level(0);
42 }
43 
44 
45 TEST_CASE("The Logger can log to stdout with the static 'log' method if the "
46  " logger's level is the same or higher than the loglevel of the "
47  "message.", "[Logger]")
48 {
49  MockCOut mock_cout;
50  SECTION("No message can be logged if the logger's level is 0.")
51  {
52  Logger::level(0);
53  Logger::log("a log message");
54  REQUIRE(mock_cout.buffer().empty());
55  mock_cout.reset();
56  Logger::log("another log message");
57  REQUIRE(mock_cout.buffer().empty());
58  }
59  SECTION("The default level of the 'log' method is 1.")
60  {
61  Logger::level(0);
62  Logger::log("not logged");
63  REQUIRE(mock_cout.buffer().empty());
64  Logger::level(1);
65  Logger::log("a log message");
66  REQUIRE(mock_cout.buffer().substr(21) == "a log message\n");
67  mock_cout.reset();
68  Logger::log("another log message");
69  REQUIRE(mock_cout.buffer().substr(21) == "another log message\n");
70  }
71  SECTION("A level of 0 will be changed to level 1.")
72  {
73  Logger::level(0);
74  Logger::log(0, "not logged");
75  REQUIRE(mock_cout.buffer().empty());
76  Logger::level(1);
77  Logger::log(0, "a log message");
78  REQUIRE(mock_cout.buffer().substr(21) == "a log message\n");
79  mock_cout.reset();
80  Logger::log(0, "another log message");
81  REQUIRE(mock_cout.buffer().substr(21) == "another log message\n");
82  }
83  SECTION("The level can be set from 1 to 65535.")
84  {
85  Logger::level(0);
86  Logger::log(65535, "not logged");
87  REQUIRE(mock_cout.buffer().empty());
88  Logger::level(65535);
89  Logger::log(65535, "a log message");
90  REQUIRE(mock_cout.buffer().substr(21) == "a log message\n");
91  mock_cout.reset();
92  Logger::log(65535, "another log message");
93  REQUIRE(mock_cout.buffer().substr(21) == "another log message\n");
94  }
95  Logger::level(0);
96 }
97 
98 
99 TEST_CASE("All logged messages have a timestamp (this can sometimes fail if "
100  "the clock ticks over a second during the test).", "[Logger]")
101 {
102  Logger::level(1);
103  MockCOut mock_cout;
104  auto t = std::time(nullptr);
105  auto tm = *std::localtime(&t);
106  Logger::log("timestamped log message");
107  REQUIRE(
108  mock_cout.buffer() ==
109  str(std::put_time(&tm, "%Y-%m-%d %H:%M:%S")) +
110  " timestamped log message\n");
111  Logger::level(0);
112 }
std::string str(const T &object)
Definition: utility.hpp:128
void reset()
Definition: common.hpp:81
static void log(std::string message)
Definition: Logger.cpp:37
static unsigned int level()
Definition: Logger.cpp:96
std::string buffer()
Definition: common.hpp:75
TEST_CASE("The Logger level can be set and retrieved with the static 'level' " "method", "[Logger]")
Definition: test_Logger.cpp:30