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, size_t size,
45 bool warming_fake) noexcept override
46 {
47 const auto finally
48 = make_finally([&]() noexcept { log_buf_pool().bounded_push(std::move(msg)); });
49 if (!warming_fake) {
50 last_level = level;
51 last_msg.assign(static_cast<const char*>(msg.get()), size);
52 }
53 }
54 LogLevel last_level{};
55 string last_msg{};
56};
57
58} // namespace
59
61
63{
65 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::None), "NONE"), 0);
66 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Crit), "CRIT"), 0);
67 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Error), "ERROR"), 0);
68 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Warn), "WARN"), 0);
69 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Metric), "METRIC"), 0);
70 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Notice), "NOTICE"), 0);
71 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Info), "INFO"), 0);
72 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel::Debug), "DEBUG"), 0);
73 BOOST_CHECK_EQUAL(strcmp(log_label(LogLevel{99}), "DEBUG"), 0);
74}
75
77{
78 TestLogger tl;
79
80 auto prev_level = set_log_level(LogLevel::Info);
81 auto& prev_logger = set_logger(tl);
82 // clang-format off
83 const auto finally = make_finally([prev_level, &prev_logger]() noexcept {
86 });
87 // clang-format on
88
89 TOOLBOX_LOG(LogLevel::Info) << "test1: " << Foo<int, int>{10, 20};
90 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
91 BOOST_CHECK_EQUAL(tl.last_msg, "test1: (10,20)");
92
93 TOOLBOX_CRIT << "test2: " << Foo<int, int>{10, 20};
94 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Crit);
95 BOOST_CHECK_EQUAL(tl.last_msg, "test2: (10,20)");
96
97 TOOLBOX_ERROR << "test3: " << Foo<int, int>{10, 20};
98 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Error);
99 BOOST_CHECK_EQUAL(tl.last_msg, "test3: (10,20)");
100
101 TOOLBOX_WARN << "test4: " << Foo<int, int>{10, 20};
102 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Warn);
103 BOOST_CHECK_EQUAL(tl.last_msg, "test4: (10,20)");
104
105 TOOLBOX_METRIC << "test5: " << Foo<int, int>{10, 20};
106 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Metric);
107 BOOST_CHECK_EQUAL(tl.last_msg, "test5: (10,20)");
108
109 TOOLBOX_NOTICE << "test6: " << Foo<int, int>{10, 20};
110 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Notice);
111 BOOST_CHECK_EQUAL(tl.last_msg, "test6: (10,20)");
112
113 TOOLBOX_INFO << "test7: " << Foo<int, int>{10, 20};
114 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
115 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
116
117 // This should not be logged.
118 TOOLBOX_DEBUG << "test8: " << Foo<int, int>{10, 20};
119 BOOST_CHECK_EQUAL(tl.last_level, LogLevel::Info);
120 BOOST_CHECK_EQUAL(tl.last_msg, "test7: (10,20)");
121}
122
#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:62
BOOST_CHECK_EQUAL(v.size(), 10U)
The Logger is implemented by types that write log messages to a sink.
Definition Logger.hpp:110
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:216
Logger & set_logger(Logger &logger) noexcept
Set logger globally for all threads.
Definition Logger.cpp:236
WallClock::time_point WallTime
Definition Time.hpp:113
StoragePtr< MaxLogLine > LogMsgPtr
Definition Logger.hpp:54
LogBufPool & log_buf_pool() noexcept
A pool of log buffers, eliminating the need for dynamic memory allocations when logging.
Definition Logger.cpp:186
LogLevel set_log_level(LogLevel level) noexcept
Set log level globally for all threads.
Definition Logger.cpp:226
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:97