Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Log.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Log.hpp"
18
20
21#include <boost/test/unit_test.hpp>
22
23#include <cstring>
24
25using namespace std;
26using namespace toolbox;
27
28namespace {
29template <typename T, typename U>
30struct Foo {
31 T first;
32 U second;
33};
34
35template <typename StreamT>
36 requires Streamable<StreamT>
38{
39 os << '(' << val.first << ',' << val.second << ')';
40 return os;
41}
42
43struct TestLogger final : Logger {
44 void do_write_log(WallTime /*ts*/, LogLevel level, int /*tid*/, LogMsgPtr&& msg,
45 size_t size) noexcept override
46 {
47 last_level = level;
48 last_msg.assign(static_cast<const char*>(msg.get()), size);
49 }
50 LogLevel last_level{};
51 string last_msg{};
52};
53
54} // namespace
55
57
59{
60 //NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
62 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::None), "NONE"), 0);
63 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Crit), "CRIT"), 0);
64 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Error), "ERROR"), 0);
65 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Warn), "WARN"), 0);
66 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Metric), "METRIC"), 0);
67 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Notice), "NOTICE"), 0);
68 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Info), "INFO"), 0);
69 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Debug), "DEBUG"), 0);
70 //NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
71 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel{99}), "DEBUG"), 0);
72}
73
75{
76 TestLogger tl;
77
78 auto prev_level = set_log_level(LogLevel::Info);
79 auto& prev_logger = set_logger(tl);
80 // clang-format off
81 const auto finally = make_finally([prev_level, &prev_logger]() noexcept {
84 });
85 // clang-format on
86
87 TOOLBOX_LOG(LogLevel::Info) << "test1: " << Foo<int, int>{10, 20};
88 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
89 BOOST_CHECK_EQUAL(tl.last_msg, "test1: (10,20)");
90
91 TOOLBOX_CRIT << "test2: " << Foo<int, int>{10, 20};
92 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Crit);
93 BOOST_CHECK_EQUAL(tl.last_msg, "test2: (10,20)");
94
95 TOOLBOX_ERROR << "test3: " << Foo<int, int>{10, 20};
96 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Error);
97 BOOST_CHECK_EQUAL(tl.last_msg, "test3: (10,20)");
98
99 TOOLBOX_WARN << "test4: " << Foo<int, int>{10, 20};
100 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Warn);
101 BOOST_CHECK_EQUAL(tl.last_msg, "test4: (10,20)");
102
103 TOOLBOX_METRIC << "test5: " << Foo<int, int>{10, 20};
104 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Metric);
105 BOOST_CHECK_EQUAL(tl.last_msg, "test5: (10,20)");
106
107 TOOLBOX_NOTICE << "test6: " << Foo<int, int>{10, 20};
108 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Notice);
109 BOOST_CHECK_EQUAL(tl.last_msg, "test6: (10,20)");
110
111 TOOLBOX_INFO << "test7: " << Foo<int, int>{10, 20};
112 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
113 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
114
115 // This should not be logged.
116 TOOLBOX_DEBUG << "test8: " << Foo<int, int>{10, 20};
117 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
118 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
119}
120
#define TOOLBOX_METRIC
Definition Log.hpp:95
#define TOOLBOX_DEBUG
Definition Log.hpp:102
#define TOOLBOX_CRIT
Definition Log.hpp:92
#define TOOLBOX_WARN
Definition Log.hpp:94
#define TOOLBOX_NOTICE
Definition Log.hpp:96
#define TOOLBOX_INFO
Definition Log.hpp:97
#define TOOLBOX_ERROR
Definition Log.hpp:93
#define TOOLBOX_LOG(LEVEL)
Definition Log.hpp:89
BOOST_AUTO_TEST_CASE(LogLabelCase)
Definition Log.ut.cpp:58
BOOST_CHECK_EQUAL(v.size(), 10U)
STL namespace.
StreamT & operator<<(StreamT &os, PutPercentiles pp)
Definition Utility.hpp:68
const char * log_label(LogLevel level) noexcept
Return log label for given log level.
Definition Logger.cpp:169
Logger & set_logger(Logger &logger) noexcept
Set logger globally for all threads.
Definition Logger.cpp:189
WallClock::time_point WallTime
Definition Time.hpp:111
StoragePtr< MaxLogLine > LogMsgPtr
Definition Logger.hpp:52
LogLevel set_log_level(LogLevel level) noexcept
Set log level globally for all threads.
Definition Logger.cpp:179
auto make_finally(FnT fn) noexcept
Definition Finally.hpp:48
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
constexpr auto bind() noexcept
Definition Slot.hpp:92